blob: bc70254558fac5ce8d0e4dfe13f901466bd51bac [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 Hiroyuki453a9bf2011-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 Hiroyuki453a9bf2011-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 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800349 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800350 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800351};
352
Glauber Costaa8964b92012-12-18 14:22:09 -0800353/* We account when limit is on, but only after call sites are patched */
354#define KMEM_ACCOUNTED_MASK \
355 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800356
357#ifdef CONFIG_MEMCG_KMEM
358static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
359{
360 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
361}
Glauber Costa7de37682012-12-18 14:22:07 -0800362
363static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
364{
365 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
366}
367
Glauber Costaa8964b92012-12-18 14:22:09 -0800368static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
369{
370 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
371}
372
Glauber Costa7de37682012-12-18 14:22:07 -0800373static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
374{
375 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
376 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
377}
378
379static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
380{
381 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
382 &memcg->kmem_account_flags);
383}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800384#endif
385
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800386/* Stuffs for move charges at task migration. */
387/*
388 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
389 * left-shifted bitmap of these types.
390 */
391enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800392 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700393 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800394 NR_MOVE_TYPE,
395};
396
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800397/* "mc" and its members are protected by cgroup_mutex */
398static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800399 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800400 struct mem_cgroup *from;
401 struct mem_cgroup *to;
402 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800403 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800404 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800405 struct task_struct *moving_task; /* a task moving charges */
406 wait_queue_head_t waitq; /* a waitq for other context */
407} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700408 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800409 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
410};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800411
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700412static bool move_anon(void)
413{
414 return test_bit(MOVE_CHARGE_TYPE_ANON,
415 &mc.to->move_charge_at_immigrate);
416}
417
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700418static bool move_file(void)
419{
420 return test_bit(MOVE_CHARGE_TYPE_FILE,
421 &mc.to->move_charge_at_immigrate);
422}
423
Balbir Singh4e416952009-09-23 15:56:39 -0700424/*
425 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
426 * limit reclaim to prevent infinite loops, if they ever occur.
427 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700428#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
429#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700430
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800431enum charge_type {
432 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700433 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800434 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700435 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700436 NR_CHARGE_TYPE,
437};
438
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800439/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800440enum res_type {
441 _MEM,
442 _MEMSWAP,
443 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800444 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800445};
446
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700447#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
448#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800449#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700450/* Used for OOM nofiier */
451#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800452
Balbir Singh75822b42009-09-23 15:56:38 -0700453/*
454 * Reclaim flags for mem_cgroup_hierarchical_reclaim
455 */
456#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
457#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
458#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
459#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700461static void mem_cgroup_get(struct mem_cgroup *memcg);
462static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000463
Wanpeng Lib2145142012-07-31 16:46:01 -0700464static inline
465struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
466{
467 return container_of(s, struct mem_cgroup, css);
468}
469
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700470static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
471{
472 return (memcg == root_mem_cgroup);
473}
474
Glauber Costae1aab162011-12-11 21:47:03 +0000475/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700476#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000477
Glauber Costae1aab162011-12-11 21:47:03 +0000478void sock_update_memcg(struct sock *sk)
479{
Glauber Costa376be5f2012-01-20 04:57:14 +0000480 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000481 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700482 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000483
484 BUG_ON(!sk->sk_prot->proto_cgroup);
485
Glauber Costaf3f511e2012-01-05 20:16:39 +0000486 /* Socket cloning can throw us here with sk_cgrp already
487 * filled. It won't however, necessarily happen from
488 * process context. So the test for root memcg given
489 * the current task's memcg won't help us in this case.
490 *
491 * Respecting the original socket's memcg is a better
492 * decision in this case.
493 */
494 if (sk->sk_cgrp) {
495 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
496 mem_cgroup_get(sk->sk_cgrp->memcg);
497 return;
498 }
499
Glauber Costae1aab162011-12-11 21:47:03 +0000500 rcu_read_lock();
501 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700502 cg_proto = sk->sk_prot->proto_cgroup(memcg);
503 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000504 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700505 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000506 }
507 rcu_read_unlock();
508 }
509}
510EXPORT_SYMBOL(sock_update_memcg);
511
512void sock_release_memcg(struct sock *sk)
513{
Glauber Costa376be5f2012-01-20 04:57:14 +0000514 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000515 struct mem_cgroup *memcg;
516 WARN_ON(!sk->sk_cgrp->memcg);
517 memcg = sk->sk_cgrp->memcg;
518 mem_cgroup_put(memcg);
519 }
520}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000521
522struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
523{
524 if (!memcg || mem_cgroup_is_root(memcg))
525 return NULL;
526
527 return &memcg->tcp_mem.cg_proto;
528}
529EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000530
Glauber Costa3f134612012-05-29 15:07:11 -0700531static void disarm_sock_keys(struct mem_cgroup *memcg)
532{
533 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
534 return;
535 static_key_slow_dec(&memcg_socket_limit_enabled);
536}
537#else
538static void disarm_sock_keys(struct mem_cgroup *memcg)
539{
540}
541#endif
542
Glauber Costaa8964b92012-12-18 14:22:09 -0800543#ifdef CONFIG_MEMCG_KMEM
544struct static_key memcg_kmem_enabled_key;
545
546static void disarm_kmem_keys(struct mem_cgroup *memcg)
547{
548 if (memcg_kmem_is_active(memcg))
549 static_key_slow_dec(&memcg_kmem_enabled_key);
550}
551#else
552static void disarm_kmem_keys(struct mem_cgroup *memcg)
553{
554}
555#endif /* CONFIG_MEMCG_KMEM */
556
557static void disarm_static_keys(struct mem_cgroup *memcg)
558{
559 disarm_sock_keys(memcg);
560 disarm_kmem_keys(memcg);
561}
562
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700563static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800564
Balbir Singhf64c3f52009-09-23 15:56:37 -0700565static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700566mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700567{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700568 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700569}
570
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700571struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100572{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700573 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100574}
575
Balbir Singhf64c3f52009-09-23 15:56:37 -0700576static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700577page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700578{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700579 int nid = page_to_nid(page);
580 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700581
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700582 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700583}
584
585static struct mem_cgroup_tree_per_zone *
586soft_limit_tree_node_zone(int nid, int zid)
587{
588 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
589}
590
591static struct mem_cgroup_tree_per_zone *
592soft_limit_tree_from_page(struct page *page)
593{
594 int nid = page_to_nid(page);
595 int zid = page_zonenum(page);
596
597 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
598}
599
600static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700601__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700602 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700603 struct mem_cgroup_tree_per_zone *mctz,
604 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700605{
606 struct rb_node **p = &mctz->rb_root.rb_node;
607 struct rb_node *parent = NULL;
608 struct mem_cgroup_per_zone *mz_node;
609
610 if (mz->on_tree)
611 return;
612
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700613 mz->usage_in_excess = new_usage_in_excess;
614 if (!mz->usage_in_excess)
615 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700616 while (*p) {
617 parent = *p;
618 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
619 tree_node);
620 if (mz->usage_in_excess < mz_node->usage_in_excess)
621 p = &(*p)->rb_left;
622 /*
623 * We can't avoid mem cgroups that are over their soft
624 * limit by the same amount
625 */
626 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
627 p = &(*p)->rb_right;
628 }
629 rb_link_node(&mz->tree_node, parent, p);
630 rb_insert_color(&mz->tree_node, &mctz->rb_root);
631 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700632}
633
634static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700635__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700636 struct mem_cgroup_per_zone *mz,
637 struct mem_cgroup_tree_per_zone *mctz)
638{
639 if (!mz->on_tree)
640 return;
641 rb_erase(&mz->tree_node, &mctz->rb_root);
642 mz->on_tree = false;
643}
644
645static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700646mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700647 struct mem_cgroup_per_zone *mz,
648 struct mem_cgroup_tree_per_zone *mctz)
649{
650 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700652 spin_unlock(&mctz->lock);
653}
654
Balbir Singhf64c3f52009-09-23 15:56:37 -0700655
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700656static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700657{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700658 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700659 struct mem_cgroup_per_zone *mz;
660 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700661 int nid = page_to_nid(page);
662 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700663 mctz = soft_limit_tree_from_page(page);
664
665 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700666 * Necessary to update all ancestors when hierarchy is used.
667 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700668 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700669 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
670 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
671 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700672 /*
673 * We have to update the tree if mz is on RB-tree or
674 * mem is over its softlimit.
675 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700676 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700677 spin_lock(&mctz->lock);
678 /* if on-tree, remove it */
679 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700680 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700681 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700682 * Insert again. mz->usage_in_excess will be updated.
683 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700684 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700686 spin_unlock(&mctz->lock);
687 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700688 }
689}
690
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700691static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700692{
693 int node, zone;
694 struct mem_cgroup_per_zone *mz;
695 struct mem_cgroup_tree_per_zone *mctz;
696
Bob Liu3ed28fa2012-01-12 17:19:04 -0800697 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700698 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700699 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700700 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700701 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700702 }
703 }
704}
705
Balbir Singh4e416952009-09-23 15:56:39 -0700706static struct mem_cgroup_per_zone *
707__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
708{
709 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700710 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700711
712retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700713 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700714 rightmost = rb_last(&mctz->rb_root);
715 if (!rightmost)
716 goto done; /* Nothing to reclaim from */
717
718 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
719 /*
720 * Remove the node now but someone else can add it back,
721 * we will to add it back at the end of reclaim to its correct
722 * position in the tree.
723 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700724 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
725 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
726 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700727 goto retry;
728done:
729 return mz;
730}
731
732static struct mem_cgroup_per_zone *
733mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
734{
735 struct mem_cgroup_per_zone *mz;
736
737 spin_lock(&mctz->lock);
738 mz = __mem_cgroup_largest_soft_limit_node(mctz);
739 spin_unlock(&mctz->lock);
740 return mz;
741}
742
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700743/*
744 * Implementation Note: reading percpu statistics for memcg.
745 *
746 * Both of vmstat[] and percpu_counter has threshold and do periodic
747 * synchronization to implement "quick" read. There are trade-off between
748 * reading cost and precision of value. Then, we may have a chance to implement
749 * a periodic synchronizion of counter in memcg's counter.
750 *
751 * But this _read() function is used for user interface now. The user accounts
752 * memory usage by memory cgroup and he _always_ requires exact value because
753 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
754 * have to visit all online cpus and make sum. So, for now, unnecessary
755 * synchronization is not implemented. (just implemented for cpu hotplug)
756 *
757 * If there are kernel internal actions which can make use of some not-exact
758 * value, and reading all cpu value can be performance bottleneck in some
759 * common workload, threashold and synchonization as vmstat[] should be
760 * implemented.
761 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700762static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700763 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800764{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700765 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800766 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800767
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700768 get_online_cpus();
769 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700770 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700771#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700772 spin_lock(&memcg->pcp_counter_lock);
773 val += memcg->nocpu_base.count[idx];
774 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700775#endif
776 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800777 return val;
778}
779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700780static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700781 bool charge)
782{
783 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700784 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700785}
786
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700787static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700788 enum mem_cgroup_events_index idx)
789{
790 unsigned long val = 0;
791 int cpu;
792
793 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700794 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700795#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700796 spin_lock(&memcg->pcp_counter_lock);
797 val += memcg->nocpu_base.events[idx];
798 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700799#endif
800 return val;
801}
802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700803static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700804 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800805{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800806 preempt_disable();
807
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700808 /*
809 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
810 * counted as CACHE even if it's on ANON LRU.
811 */
812 if (anon)
813 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700814 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800815 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700816 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700817 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700818
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800819 /* pagein of a big page is an event. So, ignore page size */
820 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700821 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800822 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700823 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800824 nr_pages = -nr_pages; /* for event */
825 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800826
Johannes Weiner13114712012-05-29 15:07:07 -0700827 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800828
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800829 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800830}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800831
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700832unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700833mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700834{
835 struct mem_cgroup_per_zone *mz;
836
837 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
838 return mz->lru_size[lru];
839}
840
841static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700843 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700844{
845 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700846 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700847 unsigned long ret = 0;
848
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700849 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700850
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700851 for_each_lru(lru) {
852 if (BIT(lru) & lru_mask)
853 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700854 }
855 return ret;
856}
857
858static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700859mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700860 int nid, unsigned int lru_mask)
861{
Ying Han889976d2011-05-26 16:25:33 -0700862 u64 total = 0;
863 int zid;
864
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700865 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700866 total += mem_cgroup_zone_nr_lru_pages(memcg,
867 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700868
Ying Han889976d2011-05-26 16:25:33 -0700869 return total;
870}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700871
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700872static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700873 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800874{
Ying Han889976d2011-05-26 16:25:33 -0700875 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800876 u64 total = 0;
877
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800878 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700879 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800880 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800881}
882
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800883static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
884 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800885{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700886 unsigned long val, next;
887
Johannes Weiner13114712012-05-29 15:07:07 -0700888 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700889 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700890 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800891 if ((long)next - (long)val < 0) {
892 switch (target) {
893 case MEM_CGROUP_TARGET_THRESH:
894 next = val + THRESHOLDS_EVENTS_TARGET;
895 break;
896 case MEM_CGROUP_TARGET_SOFTLIMIT:
897 next = val + SOFTLIMIT_EVENTS_TARGET;
898 break;
899 case MEM_CGROUP_TARGET_NUMAINFO:
900 next = val + NUMAINFO_EVENTS_TARGET;
901 break;
902 default:
903 break;
904 }
905 __this_cpu_write(memcg->stat->targets[target], next);
906 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700907 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800908 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800909}
910
911/*
912 * Check events in order.
913 *
914 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700915static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800916{
Steven Rostedt47994012011-11-02 13:38:33 -0700917 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800918 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800919 if (unlikely(mem_cgroup_event_ratelimit(memcg,
920 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800921 bool do_softlimit;
922 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800923
924 do_softlimit = mem_cgroup_event_ratelimit(memcg,
925 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700926#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800927 do_numainfo = mem_cgroup_event_ratelimit(memcg,
928 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700929#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800930 preempt_enable();
931
932 mem_cgroup_threshold(memcg);
933 if (unlikely(do_softlimit))
934 mem_cgroup_update_tree(memcg, page);
935#if MAX_NUMNODES > 1
936 if (unlikely(do_numainfo))
937 atomic_inc(&memcg->numainfo_events);
938#endif
939 } else
940 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800941}
942
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000943struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800944{
Wanpeng Lib2145142012-07-31 16:46:01 -0700945 return mem_cgroup_from_css(
946 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800947}
948
Balbir Singhcf475ad2008-04-29 01:00:16 -0700949struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800950{
Balbir Singh31a78f22008-09-28 23:09:31 +0100951 /*
952 * mm_update_next_owner() may clear mm->owner to NULL
953 * if it races with swapoff, page migration, etc.
954 * So this can be called with p == NULL.
955 */
956 if (unlikely(!p))
957 return NULL;
958
Wanpeng Lib2145142012-07-31 16:46:01 -0700959 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800960}
961
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700962struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800963{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700964 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700965
966 if (!mm)
967 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800968 /*
969 * Because we have no locks, mm->owner's may be being moved to other
970 * cgroup. We use css_tryget() here even if this looks
971 * pessimistic (rather than adding locks here).
972 */
973 rcu_read_lock();
974 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700975 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
976 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800977 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700978 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800979 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700980 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800981}
982
Johannes Weiner56600482012-01-12 17:17:59 -0800983/**
984 * mem_cgroup_iter - iterate over memory cgroup hierarchy
985 * @root: hierarchy root
986 * @prev: previously returned memcg, NULL on first invocation
987 * @reclaim: cookie for shared reclaim walks, NULL for full walks
988 *
989 * Returns references to children of the hierarchy below @root, or
990 * @root itself, or %NULL after a full round-trip.
991 *
992 * Caller must pass the return value in @prev on subsequent
993 * invocations for reference counting, or use mem_cgroup_iter_break()
994 * to cancel a hierarchy walk before the round-trip is complete.
995 *
996 * Reclaimers can specify a zone and a priority level in @reclaim to
997 * divide up the memcgs in the hierarchy among all concurrent
998 * reclaimers operating on the same zone and priority.
999 */
1000struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1001 struct mem_cgroup *prev,
1002 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001003{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001004 struct mem_cgroup *memcg = NULL;
1005 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001006
Johannes Weiner56600482012-01-12 17:17:59 -08001007 if (mem_cgroup_disabled())
1008 return NULL;
1009
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001010 if (!root)
1011 root = root_mem_cgroup;
1012
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001013 if (prev && !reclaim)
1014 id = css_id(&prev->css);
1015
1016 if (prev && prev != root)
1017 css_put(&prev->css);
1018
1019 if (!root->use_hierarchy && root != root_mem_cgroup) {
1020 if (prev)
1021 return NULL;
1022 return root;
1023 }
1024
1025 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001026 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001027 struct cgroup_subsys_state *css;
1028
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001029 if (reclaim) {
1030 int nid = zone_to_nid(reclaim->zone);
1031 int zid = zone_idx(reclaim->zone);
1032 struct mem_cgroup_per_zone *mz;
1033
1034 mz = mem_cgroup_zoneinfo(root, nid, zid);
1035 iter = &mz->reclaim_iter[reclaim->priority];
1036 if (prev && reclaim->generation != iter->generation)
1037 return NULL;
1038 id = iter->position;
1039 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001040
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001041 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001042 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1043 if (css) {
1044 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001045 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001046 } else
1047 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001048 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001049
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001050 if (reclaim) {
1051 iter->position = id;
1052 if (!css)
1053 iter->generation++;
1054 else if (!prev && memcg)
1055 reclaim->generation = iter->generation;
1056 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001057
1058 if (prev && !css)
1059 return NULL;
1060 }
1061 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001062}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001063
Johannes Weiner56600482012-01-12 17:17:59 -08001064/**
1065 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1066 * @root: hierarchy root
1067 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1068 */
1069void mem_cgroup_iter_break(struct mem_cgroup *root,
1070 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001071{
1072 if (!root)
1073 root = root_mem_cgroup;
1074 if (prev && prev != root)
1075 css_put(&prev->css);
1076}
1077
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001078/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001079 * Iteration constructs for visiting all cgroups (under a tree). If
1080 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1081 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001082 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001083#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001084 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001085 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001086 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001087
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001088#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001089 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001090 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001091 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001092
David Rientjes68ae5642012-12-12 13:51:57 -08001093void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001094{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001095 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001096
Ying Han456f9982011-05-26 16:25:38 -07001097 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001098 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1099 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001100 goto out;
1101
1102 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001103 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001104 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1105 break;
1106 case PGMAJFAULT:
1107 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001108 break;
1109 default:
1110 BUG();
1111 }
1112out:
1113 rcu_read_unlock();
1114}
David Rientjes68ae5642012-12-12 13:51:57 -08001115EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001116
Johannes Weiner925b7672012-01-12 17:18:15 -08001117/**
1118 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1119 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001120 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001121 *
1122 * Returns the lru list vector holding pages for the given @zone and
1123 * @mem. This can be the global zone lruvec, if the memory controller
1124 * is disabled.
1125 */
1126struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1127 struct mem_cgroup *memcg)
1128{
1129 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001130 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001131
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001132 if (mem_cgroup_disabled()) {
1133 lruvec = &zone->lruvec;
1134 goto out;
1135 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001136
1137 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001138 lruvec = &mz->lruvec;
1139out:
1140 /*
1141 * Since a node can be onlined after the mem_cgroup was created,
1142 * we have to be prepared to initialize lruvec->zone here;
1143 * and if offlined then reonlined, we need to reinitialize it.
1144 */
1145 if (unlikely(lruvec->zone != zone))
1146 lruvec->zone = zone;
1147 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001148}
1149
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001150/*
1151 * Following LRU functions are allowed to be used without PCG_LOCK.
1152 * Operations are called by routine of global LRU independently from memcg.
1153 * What we have to take care of here is validness of pc->mem_cgroup.
1154 *
1155 * Changes to pc->mem_cgroup happens when
1156 * 1. charge
1157 * 2. moving account
1158 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1159 * It is added to LRU before charge.
1160 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1161 * When moving account, the page is not on LRU. It's isolated.
1162 */
1163
Johannes Weiner925b7672012-01-12 17:18:15 -08001164/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001165 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001166 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001167 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001168 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001169struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001170{
1171 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001172 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001173 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001174 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001175
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001176 if (mem_cgroup_disabled()) {
1177 lruvec = &zone->lruvec;
1178 goto out;
1179 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001180
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001181 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001182 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001183
1184 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001185 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001186 * an uncharged page off lru does nothing to secure
1187 * its former mem_cgroup from sudden removal.
1188 *
1189 * Our caller holds lru_lock, and PageCgroupUsed is updated
1190 * under page_cgroup lock: between them, they make all uses
1191 * of pc->mem_cgroup safe.
1192 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001193 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001194 pc->mem_cgroup = memcg = root_mem_cgroup;
1195
Johannes Weiner925b7672012-01-12 17:18:15 -08001196 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001197 lruvec = &mz->lruvec;
1198out:
1199 /*
1200 * Since a node can be onlined after the mem_cgroup was created,
1201 * we have to be prepared to initialize lruvec->zone here;
1202 * and if offlined then reonlined, we need to reinitialize it.
1203 */
1204 if (unlikely(lruvec->zone != zone))
1205 lruvec->zone = zone;
1206 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001207}
1208
1209/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001210 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1211 * @lruvec: mem_cgroup per zone lru vector
1212 * @lru: index of lru list the page is sitting on
1213 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001214 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001215 * This function must be called when a page is added to or removed from an
1216 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001217 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001218void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1219 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001220{
1221 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001222 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001223
1224 if (mem_cgroup_disabled())
1225 return;
1226
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001227 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1228 lru_size = mz->lru_size + lru;
1229 *lru_size += nr_pages;
1230 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001231}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001232
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001233/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001234 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001235 * hierarchy subtree
1236 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001237bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1238 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001239{
Johannes Weiner91c637342012-05-29 15:06:24 -07001240 if (root_memcg == memcg)
1241 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001242 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001243 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001244 return css_is_ancestor(&memcg->css, &root_memcg->css);
1245}
1246
1247static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1248 struct mem_cgroup *memcg)
1249{
1250 bool ret;
1251
Johannes Weiner91c637342012-05-29 15:06:24 -07001252 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001253 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001254 rcu_read_unlock();
1255 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001256}
1257
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001258int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001259{
1260 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001261 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001262 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001263
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001264 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001265 if (p) {
1266 curr = try_get_mem_cgroup_from_mm(p->mm);
1267 task_unlock(p);
1268 } else {
1269 /*
1270 * All threads may have already detached their mm's, but the oom
1271 * killer still needs to detect if they have already been oom
1272 * killed to prevent needlessly killing additional tasks.
1273 */
1274 task_lock(task);
1275 curr = mem_cgroup_from_task(task);
1276 if (curr)
1277 css_get(&curr->css);
1278 task_unlock(task);
1279 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001280 if (!curr)
1281 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001282 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001283 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001284 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001285 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1286 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001287 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001288 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001289 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001290 return ret;
1291}
1292
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001293int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001294{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001295 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001296 unsigned long inactive;
1297 unsigned long active;
1298 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001299
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001300 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1301 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001302
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001303 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1304 if (gb)
1305 inactive_ratio = int_sqrt(10 * gb);
1306 else
1307 inactive_ratio = 1;
1308
Johannes Weiner9b272972011-11-02 13:38:23 -07001309 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001310}
1311
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001312int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001313{
1314 unsigned long active;
1315 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001316
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001317 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1318 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001319
1320 return (active > inactive);
1321}
1322
Balbir Singh6d61ef42009-01-07 18:08:06 -08001323#define mem_cgroup_from_res_counter(counter, member) \
1324 container_of(counter, struct mem_cgroup, member)
1325
Johannes Weiner19942822011-02-01 15:52:43 -08001326/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001327 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001328 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001329 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001330 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001331 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001332 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001333static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001334{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001335 unsigned long long margin;
1336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001337 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001338 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001339 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001340 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001341}
1342
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001343int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001344{
1345 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001346
1347 /* root ? */
1348 if (cgrp->parent == NULL)
1349 return vm_swappiness;
1350
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001351 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001352}
1353
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001354/*
1355 * memcg->moving_account is used for checking possibility that some thread is
1356 * calling move_account(). When a thread on CPU-A starts moving pages under
1357 * a memcg, other threads should check memcg->moving_account under
1358 * rcu_read_lock(), like this:
1359 *
1360 * CPU-A CPU-B
1361 * rcu_read_lock()
1362 * memcg->moving_account+1 if (memcg->mocing_account)
1363 * take heavy locks.
1364 * synchronize_rcu() update something.
1365 * rcu_read_unlock()
1366 * start move here.
1367 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001368
1369/* for quick checking without looking up memcg */
1370atomic_t memcg_moving __read_mostly;
1371
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001372static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001373{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001374 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001375 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001376 synchronize_rcu();
1377}
1378
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001379static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001380{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001381 /*
1382 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1383 * We check NULL in callee rather than caller.
1384 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001385 if (memcg) {
1386 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001387 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001388 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001389}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001390
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001391/*
1392 * 2 routines for checking "mem" is under move_account() or not.
1393 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001394 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1395 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001396 * pc->mem_cgroup may be overwritten.
1397 *
1398 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1399 * under hierarchy of moving cgroups. This is for
1400 * waiting at hith-memory prressure caused by "move".
1401 */
1402
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001403static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001404{
1405 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001406 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001407}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001408
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001409static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001410{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001411 struct mem_cgroup *from;
1412 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001413 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001414 /*
1415 * Unlike task_move routines, we access mc.to, mc.from not under
1416 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1417 */
1418 spin_lock(&mc.lock);
1419 from = mc.from;
1420 to = mc.to;
1421 if (!from)
1422 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001423
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001424 ret = mem_cgroup_same_or_subtree(memcg, from)
1425 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001426unlock:
1427 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001428 return ret;
1429}
1430
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001431static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001432{
1433 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001434 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001435 DEFINE_WAIT(wait);
1436 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1437 /* moving charge context might have finished. */
1438 if (mc.moving_task)
1439 schedule();
1440 finish_wait(&mc.waitq, &wait);
1441 return true;
1442 }
1443 }
1444 return false;
1445}
1446
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001447/*
1448 * Take this lock when
1449 * - a code tries to modify page's memcg while it's USED.
1450 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001451 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001452 */
1453static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1454 unsigned long *flags)
1455{
1456 spin_lock_irqsave(&memcg->move_lock, *flags);
1457}
1458
1459static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1460 unsigned long *flags)
1461{
1462 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1463}
1464
Balbir Singhe2224322009-04-02 16:57:39 -07001465/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001466 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001467 * @memcg: The memory cgroup that went over limit
1468 * @p: Task that is going to be killed
1469 *
1470 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1471 * enabled
1472 */
1473void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1474{
1475 struct cgroup *task_cgrp;
1476 struct cgroup *mem_cgrp;
1477 /*
1478 * Need a buffer in BSS, can't rely on allocations. The code relies
1479 * on the assumption that OOM is serialized for memory controller.
1480 * If this assumption is broken, revisit this code.
1481 */
1482 static char memcg_name[PATH_MAX];
1483 int ret;
1484
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001485 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001486 return;
1487
Balbir Singhe2224322009-04-02 16:57:39 -07001488 rcu_read_lock();
1489
1490 mem_cgrp = memcg->css.cgroup;
1491 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1492
1493 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1494 if (ret < 0) {
1495 /*
1496 * Unfortunately, we are unable to convert to a useful name
1497 * But we'll still print out the usage information
1498 */
1499 rcu_read_unlock();
1500 goto done;
1501 }
1502 rcu_read_unlock();
1503
1504 printk(KERN_INFO "Task in %s killed", memcg_name);
1505
1506 rcu_read_lock();
1507 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1508 if (ret < 0) {
1509 rcu_read_unlock();
1510 goto done;
1511 }
1512 rcu_read_unlock();
1513
1514 /*
1515 * Continues from above, so we don't need an KERN_ level
1516 */
1517 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1518done:
1519
1520 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1521 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1522 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1523 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1524 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1525 "failcnt %llu\n",
1526 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1527 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1528 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Glauber Costa510fc4e2012-12-18 14:21:47 -08001529 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1530 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1531 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1532 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Balbir Singhe2224322009-04-02 16:57:39 -07001533}
1534
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001535/*
1536 * This function returns the number of memcg under hierarchy tree. Returns
1537 * 1(self count) if no children.
1538 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001539static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001540{
1541 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001542 struct mem_cgroup *iter;
1543
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001544 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001545 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001546 return num;
1547}
1548
Balbir Singh6d61ef42009-01-07 18:08:06 -08001549/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001550 * Return the memory (and swap, if configured) limit for a memcg.
1551 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001552static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001553{
1554 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001555
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001556 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001557
David Rientjesa63d83f2010-08-09 17:19:46 -07001558 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001559 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001560 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001561 if (mem_cgroup_swappiness(memcg)) {
1562 u64 memsw;
1563
1564 limit += total_swap_pages << PAGE_SHIFT;
1565 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1566
1567 /*
1568 * If memsw is finite and limits the amount of swap space
1569 * available to this memcg, return that limit.
1570 */
1571 limit = min(limit, memsw);
1572 }
1573
1574 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001575}
1576
David Rientjes19965462012-12-11 16:00:26 -08001577static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1578 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001579{
1580 struct mem_cgroup *iter;
1581 unsigned long chosen_points = 0;
1582 unsigned long totalpages;
1583 unsigned int points = 0;
1584 struct task_struct *chosen = NULL;
1585
David Rientjes876aafb2012-07-31 16:43:48 -07001586 /*
1587 * If current has a pending SIGKILL, then automatically select it. The
1588 * goal is to allow it to allocate so that it may quickly exit and free
1589 * its memory.
1590 */
1591 if (fatal_signal_pending(current)) {
1592 set_thread_flag(TIF_MEMDIE);
1593 return;
1594 }
1595
1596 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001597 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1598 for_each_mem_cgroup_tree(iter, memcg) {
1599 struct cgroup *cgroup = iter->css.cgroup;
1600 struct cgroup_iter it;
1601 struct task_struct *task;
1602
1603 cgroup_iter_start(cgroup, &it);
1604 while ((task = cgroup_iter_next(cgroup, &it))) {
1605 switch (oom_scan_process_thread(task, totalpages, NULL,
1606 false)) {
1607 case OOM_SCAN_SELECT:
1608 if (chosen)
1609 put_task_struct(chosen);
1610 chosen = task;
1611 chosen_points = ULONG_MAX;
1612 get_task_struct(chosen);
1613 /* fall through */
1614 case OOM_SCAN_CONTINUE:
1615 continue;
1616 case OOM_SCAN_ABORT:
1617 cgroup_iter_end(cgroup, &it);
1618 mem_cgroup_iter_break(memcg, iter);
1619 if (chosen)
1620 put_task_struct(chosen);
1621 return;
1622 case OOM_SCAN_OK:
1623 break;
1624 };
1625 points = oom_badness(task, memcg, NULL, totalpages);
1626 if (points > chosen_points) {
1627 if (chosen)
1628 put_task_struct(chosen);
1629 chosen = task;
1630 chosen_points = points;
1631 get_task_struct(chosen);
1632 }
1633 }
1634 cgroup_iter_end(cgroup, &it);
1635 }
1636
1637 if (!chosen)
1638 return;
1639 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001640 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1641 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001642}
1643
Johannes Weiner56600482012-01-12 17:17:59 -08001644static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1645 gfp_t gfp_mask,
1646 unsigned long flags)
1647{
1648 unsigned long total = 0;
1649 bool noswap = false;
1650 int loop;
1651
1652 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1653 noswap = true;
1654 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1655 noswap = true;
1656
1657 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1658 if (loop)
1659 drain_all_stock_async(memcg);
1660 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1661 /*
1662 * Allow limit shrinkers, which are triggered directly
1663 * by userspace, to catch signals and stop reclaim
1664 * after minimal progress, regardless of the margin.
1665 */
1666 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1667 break;
1668 if (mem_cgroup_margin(memcg))
1669 break;
1670 /*
1671 * If nothing was reclaimed after two attempts, there
1672 * may be no reclaimable pages in this hierarchy.
1673 */
1674 if (loop && !total)
1675 break;
1676 }
1677 return total;
1678}
1679
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001680/**
1681 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001682 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001683 * @nid: the node ID to be checked.
1684 * @noswap : specify true here if the user wants flle only information.
1685 *
1686 * This function returns whether the specified memcg contains any
1687 * reclaimable pages on a node. Returns true if there are any reclaimable
1688 * pages in the node.
1689 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001690static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001691 int nid, bool noswap)
1692{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001693 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001694 return true;
1695 if (noswap || !total_swap_pages)
1696 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001697 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001698 return true;
1699 return false;
1700
1701}
Ying Han889976d2011-05-26 16:25:33 -07001702#if MAX_NUMNODES > 1
1703
1704/*
1705 * Always updating the nodemask is not very good - even if we have an empty
1706 * list or the wrong list here, we can start from some node and traverse all
1707 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1708 *
1709 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001710static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001711{
1712 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001713 /*
1714 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1715 * pagein/pageout changes since the last update.
1716 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001717 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001718 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001719 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001720 return;
1721
Ying Han889976d2011-05-26 16:25:33 -07001722 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001723 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001724
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001725 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001726
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001727 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1728 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001729 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001730
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001731 atomic_set(&memcg->numainfo_events, 0);
1732 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001733}
1734
1735/*
1736 * Selecting a node where we start reclaim from. Because what we need is just
1737 * reducing usage counter, start from anywhere is O,K. Considering
1738 * memory reclaim from current node, there are pros. and cons.
1739 *
1740 * Freeing memory from current node means freeing memory from a node which
1741 * we'll use or we've used. So, it may make LRU bad. And if several threads
1742 * hit limits, it will see a contention on a node. But freeing from remote
1743 * node means more costs for memory reclaim because of memory latency.
1744 *
1745 * Now, we use round-robin. Better algorithm is welcomed.
1746 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001747int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001748{
1749 int node;
1750
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001751 mem_cgroup_may_update_nodemask(memcg);
1752 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001753
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001754 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001755 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001756 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001757 /*
1758 * We call this when we hit limit, not when pages are added to LRU.
1759 * No LRU may hold pages because all pages are UNEVICTABLE or
1760 * memcg is too small and all pages are not on LRU. In that case,
1761 * we use curret node.
1762 */
1763 if (unlikely(node == MAX_NUMNODES))
1764 node = numa_node_id();
1765
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001766 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001767 return node;
1768}
1769
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001770/*
1771 * Check all nodes whether it contains reclaimable pages or not.
1772 * For quick scan, we make use of scan_nodes. This will allow us to skip
1773 * unused nodes. But scan_nodes is lazily updated and may not cotain
1774 * enough new information. We need to do double check.
1775 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001776static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001777{
1778 int nid;
1779
1780 /*
1781 * quick check...making use of scan_node.
1782 * We can skip unused nodes.
1783 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001784 if (!nodes_empty(memcg->scan_nodes)) {
1785 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001786 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001787 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001788
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001789 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001790 return true;
1791 }
1792 }
1793 /*
1794 * Check rest of nodes.
1795 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001796 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001797 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001798 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001800 return true;
1801 }
1802 return false;
1803}
1804
Ying Han889976d2011-05-26 16:25:33 -07001805#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001806int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001807{
1808 return 0;
1809}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001810
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001811static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001812{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001814}
Ying Han889976d2011-05-26 16:25:33 -07001815#endif
1816
Johannes Weiner56600482012-01-12 17:17:59 -08001817static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1818 struct zone *zone,
1819 gfp_t gfp_mask,
1820 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001821{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001822 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001823 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001824 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001825 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001826 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001827 struct mem_cgroup_reclaim_cookie reclaim = {
1828 .zone = zone,
1829 .priority = 0,
1830 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001831
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001832 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001833
Balbir Singh4e416952009-09-23 15:56:39 -07001834 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001835 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001836 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001837 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001838 if (loop >= 2) {
1839 /*
1840 * If we have not been able to reclaim
1841 * anything, it might because there are
1842 * no reclaimable pages under this hierarchy
1843 */
Johannes Weiner56600482012-01-12 17:17:59 -08001844 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001845 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001846 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001847 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001848 * excess >> 2 is not to excessive so as to
1849 * reclaim too much, nor too less that we keep
1850 * coming back to reclaim from this cgroup
1851 */
1852 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001853 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001854 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001855 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001856 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001857 }
Johannes Weiner56600482012-01-12 17:17:59 -08001858 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001859 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001860 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1861 zone, &nr_scanned);
1862 *total_scanned += nr_scanned;
1863 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001864 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001865 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001866 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001867 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001868}
1869
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001870/*
1871 * Check OOM-Killer is already running under our hierarchy.
1872 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001873 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001874 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001875static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001876{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001877 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001878
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001879 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001880 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001881 /*
1882 * this subtree of our hierarchy is already locked
1883 * so we cannot give a lock.
1884 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001885 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001886 mem_cgroup_iter_break(memcg, iter);
1887 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001888 } else
1889 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001890 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001891
Michal Hocko79dfdac2011-07-26 16:08:23 -07001892 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001893 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001894
1895 /*
1896 * OK, we failed to lock the whole subtree so we have to clean up
1897 * what we set up to the failing subtree
1898 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001899 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001900 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001901 mem_cgroup_iter_break(memcg, iter);
1902 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001903 }
1904 iter->oom_lock = false;
1905 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001906 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001907}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001908
Michal Hocko79dfdac2011-07-26 16:08:23 -07001909/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001910 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001911 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001912static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001913{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001914 struct mem_cgroup *iter;
1915
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001916 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001917 iter->oom_lock = false;
1918 return 0;
1919}
1920
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001921static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001922{
1923 struct mem_cgroup *iter;
1924
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001925 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001926 atomic_inc(&iter->under_oom);
1927}
1928
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001929static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001930{
1931 struct mem_cgroup *iter;
1932
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001933 /*
1934 * When a new child is created while the hierarchy is under oom,
1935 * mem_cgroup_oom_lock() may not be called. We have to use
1936 * atomic_add_unless() here.
1937 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001938 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001939 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001940}
1941
Michal Hocko1af8efe2011-07-26 16:08:24 -07001942static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001943static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1944
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001945struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001946 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001947 wait_queue_t wait;
1948};
1949
1950static int memcg_oom_wake_function(wait_queue_t *wait,
1951 unsigned mode, int sync, void *arg)
1952{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001953 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1954 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001955 struct oom_wait_info *oom_wait_info;
1956
1957 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001958 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001959
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001960 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001961 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001962 * Then we can use css_is_ancestor without taking care of RCU.
1963 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001964 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1965 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001966 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001967 return autoremove_wake_function(wait, mode, sync, arg);
1968}
1969
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001970static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001971{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001972 /* for filtering, pass "memcg" as argument. */
1973 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001974}
1975
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001976static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001977{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001978 if (memcg && atomic_read(&memcg->under_oom))
1979 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001980}
1981
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001982/*
1983 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1984 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001985static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1986 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001988 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001989 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001990
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001991 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001992 owait.wait.flags = 0;
1993 owait.wait.func = memcg_oom_wake_function;
1994 owait.wait.private = current;
1995 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001996 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001997 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001998
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001999 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002000 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002001 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002002 /*
2003 * Even if signal_pending(), we can't quit charge() loop without
2004 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2005 * under OOM is always welcomed, use TASK_KILLABLE here.
2006 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002007 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002008 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002009 need_to_kill = false;
2010 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002011 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002012 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002013
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002014 if (need_to_kill) {
2015 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002016 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002017 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002018 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002019 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002020 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002021 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002022 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002023 mem_cgroup_oom_unlock(memcg);
2024 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002025 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002026
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002027 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002028
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002029 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2030 return false;
2031 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002032 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002033 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002034}
2035
Balbir Singhd69b0422009-06-17 16:26:34 -07002036/*
2037 * Currently used to update mapped file statistics, but the routine can be
2038 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002039 *
2040 * Notes: Race condition
2041 *
2042 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2043 * it tends to be costly. But considering some conditions, we doesn't need
2044 * to do so _always_.
2045 *
2046 * Considering "charge", lock_page_cgroup() is not required because all
2047 * file-stat operations happen after a page is attached to radix-tree. There
2048 * are no race with "charge".
2049 *
2050 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2051 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2052 * if there are race with "uncharge". Statistics itself is properly handled
2053 * by flags.
2054 *
2055 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002056 * small, we check mm->moving_account and detect there are possibility of race
2057 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002058 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002059
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002060void __mem_cgroup_begin_update_page_stat(struct page *page,
2061 bool *locked, unsigned long *flags)
2062{
2063 struct mem_cgroup *memcg;
2064 struct page_cgroup *pc;
2065
2066 pc = lookup_page_cgroup(page);
2067again:
2068 memcg = pc->mem_cgroup;
2069 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2070 return;
2071 /*
2072 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002073 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002074 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002075 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002076 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002077 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002078 return;
2079
2080 move_lock_mem_cgroup(memcg, flags);
2081 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2082 move_unlock_mem_cgroup(memcg, flags);
2083 goto again;
2084 }
2085 *locked = true;
2086}
2087
2088void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2089{
2090 struct page_cgroup *pc = lookup_page_cgroup(page);
2091
2092 /*
2093 * It's guaranteed that pc->mem_cgroup never changes while
2094 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002095 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002096 */
2097 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2098}
2099
Greg Thelen2a7106f2011-01-13 15:47:37 -08002100void mem_cgroup_update_page_stat(struct page *page,
2101 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002102{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002103 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002104 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002105 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002106
Johannes Weinercfa44942012-01-12 17:18:38 -08002107 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002108 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002109
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002110 memcg = pc->mem_cgroup;
2111 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002112 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002113
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002114 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002115 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002116 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002117 break;
2118 default:
2119 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002120 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002121
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002122 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002123}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002124
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002125/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002126 * size of first charge trial. "32" comes from vmscan.c's magic value.
2127 * TODO: maybe necessary to use big numbers in big irons.
2128 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002129#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002130struct memcg_stock_pcp {
2131 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002132 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002134 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002135#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002136};
2137static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002138static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002139
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002140/**
2141 * consume_stock: Try to consume stocked charge on this cpu.
2142 * @memcg: memcg to consume from.
2143 * @nr_pages: how many pages to charge.
2144 *
2145 * The charges will only happen if @memcg matches the current cpu's memcg
2146 * stock, and at least @nr_pages are available in that stock. Failure to
2147 * service an allocation will refill the stock.
2148 *
2149 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002150 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002151static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002152{
2153 struct memcg_stock_pcp *stock;
2154 bool ret = true;
2155
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002156 if (nr_pages > CHARGE_BATCH)
2157 return false;
2158
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002159 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002160 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2161 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002162 else /* need to call res_counter_charge */
2163 ret = false;
2164 put_cpu_var(memcg_stock);
2165 return ret;
2166}
2167
2168/*
2169 * Returns stocks cached in percpu to res_counter and reset cached information.
2170 */
2171static void drain_stock(struct memcg_stock_pcp *stock)
2172{
2173 struct mem_cgroup *old = stock->cached;
2174
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002175 if (stock->nr_pages) {
2176 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2177
2178 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002179 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002180 res_counter_uncharge(&old->memsw, bytes);
2181 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002182 }
2183 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002184}
2185
2186/*
2187 * This must be called under preempt disabled or must be called by
2188 * a thread which is pinned to local cpu.
2189 */
2190static void drain_local_stock(struct work_struct *dummy)
2191{
2192 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2193 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002194 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002195}
2196
2197/*
2198 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002199 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002201static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002202{
2203 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2204
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002205 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002207 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002208 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002209 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002210 put_cpu_var(memcg_stock);
2211}
2212
2213/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002214 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002215 * of the hierarchy under it. sync flag says whether we should block
2216 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002217 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002218static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002219{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002220 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002221
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002222 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002223 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002224 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002225 for_each_online_cpu(cpu) {
2226 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002227 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002228
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002229 memcg = stock->cached;
2230 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002231 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002232 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002233 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002234 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2235 if (cpu == curcpu)
2236 drain_local_stock(&stock->work);
2237 else
2238 schedule_work_on(cpu, &stock->work);
2239 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002240 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002241 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002242
2243 if (!sync)
2244 goto out;
2245
2246 for_each_online_cpu(cpu) {
2247 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002248 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002249 flush_work(&stock->work);
2250 }
2251out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002252 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002253}
2254
2255/*
2256 * Tries to drain stocked charges in other cpus. This function is asynchronous
2257 * and just put a work per cpu for draining localy on each cpu. Caller can
2258 * expects some charges will be back to res_counter later but cannot wait for
2259 * it.
2260 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002261static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002262{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002263 /*
2264 * If someone calls draining, avoid adding more kworker runs.
2265 */
2266 if (!mutex_trylock(&percpu_charge_mutex))
2267 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002268 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002269 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002270}
2271
2272/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002273static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002274{
2275 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002276 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002277 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002278 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279}
2280
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002281/*
2282 * This function drains percpu counter value from DEAD cpu and
2283 * move it to local cpu. Note that this function can be preempted.
2284 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002285static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002286{
2287 int i;
2288
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002289 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002290 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002291 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002292
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002293 per_cpu(memcg->stat->count[i], cpu) = 0;
2294 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002295 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002296 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002297 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002298
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002299 per_cpu(memcg->stat->events[i], cpu) = 0;
2300 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002301 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002302 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002303}
2304
2305static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002306 unsigned long action,
2307 void *hcpu)
2308{
2309 int cpu = (unsigned long)hcpu;
2310 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002311 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002312
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002313 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002314 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002315
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002316 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002317 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002318
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002319 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002320 mem_cgroup_drain_pcp_counter(iter, cpu);
2321
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002322 stock = &per_cpu(memcg_stock, cpu);
2323 drain_stock(stock);
2324 return NOTIFY_OK;
2325}
2326
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002327
2328/* See __mem_cgroup_try_charge() for details */
2329enum {
2330 CHARGE_OK, /* success */
2331 CHARGE_RETRY, /* need to retry but retry is not bad */
2332 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2333 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2334 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2335};
2336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002337static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002338 unsigned int nr_pages, unsigned int min_pages,
2339 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002340{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002341 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002342 struct mem_cgroup *mem_over_limit;
2343 struct res_counter *fail_res;
2344 unsigned long flags = 0;
2345 int ret;
2346
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002347 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002348
2349 if (likely(!ret)) {
2350 if (!do_swap_account)
2351 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002352 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353 if (likely(!ret))
2354 return CHARGE_OK;
2355
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002356 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002357 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2358 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2359 } else
2360 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002361 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002362 * Never reclaim on behalf of optional batching, retry with a
2363 * single page instead.
2364 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002365 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002366 return CHARGE_RETRY;
2367
2368 if (!(gfp_mask & __GFP_WAIT))
2369 return CHARGE_WOULDBLOCK;
2370
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002371 if (gfp_mask & __GFP_NORETRY)
2372 return CHARGE_NOMEM;
2373
Johannes Weiner56600482012-01-12 17:17:59 -08002374 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002375 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002376 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002377 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002378 * Even though the limit is exceeded at this point, reclaim
2379 * may have been able to free some pages. Retry the charge
2380 * before killing the task.
2381 *
2382 * Only for regular pages, though: huge pages are rather
2383 * unlikely to succeed so close to the limit, and we fall back
2384 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002385 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002386 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002387 return CHARGE_RETRY;
2388
2389 /*
2390 * At task move, charge accounts can be doubly counted. So, it's
2391 * better to wait until the end of task_move if something is going on.
2392 */
2393 if (mem_cgroup_wait_acct_move(mem_over_limit))
2394 return CHARGE_RETRY;
2395
2396 /* If we don't need to call oom-killer at el, return immediately */
2397 if (!oom_check)
2398 return CHARGE_NOMEM;
2399 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002400 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002401 return CHARGE_OOM_DIE;
2402
2403 return CHARGE_RETRY;
2404}
2405
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002406/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002407 * __mem_cgroup_try_charge() does
2408 * 1. detect memcg to be charged against from passed *mm and *ptr,
2409 * 2. update res_counter
2410 * 3. call memory reclaim if necessary.
2411 *
2412 * In some special case, if the task is fatal, fatal_signal_pending() or
2413 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2414 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2415 * as possible without any hazards. 2: all pages should have a valid
2416 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2417 * pointer, that is treated as a charge to root_mem_cgroup.
2418 *
2419 * So __mem_cgroup_try_charge() will return
2420 * 0 ... on success, filling *ptr with a valid memcg pointer.
2421 * -ENOMEM ... charge failure because of resource limits.
2422 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2423 *
2424 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2425 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002426 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002427static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002428 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002429 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002430 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002431 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002432{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002433 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002434 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002435 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002436 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002437
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002438 /*
2439 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2440 * in system level. So, allow to go ahead dying process in addition to
2441 * MEMDIE process.
2442 */
2443 if (unlikely(test_thread_flag(TIF_MEMDIE)
2444 || fatal_signal_pending(current)))
2445 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002446
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002447 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002448 * We always charge the cgroup the mm_struct belongs to.
2449 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002450 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002451 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002452 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002453 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002454 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002455again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002456 if (*ptr) { /* css should be a valid one */
2457 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002459 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002460 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002461 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002462 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002463 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002464 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002465
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002466 rcu_read_lock();
2467 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002468 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002469 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002471 * race with swapoff. Then, we have small risk of mis-accouning.
2472 * But such kind of mis-account by race always happens because
2473 * we don't have cgroup_mutex(). It's overkill and we allo that
2474 * small race, here.
2475 * (*) swapoff at el will charge against mm-struct not against
2476 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002477 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002478 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002479 if (!memcg)
2480 memcg = root_mem_cgroup;
2481 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002482 rcu_read_unlock();
2483 goto done;
2484 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002485 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002486 /*
2487 * It seems dagerous to access memcg without css_get().
2488 * But considering how consume_stok works, it's not
2489 * necessary. If consume_stock success, some charges
2490 * from this memcg are cached on this cpu. So, we
2491 * don't need to call css_get()/css_tryget() before
2492 * calling consume_stock().
2493 */
2494 rcu_read_unlock();
2495 goto done;
2496 }
2497 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002498 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002499 rcu_read_unlock();
2500 goto again;
2501 }
2502 rcu_read_unlock();
2503 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002504
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002505 do {
2506 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002507
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002508 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002509 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002510 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002511 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002512 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002513
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002514 oom_check = false;
2515 if (oom && !nr_oom_retries) {
2516 oom_check = true;
2517 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2518 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002519
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002520 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2521 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002522 switch (ret) {
2523 case CHARGE_OK:
2524 break;
2525 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002526 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002527 css_put(&memcg->css);
2528 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002529 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002530 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002531 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002532 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002533 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002534 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002535 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002536 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002537 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002538 /* If oom, we never return -ENOMEM */
2539 nr_oom_retries--;
2540 break;
2541 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002542 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002543 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002544 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002545 } while (ret != CHARGE_OK);
2546
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002547 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002548 refill_stock(memcg, batch - nr_pages);
2549 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002550done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002551 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002552 return 0;
2553nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002554 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002555 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002556bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002557 *ptr = root_mem_cgroup;
2558 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002559}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002560
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002561/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002562 * Somemtimes we have to undo a charge we got by try_charge().
2563 * This function is for that and do uncharge, put css's refcnt.
2564 * gotten by try_charge().
2565 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002567 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002568{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002569 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002570 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002571
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002572 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002573 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002574 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002575 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002576}
2577
2578/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002579 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2580 * This is useful when moving usage to parent cgroup.
2581 */
2582static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2583 unsigned int nr_pages)
2584{
2585 unsigned long bytes = nr_pages * PAGE_SIZE;
2586
2587 if (mem_cgroup_is_root(memcg))
2588 return;
2589
2590 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2591 if (do_swap_account)
2592 res_counter_uncharge_until(&memcg->memsw,
2593 memcg->memsw.parent, bytes);
2594}
2595
2596/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002597 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002598 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2599 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2600 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002601 */
2602static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2603{
2604 struct cgroup_subsys_state *css;
2605
2606 /* ID 0 is unused ID */
2607 if (!id)
2608 return NULL;
2609 css = css_lookup(&mem_cgroup_subsys, id);
2610 if (!css)
2611 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002612 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002613}
2614
Wu Fengguange42d9d52009-12-16 12:19:59 +01002615struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002616{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002617 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002618 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002619 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002620 swp_entry_t ent;
2621
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002622 VM_BUG_ON(!PageLocked(page));
2623
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002624 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002625 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002626 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002627 memcg = pc->mem_cgroup;
2628 if (memcg && !css_tryget(&memcg->css))
2629 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002630 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002631 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002632 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002633 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002634 memcg = mem_cgroup_lookup(id);
2635 if (memcg && !css_tryget(&memcg->css))
2636 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002637 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002638 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002639 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002640 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002641}
2642
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002643static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002644 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002645 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002646 enum charge_type ctype,
2647 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002648{
Johannes Weinerce587e62012-04-24 20:22:33 +02002649 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002650 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002651 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002652 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002653 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002654
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002655 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002656 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002657 /*
2658 * we don't need page_cgroup_lock about tail pages, becase they are not
2659 * accessed by any other context at this point.
2660 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002661
2662 /*
2663 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2664 * may already be on some other mem_cgroup's LRU. Take care of it.
2665 */
2666 if (lrucare) {
2667 zone = page_zone(page);
2668 spin_lock_irq(&zone->lru_lock);
2669 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002670 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002671 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002672 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002673 was_on_lru = true;
2674 }
2675 }
2676
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002677 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002678 /*
2679 * We access a page_cgroup asynchronously without lock_page_cgroup().
2680 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2681 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2682 * before USED bit, we need memory barrier here.
2683 * See mem_cgroup_add_lru_list(), etc.
2684 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002685 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002686 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002687
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002688 if (lrucare) {
2689 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002690 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002691 VM_BUG_ON(PageLRU(page));
2692 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002693 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002694 }
2695 spin_unlock_irq(&zone->lru_lock);
2696 }
2697
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002698 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002699 anon = true;
2700 else
2701 anon = false;
2702
2703 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002704 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002705
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002706 /*
2707 * "charge_statistics" updated event counter. Then, check it.
2708 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2709 * if they exceeds softlimit.
2710 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002711 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002712}
2713
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002714#ifdef CONFIG_MEMCG_KMEM
2715static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2716{
2717 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2718 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2719}
2720
2721static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2722{
2723 struct res_counter *fail_res;
2724 struct mem_cgroup *_memcg;
2725 int ret = 0;
2726 bool may_oom;
2727
2728 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2729 if (ret)
2730 return ret;
2731
2732 /*
2733 * Conditions under which we can wait for the oom_killer. Those are
2734 * the same conditions tested by the core page allocator
2735 */
2736 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2737
2738 _memcg = memcg;
2739 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2740 &_memcg, may_oom);
2741
2742 if (ret == -EINTR) {
2743 /*
2744 * __mem_cgroup_try_charge() chosed to bypass to root due to
2745 * OOM kill or fatal signal. Since our only options are to
2746 * either fail the allocation or charge it to this cgroup, do
2747 * it as a temporary condition. But we can't fail. From a
2748 * kmem/slab perspective, the cache has already been selected,
2749 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2750 * our minds.
2751 *
2752 * This condition will only trigger if the task entered
2753 * memcg_charge_kmem in a sane state, but was OOM-killed during
2754 * __mem_cgroup_try_charge() above. Tasks that were already
2755 * dying when the allocation triggers should have been already
2756 * directed to the root cgroup in memcontrol.h
2757 */
2758 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2759 if (do_swap_account)
2760 res_counter_charge_nofail(&memcg->memsw, size,
2761 &fail_res);
2762 ret = 0;
2763 } else if (ret)
2764 res_counter_uncharge(&memcg->kmem, size);
2765
2766 return ret;
2767}
2768
2769static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2770{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002771 res_counter_uncharge(&memcg->res, size);
2772 if (do_swap_account)
2773 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002774
2775 /* Not down to 0 */
2776 if (res_counter_uncharge(&memcg->kmem, size))
2777 return;
2778
2779 if (memcg_kmem_test_and_clear_dead(memcg))
2780 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002781}
2782
2783/*
2784 * We need to verify if the allocation against current->mm->owner's memcg is
2785 * possible for the given order. But the page is not allocated yet, so we'll
2786 * need a further commit step to do the final arrangements.
2787 *
2788 * It is possible for the task to switch cgroups in this mean time, so at
2789 * commit time, we can't rely on task conversion any longer. We'll then use
2790 * the handle argument to return to the caller which cgroup we should commit
2791 * against. We could also return the memcg directly and avoid the pointer
2792 * passing, but a boolean return value gives better semantics considering
2793 * the compiled-out case as well.
2794 *
2795 * Returning true means the allocation is possible.
2796 */
2797bool
2798__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2799{
2800 struct mem_cgroup *memcg;
2801 int ret;
2802
2803 *_memcg = NULL;
2804 memcg = try_get_mem_cgroup_from_mm(current->mm);
2805
2806 /*
2807 * very rare case described in mem_cgroup_from_task. Unfortunately there
2808 * isn't much we can do without complicating this too much, and it would
2809 * be gfp-dependent anyway. Just let it go
2810 */
2811 if (unlikely(!memcg))
2812 return true;
2813
2814 if (!memcg_can_account_kmem(memcg)) {
2815 css_put(&memcg->css);
2816 return true;
2817 }
2818
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002819 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
2820 if (!ret)
2821 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002822
2823 css_put(&memcg->css);
2824 return (ret == 0);
2825}
2826
2827void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2828 int order)
2829{
2830 struct page_cgroup *pc;
2831
2832 VM_BUG_ON(mem_cgroup_is_root(memcg));
2833
2834 /* The page allocation failed. Revert */
2835 if (!page) {
2836 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002837 return;
2838 }
2839
2840 pc = lookup_page_cgroup(page);
2841 lock_page_cgroup(pc);
2842 pc->mem_cgroup = memcg;
2843 SetPageCgroupUsed(pc);
2844 unlock_page_cgroup(pc);
2845}
2846
2847void __memcg_kmem_uncharge_pages(struct page *page, int order)
2848{
2849 struct mem_cgroup *memcg = NULL;
2850 struct page_cgroup *pc;
2851
2852
2853 pc = lookup_page_cgroup(page);
2854 /*
2855 * Fast unlocked return. Theoretically might have changed, have to
2856 * check again after locking.
2857 */
2858 if (!PageCgroupUsed(pc))
2859 return;
2860
2861 lock_page_cgroup(pc);
2862 if (PageCgroupUsed(pc)) {
2863 memcg = pc->mem_cgroup;
2864 ClearPageCgroupUsed(pc);
2865 }
2866 unlock_page_cgroup(pc);
2867
2868 /*
2869 * We trust that only if there is a memcg associated with the page, it
2870 * is a valid allocation
2871 */
2872 if (!memcg)
2873 return;
2874
2875 VM_BUG_ON(mem_cgroup_is_root(memcg));
2876 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002877}
2878#endif /* CONFIG_MEMCG_KMEM */
2879
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002880#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2881
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002882#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002883/*
2884 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002885 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2886 * charge/uncharge will be never happen and move_account() is done under
2887 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002888 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002889void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002890{
2891 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002892 struct page_cgroup *pc;
2893 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002894
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002895 if (mem_cgroup_disabled())
2896 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002897 for (i = 1; i < HPAGE_PMD_NR; i++) {
2898 pc = head_pc + i;
2899 pc->mem_cgroup = head_pc->mem_cgroup;
2900 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002901 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2902 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002903}
Hugh Dickins12d27102012-01-12 17:19:52 -08002904#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002905
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002906/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002907 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002908 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002909 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002910 * @pc: page_cgroup of the page.
2911 * @from: mem_cgroup which the page is moved from.
2912 * @to: mem_cgroup which the page is moved to. @from != @to.
2913 *
2914 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002915 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002916 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002917 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002918 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2919 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002920 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002921static int mem_cgroup_move_account(struct page *page,
2922 unsigned int nr_pages,
2923 struct page_cgroup *pc,
2924 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002925 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002926{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002927 unsigned long flags;
2928 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002929 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002930
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002931 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002932 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002933 /*
2934 * The page is isolated from LRU. So, collapse function
2935 * will not handle this page. But page splitting can happen.
2936 * Do this check under compound_page_lock(). The caller should
2937 * hold it.
2938 */
2939 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002940 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002941 goto out;
2942
2943 lock_page_cgroup(pc);
2944
2945 ret = -EINVAL;
2946 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2947 goto unlock;
2948
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002949 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002950
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002951 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002952 /* Update mapped_file data for mem_cgroup */
2953 preempt_disable();
2954 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2955 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2956 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002957 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002958 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002959
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002960 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002961 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002962 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002963 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002964 ret = 0;
2965unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002966 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002967 /*
2968 * check events
2969 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002970 memcg_check_events(to, page);
2971 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002972out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002973 return ret;
2974}
2975
Michal Hocko2ef37d32012-10-26 13:37:30 +02002976/**
2977 * mem_cgroup_move_parent - moves page to the parent group
2978 * @page: the page to move
2979 * @pc: page_cgroup of the page
2980 * @child: page's cgroup
2981 *
2982 * move charges to its parent or the root cgroup if the group has no
2983 * parent (aka use_hierarchy==0).
2984 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2985 * mem_cgroup_move_account fails) the failure is always temporary and
2986 * it signals a race with a page removal/uncharge or migration. In the
2987 * first case the page is on the way out and it will vanish from the LRU
2988 * on the next attempt and the call should be retried later.
2989 * Isolation from the LRU fails only if page has been isolated from
2990 * the LRU since we looked at it and that usually means either global
2991 * reclaim or migration going on. The page will either get back to the
2992 * LRU or vanish.
2993 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2994 * (!PageCgroupUsed) or moved to a different group. The page will
2995 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002996 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002997static int mem_cgroup_move_parent(struct page *page,
2998 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07002999 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003000{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003001 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003002 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003003 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003004 int ret;
3005
Michal Hockod8423012012-10-26 13:37:29 +02003006 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003007
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003008 ret = -EBUSY;
3009 if (!get_page_unless_zero(page))
3010 goto out;
3011 if (isolate_lru_page(page))
3012 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003013
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003014 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003015
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003016 parent = parent_mem_cgroup(child);
3017 /*
3018 * If no parent, move charges to root cgroup.
3019 */
3020 if (!parent)
3021 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003022
Michal Hocko2ef37d32012-10-26 13:37:30 +02003023 if (nr_pages > 1) {
3024 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003025 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003026 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003027
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003028 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003029 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003030 if (!ret)
3031 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003032
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003033 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003034 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003035 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003036put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003037 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003038out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003039 return ret;
3040}
3041
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003042/*
3043 * Charge the memory controller for page usage.
3044 * Return
3045 * 0 if the charge was successful
3046 * < 0 if the cgroup is over its limit
3047 */
3048static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003049 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003050{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003051 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003052 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003053 bool oom = true;
3054 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003055
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003056 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003057 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003058 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003059 /*
3060 * Never OOM-kill a process for a huge page. The
3061 * fault handler will fall back to regular pages.
3062 */
3063 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003064 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003066 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003067 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003068 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003069 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003070 return 0;
3071}
3072
3073int mem_cgroup_newpage_charge(struct page *page,
3074 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003075{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003076 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003077 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003078 VM_BUG_ON(page_mapped(page));
3079 VM_BUG_ON(page->mapping && !PageAnon(page));
3080 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003081 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003082 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003083}
3084
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003085/*
3086 * While swap-in, try_charge -> commit or cancel, the page is locked.
3087 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003088 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003089 * "commit()" or removed by "cancel()"
3090 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003091static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3092 struct page *page,
3093 gfp_t mask,
3094 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003095{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003096 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003097 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003098 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003099
Johannes Weiner90deb782012-07-31 16:45:47 -07003100 pc = lookup_page_cgroup(page);
3101 /*
3102 * Every swap fault against a single page tries to charge the
3103 * page, bail as early as possible. shmem_unuse() encounters
3104 * already charged pages, too. The USED bit is protected by
3105 * the page lock, which serializes swap cache removal, which
3106 * in turn serializes uncharging.
3107 */
3108 if (PageCgroupUsed(pc))
3109 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003110 if (!do_swap_account)
3111 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003112 memcg = try_get_mem_cgroup_from_page(page);
3113 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003114 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003115 *memcgp = memcg;
3116 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003117 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003118 if (ret == -EINTR)
3119 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003120 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003121charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003122 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3123 if (ret == -EINTR)
3124 ret = 0;
3125 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003126}
3127
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003128int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3129 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3130{
3131 *memcgp = NULL;
3132 if (mem_cgroup_disabled())
3133 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003134 /*
3135 * A racing thread's fault, or swapoff, may have already
3136 * updated the pte, and even removed page from swap cache: in
3137 * those cases unuse_pte()'s pte_same() test will fail; but
3138 * there's also a KSM case which does need to charge the page.
3139 */
3140 if (!PageSwapCache(page)) {
3141 int ret;
3142
3143 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3144 if (ret == -EINTR)
3145 ret = 0;
3146 return ret;
3147 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003148 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3149}
3150
Johannes Weiner827a03d2012-07-31 16:45:36 -07003151void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3152{
3153 if (mem_cgroup_disabled())
3154 return;
3155 if (!memcg)
3156 return;
3157 __mem_cgroup_cancel_charge(memcg, 1);
3158}
3159
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003160static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003161__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003162 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003163{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003164 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003165 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003166 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003167 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003168
Johannes Weinerce587e62012-04-24 20:22:33 +02003169 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003170 /*
3171 * Now swap is on-memory. This means this page may be
3172 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003173 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3174 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3175 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003176 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003177 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003178 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003179 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003180 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003181}
3182
Johannes Weiner72835c82012-01-12 17:18:32 -08003183void mem_cgroup_commit_charge_swapin(struct page *page,
3184 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003185{
Johannes Weiner72835c82012-01-12 17:18:32 -08003186 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003187 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003188}
3189
Johannes Weiner827a03d2012-07-31 16:45:36 -07003190int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3191 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003192{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003193 struct mem_cgroup *memcg = NULL;
3194 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3195 int ret;
3196
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003197 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003198 return 0;
3199 if (PageCompound(page))
3200 return 0;
3201
Johannes Weiner827a03d2012-07-31 16:45:36 -07003202 if (!PageSwapCache(page))
3203 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3204 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003205 ret = __mem_cgroup_try_charge_swapin(mm, page,
3206 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003207 if (!ret)
3208 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3209 }
3210 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003211}
3212
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003213static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003214 unsigned int nr_pages,
3215 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003216{
3217 struct memcg_batch_info *batch = NULL;
3218 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003219
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003220 /* If swapout, usage of swap doesn't decrease */
3221 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3222 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003223
3224 batch = &current->memcg_batch;
3225 /*
3226 * In usual, we do css_get() when we remember memcg pointer.
3227 * But in this case, we keep res->usage until end of a series of
3228 * uncharges. Then, it's ok to ignore memcg's refcnt.
3229 */
3230 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003231 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003232 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003233 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003234 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003235 * the same cgroup and we have chance to coalesce uncharges.
3236 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3237 * because we want to do uncharge as soon as possible.
3238 */
3239
3240 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3241 goto direct_uncharge;
3242
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003243 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003244 goto direct_uncharge;
3245
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003246 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003247 * In typical case, batch->memcg == mem. This means we can
3248 * merge a series of uncharges to an uncharge of res_counter.
3249 * If not, we uncharge res_counter ony by one.
3250 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003251 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003252 goto direct_uncharge;
3253 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003254 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003255 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003256 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003257 return;
3258direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003259 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003260 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003261 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3262 if (unlikely(batch->memcg != memcg))
3263 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003264}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003265
Balbir Singh8697d332008-02-07 00:13:59 -08003266/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003267 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003268 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003269static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003270__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3271 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003272{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003273 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003274 unsigned int nr_pages = 1;
3275 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003276 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003277
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003278 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003279 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003280
Johannes Weiner0c59b892012-07-31 16:45:31 -07003281 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003282
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003283 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003284 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003285 VM_BUG_ON(!PageTransHuge(page));
3286 }
Balbir Singh8697d332008-02-07 00:13:59 -08003287 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003288 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003289 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003290 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003291 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003292 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003293
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003294 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003295
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003296 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003297
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003298 if (!PageCgroupUsed(pc))
3299 goto unlock_out;
3300
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003301 anon = PageAnon(page);
3302
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003303 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003304 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003305 /*
3306 * Generally PageAnon tells if it's the anon statistics to be
3307 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3308 * used before page reached the stage of being marked PageAnon.
3309 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003310 anon = true;
3311 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003312 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003313 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003314 if (page_mapped(page))
3315 goto unlock_out;
3316 /*
3317 * Pages under migration may not be uncharged. But
3318 * end_migration() /must/ be the one uncharging the
3319 * unused post-migration page and so it has to call
3320 * here with the migration bit still set. See the
3321 * res_counter handling below.
3322 */
3323 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003324 goto unlock_out;
3325 break;
3326 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3327 if (!PageAnon(page)) { /* Shared memory */
3328 if (page->mapping && !page_is_file_cache(page))
3329 goto unlock_out;
3330 } else if (page_mapped(page)) /* Anon */
3331 goto unlock_out;
3332 break;
3333 default:
3334 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003335 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003336
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003337 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003338
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003339 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003340 /*
3341 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3342 * freed from LRU. This is safe because uncharged page is expected not
3343 * to be reused (freed soon). Exception is SwapCache, it's handled by
3344 * special functions.
3345 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003346
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003347 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003348 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003349 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003350 * will never be freed.
3351 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003352 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003353 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003354 mem_cgroup_swap_statistics(memcg, true);
3355 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003356 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003357 /*
3358 * Migration does not charge the res_counter for the
3359 * replacement page, so leave it alone when phasing out the
3360 * page that is unused after the migration.
3361 */
3362 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003363 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003365 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003366
3367unlock_out:
3368 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003369 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003370}
3371
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003372void mem_cgroup_uncharge_page(struct page *page)
3373{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003374 /* early check. */
3375 if (page_mapped(page))
3376 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003377 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003378 if (PageSwapCache(page))
3379 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003380 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003381}
3382
3383void mem_cgroup_uncharge_cache_page(struct page *page)
3384{
3385 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003386 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003387 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003388}
3389
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003390/*
3391 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3392 * In that cases, pages are freed continuously and we can expect pages
3393 * are in the same memcg. All these calls itself limits the number of
3394 * pages freed at once, then uncharge_start/end() is called properly.
3395 * This may be called prural(2) times in a context,
3396 */
3397
3398void mem_cgroup_uncharge_start(void)
3399{
3400 current->memcg_batch.do_batch++;
3401 /* We can do nest. */
3402 if (current->memcg_batch.do_batch == 1) {
3403 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003404 current->memcg_batch.nr_pages = 0;
3405 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003406 }
3407}
3408
3409void mem_cgroup_uncharge_end(void)
3410{
3411 struct memcg_batch_info *batch = &current->memcg_batch;
3412
3413 if (!batch->do_batch)
3414 return;
3415
3416 batch->do_batch--;
3417 if (batch->do_batch) /* If stacked, do nothing. */
3418 return;
3419
3420 if (!batch->memcg)
3421 return;
3422 /*
3423 * This "batch->memcg" is valid without any css_get/put etc...
3424 * bacause we hide charges behind us.
3425 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003426 if (batch->nr_pages)
3427 res_counter_uncharge(&batch->memcg->res,
3428 batch->nr_pages * PAGE_SIZE);
3429 if (batch->memsw_nr_pages)
3430 res_counter_uncharge(&batch->memcg->memsw,
3431 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003432 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003433 /* forget this pointer (for sanity check) */
3434 batch->memcg = NULL;
3435}
3436
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003437#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003438/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003439 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003440 * memcg information is recorded to swap_cgroup of "ent"
3441 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003442void
3443mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003444{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003445 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003446 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003447
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003448 if (!swapout) /* this was a swap cache but the swap is unused ! */
3449 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3450
Johannes Weiner0030f532012-07-31 16:45:25 -07003451 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003452
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003453 /*
3454 * record memcg information, if swapout && memcg != NULL,
3455 * mem_cgroup_get() was called in uncharge().
3456 */
3457 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003458 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003459}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003460#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003461
Andrew Mortonc255a452012-07-31 16:43:02 -07003462#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003463/*
3464 * called from swap_entry_free(). remove record in swap_cgroup and
3465 * uncharge "memsw" account.
3466 */
3467void mem_cgroup_uncharge_swap(swp_entry_t ent)
3468{
3469 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003470 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003471
3472 if (!do_swap_account)
3473 return;
3474
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003475 id = swap_cgroup_record(ent, 0);
3476 rcu_read_lock();
3477 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003478 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003479 /*
3480 * We uncharge this because swap is freed.
3481 * This memcg can be obsolete one. We avoid calling css_tryget
3482 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003483 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003484 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003485 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003486 mem_cgroup_put(memcg);
3487 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003488 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003490
3491/**
3492 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3493 * @entry: swap entry to be moved
3494 * @from: mem_cgroup which the entry is moved from
3495 * @to: mem_cgroup which the entry is moved to
3496 *
3497 * It succeeds only when the swap_cgroup's record for this entry is the same
3498 * as the mem_cgroup's id of @from.
3499 *
3500 * Returns 0 on success, -EINVAL on failure.
3501 *
3502 * The caller must have charged to @to, IOW, called res_counter_charge() about
3503 * both res and memsw, and called css_get().
3504 */
3505static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003506 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003507{
3508 unsigned short old_id, new_id;
3509
3510 old_id = css_id(&from->css);
3511 new_id = css_id(&to->css);
3512
3513 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003514 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003515 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003516 /*
3517 * This function is only called from task migration context now.
3518 * It postpones res_counter and refcount handling till the end
3519 * of task migration(mem_cgroup_clear_mc()) for performance
3520 * improvement. But we cannot postpone mem_cgroup_get(to)
3521 * because if the process that has been moved to @to does
3522 * swap-in, the refcount of @to might be decreased to 0.
3523 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003524 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003525 return 0;
3526 }
3527 return -EINVAL;
3528}
3529#else
3530static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003531 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003532{
3533 return -EINVAL;
3534}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003535#endif
3536
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003537/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003538 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3539 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003540 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003541void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3542 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003543{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003544 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00003545 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003546 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003547 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003548
Johannes Weiner72835c82012-01-12 17:18:32 -08003549 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003550
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003551 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003552 return;
Balbir Singh40779602008-04-04 14:29:59 -07003553
Mel Gormanb32967f2012-11-19 12:35:47 +00003554 if (PageTransHuge(page))
3555 nr_pages <<= compound_order(page);
3556
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003557 pc = lookup_page_cgroup(page);
3558 lock_page_cgroup(pc);
3559 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003560 memcg = pc->mem_cgroup;
3561 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003562 /*
3563 * At migrating an anonymous page, its mapcount goes down
3564 * to 0 and uncharge() will be called. But, even if it's fully
3565 * unmapped, migration may fail and this page has to be
3566 * charged again. We set MIGRATION flag here and delay uncharge
3567 * until end_migration() is called
3568 *
3569 * Corner Case Thinking
3570 * A)
3571 * When the old page was mapped as Anon and it's unmap-and-freed
3572 * while migration was ongoing.
3573 * If unmap finds the old page, uncharge() of it will be delayed
3574 * until end_migration(). If unmap finds a new page, it's
3575 * uncharged when it make mapcount to be 1->0. If unmap code
3576 * finds swap_migration_entry, the new page will not be mapped
3577 * and end_migration() will find it(mapcount==0).
3578 *
3579 * B)
3580 * When the old page was mapped but migraion fails, the kernel
3581 * remaps it. A charge for it is kept by MIGRATION flag even
3582 * if mapcount goes down to 0. We can do remap successfully
3583 * without charging it again.
3584 *
3585 * C)
3586 * The "old" page is under lock_page() until the end of
3587 * migration, so, the old page itself will not be swapped-out.
3588 * If the new page is swapped out before end_migraton, our
3589 * hook to usual swap-out path will catch the event.
3590 */
3591 if (PageAnon(page))
3592 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003593 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003594 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003595 /*
3596 * If the page is not charged at this point,
3597 * we return here.
3598 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003599 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003600 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003601
Johannes Weiner72835c82012-01-12 17:18:32 -08003602 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003603 /*
3604 * We charge new page before it's used/mapped. So, even if unlock_page()
3605 * is called before end_migration, we can catch all events on this new
3606 * page. In the case new page is migrated but not remapped, new page's
3607 * mapcount will be finally 0 and we call uncharge in end_migration().
3608 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003609 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003610 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003611 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003612 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003613 /*
3614 * The page is committed to the memcg, but it's not actually
3615 * charged to the res_counter since we plan on replacing the
3616 * old one and only one page is going to be left afterwards.
3617 */
Mel Gormanb32967f2012-11-19 12:35:47 +00003618 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003619}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003620
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003621/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003622void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003623 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003624{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003625 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003626 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003627 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003628
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003629 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003630 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003631
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003632 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003633 used = oldpage;
3634 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003635 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003636 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003637 unused = oldpage;
3638 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003639 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003640 __mem_cgroup_uncharge_common(unused,
3641 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3642 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3643 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003644 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003645 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003646 * We disallowed uncharge of pages under migration because mapcount
3647 * of the page goes down to zero, temporarly.
3648 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003649 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003650 pc = lookup_page_cgroup(oldpage);
3651 lock_page_cgroup(pc);
3652 ClearPageCgroupMigration(pc);
3653 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003654
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003655 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003656 * If a page is a file cache, radix-tree replacement is very atomic
3657 * and we can skip this check. When it was an Anon page, its mapcount
3658 * goes down to 0. But because we added MIGRATION flage, it's not
3659 * uncharged yet. There are several case but page->mapcount check
3660 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3661 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003662 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003663 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003664 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003665}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003666
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003667/*
3668 * At replace page cache, newpage is not under any memcg but it's on
3669 * LRU. So, this function doesn't touch res_counter but handles LRU
3670 * in correct way. Both pages are locked so we cannot race with uncharge.
3671 */
3672void mem_cgroup_replace_page_cache(struct page *oldpage,
3673 struct page *newpage)
3674{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003675 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003676 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003677 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003678
3679 if (mem_cgroup_disabled())
3680 return;
3681
3682 pc = lookup_page_cgroup(oldpage);
3683 /* fix accounting on old pages */
3684 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003685 if (PageCgroupUsed(pc)) {
3686 memcg = pc->mem_cgroup;
3687 mem_cgroup_charge_statistics(memcg, false, -1);
3688 ClearPageCgroupUsed(pc);
3689 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003690 unlock_page_cgroup(pc);
3691
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003692 /*
3693 * When called from shmem_replace_page(), in some cases the
3694 * oldpage has already been charged, and in some cases not.
3695 */
3696 if (!memcg)
3697 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003698 /*
3699 * Even if newpage->mapping was NULL before starting replacement,
3700 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3701 * LRU while we overwrite pc->mem_cgroup.
3702 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003703 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003704}
3705
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003706#ifdef CONFIG_DEBUG_VM
3707static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3708{
3709 struct page_cgroup *pc;
3710
3711 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003712 /*
3713 * Can be NULL while feeding pages into the page allocator for
3714 * the first time, i.e. during boot or memory hotplug;
3715 * or when mem_cgroup_disabled().
3716 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003717 if (likely(pc) && PageCgroupUsed(pc))
3718 return pc;
3719 return NULL;
3720}
3721
3722bool mem_cgroup_bad_page_check(struct page *page)
3723{
3724 if (mem_cgroup_disabled())
3725 return false;
3726
3727 return lookup_page_cgroup_used(page) != NULL;
3728}
3729
3730void mem_cgroup_print_bad_page(struct page *page)
3731{
3732 struct page_cgroup *pc;
3733
3734 pc = lookup_page_cgroup_used(page);
3735 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003736 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003737 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003738 }
3739}
3740#endif
3741
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003742static DEFINE_MUTEX(set_limit_mutex);
3743
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003744static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003745 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003746{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003747 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003748 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003749 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003750 int children = mem_cgroup_count_children(memcg);
3751 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003752 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003753
3754 /*
3755 * For keeping hierarchical_reclaim simple, how long we should retry
3756 * is depends on callers. We set our retry-count to be function
3757 * of # of children which we should visit in this loop.
3758 */
3759 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3760
3761 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003762
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003763 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003764 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003765 if (signal_pending(current)) {
3766 ret = -EINTR;
3767 break;
3768 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003769 /*
3770 * Rather than hide all in some function, I do this in
3771 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003772 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003773 */
3774 mutex_lock(&set_limit_mutex);
3775 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3776 if (memswlimit < val) {
3777 ret = -EINVAL;
3778 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003779 break;
3780 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003781
3782 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3783 if (memlimit < val)
3784 enlarge = 1;
3785
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003786 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003787 if (!ret) {
3788 if (memswlimit == val)
3789 memcg->memsw_is_minimum = true;
3790 else
3791 memcg->memsw_is_minimum = false;
3792 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793 mutex_unlock(&set_limit_mutex);
3794
3795 if (!ret)
3796 break;
3797
Johannes Weiner56600482012-01-12 17:17:59 -08003798 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3799 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003800 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3801 /* Usage is reduced ? */
3802 if (curusage >= oldusage)
3803 retry_count--;
3804 else
3805 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003806 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003807 if (!ret && enlarge)
3808 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003809
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003810 return ret;
3811}
3812
Li Zefan338c8432009-06-17 16:27:15 -07003813static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3814 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003815{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003816 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003817 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003818 int children = mem_cgroup_count_children(memcg);
3819 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003820 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003821
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003822 /* see mem_cgroup_resize_res_limit */
3823 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3824 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003825 while (retry_count) {
3826 if (signal_pending(current)) {
3827 ret = -EINTR;
3828 break;
3829 }
3830 /*
3831 * Rather than hide all in some function, I do this in
3832 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003833 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003834 */
3835 mutex_lock(&set_limit_mutex);
3836 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3837 if (memlimit > val) {
3838 ret = -EINVAL;
3839 mutex_unlock(&set_limit_mutex);
3840 break;
3841 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003842 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3843 if (memswlimit < val)
3844 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003845 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003846 if (!ret) {
3847 if (memlimit == val)
3848 memcg->memsw_is_minimum = true;
3849 else
3850 memcg->memsw_is_minimum = false;
3851 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003852 mutex_unlock(&set_limit_mutex);
3853
3854 if (!ret)
3855 break;
3856
Johannes Weiner56600482012-01-12 17:17:59 -08003857 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3858 MEM_CGROUP_RECLAIM_NOSWAP |
3859 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003860 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003861 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003862 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003863 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003864 else
3865 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003866 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003867 if (!ret && enlarge)
3868 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003869 return ret;
3870}
3871
Balbir Singh4e416952009-09-23 15:56:39 -07003872unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003873 gfp_t gfp_mask,
3874 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003875{
3876 unsigned long nr_reclaimed = 0;
3877 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3878 unsigned long reclaimed;
3879 int loop = 0;
3880 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003881 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003882 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003883
3884 if (order > 0)
3885 return 0;
3886
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003887 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003888 /*
3889 * This loop can run a while, specially if mem_cgroup's continuously
3890 * keep exceeding their soft limit and putting the system under
3891 * pressure
3892 */
3893 do {
3894 if (next_mz)
3895 mz = next_mz;
3896 else
3897 mz = mem_cgroup_largest_soft_limit_node(mctz);
3898 if (!mz)
3899 break;
3900
Ying Han0ae5e892011-05-26 16:25:25 -07003901 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003902 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003903 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003904 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003905 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003906 spin_lock(&mctz->lock);
3907
3908 /*
3909 * If we failed to reclaim anything from this memory cgroup
3910 * it is time to move on to the next cgroup
3911 */
3912 next_mz = NULL;
3913 if (!reclaimed) {
3914 do {
3915 /*
3916 * Loop until we find yet another one.
3917 *
3918 * By the time we get the soft_limit lock
3919 * again, someone might have aded the
3920 * group back on the RB tree. Iterate to
3921 * make sure we get a different mem.
3922 * mem_cgroup_largest_soft_limit_node returns
3923 * NULL if no other cgroup is present on
3924 * the tree
3925 */
3926 next_mz =
3927 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003928 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003929 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003930 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003931 break;
3932 } while (1);
3933 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003934 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3935 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003936 /*
3937 * One school of thought says that we should not add
3938 * back the node to the tree if reclaim returns 0.
3939 * But our reclaim could return 0, simply because due
3940 * to priority we are exposing a smaller subset of
3941 * memory to reclaim from. Consider this as a longer
3942 * term TODO.
3943 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003944 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003945 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003946 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003947 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003948 loop++;
3949 /*
3950 * Could not reclaim anything and there are no more
3951 * mem cgroups to try or we seem to be looping without
3952 * reclaiming anything.
3953 */
3954 if (!nr_reclaimed &&
3955 (next_mz == NULL ||
3956 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3957 break;
3958 } while (!nr_reclaimed);
3959 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003960 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003961 return nr_reclaimed;
3962}
3963
Michal Hocko2ef37d32012-10-26 13:37:30 +02003964/**
3965 * mem_cgroup_force_empty_list - clears LRU of a group
3966 * @memcg: group to clear
3967 * @node: NUMA node
3968 * @zid: zone id
3969 * @lru: lru to to clear
3970 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003971 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003972 * reclaim the pages page themselves - pages are moved to the parent (or root)
3973 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003974 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003975static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003976 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003977{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003978 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003979 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003980 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003981 struct page *busy;
3982 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003983
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003984 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003985 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3986 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003987
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003988 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003989 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003990 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003991 struct page *page;
3992
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003993 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003994 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003995 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003996 break;
3997 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003998 page = list_entry(list->prev, struct page, lru);
3999 if (busy == page) {
4000 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004001 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004002 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004003 continue;
4004 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004005 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004006
Johannes Weiner925b7672012-01-12 17:18:15 -08004007 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004008
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004009 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004010 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004011 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004012 cond_resched();
4013 } else
4014 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004015 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004016}
4017
4018/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004019 * make mem_cgroup's charge to be 0 if there is no task by moving
4020 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004021 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004022 *
4023 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004024 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004025static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004026{
Michal Hockoc26251f2012-10-26 13:37:28 +02004027 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004028
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004029 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004030 /* This is for making all *used* pages to be on LRU. */
4031 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004032 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004033 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004034 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004035 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004036 enum lru_list lru;
4037 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004038 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004039 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004040 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004041 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004042 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 mem_cgroup_end_move(memcg);
4044 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004045 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004046
Michal Hocko2ef37d32012-10-26 13:37:30 +02004047 /*
4048 * This is a safety check because mem_cgroup_force_empty_list
4049 * could have raced with mem_cgroup_replace_page_cache callers
4050 * so the lru seemed empty but the page could have been added
4051 * right after the check. RES_USAGE should be safe as we always
4052 * charge before adding to the LRU.
4053 */
4054 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004055}
4056
4057/*
4058 * Reclaims as many pages from the given memcg as possible and moves
4059 * the rest to the parent.
4060 *
4061 * Caller is responsible for holding css reference for memcg.
4062 */
4063static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4064{
4065 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4066 struct cgroup *cgrp = memcg->css.cgroup;
4067
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004068 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004069 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4070 return -EBUSY;
4071
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004072 /* we call try-to-free pages for make this cgroup empty */
4073 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004074 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004075 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004076 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004077
Michal Hockoc26251f2012-10-26 13:37:28 +02004078 if (signal_pending(current))
4079 return -EINTR;
4080
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004081 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004082 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004083 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004084 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004085 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004086 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004087 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004088
4089 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004090 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004091 mem_cgroup_reparent_charges(memcg);
4092
4093 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004094}
4095
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004096static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004097{
Michal Hockoc26251f2012-10-26 13:37:28 +02004098 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4099 int ret;
4100
Michal Hockod8423012012-10-26 13:37:29 +02004101 if (mem_cgroup_is_root(memcg))
4102 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004103 css_get(&memcg->css);
4104 ret = mem_cgroup_force_empty(memcg);
4105 css_put(&memcg->css);
4106
4107 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004108}
4109
4110
Balbir Singh18f59ea2009-01-07 18:08:07 -08004111static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4112{
4113 return mem_cgroup_from_cont(cont)->use_hierarchy;
4114}
4115
4116static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4117 u64 val)
4118{
4119 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004120 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004121 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004122 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004123
4124 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004125 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004126
4127 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004128
4129 if (memcg->use_hierarchy == val)
4130 goto out;
4131
Balbir Singh18f59ea2009-01-07 18:08:07 -08004132 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004133 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004134 * in the child subtrees. If it is unset, then the change can
4135 * occur, provided the current cgroup has no children.
4136 *
4137 * For the root cgroup, parent_mem is NULL, we allow value to be
4138 * set if there are no children.
4139 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004140 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004141 (val == 1 || val == 0)) {
4142 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004143 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004144 else
4145 retval = -EBUSY;
4146 } else
4147 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004148
4149out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004150 cgroup_unlock();
4151
4152 return retval;
4153}
4154
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004155
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004156static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004157 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004158{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004159 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004160 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004161
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004162 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004163 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004164 val += mem_cgroup_read_stat(iter, idx);
4165
4166 if (val < 0) /* race ? */
4167 val = 0;
4168 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004169}
4170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004171static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004172{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004173 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004174
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004175 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004176 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004177 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004178 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004179 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004180 }
4181
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004182 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4183 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004184
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004185 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004186 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004187
4188 return val << PAGE_SHIFT;
4189}
4190
Tejun Heoaf36f902012-04-01 12:09:55 -07004191static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4192 struct file *file, char __user *buf,
4193 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004194{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004195 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004196 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004197 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004198 int name, len;
4199 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004200
4201 type = MEMFILE_TYPE(cft->private);
4202 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004203
4204 if (!do_swap_account && type == _MEMSWAP)
4205 return -EOPNOTSUPP;
4206
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004207 switch (type) {
4208 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004209 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004210 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004211 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004212 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004213 break;
4214 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004215 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004216 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004217 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004218 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004219 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004220 case _KMEM:
4221 val = res_counter_read_u64(&memcg->kmem, name);
4222 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004223 default:
4224 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004225 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004226
4227 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4228 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004229}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004230
4231static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4232{
4233 int ret = -EINVAL;
4234#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004235 bool must_inc_static_branch = false;
4236
Glauber Costa510fc4e2012-12-18 14:21:47 -08004237 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4238 /*
4239 * For simplicity, we won't allow this to be disabled. It also can't
4240 * be changed if the cgroup has children already, or if tasks had
4241 * already joined.
4242 *
4243 * If tasks join before we set the limit, a person looking at
4244 * kmem.usage_in_bytes will have no way to determine when it took
4245 * place, which makes the value quite meaningless.
4246 *
4247 * After it first became limited, changes in the value of the limit are
4248 * of course permitted.
4249 *
4250 * Taking the cgroup_lock is really offensive, but it is so far the only
4251 * way to guarantee that no children will appear. There are plenty of
4252 * other offenders, and they should all go away. Fine grained locking
4253 * is probably the way to go here. When we are fully hierarchical, we
4254 * can also get rid of the use_hierarchy check.
4255 */
4256 cgroup_lock();
4257 mutex_lock(&set_limit_mutex);
4258 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4259 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4260 !list_empty(&cont->children))) {
4261 ret = -EBUSY;
4262 goto out;
4263 }
4264 ret = res_counter_set_limit(&memcg->kmem, val);
4265 VM_BUG_ON(ret);
4266
Glauber Costaa8964b92012-12-18 14:22:09 -08004267 /*
4268 * After this point, kmem_accounted (that we test atomically in
4269 * the beginning of this conditional), is no longer 0. This
4270 * guarantees only one process will set the following boolean
4271 * to true. We don't need test_and_set because we're protected
4272 * by the set_limit_mutex anyway.
4273 */
4274 memcg_kmem_set_activated(memcg);
4275 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08004276 /*
4277 * kmem charges can outlive the cgroup. In the case of slab
4278 * pages, for instance, a page contain objects from various
4279 * processes, so it is unfeasible to migrate them away. We
4280 * need to reference count the memcg because of that.
4281 */
4282 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004283 } else
4284 ret = res_counter_set_limit(&memcg->kmem, val);
4285out:
4286 mutex_unlock(&set_limit_mutex);
4287 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08004288
4289 /*
4290 * We are by now familiar with the fact that we can't inc the static
4291 * branch inside cgroup_lock. See disarm functions for details. A
4292 * worker here is overkill, but also wrong: After the limit is set, we
4293 * must start accounting right away. Since this operation can't fail,
4294 * we can safely defer it to here - no rollback will be needed.
4295 *
4296 * The boolean used to control this is also safe, because
4297 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
4298 * able to set it to true;
4299 */
4300 if (must_inc_static_branch) {
4301 static_key_slow_inc(&memcg_kmem_enabled_key);
4302 /*
4303 * setting the active bit after the inc will guarantee no one
4304 * starts accounting before all call sites are patched
4305 */
4306 memcg_kmem_set_active(memcg);
4307 }
4308
Glauber Costa510fc4e2012-12-18 14:21:47 -08004309#endif
4310 return ret;
4311}
4312
4313static void memcg_propagate_kmem(struct mem_cgroup *memcg)
4314{
4315 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4316 if (!parent)
4317 return;
4318 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08004319#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004320 /*
4321 * When that happen, we need to disable the static branch only on those
4322 * memcgs that enabled it. To achieve this, we would be forced to
4323 * complicate the code by keeping track of which memcgs were the ones
4324 * that actually enabled limits, and which ones got it from its
4325 * parents.
4326 *
4327 * It is a lot simpler just to do static_key_slow_inc() on every child
4328 * that is accounted.
4329 */
4330 if (memcg_kmem_is_active(memcg)) {
Glauber Costa7de37682012-12-18 14:22:07 -08004331 mem_cgroup_get(memcg);
Glauber Costaa8964b92012-12-18 14:22:09 -08004332 static_key_slow_inc(&memcg_kmem_enabled_key);
4333 }
Glauber Costa7de37682012-12-18 14:22:07 -08004334#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004335}
4336
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004337/*
4338 * The user of this function is...
4339 * RES_LIMIT.
4340 */
Paul Menage856c13a2008-07-25 01:47:04 -07004341static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4342 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004343{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004344 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004345 enum res_type type;
4346 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004347 unsigned long long val;
4348 int ret;
4349
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004350 type = MEMFILE_TYPE(cft->private);
4351 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004352
4353 if (!do_swap_account && type == _MEMSWAP)
4354 return -EOPNOTSUPP;
4355
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004356 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004357 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004358 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4359 ret = -EINVAL;
4360 break;
4361 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004362 /* This function does all necessary parse...reuse it */
4363 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004364 if (ret)
4365 break;
4366 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004367 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004368 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004369 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004370 else if (type == _KMEM)
4371 ret = memcg_update_kmem_limit(cont, val);
4372 else
4373 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004374 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004375 case RES_SOFT_LIMIT:
4376 ret = res_counter_memparse_write_strategy(buffer, &val);
4377 if (ret)
4378 break;
4379 /*
4380 * For memsw, soft limits are hard to implement in terms
4381 * of semantics, for now, we support soft limits for
4382 * control without swap
4383 */
4384 if (type == _MEM)
4385 ret = res_counter_set_soft_limit(&memcg->res, val);
4386 else
4387 ret = -EINVAL;
4388 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004389 default:
4390 ret = -EINVAL; /* should be BUG() ? */
4391 break;
4392 }
4393 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004394}
4395
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004396static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4397 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4398{
4399 struct cgroup *cgroup;
4400 unsigned long long min_limit, min_memsw_limit, tmp;
4401
4402 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4403 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4404 cgroup = memcg->css.cgroup;
4405 if (!memcg->use_hierarchy)
4406 goto out;
4407
4408 while (cgroup->parent) {
4409 cgroup = cgroup->parent;
4410 memcg = mem_cgroup_from_cont(cgroup);
4411 if (!memcg->use_hierarchy)
4412 break;
4413 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4414 min_limit = min(min_limit, tmp);
4415 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4416 min_memsw_limit = min(min_memsw_limit, tmp);
4417 }
4418out:
4419 *mem_limit = min_limit;
4420 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004421}
4422
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004423static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004424{
Tejun Heoaf36f902012-04-01 12:09:55 -07004425 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004426 int name;
4427 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004428
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004429 type = MEMFILE_TYPE(event);
4430 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004431
4432 if (!do_swap_account && type == _MEMSWAP)
4433 return -EOPNOTSUPP;
4434
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004435 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004436 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004437 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004438 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004439 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004440 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004441 else if (type == _KMEM)
4442 res_counter_reset_max(&memcg->kmem);
4443 else
4444 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004445 break;
4446 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004447 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004448 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004449 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004450 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004451 else if (type == _KMEM)
4452 res_counter_reset_failcnt(&memcg->kmem);
4453 else
4454 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004455 break;
4456 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004457
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004458 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004459}
4460
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004461static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4462 struct cftype *cft)
4463{
4464 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4465}
4466
Daisuke Nishimura02491442010-03-10 15:22:17 -08004467#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004468static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4469 struct cftype *cft, u64 val)
4470{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004471 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004472
4473 if (val >= (1 << NR_MOVE_TYPE))
4474 return -EINVAL;
4475 /*
4476 * We check this value several times in both in can_attach() and
4477 * attach(), so we need cgroup lock to prevent this value from being
4478 * inconsistent.
4479 */
4480 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004481 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004482 cgroup_unlock();
4483
4484 return 0;
4485}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004486#else
4487static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4488 struct cftype *cft, u64 val)
4489{
4490 return -ENOSYS;
4491}
4492#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004493
Ying Han406eb0c2011-05-26 16:25:37 -07004494#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004495static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004496 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004497{
4498 int nid;
4499 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4500 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004501 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004502
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004503 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004504 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004505 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004506 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004507 seq_printf(m, " N%d=%lu", nid, node_nr);
4508 }
4509 seq_putc(m, '\n');
4510
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004511 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004512 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004513 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004514 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004515 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004516 seq_printf(m, " N%d=%lu", nid, node_nr);
4517 }
4518 seq_putc(m, '\n');
4519
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004520 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004521 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004522 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004523 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004524 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004525 seq_printf(m, " N%d=%lu", nid, node_nr);
4526 }
4527 seq_putc(m, '\n');
4528
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004529 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004530 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004531 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004532 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004533 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004534 seq_printf(m, " N%d=%lu", nid, node_nr);
4535 }
4536 seq_putc(m, '\n');
4537 return 0;
4538}
4539#endif /* CONFIG_NUMA */
4540
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004541static const char * const mem_cgroup_lru_names[] = {
4542 "inactive_anon",
4543 "active_anon",
4544 "inactive_file",
4545 "active_file",
4546 "unevictable",
4547};
4548
4549static inline void mem_cgroup_lru_names_not_uptodate(void)
4550{
4551 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4552}
4553
Wanpeng Liab215882012-07-31 16:43:09 -07004554static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004555 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004556{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004557 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004558 struct mem_cgroup *mi;
4559 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004560
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004561 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004562 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004563 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004564 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4565 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004566 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004567
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004568 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4569 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4570 mem_cgroup_read_events(memcg, i));
4571
4572 for (i = 0; i < NR_LRU_LISTS; i++)
4573 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4574 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4575
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004576 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004577 {
4578 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004579 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004580 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004581 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004582 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4583 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004584 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004585
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004586 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4587 long long val = 0;
4588
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004589 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004590 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004591 for_each_mem_cgroup_tree(mi, memcg)
4592 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4593 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4594 }
4595
4596 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4597 unsigned long long val = 0;
4598
4599 for_each_mem_cgroup_tree(mi, memcg)
4600 val += mem_cgroup_read_events(mi, i);
4601 seq_printf(m, "total_%s %llu\n",
4602 mem_cgroup_events_names[i], val);
4603 }
4604
4605 for (i = 0; i < NR_LRU_LISTS; i++) {
4606 unsigned long long val = 0;
4607
4608 for_each_mem_cgroup_tree(mi, memcg)
4609 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4610 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004611 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004612
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004613#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004614 {
4615 int nid, zid;
4616 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004617 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004618 unsigned long recent_rotated[2] = {0, 0};
4619 unsigned long recent_scanned[2] = {0, 0};
4620
4621 for_each_online_node(nid)
4622 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004623 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004624 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004625
Hugh Dickins89abfab2012-05-29 15:06:53 -07004626 recent_rotated[0] += rstat->recent_rotated[0];
4627 recent_rotated[1] += rstat->recent_rotated[1];
4628 recent_scanned[0] += rstat->recent_scanned[0];
4629 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004630 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004631 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4632 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4633 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4634 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004635 }
4636#endif
4637
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004638 return 0;
4639}
4640
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004641static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4642{
4643 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4644
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004645 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004646}
4647
4648static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4649 u64 val)
4650{
4651 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4652 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004653
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004654 if (val > 100)
4655 return -EINVAL;
4656
4657 if (cgrp->parent == NULL)
4658 return -EINVAL;
4659
4660 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004661
4662 cgroup_lock();
4663
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004664 /* If under hierarchy, only empty-root can set this value */
4665 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004666 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4667 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004668 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004669 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004670
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004671 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004672
Li Zefan068b38c2009-01-15 13:51:26 -08004673 cgroup_unlock();
4674
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004675 return 0;
4676}
4677
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004678static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4679{
4680 struct mem_cgroup_threshold_ary *t;
4681 u64 usage;
4682 int i;
4683
4684 rcu_read_lock();
4685 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004686 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004687 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004688 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004689
4690 if (!t)
4691 goto unlock;
4692
4693 usage = mem_cgroup_usage(memcg, swap);
4694
4695 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004696 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004697 * If it's not true, a threshold was crossed after last
4698 * call of __mem_cgroup_threshold().
4699 */
Phil Carmody5407a562010-05-26 14:42:42 -07004700 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004701
4702 /*
4703 * Iterate backward over array of thresholds starting from
4704 * current_threshold and check if a threshold is crossed.
4705 * If none of thresholds below usage is crossed, we read
4706 * only one element of the array here.
4707 */
4708 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4709 eventfd_signal(t->entries[i].eventfd, 1);
4710
4711 /* i = current_threshold + 1 */
4712 i++;
4713
4714 /*
4715 * Iterate forward over array of thresholds starting from
4716 * current_threshold+1 and check if a threshold is crossed.
4717 * If none of thresholds above usage is crossed, we read
4718 * only one element of the array here.
4719 */
4720 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4721 eventfd_signal(t->entries[i].eventfd, 1);
4722
4723 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004724 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004725unlock:
4726 rcu_read_unlock();
4727}
4728
4729static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4730{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004731 while (memcg) {
4732 __mem_cgroup_threshold(memcg, false);
4733 if (do_swap_account)
4734 __mem_cgroup_threshold(memcg, true);
4735
4736 memcg = parent_mem_cgroup(memcg);
4737 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004738}
4739
4740static int compare_thresholds(const void *a, const void *b)
4741{
4742 const struct mem_cgroup_threshold *_a = a;
4743 const struct mem_cgroup_threshold *_b = b;
4744
4745 return _a->threshold - _b->threshold;
4746}
4747
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004748static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004749{
4750 struct mem_cgroup_eventfd_list *ev;
4751
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004752 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004753 eventfd_signal(ev->eventfd, 1);
4754 return 0;
4755}
4756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004757static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004758{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004759 struct mem_cgroup *iter;
4760
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004761 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004762 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004763}
4764
4765static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4766 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004767{
4768 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004769 struct mem_cgroup_thresholds *thresholds;
4770 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004771 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004772 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004773 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004774
4775 ret = res_counter_memparse_write_strategy(args, &threshold);
4776 if (ret)
4777 return ret;
4778
4779 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004780
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004781 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004782 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004783 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004784 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004785 else
4786 BUG();
4787
4788 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4789
4790 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004791 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004792 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4793
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004794 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004795
4796 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004797 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004798 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004799 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004800 ret = -ENOMEM;
4801 goto unlock;
4802 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004803 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004804
4805 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004806 if (thresholds->primary) {
4807 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004808 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004809 }
4810
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004811 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004812 new->entries[size - 1].eventfd = eventfd;
4813 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004814
4815 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004816 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004817 compare_thresholds, NULL);
4818
4819 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004820 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004821 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004822 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004823 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004824 * new->current_threshold will not be used until
4825 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004826 * it here.
4827 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004828 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004829 } else
4830 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004831 }
4832
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004833 /* Free old spare buffer and save old primary buffer as spare */
4834 kfree(thresholds->spare);
4835 thresholds->spare = thresholds->primary;
4836
4837 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004838
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004839 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004840 synchronize_rcu();
4841
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004842unlock:
4843 mutex_unlock(&memcg->thresholds_lock);
4844
4845 return ret;
4846}
4847
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004848static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004849 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004850{
4851 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004852 struct mem_cgroup_thresholds *thresholds;
4853 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004854 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004855 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004856 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004857
4858 mutex_lock(&memcg->thresholds_lock);
4859 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004860 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004861 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004862 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004863 else
4864 BUG();
4865
Anton Vorontsov371528c2012-02-24 05:14:46 +04004866 if (!thresholds->primary)
4867 goto unlock;
4868
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004869 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4870
4871 /* Check if a threshold crossed before removing */
4872 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4873
4874 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004875 size = 0;
4876 for (i = 0; i < thresholds->primary->size; i++) {
4877 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004878 size++;
4879 }
4880
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004881 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004882
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004883 /* Set thresholds array to NULL if we don't have thresholds */
4884 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004885 kfree(new);
4886 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004887 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004888 }
4889
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004890 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004891
4892 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004893 new->current_threshold = -1;
4894 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4895 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004896 continue;
4897
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004898 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004899 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004900 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004901 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004902 * until rcu_assign_pointer(), so it's safe to increment
4903 * it here.
4904 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004905 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004906 }
4907 j++;
4908 }
4909
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004910swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004911 /* Swap primary and spare array */
4912 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004913 /* If all events are unregistered, free the spare array */
4914 if (!new) {
4915 kfree(thresholds->spare);
4916 thresholds->spare = NULL;
4917 }
4918
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004919 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004920
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004921 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004922 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004923unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004924 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004925}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004926
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004927static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4928 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4929{
4930 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4931 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004932 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004933
4934 BUG_ON(type != _OOM_TYPE);
4935 event = kmalloc(sizeof(*event), GFP_KERNEL);
4936 if (!event)
4937 return -ENOMEM;
4938
Michal Hocko1af8efe2011-07-26 16:08:24 -07004939 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004940
4941 event->eventfd = eventfd;
4942 list_add(&event->list, &memcg->oom_notify);
4943
4944 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004945 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004946 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004947 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004948
4949 return 0;
4950}
4951
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004952static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004953 struct cftype *cft, struct eventfd_ctx *eventfd)
4954{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004955 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004956 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004957 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004958
4959 BUG_ON(type != _OOM_TYPE);
4960
Michal Hocko1af8efe2011-07-26 16:08:24 -07004961 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004962
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004963 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004964 if (ev->eventfd == eventfd) {
4965 list_del(&ev->list);
4966 kfree(ev);
4967 }
4968 }
4969
Michal Hocko1af8efe2011-07-26 16:08:24 -07004970 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004971}
4972
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004973static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4974 struct cftype *cft, struct cgroup_map_cb *cb)
4975{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004976 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004977
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004978 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004979
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004980 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004981 cb->fill(cb, "under_oom", 1);
4982 else
4983 cb->fill(cb, "under_oom", 0);
4984 return 0;
4985}
4986
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004987static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4988 struct cftype *cft, u64 val)
4989{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004990 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004991 struct mem_cgroup *parent;
4992
4993 /* cannot set to root cgroup and only 0 and 1 are allowed */
4994 if (!cgrp->parent || !((val == 0) || (val == 1)))
4995 return -EINVAL;
4996
4997 parent = mem_cgroup_from_cont(cgrp->parent);
4998
4999 cgroup_lock();
5000 /* oom-kill-disable is a flag for subhierarchy. */
5001 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005002 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005003 cgroup_unlock();
5004 return -EINVAL;
5005 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005006 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005007 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005008 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005009 cgroup_unlock();
5010 return 0;
5011}
5012
Andrew Mortonc255a452012-07-31 16:43:02 -07005013#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005014static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005015{
Glauber Costa510fc4e2012-12-18 14:21:47 -08005016 memcg_propagate_kmem(memcg);
Glauber Costa1d62e432012-04-09 19:36:33 -03005017 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005018};
5019
Glauber Costa1d62e432012-04-09 19:36:33 -03005020static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005021{
Glauber Costa1d62e432012-04-09 19:36:33 -03005022 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005023
5024 memcg_kmem_mark_dead(memcg);
5025
5026 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5027 return;
5028
5029 /*
5030 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5031 * path here, being careful not to race with memcg_uncharge_kmem: it is
5032 * possible that the charges went down to 0 between mark_dead and the
5033 * res_counter read, so in that case, we don't need the put
5034 */
5035 if (memcg_kmem_test_and_clear_dead(memcg))
5036 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005037}
Glauber Costae5671df2011-12-11 21:47:01 +00005038#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005039static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005040{
5041 return 0;
5042}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005043
Glauber Costa1d62e432012-04-09 19:36:33 -03005044static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005045{
5046}
Glauber Costae5671df2011-12-11 21:47:01 +00005047#endif
5048
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005049static struct cftype mem_cgroup_files[] = {
5050 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005051 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005052 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005053 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005054 .register_event = mem_cgroup_usage_register_event,
5055 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005056 },
5057 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005058 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005059 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005060 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005061 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005062 },
5063 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005064 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005065 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005066 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005067 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005068 },
5069 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005070 .name = "soft_limit_in_bytes",
5071 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5072 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005073 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005074 },
5075 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005076 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005077 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005078 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005079 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005080 },
Balbir Singh8697d332008-02-07 00:13:59 -08005081 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005082 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005083 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005084 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005085 {
5086 .name = "force_empty",
5087 .trigger = mem_cgroup_force_empty_write,
5088 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005089 {
5090 .name = "use_hierarchy",
5091 .write_u64 = mem_cgroup_hierarchy_write,
5092 .read_u64 = mem_cgroup_hierarchy_read,
5093 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005094 {
5095 .name = "swappiness",
5096 .read_u64 = mem_cgroup_swappiness_read,
5097 .write_u64 = mem_cgroup_swappiness_write,
5098 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005099 {
5100 .name = "move_charge_at_immigrate",
5101 .read_u64 = mem_cgroup_move_charge_read,
5102 .write_u64 = mem_cgroup_move_charge_write,
5103 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005104 {
5105 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005106 .read_map = mem_cgroup_oom_control_read,
5107 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005108 .register_event = mem_cgroup_oom_register_event,
5109 .unregister_event = mem_cgroup_oom_unregister_event,
5110 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5111 },
Ying Han406eb0c2011-05-26 16:25:37 -07005112#ifdef CONFIG_NUMA
5113 {
5114 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005115 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005116 },
5117#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07005118#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005119 {
5120 .name = "memsw.usage_in_bytes",
5121 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005122 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005123 .register_event = mem_cgroup_usage_register_event,
5124 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005125 },
5126 {
5127 .name = "memsw.max_usage_in_bytes",
5128 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5129 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005130 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005131 },
5132 {
5133 .name = "memsw.limit_in_bytes",
5134 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5135 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005136 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005137 },
5138 {
5139 .name = "memsw.failcnt",
5140 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5141 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005142 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005143 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005144#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005145#ifdef CONFIG_MEMCG_KMEM
5146 {
5147 .name = "kmem.limit_in_bytes",
5148 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5149 .write_string = mem_cgroup_write,
5150 .read = mem_cgroup_read,
5151 },
5152 {
5153 .name = "kmem.usage_in_bytes",
5154 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5155 .read = mem_cgroup_read,
5156 },
5157 {
5158 .name = "kmem.failcnt",
5159 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5160 .trigger = mem_cgroup_reset,
5161 .read = mem_cgroup_read,
5162 },
5163 {
5164 .name = "kmem.max_usage_in_bytes",
5165 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5166 .trigger = mem_cgroup_reset,
5167 .read = mem_cgroup_read,
5168 },
5169#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005170 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005171};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005172
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005173static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005174{
5175 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005176 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005177 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005178 /*
5179 * This routine is called against possible nodes.
5180 * But it's BUG to call kmalloc() against offline node.
5181 *
5182 * TODO: this routine can waste much memory for nodes which will
5183 * never be onlined. It's better to use memory hotplug callback
5184 * function.
5185 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005186 if (!node_state(node, N_NORMAL_MEMORY))
5187 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005188 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005189 if (!pn)
5190 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005191
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005192 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5193 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005194 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005195 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005196 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005197 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005198 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005199 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005200 return 0;
5201}
5202
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005203static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005204{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005205 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005206}
5207
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005208static struct mem_cgroup *mem_cgroup_alloc(void)
5209{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005210 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005211 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005212
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005213 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005214 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005215 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005216 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005217 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005218
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005219 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005220 return NULL;
5221
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005222 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5223 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005224 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005225 spin_lock_init(&memcg->pcp_counter_lock);
5226 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005227
5228out_free:
5229 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005230 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005231 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005232 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005233 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005234}
5235
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005236/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07005237 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07005238 * but in process context. The work_freeing structure is overlaid
5239 * on the rcu_freeing structure, which itself is overlaid on memsw.
5240 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07005241static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005242{
5243 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005244 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005245
5246 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07005247 /*
5248 * We need to make sure that (at least for now), the jump label
5249 * destruction code runs outside of the cgroup lock. This is because
5250 * get_online_cpus(), which is called from the static_branch update,
5251 * can't be called inside the cgroup_lock. cpusets are the ones
5252 * enforcing this dependency, so if they ever change, we might as well.
5253 *
5254 * schedule_work() will guarantee this happens. Be careful if you need
5255 * to move this code around, and make sure it is outside
5256 * the cgroup_lock.
5257 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005258 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005259 if (size < PAGE_SIZE)
5260 kfree(memcg);
5261 else
5262 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005263}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005264
5265static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005266{
5267 struct mem_cgroup *memcg;
5268
5269 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005270 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005271 schedule_work(&memcg->work_freeing);
5272}
5273
5274/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005275 * At destroying mem_cgroup, references from swap_cgroup can remain.
5276 * (scanning all at force_empty is too costly...)
5277 *
5278 * Instead of clearing all references at force_empty, we remember
5279 * the number of reference from swap_cgroup and free mem_cgroup when
5280 * it goes down to 0.
5281 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005282 * Removal of cgroup itself succeeds regardless of refs from swap.
5283 */
5284
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005285static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005286{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005287 int node;
5288
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005289 mem_cgroup_remove_from_trees(memcg);
5290 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005291
Bob Liu3ed28fa2012-01-12 17:19:04 -08005292 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005293 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005294
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005295 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005296 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005297}
5298
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005299static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005300{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005301 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005302}
5303
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005304static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005305{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005306 if (atomic_sub_and_test(count, &memcg->refcnt)) {
5307 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5308 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005309 if (parent)
5310 mem_cgroup_put(parent);
5311 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005312}
5313
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005314static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005315{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005316 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005317}
5318
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005319/*
5320 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5321 */
Glauber Costae1aab162011-12-11 21:47:03 +00005322struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005323{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005324 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005325 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005326 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005327}
Glauber Costae1aab162011-12-11 21:47:03 +00005328EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005329
Andrew Mortonc255a452012-07-31 16:43:02 -07005330#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005331static void __init enable_swap_cgroup(void)
5332{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08005333 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005334 do_swap_account = 1;
5335}
5336#else
5337static void __init enable_swap_cgroup(void)
5338{
5339}
5340#endif
5341
Balbir Singhf64c3f52009-09-23 15:56:37 -07005342static int mem_cgroup_soft_limit_tree_init(void)
5343{
5344 struct mem_cgroup_tree_per_node *rtpn;
5345 struct mem_cgroup_tree_per_zone *rtpz;
5346 int tmp, node, zone;
5347
Bob Liu3ed28fa2012-01-12 17:19:04 -08005348 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07005349 tmp = node;
5350 if (!node_state(node, N_NORMAL_MEMORY))
5351 tmp = -1;
5352 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5353 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08005354 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005355
5356 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5357
5358 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5359 rtpz = &rtpn->rb_tree_per_zone[zone];
5360 rtpz->rb_root = RB_ROOT;
5361 spin_lock_init(&rtpz->lock);
5362 }
5363 }
5364 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08005365
5366err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08005367 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08005368 if (!soft_limit_tree.rb_tree_per_node[node])
5369 break;
5370 kfree(soft_limit_tree.rb_tree_per_node[node]);
5371 soft_limit_tree.rb_tree_per_node[node] = NULL;
5372 }
5373 return 1;
5374
Balbir Singhf64c3f52009-09-23 15:56:37 -07005375}
5376
Li Zefan0eb253e2009-01-15 13:51:25 -08005377static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08005378mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005379{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005380 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005381 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005382 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005383
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005384 memcg = mem_cgroup_alloc();
5385 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005386 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005387
Bob Liu3ed28fa2012-01-12 17:19:04 -08005388 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005389 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005390 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005391
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005392 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005393 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005394 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005395 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005396 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005397 if (mem_cgroup_soft_limit_tree_init())
5398 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08005399 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005400 for_each_possible_cpu(cpu) {
5401 struct memcg_stock_pcp *stock =
5402 &per_cpu(memcg_stock, cpu);
5403 INIT_WORK(&stock->work, drain_local_stock);
5404 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005405 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005406 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005407 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005408 memcg->use_hierarchy = parent->use_hierarchy;
5409 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005410 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005411
Balbir Singh18f59ea2009-01-07 18:08:07 -08005412 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005413 res_counter_init(&memcg->res, &parent->res);
5414 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005415 res_counter_init(&memcg->kmem, &parent->kmem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005416 /*
5417 * We increment refcnt of the parent to ensure that we can
5418 * safely access it on res_counter_charge/uncharge.
5419 * This refcnt will be decremented when freeing this
5420 * mem_cgroup(see mem_cgroup_put).
5421 */
5422 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005423 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005424 res_counter_init(&memcg->res, NULL);
5425 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005426 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005427 /*
5428 * Deeper hierachy with use_hierarchy == false doesn't make
5429 * much sense so let cgroup subsystem know about this
5430 * unfortunate state in our controller.
5431 */
5432 if (parent && parent != root_mem_cgroup)
5433 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005434 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005435 memcg->last_scanned_node = MAX_NUMNODES;
5436 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005437
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005438 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005439 memcg->swappiness = mem_cgroup_swappiness(parent);
5440 atomic_set(&memcg->refcnt, 1);
5441 memcg->move_charge_at_immigrate = 0;
5442 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005443 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005444
5445 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5446 if (error) {
5447 /*
5448 * We call put now because our (and parent's) refcnts
5449 * are already in place. mem_cgroup_put() will internally
5450 * call __mem_cgroup_free, so return directly
5451 */
5452 mem_cgroup_put(memcg);
5453 return ERR_PTR(error);
5454 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005455 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005456free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005457 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005458 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005459}
5460
Tejun Heo92fb9742012-11-19 08:13:38 -08005461static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005462{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005463 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005464
Michal Hockoab5196c2012-10-26 13:37:32 +02005465 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005466}
5467
Tejun Heo92fb9742012-11-19 08:13:38 -08005468static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005469{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005470 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005471
Glauber Costa1d62e432012-04-09 19:36:33 -03005472 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005473
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005474 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005475}
5476
Daisuke Nishimura02491442010-03-10 15:22:17 -08005477#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005478/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005479#define PRECHARGE_COUNT_AT_ONCE 256
5480static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005481{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005482 int ret = 0;
5483 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005484 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005485
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005486 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005487 mc.precharge += count;
5488 /* we don't need css_get for root */
5489 return ret;
5490 }
5491 /* try to charge at once */
5492 if (count > 1) {
5493 struct res_counter *dummy;
5494 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005495 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005496 * by cgroup_lock_live_cgroup() that it is not removed and we
5497 * are still under the same cgroup_mutex. So we can postpone
5498 * css_get().
5499 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005500 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005501 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005502 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005503 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005504 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005505 goto one_by_one;
5506 }
5507 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005508 return ret;
5509 }
5510one_by_one:
5511 /* fall back to one by one charge */
5512 while (count--) {
5513 if (signal_pending(current)) {
5514 ret = -EINTR;
5515 break;
5516 }
5517 if (!batch_count--) {
5518 batch_count = PRECHARGE_COUNT_AT_ONCE;
5519 cond_resched();
5520 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005521 ret = __mem_cgroup_try_charge(NULL,
5522 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005523 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005524 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005525 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005526 mc.precharge++;
5527 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005528 return ret;
5529}
5530
5531/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005532 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005533 * @vma: the vma the pte to be checked belongs
5534 * @addr: the address corresponding to the pte to be checked
5535 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005536 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005537 *
5538 * Returns
5539 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5540 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5541 * move charge. if @target is not NULL, the page is stored in target->page
5542 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005543 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5544 * target for charge migration. if @target is not NULL, the entry is stored
5545 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005546 *
5547 * Called with pte lock held.
5548 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005549union mc_target {
5550 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005551 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005552};
5553
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005554enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005555 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005556 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005557 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005558};
5559
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005560static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5561 unsigned long addr, pte_t ptent)
5562{
5563 struct page *page = vm_normal_page(vma, addr, ptent);
5564
5565 if (!page || !page_mapped(page))
5566 return NULL;
5567 if (PageAnon(page)) {
5568 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005569 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005570 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005571 } else if (!move_file())
5572 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005573 return NULL;
5574 if (!get_page_unless_zero(page))
5575 return NULL;
5576
5577 return page;
5578}
5579
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005580#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005581static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5582 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5583{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005584 struct page *page = NULL;
5585 swp_entry_t ent = pte_to_swp_entry(ptent);
5586
5587 if (!move_anon() || non_swap_entry(ent))
5588 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005589 /*
5590 * Because lookup_swap_cache() updates some statistics counter,
5591 * we call find_get_page() with swapper_space directly.
5592 */
5593 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005594 if (do_swap_account)
5595 entry->val = ent.val;
5596
5597 return page;
5598}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005599#else
5600static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5601 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5602{
5603 return NULL;
5604}
5605#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005606
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005607static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5608 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5609{
5610 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005611 struct address_space *mapping;
5612 pgoff_t pgoff;
5613
5614 if (!vma->vm_file) /* anonymous vma */
5615 return NULL;
5616 if (!move_file())
5617 return NULL;
5618
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005619 mapping = vma->vm_file->f_mapping;
5620 if (pte_none(ptent))
5621 pgoff = linear_page_index(vma, addr);
5622 else /* pte_file(ptent) is true */
5623 pgoff = pte_to_pgoff(ptent);
5624
5625 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005626 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005627
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005628#ifdef CONFIG_SWAP
5629 /* shmem/tmpfs may report page out on swap: account for that too. */
5630 if (radix_tree_exceptional_entry(page)) {
5631 swp_entry_t swap = radix_to_swp_entry(page);
5632 if (do_swap_account)
5633 *entry = swap;
5634 page = find_get_page(&swapper_space, swap.val);
5635 }
5636#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005637 return page;
5638}
5639
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005640static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005641 unsigned long addr, pte_t ptent, union mc_target *target)
5642{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005643 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005644 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005645 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005646 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005647
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005648 if (pte_present(ptent))
5649 page = mc_handle_present_pte(vma, addr, ptent);
5650 else if (is_swap_pte(ptent))
5651 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005652 else if (pte_none(ptent) || pte_file(ptent))
5653 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005654
5655 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005656 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005657 if (page) {
5658 pc = lookup_page_cgroup(page);
5659 /*
5660 * Do only loose check w/o page_cgroup lock.
5661 * mem_cgroup_move_account() checks the pc is valid or not under
5662 * the lock.
5663 */
5664 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5665 ret = MC_TARGET_PAGE;
5666 if (target)
5667 target->page = page;
5668 }
5669 if (!ret || !target)
5670 put_page(page);
5671 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005672 /* There is a swap entry and a page doesn't exist or isn't charged */
5673 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005674 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005675 ret = MC_TARGET_SWAP;
5676 if (target)
5677 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005678 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005679 return ret;
5680}
5681
Naoya Horiguchi12724852012-03-21 16:34:28 -07005682#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5683/*
5684 * We don't consider swapping or file mapped pages because THP does not
5685 * support them for now.
5686 * Caller should make sure that pmd_trans_huge(pmd) is true.
5687 */
5688static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5689 unsigned long addr, pmd_t pmd, union mc_target *target)
5690{
5691 struct page *page = NULL;
5692 struct page_cgroup *pc;
5693 enum mc_target_type ret = MC_TARGET_NONE;
5694
5695 page = pmd_page(pmd);
5696 VM_BUG_ON(!page || !PageHead(page));
5697 if (!move_anon())
5698 return ret;
5699 pc = lookup_page_cgroup(page);
5700 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5701 ret = MC_TARGET_PAGE;
5702 if (target) {
5703 get_page(page);
5704 target->page = page;
5705 }
5706 }
5707 return ret;
5708}
5709#else
5710static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5711 unsigned long addr, pmd_t pmd, union mc_target *target)
5712{
5713 return MC_TARGET_NONE;
5714}
5715#endif
5716
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005717static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5718 unsigned long addr, unsigned long end,
5719 struct mm_walk *walk)
5720{
5721 struct vm_area_struct *vma = walk->private;
5722 pte_t *pte;
5723 spinlock_t *ptl;
5724
Naoya Horiguchi12724852012-03-21 16:34:28 -07005725 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5726 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5727 mc.precharge += HPAGE_PMD_NR;
5728 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005729 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005730 }
Dave Hansen03319322011-03-22 16:32:56 -07005731
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005732 if (pmd_trans_unstable(pmd))
5733 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005734 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5735 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005736 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005737 mc.precharge++; /* increment precharge temporarily */
5738 pte_unmap_unlock(pte - 1, ptl);
5739 cond_resched();
5740
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005741 return 0;
5742}
5743
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005744static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5745{
5746 unsigned long precharge;
5747 struct vm_area_struct *vma;
5748
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005749 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005750 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5751 struct mm_walk mem_cgroup_count_precharge_walk = {
5752 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5753 .mm = mm,
5754 .private = vma,
5755 };
5756 if (is_vm_hugetlb_page(vma))
5757 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005758 walk_page_range(vma->vm_start, vma->vm_end,
5759 &mem_cgroup_count_precharge_walk);
5760 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005761 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005762
5763 precharge = mc.precharge;
5764 mc.precharge = 0;
5765
5766 return precharge;
5767}
5768
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005769static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5770{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005771 unsigned long precharge = mem_cgroup_count_precharge(mm);
5772
5773 VM_BUG_ON(mc.moving_task);
5774 mc.moving_task = current;
5775 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005776}
5777
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005778/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5779static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005780{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005781 struct mem_cgroup *from = mc.from;
5782 struct mem_cgroup *to = mc.to;
5783
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005784 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005785 if (mc.precharge) {
5786 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5787 mc.precharge = 0;
5788 }
5789 /*
5790 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5791 * we must uncharge here.
5792 */
5793 if (mc.moved_charge) {
5794 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5795 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005796 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005797 /* we must fixup refcnts and charges */
5798 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005799 /* uncharge swap account from the old cgroup */
5800 if (!mem_cgroup_is_root(mc.from))
5801 res_counter_uncharge(&mc.from->memsw,
5802 PAGE_SIZE * mc.moved_swap);
5803 __mem_cgroup_put(mc.from, mc.moved_swap);
5804
5805 if (!mem_cgroup_is_root(mc.to)) {
5806 /*
5807 * we charged both to->res and to->memsw, so we should
5808 * uncharge to->res.
5809 */
5810 res_counter_uncharge(&mc.to->res,
5811 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005812 }
5813 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005814 mc.moved_swap = 0;
5815 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005816 memcg_oom_recover(from);
5817 memcg_oom_recover(to);
5818 wake_up_all(&mc.waitq);
5819}
5820
5821static void mem_cgroup_clear_mc(void)
5822{
5823 struct mem_cgroup *from = mc.from;
5824
5825 /*
5826 * we must clear moving_task before waking up waiters at the end of
5827 * task migration.
5828 */
5829 mc.moving_task = NULL;
5830 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005831 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005832 mc.from = NULL;
5833 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005834 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005835 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005836}
5837
Li Zefan761b3ef2012-01-31 13:47:36 +08005838static int mem_cgroup_can_attach(struct cgroup *cgroup,
5839 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005840{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005841 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005842 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005843 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005845 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005846 struct mm_struct *mm;
5847 struct mem_cgroup *from = mem_cgroup_from_task(p);
5848
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005849 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005850
5851 mm = get_task_mm(p);
5852 if (!mm)
5853 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005854 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005855 if (mm->owner == p) {
5856 VM_BUG_ON(mc.from);
5857 VM_BUG_ON(mc.to);
5858 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005859 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005860 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005861 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005862 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005863 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005864 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005865 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005866 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005867
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005868 ret = mem_cgroup_precharge_mc(mm);
5869 if (ret)
5870 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005871 }
5872 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005873 }
5874 return ret;
5875}
5876
Li Zefan761b3ef2012-01-31 13:47:36 +08005877static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5878 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005879{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005880 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005881}
5882
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005883static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5884 unsigned long addr, unsigned long end,
5885 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005886{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005887 int ret = 0;
5888 struct vm_area_struct *vma = walk->private;
5889 pte_t *pte;
5890 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005891 enum mc_target_type target_type;
5892 union mc_target target;
5893 struct page *page;
5894 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005895
Naoya Horiguchi12724852012-03-21 16:34:28 -07005896 /*
5897 * We don't take compound_lock() here but no race with splitting thp
5898 * happens because:
5899 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5900 * under splitting, which means there's no concurrent thp split,
5901 * - if another thread runs into split_huge_page() just after we
5902 * entered this if-block, the thread must wait for page table lock
5903 * to be unlocked in __split_huge_page_splitting(), where the main
5904 * part of thp split is not executed yet.
5905 */
5906 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005907 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005908 spin_unlock(&vma->vm_mm->page_table_lock);
5909 return 0;
5910 }
5911 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5912 if (target_type == MC_TARGET_PAGE) {
5913 page = target.page;
5914 if (!isolate_lru_page(page)) {
5915 pc = lookup_page_cgroup(page);
5916 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005917 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005918 mc.precharge -= HPAGE_PMD_NR;
5919 mc.moved_charge += HPAGE_PMD_NR;
5920 }
5921 putback_lru_page(page);
5922 }
5923 put_page(page);
5924 }
5925 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005926 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005927 }
5928
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005929 if (pmd_trans_unstable(pmd))
5930 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005931retry:
5932 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5933 for (; addr != end; addr += PAGE_SIZE) {
5934 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005935 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005936
5937 if (!mc.precharge)
5938 break;
5939
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005940 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005941 case MC_TARGET_PAGE:
5942 page = target.page;
5943 if (isolate_lru_page(page))
5944 goto put;
5945 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005946 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005947 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005948 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005949 /* we uncharge from mc.from later. */
5950 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005951 }
5952 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005953put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005954 put_page(page);
5955 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005956 case MC_TARGET_SWAP:
5957 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005958 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005959 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005960 /* we fixup refcnts and charges later. */
5961 mc.moved_swap++;
5962 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005963 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005964 default:
5965 break;
5966 }
5967 }
5968 pte_unmap_unlock(pte - 1, ptl);
5969 cond_resched();
5970
5971 if (addr != end) {
5972 /*
5973 * We have consumed all precharges we got in can_attach().
5974 * We try charge one by one, but don't do any additional
5975 * charges to mc.to if we have failed in charge once in attach()
5976 * phase.
5977 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005978 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005979 if (!ret)
5980 goto retry;
5981 }
5982
5983 return ret;
5984}
5985
5986static void mem_cgroup_move_charge(struct mm_struct *mm)
5987{
5988 struct vm_area_struct *vma;
5989
5990 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005991retry:
5992 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5993 /*
5994 * Someone who are holding the mmap_sem might be waiting in
5995 * waitq. So we cancel all extra charges, wake up all waiters,
5996 * and retry. Because we cancel precharges, we might not be able
5997 * to move enough charges, but moving charge is a best-effort
5998 * feature anyway, so it wouldn't be a big problem.
5999 */
6000 __mem_cgroup_clear_mc();
6001 cond_resched();
6002 goto retry;
6003 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006004 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6005 int ret;
6006 struct mm_walk mem_cgroup_move_charge_walk = {
6007 .pmd_entry = mem_cgroup_move_charge_pte_range,
6008 .mm = mm,
6009 .private = vma,
6010 };
6011 if (is_vm_hugetlb_page(vma))
6012 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006013 ret = walk_page_range(vma->vm_start, vma->vm_end,
6014 &mem_cgroup_move_charge_walk);
6015 if (ret)
6016 /*
6017 * means we have consumed all precharges and failed in
6018 * doing additional charge. Just abandon here.
6019 */
6020 break;
6021 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006022 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006023}
6024
Li Zefan761b3ef2012-01-31 13:47:36 +08006025static void mem_cgroup_move_task(struct cgroup *cont,
6026 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006027{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006028 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006029 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006030
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006031 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006032 if (mc.to)
6033 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006034 mmput(mm);
6035 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006036 if (mc.to)
6037 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006038}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006039#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006040static int mem_cgroup_can_attach(struct cgroup *cgroup,
6041 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006042{
6043 return 0;
6044}
Li Zefan761b3ef2012-01-31 13:47:36 +08006045static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6046 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006047{
6048}
Li Zefan761b3ef2012-01-31 13:47:36 +08006049static void mem_cgroup_move_task(struct cgroup *cont,
6050 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006051{
6052}
6053#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006054
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006055struct cgroup_subsys mem_cgroup_subsys = {
6056 .name = "memory",
6057 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006058 .css_alloc = mem_cgroup_css_alloc,
6059 .css_offline = mem_cgroup_css_offline,
6060 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006061 .can_attach = mem_cgroup_can_attach,
6062 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006063 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006064 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006065 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006066 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006067};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006068
Andrew Mortonc255a452012-07-31 16:43:02 -07006069#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006070static int __init enable_swap_account(char *s)
6071{
6072 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006073 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006074 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006075 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006076 really_do_swap_account = 0;
6077 return 1;
6078}
Michal Hockoa2c89902011-05-24 17:12:50 -07006079__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006080
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006081#endif