blob: f96ccc90fa664a08064436f28e8ba7f9fb205da3 [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);
Glauber Costabea207c2012-12-18 14:22:11 -0800550 /*
551 * This check can't live in kmem destruction function,
552 * since the charges will outlive the cgroup
553 */
554 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800555}
556#else
557static void disarm_kmem_keys(struct mem_cgroup *memcg)
558{
559}
560#endif /* CONFIG_MEMCG_KMEM */
561
562static void disarm_static_keys(struct mem_cgroup *memcg)
563{
564 disarm_sock_keys(memcg);
565 disarm_kmem_keys(memcg);
566}
567
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700568static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800569
Balbir Singhf64c3f52009-09-23 15:56:37 -0700570static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700571mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700572{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700573 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700574}
575
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700576struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100577{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700578 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100579}
580
Balbir Singhf64c3f52009-09-23 15:56:37 -0700581static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700582page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700583{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700584 int nid = page_to_nid(page);
585 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700586
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700587 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700588}
589
590static struct mem_cgroup_tree_per_zone *
591soft_limit_tree_node_zone(int nid, int zid)
592{
593 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
594}
595
596static struct mem_cgroup_tree_per_zone *
597soft_limit_tree_from_page(struct page *page)
598{
599 int nid = page_to_nid(page);
600 int zid = page_zonenum(page);
601
602 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
603}
604
605static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700606__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700607 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700608 struct mem_cgroup_tree_per_zone *mctz,
609 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700610{
611 struct rb_node **p = &mctz->rb_root.rb_node;
612 struct rb_node *parent = NULL;
613 struct mem_cgroup_per_zone *mz_node;
614
615 if (mz->on_tree)
616 return;
617
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700618 mz->usage_in_excess = new_usage_in_excess;
619 if (!mz->usage_in_excess)
620 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700621 while (*p) {
622 parent = *p;
623 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
624 tree_node);
625 if (mz->usage_in_excess < mz_node->usage_in_excess)
626 p = &(*p)->rb_left;
627 /*
628 * We can't avoid mem cgroups that are over their soft
629 * limit by the same amount
630 */
631 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
632 p = &(*p)->rb_right;
633 }
634 rb_link_node(&mz->tree_node, parent, p);
635 rb_insert_color(&mz->tree_node, &mctz->rb_root);
636 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700637}
638
639static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700640__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700641 struct mem_cgroup_per_zone *mz,
642 struct mem_cgroup_tree_per_zone *mctz)
643{
644 if (!mz->on_tree)
645 return;
646 rb_erase(&mz->tree_node, &mctz->rb_root);
647 mz->on_tree = false;
648}
649
650static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700652 struct mem_cgroup_per_zone *mz,
653 struct mem_cgroup_tree_per_zone *mctz)
654{
655 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700656 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700657 spin_unlock(&mctz->lock);
658}
659
Balbir Singhf64c3f52009-09-23 15:56:37 -0700660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700662{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700663 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700664 struct mem_cgroup_per_zone *mz;
665 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700666 int nid = page_to_nid(page);
667 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700668 mctz = soft_limit_tree_from_page(page);
669
670 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700671 * Necessary to update all ancestors when hierarchy is used.
672 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700673 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
675 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
676 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700677 /*
678 * We have to update the tree if mz is on RB-tree or
679 * mem is over its softlimit.
680 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700681 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700682 spin_lock(&mctz->lock);
683 /* if on-tree, remove it */
684 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700685 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700686 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700687 * Insert again. mz->usage_in_excess will be updated.
688 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700689 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700690 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700691 spin_unlock(&mctz->lock);
692 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700693 }
694}
695
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700696static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700697{
698 int node, zone;
699 struct mem_cgroup_per_zone *mz;
700 struct mem_cgroup_tree_per_zone *mctz;
701
Bob Liu3ed28fa2012-01-12 17:19:04 -0800702 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700703 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700704 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700705 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700706 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700707 }
708 }
709}
710
Balbir Singh4e416952009-09-23 15:56:39 -0700711static struct mem_cgroup_per_zone *
712__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
713{
714 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700715 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700716
717retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700718 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700719 rightmost = rb_last(&mctz->rb_root);
720 if (!rightmost)
721 goto done; /* Nothing to reclaim from */
722
723 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
724 /*
725 * Remove the node now but someone else can add it back,
726 * we will to add it back at the end of reclaim to its correct
727 * position in the tree.
728 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700729 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
730 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
731 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700732 goto retry;
733done:
734 return mz;
735}
736
737static struct mem_cgroup_per_zone *
738mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
739{
740 struct mem_cgroup_per_zone *mz;
741
742 spin_lock(&mctz->lock);
743 mz = __mem_cgroup_largest_soft_limit_node(mctz);
744 spin_unlock(&mctz->lock);
745 return mz;
746}
747
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700748/*
749 * Implementation Note: reading percpu statistics for memcg.
750 *
751 * Both of vmstat[] and percpu_counter has threshold and do periodic
752 * synchronization to implement "quick" read. There are trade-off between
753 * reading cost and precision of value. Then, we may have a chance to implement
754 * a periodic synchronizion of counter in memcg's counter.
755 *
756 * But this _read() function is used for user interface now. The user accounts
757 * memory usage by memory cgroup and he _always_ requires exact value because
758 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
759 * have to visit all online cpus and make sum. So, for now, unnecessary
760 * synchronization is not implemented. (just implemented for cpu hotplug)
761 *
762 * If there are kernel internal actions which can make use of some not-exact
763 * value, and reading all cpu value can be performance bottleneck in some
764 * common workload, threashold and synchonization as vmstat[] should be
765 * implemented.
766 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700767static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700768 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800769{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700770 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800771 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800772
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700773 get_online_cpus();
774 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700776#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700777 spin_lock(&memcg->pcp_counter_lock);
778 val += memcg->nocpu_base.count[idx];
779 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700780#endif
781 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800782 return val;
783}
784
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700785static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700786 bool charge)
787{
788 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700789 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700790}
791
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700792static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700793 enum mem_cgroup_events_index idx)
794{
795 unsigned long val = 0;
796 int cpu;
797
798 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700799 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700800#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700801 spin_lock(&memcg->pcp_counter_lock);
802 val += memcg->nocpu_base.events[idx];
803 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700804#endif
805 return val;
806}
807
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700808static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700809 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800810{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800811 preempt_disable();
812
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700813 /*
814 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
815 * counted as CACHE even if it's on ANON LRU.
816 */
817 if (anon)
818 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700819 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800820 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700821 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700822 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700823
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800824 /* pagein of a big page is an event. So, ignore page size */
825 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700826 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800827 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700828 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800829 nr_pages = -nr_pages; /* for event */
830 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800831
Johannes Weiner13114712012-05-29 15:07:07 -0700832 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800833
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800834 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800835}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800836
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700837unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700838mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700839{
840 struct mem_cgroup_per_zone *mz;
841
842 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
843 return mz->lru_size[lru];
844}
845
846static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700847mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700848 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700849{
850 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700851 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700852 unsigned long ret = 0;
853
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700854 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700855
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700856 for_each_lru(lru) {
857 if (BIT(lru) & lru_mask)
858 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700859 }
860 return ret;
861}
862
863static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700864mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700865 int nid, unsigned int lru_mask)
866{
Ying Han889976d2011-05-26 16:25:33 -0700867 u64 total = 0;
868 int zid;
869
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700870 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700871 total += mem_cgroup_zone_nr_lru_pages(memcg,
872 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700873
Ying Han889976d2011-05-26 16:25:33 -0700874 return total;
875}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700876
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700877static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700878 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800879{
Ying Han889976d2011-05-26 16:25:33 -0700880 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800881 u64 total = 0;
882
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800883 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800885 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800886}
887
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800888static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
889 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800890{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700891 unsigned long val, next;
892
Johannes Weiner13114712012-05-29 15:07:07 -0700893 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700894 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700895 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800896 if ((long)next - (long)val < 0) {
897 switch (target) {
898 case MEM_CGROUP_TARGET_THRESH:
899 next = val + THRESHOLDS_EVENTS_TARGET;
900 break;
901 case MEM_CGROUP_TARGET_SOFTLIMIT:
902 next = val + SOFTLIMIT_EVENTS_TARGET;
903 break;
904 case MEM_CGROUP_TARGET_NUMAINFO:
905 next = val + NUMAINFO_EVENTS_TARGET;
906 break;
907 default:
908 break;
909 }
910 __this_cpu_write(memcg->stat->targets[target], next);
911 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700912 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800913 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800914}
915
916/*
917 * Check events in order.
918 *
919 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700920static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800921{
Steven Rostedt47994012011-11-02 13:38:33 -0700922 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800923 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800924 if (unlikely(mem_cgroup_event_ratelimit(memcg,
925 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800926 bool do_softlimit;
927 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800928
929 do_softlimit = mem_cgroup_event_ratelimit(memcg,
930 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700931#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800932 do_numainfo = mem_cgroup_event_ratelimit(memcg,
933 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700934#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800935 preempt_enable();
936
937 mem_cgroup_threshold(memcg);
938 if (unlikely(do_softlimit))
939 mem_cgroup_update_tree(memcg, page);
940#if MAX_NUMNODES > 1
941 if (unlikely(do_numainfo))
942 atomic_inc(&memcg->numainfo_events);
943#endif
944 } else
945 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800946}
947
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000948struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800949{
Wanpeng Lib2145142012-07-31 16:46:01 -0700950 return mem_cgroup_from_css(
951 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800952}
953
Balbir Singhcf475ad2008-04-29 01:00:16 -0700954struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800955{
Balbir Singh31a78f22008-09-28 23:09:31 +0100956 /*
957 * mm_update_next_owner() may clear mm->owner to NULL
958 * if it races with swapoff, page migration, etc.
959 * So this can be called with p == NULL.
960 */
961 if (unlikely(!p))
962 return NULL;
963
Wanpeng Lib2145142012-07-31 16:46:01 -0700964 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800965}
966
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700967struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800968{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700969 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700970
971 if (!mm)
972 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800973 /*
974 * Because we have no locks, mm->owner's may be being moved to other
975 * cgroup. We use css_tryget() here even if this looks
976 * pessimistic (rather than adding locks here).
977 */
978 rcu_read_lock();
979 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700980 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
981 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800982 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700983 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800984 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700985 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800986}
987
Johannes Weiner56600482012-01-12 17:17:59 -0800988/**
989 * mem_cgroup_iter - iterate over memory cgroup hierarchy
990 * @root: hierarchy root
991 * @prev: previously returned memcg, NULL on first invocation
992 * @reclaim: cookie for shared reclaim walks, NULL for full walks
993 *
994 * Returns references to children of the hierarchy below @root, or
995 * @root itself, or %NULL after a full round-trip.
996 *
997 * Caller must pass the return value in @prev on subsequent
998 * invocations for reference counting, or use mem_cgroup_iter_break()
999 * to cancel a hierarchy walk before the round-trip is complete.
1000 *
1001 * Reclaimers can specify a zone and a priority level in @reclaim to
1002 * divide up the memcgs in the hierarchy among all concurrent
1003 * reclaimers operating on the same zone and priority.
1004 */
1005struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1006 struct mem_cgroup *prev,
1007 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001008{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001009 struct mem_cgroup *memcg = NULL;
1010 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001011
Johannes Weiner56600482012-01-12 17:17:59 -08001012 if (mem_cgroup_disabled())
1013 return NULL;
1014
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001015 if (!root)
1016 root = root_mem_cgroup;
1017
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001018 if (prev && !reclaim)
1019 id = css_id(&prev->css);
1020
1021 if (prev && prev != root)
1022 css_put(&prev->css);
1023
1024 if (!root->use_hierarchy && root != root_mem_cgroup) {
1025 if (prev)
1026 return NULL;
1027 return root;
1028 }
1029
1030 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001031 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001032 struct cgroup_subsys_state *css;
1033
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001034 if (reclaim) {
1035 int nid = zone_to_nid(reclaim->zone);
1036 int zid = zone_idx(reclaim->zone);
1037 struct mem_cgroup_per_zone *mz;
1038
1039 mz = mem_cgroup_zoneinfo(root, nid, zid);
1040 iter = &mz->reclaim_iter[reclaim->priority];
1041 if (prev && reclaim->generation != iter->generation)
1042 return NULL;
1043 id = iter->position;
1044 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001045
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001046 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001047 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1048 if (css) {
1049 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001050 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001051 } else
1052 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001053 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001054
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001055 if (reclaim) {
1056 iter->position = id;
1057 if (!css)
1058 iter->generation++;
1059 else if (!prev && memcg)
1060 reclaim->generation = iter->generation;
1061 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001062
1063 if (prev && !css)
1064 return NULL;
1065 }
1066 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001067}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001068
Johannes Weiner56600482012-01-12 17:17:59 -08001069/**
1070 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1071 * @root: hierarchy root
1072 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1073 */
1074void mem_cgroup_iter_break(struct mem_cgroup *root,
1075 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001076{
1077 if (!root)
1078 root = root_mem_cgroup;
1079 if (prev && prev != root)
1080 css_put(&prev->css);
1081}
1082
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001083/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001084 * Iteration constructs for visiting all cgroups (under a tree). If
1085 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1086 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001087 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001088#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001089 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001090 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001091 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001092
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001093#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001094 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001095 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001096 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001097
David Rientjes68ae5642012-12-12 13:51:57 -08001098void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001099{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001100 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001101
Ying Han456f9982011-05-26 16:25:38 -07001102 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001103 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1104 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001105 goto out;
1106
1107 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001108 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001109 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1110 break;
1111 case PGMAJFAULT:
1112 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001113 break;
1114 default:
1115 BUG();
1116 }
1117out:
1118 rcu_read_unlock();
1119}
David Rientjes68ae5642012-12-12 13:51:57 -08001120EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001121
Johannes Weiner925b7672012-01-12 17:18:15 -08001122/**
1123 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1124 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001125 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001126 *
1127 * Returns the lru list vector holding pages for the given @zone and
1128 * @mem. This can be the global zone lruvec, if the memory controller
1129 * is disabled.
1130 */
1131struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1132 struct mem_cgroup *memcg)
1133{
1134 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001135 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001136
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001137 if (mem_cgroup_disabled()) {
1138 lruvec = &zone->lruvec;
1139 goto out;
1140 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001141
1142 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001143 lruvec = &mz->lruvec;
1144out:
1145 /*
1146 * Since a node can be onlined after the mem_cgroup was created,
1147 * we have to be prepared to initialize lruvec->zone here;
1148 * and if offlined then reonlined, we need to reinitialize it.
1149 */
1150 if (unlikely(lruvec->zone != zone))
1151 lruvec->zone = zone;
1152 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001153}
1154
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001155/*
1156 * Following LRU functions are allowed to be used without PCG_LOCK.
1157 * Operations are called by routine of global LRU independently from memcg.
1158 * What we have to take care of here is validness of pc->mem_cgroup.
1159 *
1160 * Changes to pc->mem_cgroup happens when
1161 * 1. charge
1162 * 2. moving account
1163 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1164 * It is added to LRU before charge.
1165 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1166 * When moving account, the page is not on LRU. It's isolated.
1167 */
1168
Johannes Weiner925b7672012-01-12 17:18:15 -08001169/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001170 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001171 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001172 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001173 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001174struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001175{
1176 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001177 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001178 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001179 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001180
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001181 if (mem_cgroup_disabled()) {
1182 lruvec = &zone->lruvec;
1183 goto out;
1184 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001185
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001186 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001187 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001188
1189 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001190 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001191 * an uncharged page off lru does nothing to secure
1192 * its former mem_cgroup from sudden removal.
1193 *
1194 * Our caller holds lru_lock, and PageCgroupUsed is updated
1195 * under page_cgroup lock: between them, they make all uses
1196 * of pc->mem_cgroup safe.
1197 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001198 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001199 pc->mem_cgroup = memcg = root_mem_cgroup;
1200
Johannes Weiner925b7672012-01-12 17:18:15 -08001201 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001202 lruvec = &mz->lruvec;
1203out:
1204 /*
1205 * Since a node can be onlined after the mem_cgroup was created,
1206 * we have to be prepared to initialize lruvec->zone here;
1207 * and if offlined then reonlined, we need to reinitialize it.
1208 */
1209 if (unlikely(lruvec->zone != zone))
1210 lruvec->zone = zone;
1211 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001212}
1213
1214/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001215 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1216 * @lruvec: mem_cgroup per zone lru vector
1217 * @lru: index of lru list the page is sitting on
1218 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001219 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001220 * This function must be called when a page is added to or removed from an
1221 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001222 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001223void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1224 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001225{
1226 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001227 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001228
1229 if (mem_cgroup_disabled())
1230 return;
1231
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001232 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1233 lru_size = mz->lru_size + lru;
1234 *lru_size += nr_pages;
1235 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001236}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001237
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001238/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001239 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001240 * hierarchy subtree
1241 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001242bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1243 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001244{
Johannes Weiner91c637342012-05-29 15:06:24 -07001245 if (root_memcg == memcg)
1246 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001247 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001248 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001249 return css_is_ancestor(&memcg->css, &root_memcg->css);
1250}
1251
1252static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1253 struct mem_cgroup *memcg)
1254{
1255 bool ret;
1256
Johannes Weiner91c637342012-05-29 15:06:24 -07001257 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001258 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001259 rcu_read_unlock();
1260 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001261}
1262
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001263int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001264{
1265 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001266 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001267 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001268
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001269 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001270 if (p) {
1271 curr = try_get_mem_cgroup_from_mm(p->mm);
1272 task_unlock(p);
1273 } else {
1274 /*
1275 * All threads may have already detached their mm's, but the oom
1276 * killer still needs to detect if they have already been oom
1277 * killed to prevent needlessly killing additional tasks.
1278 */
1279 task_lock(task);
1280 curr = mem_cgroup_from_task(task);
1281 if (curr)
1282 css_get(&curr->css);
1283 task_unlock(task);
1284 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001285 if (!curr)
1286 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001287 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001288 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001289 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001290 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1291 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001292 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001293 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001294 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001295 return ret;
1296}
1297
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001298int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001299{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001300 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001301 unsigned long inactive;
1302 unsigned long active;
1303 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001304
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001305 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1306 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001307
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001308 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1309 if (gb)
1310 inactive_ratio = int_sqrt(10 * gb);
1311 else
1312 inactive_ratio = 1;
1313
Johannes Weiner9b272972011-11-02 13:38:23 -07001314 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001315}
1316
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001317int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001318{
1319 unsigned long active;
1320 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001321
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001322 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1323 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001324
1325 return (active > inactive);
1326}
1327
Balbir Singh6d61ef42009-01-07 18:08:06 -08001328#define mem_cgroup_from_res_counter(counter, member) \
1329 container_of(counter, struct mem_cgroup, member)
1330
Johannes Weiner19942822011-02-01 15:52:43 -08001331/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001332 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001333 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001334 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001335 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001336 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001337 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001338static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001339{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001340 unsigned long long margin;
1341
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001342 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001343 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001344 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001345 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001346}
1347
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001348int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001349{
1350 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001351
1352 /* root ? */
1353 if (cgrp->parent == NULL)
1354 return vm_swappiness;
1355
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001356 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001357}
1358
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001359/*
1360 * memcg->moving_account is used for checking possibility that some thread is
1361 * calling move_account(). When a thread on CPU-A starts moving pages under
1362 * a memcg, other threads should check memcg->moving_account under
1363 * rcu_read_lock(), like this:
1364 *
1365 * CPU-A CPU-B
1366 * rcu_read_lock()
1367 * memcg->moving_account+1 if (memcg->mocing_account)
1368 * take heavy locks.
1369 * synchronize_rcu() update something.
1370 * rcu_read_unlock()
1371 * start move here.
1372 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001373
1374/* for quick checking without looking up memcg */
1375atomic_t memcg_moving __read_mostly;
1376
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001377static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001378{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001379 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001380 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001381 synchronize_rcu();
1382}
1383
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001384static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001385{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001386 /*
1387 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1388 * We check NULL in callee rather than caller.
1389 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001390 if (memcg) {
1391 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001392 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001393 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001394}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001395
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001396/*
1397 * 2 routines for checking "mem" is under move_account() or not.
1398 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001399 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1400 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001401 * pc->mem_cgroup may be overwritten.
1402 *
1403 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1404 * under hierarchy of moving cgroups. This is for
1405 * waiting at hith-memory prressure caused by "move".
1406 */
1407
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001408static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001409{
1410 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001411 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001412}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001414static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001415{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001416 struct mem_cgroup *from;
1417 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001418 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001419 /*
1420 * Unlike task_move routines, we access mc.to, mc.from not under
1421 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1422 */
1423 spin_lock(&mc.lock);
1424 from = mc.from;
1425 to = mc.to;
1426 if (!from)
1427 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001429 ret = mem_cgroup_same_or_subtree(memcg, from)
1430 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001431unlock:
1432 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001433 return ret;
1434}
1435
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001436static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001437{
1438 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001439 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001440 DEFINE_WAIT(wait);
1441 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1442 /* moving charge context might have finished. */
1443 if (mc.moving_task)
1444 schedule();
1445 finish_wait(&mc.waitq, &wait);
1446 return true;
1447 }
1448 }
1449 return false;
1450}
1451
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001452/*
1453 * Take this lock when
1454 * - a code tries to modify page's memcg while it's USED.
1455 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001456 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001457 */
1458static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1459 unsigned long *flags)
1460{
1461 spin_lock_irqsave(&memcg->move_lock, *flags);
1462}
1463
1464static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1465 unsigned long *flags)
1466{
1467 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1468}
1469
Balbir Singhe2224322009-04-02 16:57:39 -07001470/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001471 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001472 * @memcg: The memory cgroup that went over limit
1473 * @p: Task that is going to be killed
1474 *
1475 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1476 * enabled
1477 */
1478void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1479{
1480 struct cgroup *task_cgrp;
1481 struct cgroup *mem_cgrp;
1482 /*
1483 * Need a buffer in BSS, can't rely on allocations. The code relies
1484 * on the assumption that OOM is serialized for memory controller.
1485 * If this assumption is broken, revisit this code.
1486 */
1487 static char memcg_name[PATH_MAX];
1488 int ret;
1489
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001490 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001491 return;
1492
Balbir Singhe2224322009-04-02 16:57:39 -07001493 rcu_read_lock();
1494
1495 mem_cgrp = memcg->css.cgroup;
1496 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1497
1498 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1499 if (ret < 0) {
1500 /*
1501 * Unfortunately, we are unable to convert to a useful name
1502 * But we'll still print out the usage information
1503 */
1504 rcu_read_unlock();
1505 goto done;
1506 }
1507 rcu_read_unlock();
1508
1509 printk(KERN_INFO "Task in %s killed", memcg_name);
1510
1511 rcu_read_lock();
1512 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1513 if (ret < 0) {
1514 rcu_read_unlock();
1515 goto done;
1516 }
1517 rcu_read_unlock();
1518
1519 /*
1520 * Continues from above, so we don't need an KERN_ level
1521 */
1522 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1523done:
1524
1525 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1526 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1527 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1528 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1529 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1530 "failcnt %llu\n",
1531 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1532 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1533 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Glauber Costa510fc4e2012-12-18 14:21:47 -08001534 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1535 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1536 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1537 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Balbir Singhe2224322009-04-02 16:57:39 -07001538}
1539
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001540/*
1541 * This function returns the number of memcg under hierarchy tree. Returns
1542 * 1(self count) if no children.
1543 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001544static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001545{
1546 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001547 struct mem_cgroup *iter;
1548
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001549 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001550 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001551 return num;
1552}
1553
Balbir Singh6d61ef42009-01-07 18:08:06 -08001554/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001555 * Return the memory (and swap, if configured) limit for a memcg.
1556 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001557static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001558{
1559 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001560
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001561 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001562
David Rientjesa63d83f2010-08-09 17:19:46 -07001563 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001564 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001565 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001566 if (mem_cgroup_swappiness(memcg)) {
1567 u64 memsw;
1568
1569 limit += total_swap_pages << PAGE_SHIFT;
1570 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1571
1572 /*
1573 * If memsw is finite and limits the amount of swap space
1574 * available to this memcg, return that limit.
1575 */
1576 limit = min(limit, memsw);
1577 }
1578
1579 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001580}
1581
David Rientjes19965462012-12-11 16:00:26 -08001582static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1583 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001584{
1585 struct mem_cgroup *iter;
1586 unsigned long chosen_points = 0;
1587 unsigned long totalpages;
1588 unsigned int points = 0;
1589 struct task_struct *chosen = NULL;
1590
David Rientjes876aafb2012-07-31 16:43:48 -07001591 /*
1592 * If current has a pending SIGKILL, then automatically select it. The
1593 * goal is to allow it to allocate so that it may quickly exit and free
1594 * its memory.
1595 */
1596 if (fatal_signal_pending(current)) {
1597 set_thread_flag(TIF_MEMDIE);
1598 return;
1599 }
1600
1601 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001602 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1603 for_each_mem_cgroup_tree(iter, memcg) {
1604 struct cgroup *cgroup = iter->css.cgroup;
1605 struct cgroup_iter it;
1606 struct task_struct *task;
1607
1608 cgroup_iter_start(cgroup, &it);
1609 while ((task = cgroup_iter_next(cgroup, &it))) {
1610 switch (oom_scan_process_thread(task, totalpages, NULL,
1611 false)) {
1612 case OOM_SCAN_SELECT:
1613 if (chosen)
1614 put_task_struct(chosen);
1615 chosen = task;
1616 chosen_points = ULONG_MAX;
1617 get_task_struct(chosen);
1618 /* fall through */
1619 case OOM_SCAN_CONTINUE:
1620 continue;
1621 case OOM_SCAN_ABORT:
1622 cgroup_iter_end(cgroup, &it);
1623 mem_cgroup_iter_break(memcg, iter);
1624 if (chosen)
1625 put_task_struct(chosen);
1626 return;
1627 case OOM_SCAN_OK:
1628 break;
1629 };
1630 points = oom_badness(task, memcg, NULL, totalpages);
1631 if (points > chosen_points) {
1632 if (chosen)
1633 put_task_struct(chosen);
1634 chosen = task;
1635 chosen_points = points;
1636 get_task_struct(chosen);
1637 }
1638 }
1639 cgroup_iter_end(cgroup, &it);
1640 }
1641
1642 if (!chosen)
1643 return;
1644 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001645 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1646 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001647}
1648
Johannes Weiner56600482012-01-12 17:17:59 -08001649static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1650 gfp_t gfp_mask,
1651 unsigned long flags)
1652{
1653 unsigned long total = 0;
1654 bool noswap = false;
1655 int loop;
1656
1657 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1658 noswap = true;
1659 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1660 noswap = true;
1661
1662 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1663 if (loop)
1664 drain_all_stock_async(memcg);
1665 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1666 /*
1667 * Allow limit shrinkers, which are triggered directly
1668 * by userspace, to catch signals and stop reclaim
1669 * after minimal progress, regardless of the margin.
1670 */
1671 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1672 break;
1673 if (mem_cgroup_margin(memcg))
1674 break;
1675 /*
1676 * If nothing was reclaimed after two attempts, there
1677 * may be no reclaimable pages in this hierarchy.
1678 */
1679 if (loop && !total)
1680 break;
1681 }
1682 return total;
1683}
1684
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001685/**
1686 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001687 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001688 * @nid: the node ID to be checked.
1689 * @noswap : specify true here if the user wants flle only information.
1690 *
1691 * This function returns whether the specified memcg contains any
1692 * reclaimable pages on a node. Returns true if there are any reclaimable
1693 * pages in the node.
1694 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001695static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001696 int nid, bool noswap)
1697{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001698 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001699 return true;
1700 if (noswap || !total_swap_pages)
1701 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001702 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001703 return true;
1704 return false;
1705
1706}
Ying Han889976d2011-05-26 16:25:33 -07001707#if MAX_NUMNODES > 1
1708
1709/*
1710 * Always updating the nodemask is not very good - even if we have an empty
1711 * list or the wrong list here, we can start from some node and traverse all
1712 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1713 *
1714 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001715static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001716{
1717 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001718 /*
1719 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1720 * pagein/pageout changes since the last update.
1721 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001722 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001723 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001724 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001725 return;
1726
Ying Han889976d2011-05-26 16:25:33 -07001727 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001728 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001729
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001730 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001731
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001732 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1733 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001734 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001735
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001736 atomic_set(&memcg->numainfo_events, 0);
1737 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001738}
1739
1740/*
1741 * Selecting a node where we start reclaim from. Because what we need is just
1742 * reducing usage counter, start from anywhere is O,K. Considering
1743 * memory reclaim from current node, there are pros. and cons.
1744 *
1745 * Freeing memory from current node means freeing memory from a node which
1746 * we'll use or we've used. So, it may make LRU bad. And if several threads
1747 * hit limits, it will see a contention on a node. But freeing from remote
1748 * node means more costs for memory reclaim because of memory latency.
1749 *
1750 * Now, we use round-robin. Better algorithm is welcomed.
1751 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001752int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001753{
1754 int node;
1755
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001756 mem_cgroup_may_update_nodemask(memcg);
1757 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001758
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001759 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001760 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001761 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001762 /*
1763 * We call this when we hit limit, not when pages are added to LRU.
1764 * No LRU may hold pages because all pages are UNEVICTABLE or
1765 * memcg is too small and all pages are not on LRU. In that case,
1766 * we use curret node.
1767 */
1768 if (unlikely(node == MAX_NUMNODES))
1769 node = numa_node_id();
1770
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001771 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001772 return node;
1773}
1774
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001775/*
1776 * Check all nodes whether it contains reclaimable pages or not.
1777 * For quick scan, we make use of scan_nodes. This will allow us to skip
1778 * unused nodes. But scan_nodes is lazily updated and may not cotain
1779 * enough new information. We need to do double check.
1780 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001781static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001782{
1783 int nid;
1784
1785 /*
1786 * quick check...making use of scan_node.
1787 * We can skip unused nodes.
1788 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001789 if (!nodes_empty(memcg->scan_nodes)) {
1790 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001791 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001792 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001793
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001794 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001795 return true;
1796 }
1797 }
1798 /*
1799 * Check rest of nodes.
1800 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001801 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001802 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001803 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001804 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001805 return true;
1806 }
1807 return false;
1808}
1809
Ying Han889976d2011-05-26 16:25:33 -07001810#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001811int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001812{
1813 return 0;
1814}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001815
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001816static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001817{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001818 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001819}
Ying Han889976d2011-05-26 16:25:33 -07001820#endif
1821
Johannes Weiner56600482012-01-12 17:17:59 -08001822static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1823 struct zone *zone,
1824 gfp_t gfp_mask,
1825 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001826{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001827 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001828 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001829 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001830 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001831 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001832 struct mem_cgroup_reclaim_cookie reclaim = {
1833 .zone = zone,
1834 .priority = 0,
1835 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001836
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001837 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001838
Balbir Singh4e416952009-09-23 15:56:39 -07001839 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001840 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001841 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001842 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001843 if (loop >= 2) {
1844 /*
1845 * If we have not been able to reclaim
1846 * anything, it might because there are
1847 * no reclaimable pages under this hierarchy
1848 */
Johannes Weiner56600482012-01-12 17:17:59 -08001849 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001850 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001851 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001852 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001853 * excess >> 2 is not to excessive so as to
1854 * reclaim too much, nor too less that we keep
1855 * coming back to reclaim from this cgroup
1856 */
1857 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001858 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001859 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001860 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001861 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001862 }
Johannes Weiner56600482012-01-12 17:17:59 -08001863 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001864 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001865 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1866 zone, &nr_scanned);
1867 *total_scanned += nr_scanned;
1868 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001869 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001870 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001871 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001872 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001873}
1874
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001875/*
1876 * Check OOM-Killer is already running under our hierarchy.
1877 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001878 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001879 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001880static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001881{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001882 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001883
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001884 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001885 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001886 /*
1887 * this subtree of our hierarchy is already locked
1888 * so we cannot give a lock.
1889 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001890 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001891 mem_cgroup_iter_break(memcg, iter);
1892 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001893 } else
1894 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001895 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001896
Michal Hocko79dfdac2011-07-26 16:08:23 -07001897 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001898 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001899
1900 /*
1901 * OK, we failed to lock the whole subtree so we have to clean up
1902 * what we set up to the failing subtree
1903 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001904 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001905 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001906 mem_cgroup_iter_break(memcg, iter);
1907 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001908 }
1909 iter->oom_lock = false;
1910 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001911 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001912}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001913
Michal Hocko79dfdac2011-07-26 16:08:23 -07001914/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001915 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001916 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001917static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001918{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001919 struct mem_cgroup *iter;
1920
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001921 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001922 iter->oom_lock = false;
1923 return 0;
1924}
1925
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001926static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001927{
1928 struct mem_cgroup *iter;
1929
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001930 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001931 atomic_inc(&iter->under_oom);
1932}
1933
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001935{
1936 struct mem_cgroup *iter;
1937
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001938 /*
1939 * When a new child is created while the hierarchy is under oom,
1940 * mem_cgroup_oom_lock() may not be called. We have to use
1941 * atomic_add_unless() here.
1942 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001943 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001944 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001945}
1946
Michal Hocko1af8efe2011-07-26 16:08:24 -07001947static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001948static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1949
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001950struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001951 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001952 wait_queue_t wait;
1953};
1954
1955static int memcg_oom_wake_function(wait_queue_t *wait,
1956 unsigned mode, int sync, void *arg)
1957{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001958 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1959 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001960 struct oom_wait_info *oom_wait_info;
1961
1962 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001963 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001964
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001965 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001966 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001967 * Then we can use css_is_ancestor without taking care of RCU.
1968 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001969 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1970 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001971 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001972 return autoremove_wake_function(wait, mode, sync, arg);
1973}
1974
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001975static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001976{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977 /* for filtering, pass "memcg" as argument. */
1978 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001979}
1980
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001982{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001983 if (memcg && atomic_read(&memcg->under_oom))
1984 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001985}
1986
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987/*
1988 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1989 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001990static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1991 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001992{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001993 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001994 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001995
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001996 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001997 owait.wait.flags = 0;
1998 owait.wait.func = memcg_oom_wake_function;
1999 owait.wait.private = current;
2000 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002001 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002002 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002003
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002004 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002005 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002006 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002007 /*
2008 * Even if signal_pending(), we can't quit charge() loop without
2009 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2010 * under OOM is always welcomed, use TASK_KILLABLE here.
2011 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002012 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002013 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002014 need_to_kill = false;
2015 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002016 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002017 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002018
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002019 if (need_to_kill) {
2020 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002021 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002022 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002023 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002024 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002025 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002026 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002027 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002028 mem_cgroup_oom_unlock(memcg);
2029 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002030 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002031
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002032 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002033
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002034 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2035 return false;
2036 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002037 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002038 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002039}
2040
Balbir Singhd69b0422009-06-17 16:26:34 -07002041/*
2042 * Currently used to update mapped file statistics, but the routine can be
2043 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002044 *
2045 * Notes: Race condition
2046 *
2047 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2048 * it tends to be costly. But considering some conditions, we doesn't need
2049 * to do so _always_.
2050 *
2051 * Considering "charge", lock_page_cgroup() is not required because all
2052 * file-stat operations happen after a page is attached to radix-tree. There
2053 * are no race with "charge".
2054 *
2055 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2056 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2057 * if there are race with "uncharge". Statistics itself is properly handled
2058 * by flags.
2059 *
2060 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002061 * small, we check mm->moving_account and detect there are possibility of race
2062 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002063 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002064
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002065void __mem_cgroup_begin_update_page_stat(struct page *page,
2066 bool *locked, unsigned long *flags)
2067{
2068 struct mem_cgroup *memcg;
2069 struct page_cgroup *pc;
2070
2071 pc = lookup_page_cgroup(page);
2072again:
2073 memcg = pc->mem_cgroup;
2074 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2075 return;
2076 /*
2077 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002078 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002079 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002080 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002081 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002082 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002083 return;
2084
2085 move_lock_mem_cgroup(memcg, flags);
2086 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2087 move_unlock_mem_cgroup(memcg, flags);
2088 goto again;
2089 }
2090 *locked = true;
2091}
2092
2093void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2094{
2095 struct page_cgroup *pc = lookup_page_cgroup(page);
2096
2097 /*
2098 * It's guaranteed that pc->mem_cgroup never changes while
2099 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002100 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002101 */
2102 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2103}
2104
Greg Thelen2a7106f2011-01-13 15:47:37 -08002105void mem_cgroup_update_page_stat(struct page *page,
2106 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002107{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002108 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002109 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002110 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002111
Johannes Weinercfa44942012-01-12 17:18:38 -08002112 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002113 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002114
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002115 memcg = pc->mem_cgroup;
2116 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002117 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002118
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002119 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002120 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002121 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002122 break;
2123 default:
2124 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002125 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002126
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002127 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002128}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002129
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002130/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002131 * size of first charge trial. "32" comes from vmscan.c's magic value.
2132 * TODO: maybe necessary to use big numbers in big irons.
2133 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002134#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002135struct memcg_stock_pcp {
2136 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002137 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002138 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002139 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002140#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002141};
2142static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002143static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002144
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002145/**
2146 * consume_stock: Try to consume stocked charge on this cpu.
2147 * @memcg: memcg to consume from.
2148 * @nr_pages: how many pages to charge.
2149 *
2150 * The charges will only happen if @memcg matches the current cpu's memcg
2151 * stock, and at least @nr_pages are available in that stock. Failure to
2152 * service an allocation will refill the stock.
2153 *
2154 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002155 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002156static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002157{
2158 struct memcg_stock_pcp *stock;
2159 bool ret = true;
2160
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002161 if (nr_pages > CHARGE_BATCH)
2162 return false;
2163
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002165 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2166 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002167 else /* need to call res_counter_charge */
2168 ret = false;
2169 put_cpu_var(memcg_stock);
2170 return ret;
2171}
2172
2173/*
2174 * Returns stocks cached in percpu to res_counter and reset cached information.
2175 */
2176static void drain_stock(struct memcg_stock_pcp *stock)
2177{
2178 struct mem_cgroup *old = stock->cached;
2179
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002180 if (stock->nr_pages) {
2181 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2182
2183 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002184 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002185 res_counter_uncharge(&old->memsw, bytes);
2186 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002187 }
2188 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002189}
2190
2191/*
2192 * This must be called under preempt disabled or must be called by
2193 * a thread which is pinned to local cpu.
2194 */
2195static void drain_local_stock(struct work_struct *dummy)
2196{
2197 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2198 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002199 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200}
2201
2202/*
2203 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002204 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002205 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002206static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002207{
2208 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2209
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002210 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002211 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002212 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002213 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002214 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002215 put_cpu_var(memcg_stock);
2216}
2217
2218/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002219 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002220 * of the hierarchy under it. sync flag says whether we should block
2221 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002222 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002223static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002225 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002226
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002227 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002228 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002229 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002230 for_each_online_cpu(cpu) {
2231 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002232 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002233
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002234 memcg = stock->cached;
2235 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002236 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002237 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002238 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002239 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2240 if (cpu == curcpu)
2241 drain_local_stock(&stock->work);
2242 else
2243 schedule_work_on(cpu, &stock->work);
2244 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002245 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002246 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002247
2248 if (!sync)
2249 goto out;
2250
2251 for_each_online_cpu(cpu) {
2252 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002253 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002254 flush_work(&stock->work);
2255 }
2256out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002257 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002258}
2259
2260/*
2261 * Tries to drain stocked charges in other cpus. This function is asynchronous
2262 * and just put a work per cpu for draining localy on each cpu. Caller can
2263 * expects some charges will be back to res_counter later but cannot wait for
2264 * it.
2265 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002266static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002267{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002268 /*
2269 * If someone calls draining, avoid adding more kworker runs.
2270 */
2271 if (!mutex_trylock(&percpu_charge_mutex))
2272 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002273 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002274 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002275}
2276
2277/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002278static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279{
2280 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002281 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002282 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002283 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002284}
2285
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002286/*
2287 * This function drains percpu counter value from DEAD cpu and
2288 * move it to local cpu. Note that this function can be preempted.
2289 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002290static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002291{
2292 int i;
2293
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002294 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002295 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002296 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002297
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002298 per_cpu(memcg->stat->count[i], cpu) = 0;
2299 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002300 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002301 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002302 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002303
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002304 per_cpu(memcg->stat->events[i], cpu) = 0;
2305 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002306 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002307 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002308}
2309
2310static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002311 unsigned long action,
2312 void *hcpu)
2313{
2314 int cpu = (unsigned long)hcpu;
2315 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002316 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002317
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002318 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002319 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002320
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002321 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002322 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002323
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002324 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002325 mem_cgroup_drain_pcp_counter(iter, cpu);
2326
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002327 stock = &per_cpu(memcg_stock, cpu);
2328 drain_stock(stock);
2329 return NOTIFY_OK;
2330}
2331
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002332
2333/* See __mem_cgroup_try_charge() for details */
2334enum {
2335 CHARGE_OK, /* success */
2336 CHARGE_RETRY, /* need to retry but retry is not bad */
2337 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2338 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2339 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2340};
2341
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002342static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002343 unsigned int nr_pages, unsigned int min_pages,
2344 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002345{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002346 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002347 struct mem_cgroup *mem_over_limit;
2348 struct res_counter *fail_res;
2349 unsigned long flags = 0;
2350 int ret;
2351
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002352 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353
2354 if (likely(!ret)) {
2355 if (!do_swap_account)
2356 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002357 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002358 if (likely(!ret))
2359 return CHARGE_OK;
2360
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002361 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002362 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2363 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2364 } else
2365 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002366 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002367 * Never reclaim on behalf of optional batching, retry with a
2368 * single page instead.
2369 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002370 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002371 return CHARGE_RETRY;
2372
2373 if (!(gfp_mask & __GFP_WAIT))
2374 return CHARGE_WOULDBLOCK;
2375
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002376 if (gfp_mask & __GFP_NORETRY)
2377 return CHARGE_NOMEM;
2378
Johannes Weiner56600482012-01-12 17:17:59 -08002379 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002380 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002381 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002382 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002383 * Even though the limit is exceeded at this point, reclaim
2384 * may have been able to free some pages. Retry the charge
2385 * before killing the task.
2386 *
2387 * Only for regular pages, though: huge pages are rather
2388 * unlikely to succeed so close to the limit, and we fall back
2389 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002390 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002391 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002392 return CHARGE_RETRY;
2393
2394 /*
2395 * At task move, charge accounts can be doubly counted. So, it's
2396 * better to wait until the end of task_move if something is going on.
2397 */
2398 if (mem_cgroup_wait_acct_move(mem_over_limit))
2399 return CHARGE_RETRY;
2400
2401 /* If we don't need to call oom-killer at el, return immediately */
2402 if (!oom_check)
2403 return CHARGE_NOMEM;
2404 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002405 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002406 return CHARGE_OOM_DIE;
2407
2408 return CHARGE_RETRY;
2409}
2410
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002411/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002412 * __mem_cgroup_try_charge() does
2413 * 1. detect memcg to be charged against from passed *mm and *ptr,
2414 * 2. update res_counter
2415 * 3. call memory reclaim if necessary.
2416 *
2417 * In some special case, if the task is fatal, fatal_signal_pending() or
2418 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2419 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2420 * as possible without any hazards. 2: all pages should have a valid
2421 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2422 * pointer, that is treated as a charge to root_mem_cgroup.
2423 *
2424 * So __mem_cgroup_try_charge() will return
2425 * 0 ... on success, filling *ptr with a valid memcg pointer.
2426 * -ENOMEM ... charge failure because of resource limits.
2427 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2428 *
2429 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2430 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002431 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002432static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002433 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002434 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002435 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002436 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002437{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002438 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002439 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002440 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002441 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002442
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002443 /*
2444 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2445 * in system level. So, allow to go ahead dying process in addition to
2446 * MEMDIE process.
2447 */
2448 if (unlikely(test_thread_flag(TIF_MEMDIE)
2449 || fatal_signal_pending(current)))
2450 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002451
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002452 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002453 * We always charge the cgroup the mm_struct belongs to.
2454 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002455 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002456 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002457 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002459 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002460again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002461 if (*ptr) { /* css should be a valid one */
2462 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002463 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002464 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002465 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002466 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002467 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002468 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002469 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002470
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002471 rcu_read_lock();
2472 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002473 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002474 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002475 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002476 * race with swapoff. Then, we have small risk of mis-accouning.
2477 * But such kind of mis-account by race always happens because
2478 * we don't have cgroup_mutex(). It's overkill and we allo that
2479 * small race, here.
2480 * (*) swapoff at el will charge against mm-struct not against
2481 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002482 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002483 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002484 if (!memcg)
2485 memcg = root_mem_cgroup;
2486 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002487 rcu_read_unlock();
2488 goto done;
2489 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002490 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002491 /*
2492 * It seems dagerous to access memcg without css_get().
2493 * But considering how consume_stok works, it's not
2494 * necessary. If consume_stock success, some charges
2495 * from this memcg are cached on this cpu. So, we
2496 * don't need to call css_get()/css_tryget() before
2497 * calling consume_stock().
2498 */
2499 rcu_read_unlock();
2500 goto done;
2501 }
2502 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002503 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002504 rcu_read_unlock();
2505 goto again;
2506 }
2507 rcu_read_unlock();
2508 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002509
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002510 do {
2511 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002512
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002513 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002514 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002515 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002516 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002517 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002518
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002519 oom_check = false;
2520 if (oom && !nr_oom_retries) {
2521 oom_check = true;
2522 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2523 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002524
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002525 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2526 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002527 switch (ret) {
2528 case CHARGE_OK:
2529 break;
2530 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002531 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002532 css_put(&memcg->css);
2533 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002534 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002535 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002536 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002537 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002538 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002539 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002540 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002541 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002542 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002543 /* If oom, we never return -ENOMEM */
2544 nr_oom_retries--;
2545 break;
2546 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002547 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002548 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002549 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002550 } while (ret != CHARGE_OK);
2551
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002552 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002553 refill_stock(memcg, batch - nr_pages);
2554 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002555done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002556 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002557 return 0;
2558nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002559 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002560 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002561bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002562 *ptr = root_mem_cgroup;
2563 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002564}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002565
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002566/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002567 * Somemtimes we have to undo a charge we got by try_charge().
2568 * This function is for that and do uncharge, put css's refcnt.
2569 * gotten by try_charge().
2570 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002572 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002573{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002574 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002575 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002576
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002577 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002578 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002579 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002580 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002581}
2582
2583/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002584 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2585 * This is useful when moving usage to parent cgroup.
2586 */
2587static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2588 unsigned int nr_pages)
2589{
2590 unsigned long bytes = nr_pages * PAGE_SIZE;
2591
2592 if (mem_cgroup_is_root(memcg))
2593 return;
2594
2595 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2596 if (do_swap_account)
2597 res_counter_uncharge_until(&memcg->memsw,
2598 memcg->memsw.parent, bytes);
2599}
2600
2601/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002602 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002603 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2604 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2605 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002606 */
2607static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2608{
2609 struct cgroup_subsys_state *css;
2610
2611 /* ID 0 is unused ID */
2612 if (!id)
2613 return NULL;
2614 css = css_lookup(&mem_cgroup_subsys, id);
2615 if (!css)
2616 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002617 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002618}
2619
Wu Fengguange42d9d52009-12-16 12:19:59 +01002620struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002621{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002622 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002623 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002624 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002625 swp_entry_t ent;
2626
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002627 VM_BUG_ON(!PageLocked(page));
2628
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002629 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002630 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002631 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002632 memcg = pc->mem_cgroup;
2633 if (memcg && !css_tryget(&memcg->css))
2634 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002635 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002636 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002637 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002638 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002639 memcg = mem_cgroup_lookup(id);
2640 if (memcg && !css_tryget(&memcg->css))
2641 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002642 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002643 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002644 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002645 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002646}
2647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002648static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002649 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002650 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002651 enum charge_type ctype,
2652 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002653{
Johannes Weinerce587e62012-04-24 20:22:33 +02002654 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002655 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002656 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002657 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002658 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002659
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002660 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002661 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002662 /*
2663 * we don't need page_cgroup_lock about tail pages, becase they are not
2664 * accessed by any other context at this point.
2665 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002666
2667 /*
2668 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2669 * may already be on some other mem_cgroup's LRU. Take care of it.
2670 */
2671 if (lrucare) {
2672 zone = page_zone(page);
2673 spin_lock_irq(&zone->lru_lock);
2674 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002675 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002676 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002677 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002678 was_on_lru = true;
2679 }
2680 }
2681
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002682 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002683 /*
2684 * We access a page_cgroup asynchronously without lock_page_cgroup().
2685 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2686 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2687 * before USED bit, we need memory barrier here.
2688 * See mem_cgroup_add_lru_list(), etc.
2689 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002690 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002691 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002692
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002693 if (lrucare) {
2694 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002695 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002696 VM_BUG_ON(PageLRU(page));
2697 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002698 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002699 }
2700 spin_unlock_irq(&zone->lru_lock);
2701 }
2702
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002703 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002704 anon = true;
2705 else
2706 anon = false;
2707
2708 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002709 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002710
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002711 /*
2712 * "charge_statistics" updated event counter. Then, check it.
2713 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2714 * if they exceeds softlimit.
2715 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002716 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002717}
2718
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002719#ifdef CONFIG_MEMCG_KMEM
2720static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2721{
2722 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2723 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2724}
2725
2726static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2727{
2728 struct res_counter *fail_res;
2729 struct mem_cgroup *_memcg;
2730 int ret = 0;
2731 bool may_oom;
2732
2733 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2734 if (ret)
2735 return ret;
2736
2737 /*
2738 * Conditions under which we can wait for the oom_killer. Those are
2739 * the same conditions tested by the core page allocator
2740 */
2741 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2742
2743 _memcg = memcg;
2744 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2745 &_memcg, may_oom);
2746
2747 if (ret == -EINTR) {
2748 /*
2749 * __mem_cgroup_try_charge() chosed to bypass to root due to
2750 * OOM kill or fatal signal. Since our only options are to
2751 * either fail the allocation or charge it to this cgroup, do
2752 * it as a temporary condition. But we can't fail. From a
2753 * kmem/slab perspective, the cache has already been selected,
2754 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2755 * our minds.
2756 *
2757 * This condition will only trigger if the task entered
2758 * memcg_charge_kmem in a sane state, but was OOM-killed during
2759 * __mem_cgroup_try_charge() above. Tasks that were already
2760 * dying when the allocation triggers should have been already
2761 * directed to the root cgroup in memcontrol.h
2762 */
2763 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2764 if (do_swap_account)
2765 res_counter_charge_nofail(&memcg->memsw, size,
2766 &fail_res);
2767 ret = 0;
2768 } else if (ret)
2769 res_counter_uncharge(&memcg->kmem, size);
2770
2771 return ret;
2772}
2773
2774static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2775{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002776 res_counter_uncharge(&memcg->res, size);
2777 if (do_swap_account)
2778 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002779
2780 /* Not down to 0 */
2781 if (res_counter_uncharge(&memcg->kmem, size))
2782 return;
2783
2784 if (memcg_kmem_test_and_clear_dead(memcg))
2785 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002786}
2787
2788/*
2789 * We need to verify if the allocation against current->mm->owner's memcg is
2790 * possible for the given order. But the page is not allocated yet, so we'll
2791 * need a further commit step to do the final arrangements.
2792 *
2793 * It is possible for the task to switch cgroups in this mean time, so at
2794 * commit time, we can't rely on task conversion any longer. We'll then use
2795 * the handle argument to return to the caller which cgroup we should commit
2796 * against. We could also return the memcg directly and avoid the pointer
2797 * passing, but a boolean return value gives better semantics considering
2798 * the compiled-out case as well.
2799 *
2800 * Returning true means the allocation is possible.
2801 */
2802bool
2803__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2804{
2805 struct mem_cgroup *memcg;
2806 int ret;
2807
2808 *_memcg = NULL;
2809 memcg = try_get_mem_cgroup_from_mm(current->mm);
2810
2811 /*
2812 * very rare case described in mem_cgroup_from_task. Unfortunately there
2813 * isn't much we can do without complicating this too much, and it would
2814 * be gfp-dependent anyway. Just let it go
2815 */
2816 if (unlikely(!memcg))
2817 return true;
2818
2819 if (!memcg_can_account_kmem(memcg)) {
2820 css_put(&memcg->css);
2821 return true;
2822 }
2823
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002824 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
2825 if (!ret)
2826 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002827
2828 css_put(&memcg->css);
2829 return (ret == 0);
2830}
2831
2832void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2833 int order)
2834{
2835 struct page_cgroup *pc;
2836
2837 VM_BUG_ON(mem_cgroup_is_root(memcg));
2838
2839 /* The page allocation failed. Revert */
2840 if (!page) {
2841 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002842 return;
2843 }
2844
2845 pc = lookup_page_cgroup(page);
2846 lock_page_cgroup(pc);
2847 pc->mem_cgroup = memcg;
2848 SetPageCgroupUsed(pc);
2849 unlock_page_cgroup(pc);
2850}
2851
2852void __memcg_kmem_uncharge_pages(struct page *page, int order)
2853{
2854 struct mem_cgroup *memcg = NULL;
2855 struct page_cgroup *pc;
2856
2857
2858 pc = lookup_page_cgroup(page);
2859 /*
2860 * Fast unlocked return. Theoretically might have changed, have to
2861 * check again after locking.
2862 */
2863 if (!PageCgroupUsed(pc))
2864 return;
2865
2866 lock_page_cgroup(pc);
2867 if (PageCgroupUsed(pc)) {
2868 memcg = pc->mem_cgroup;
2869 ClearPageCgroupUsed(pc);
2870 }
2871 unlock_page_cgroup(pc);
2872
2873 /*
2874 * We trust that only if there is a memcg associated with the page, it
2875 * is a valid allocation
2876 */
2877 if (!memcg)
2878 return;
2879
2880 VM_BUG_ON(mem_cgroup_is_root(memcg));
2881 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002882}
2883#endif /* CONFIG_MEMCG_KMEM */
2884
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002885#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2886
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002887#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002888/*
2889 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002890 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2891 * charge/uncharge will be never happen and move_account() is done under
2892 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002893 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002894void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002895{
2896 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002897 struct page_cgroup *pc;
2898 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002899
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002900 if (mem_cgroup_disabled())
2901 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002902 for (i = 1; i < HPAGE_PMD_NR; i++) {
2903 pc = head_pc + i;
2904 pc->mem_cgroup = head_pc->mem_cgroup;
2905 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002906 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2907 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002908}
Hugh Dickins12d27102012-01-12 17:19:52 -08002909#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002910
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002911/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002912 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002913 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002914 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002915 * @pc: page_cgroup of the page.
2916 * @from: mem_cgroup which the page is moved from.
2917 * @to: mem_cgroup which the page is moved to. @from != @to.
2918 *
2919 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002920 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002921 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002922 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002923 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2924 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002925 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002926static int mem_cgroup_move_account(struct page *page,
2927 unsigned int nr_pages,
2928 struct page_cgroup *pc,
2929 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002930 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002931{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002932 unsigned long flags;
2933 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002934 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002935
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002936 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002937 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002938 /*
2939 * The page is isolated from LRU. So, collapse function
2940 * will not handle this page. But page splitting can happen.
2941 * Do this check under compound_page_lock(). The caller should
2942 * hold it.
2943 */
2944 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002945 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002946 goto out;
2947
2948 lock_page_cgroup(pc);
2949
2950 ret = -EINVAL;
2951 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2952 goto unlock;
2953
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002954 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002955
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002956 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002957 /* Update mapped_file data for mem_cgroup */
2958 preempt_disable();
2959 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2960 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2961 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002962 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002963 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002964
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002965 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002966 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002967 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002968 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002969 ret = 0;
2970unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002971 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002972 /*
2973 * check events
2974 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002975 memcg_check_events(to, page);
2976 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002977out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002978 return ret;
2979}
2980
Michal Hocko2ef37d32012-10-26 13:37:30 +02002981/**
2982 * mem_cgroup_move_parent - moves page to the parent group
2983 * @page: the page to move
2984 * @pc: page_cgroup of the page
2985 * @child: page's cgroup
2986 *
2987 * move charges to its parent or the root cgroup if the group has no
2988 * parent (aka use_hierarchy==0).
2989 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2990 * mem_cgroup_move_account fails) the failure is always temporary and
2991 * it signals a race with a page removal/uncharge or migration. In the
2992 * first case the page is on the way out and it will vanish from the LRU
2993 * on the next attempt and the call should be retried later.
2994 * Isolation from the LRU fails only if page has been isolated from
2995 * the LRU since we looked at it and that usually means either global
2996 * reclaim or migration going on. The page will either get back to the
2997 * LRU or vanish.
2998 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2999 * (!PageCgroupUsed) or moved to a different group. The page will
3000 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003001 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003002static int mem_cgroup_move_parent(struct page *page,
3003 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003004 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003005{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003006 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003007 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003008 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003009 int ret;
3010
Michal Hockod8423012012-10-26 13:37:29 +02003011 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003012
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003013 ret = -EBUSY;
3014 if (!get_page_unless_zero(page))
3015 goto out;
3016 if (isolate_lru_page(page))
3017 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003018
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003019 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003020
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003021 parent = parent_mem_cgroup(child);
3022 /*
3023 * If no parent, move charges to root cgroup.
3024 */
3025 if (!parent)
3026 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003027
Michal Hocko2ef37d32012-10-26 13:37:30 +02003028 if (nr_pages > 1) {
3029 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003030 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003031 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003032
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003033 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003034 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003035 if (!ret)
3036 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003037
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003038 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003039 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003040 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003041put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003042 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003043out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003044 return ret;
3045}
3046
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003047/*
3048 * Charge the memory controller for page usage.
3049 * Return
3050 * 0 if the charge was successful
3051 * < 0 if the cgroup is over its limit
3052 */
3053static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003054 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003055{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003056 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003057 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003058 bool oom = true;
3059 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003060
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003061 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003062 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003063 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003064 /*
3065 * Never OOM-kill a process for a huge page. The
3066 * fault handler will fall back to regular pages.
3067 */
3068 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003069 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003070
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003071 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003072 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003073 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003074 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003075 return 0;
3076}
3077
3078int mem_cgroup_newpage_charge(struct page *page,
3079 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003080{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003081 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003082 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003083 VM_BUG_ON(page_mapped(page));
3084 VM_BUG_ON(page->mapping && !PageAnon(page));
3085 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003086 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003087 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003088}
3089
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003090/*
3091 * While swap-in, try_charge -> commit or cancel, the page is locked.
3092 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003093 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003094 * "commit()" or removed by "cancel()"
3095 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003096static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3097 struct page *page,
3098 gfp_t mask,
3099 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003100{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003101 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003102 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003103 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003104
Johannes Weiner90deb782012-07-31 16:45:47 -07003105 pc = lookup_page_cgroup(page);
3106 /*
3107 * Every swap fault against a single page tries to charge the
3108 * page, bail as early as possible. shmem_unuse() encounters
3109 * already charged pages, too. The USED bit is protected by
3110 * the page lock, which serializes swap cache removal, which
3111 * in turn serializes uncharging.
3112 */
3113 if (PageCgroupUsed(pc))
3114 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003115 if (!do_swap_account)
3116 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003117 memcg = try_get_mem_cgroup_from_page(page);
3118 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003119 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003120 *memcgp = memcg;
3121 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003122 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003123 if (ret == -EINTR)
3124 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003125 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003126charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003127 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3128 if (ret == -EINTR)
3129 ret = 0;
3130 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003131}
3132
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003133int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3134 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3135{
3136 *memcgp = NULL;
3137 if (mem_cgroup_disabled())
3138 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003139 /*
3140 * A racing thread's fault, or swapoff, may have already
3141 * updated the pte, and even removed page from swap cache: in
3142 * those cases unuse_pte()'s pte_same() test will fail; but
3143 * there's also a KSM case which does need to charge the page.
3144 */
3145 if (!PageSwapCache(page)) {
3146 int ret;
3147
3148 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3149 if (ret == -EINTR)
3150 ret = 0;
3151 return ret;
3152 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003153 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3154}
3155
Johannes Weiner827a03d2012-07-31 16:45:36 -07003156void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3157{
3158 if (mem_cgroup_disabled())
3159 return;
3160 if (!memcg)
3161 return;
3162 __mem_cgroup_cancel_charge(memcg, 1);
3163}
3164
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003165static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003166__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003167 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003168{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003169 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003170 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003171 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003172 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003173
Johannes Weinerce587e62012-04-24 20:22:33 +02003174 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003175 /*
3176 * Now swap is on-memory. This means this page may be
3177 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003178 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3179 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3180 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003181 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003182 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003183 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003184 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003185 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003186}
3187
Johannes Weiner72835c82012-01-12 17:18:32 -08003188void mem_cgroup_commit_charge_swapin(struct page *page,
3189 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003190{
Johannes Weiner72835c82012-01-12 17:18:32 -08003191 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003192 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003193}
3194
Johannes Weiner827a03d2012-07-31 16:45:36 -07003195int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3196 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003197{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003198 struct mem_cgroup *memcg = NULL;
3199 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3200 int ret;
3201
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003202 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003203 return 0;
3204 if (PageCompound(page))
3205 return 0;
3206
Johannes Weiner827a03d2012-07-31 16:45:36 -07003207 if (!PageSwapCache(page))
3208 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3209 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003210 ret = __mem_cgroup_try_charge_swapin(mm, page,
3211 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003212 if (!ret)
3213 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3214 }
3215 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003216}
3217
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003218static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003219 unsigned int nr_pages,
3220 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003221{
3222 struct memcg_batch_info *batch = NULL;
3223 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003224
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003225 /* If swapout, usage of swap doesn't decrease */
3226 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3227 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003228
3229 batch = &current->memcg_batch;
3230 /*
3231 * In usual, we do css_get() when we remember memcg pointer.
3232 * But in this case, we keep res->usage until end of a series of
3233 * uncharges. Then, it's ok to ignore memcg's refcnt.
3234 */
3235 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003236 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003237 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003238 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003239 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003240 * the same cgroup and we have chance to coalesce uncharges.
3241 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3242 * because we want to do uncharge as soon as possible.
3243 */
3244
3245 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3246 goto direct_uncharge;
3247
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003248 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003249 goto direct_uncharge;
3250
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003251 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003252 * In typical case, batch->memcg == mem. This means we can
3253 * merge a series of uncharges to an uncharge of res_counter.
3254 * If not, we uncharge res_counter ony by one.
3255 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003256 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003257 goto direct_uncharge;
3258 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003259 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003260 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003261 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003262 return;
3263direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003264 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003265 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003266 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3267 if (unlikely(batch->memcg != memcg))
3268 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003269}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003270
Balbir Singh8697d332008-02-07 00:13:59 -08003271/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003272 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003273 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003274static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003275__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3276 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003277{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003278 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003279 unsigned int nr_pages = 1;
3280 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003281 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003282
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003283 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003284 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003285
Johannes Weiner0c59b892012-07-31 16:45:31 -07003286 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003287
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003288 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003289 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003290 VM_BUG_ON(!PageTransHuge(page));
3291 }
Balbir Singh8697d332008-02-07 00:13:59 -08003292 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003293 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003294 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003295 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003296 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003297 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003298
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003299 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003300
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003301 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003302
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003303 if (!PageCgroupUsed(pc))
3304 goto unlock_out;
3305
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003306 anon = PageAnon(page);
3307
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003308 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003309 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003310 /*
3311 * Generally PageAnon tells if it's the anon statistics to be
3312 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3313 * used before page reached the stage of being marked PageAnon.
3314 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003315 anon = true;
3316 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003317 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003318 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003319 if (page_mapped(page))
3320 goto unlock_out;
3321 /*
3322 * Pages under migration may not be uncharged. But
3323 * end_migration() /must/ be the one uncharging the
3324 * unused post-migration page and so it has to call
3325 * here with the migration bit still set. See the
3326 * res_counter handling below.
3327 */
3328 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003329 goto unlock_out;
3330 break;
3331 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3332 if (!PageAnon(page)) { /* Shared memory */
3333 if (page->mapping && !page_is_file_cache(page))
3334 goto unlock_out;
3335 } else if (page_mapped(page)) /* Anon */
3336 goto unlock_out;
3337 break;
3338 default:
3339 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003340 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003341
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003342 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003343
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003344 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003345 /*
3346 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3347 * freed from LRU. This is safe because uncharged page is expected not
3348 * to be reused (freed soon). Exception is SwapCache, it's handled by
3349 * special functions.
3350 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003351
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003352 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003353 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003354 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003355 * will never be freed.
3356 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003357 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003358 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003359 mem_cgroup_swap_statistics(memcg, true);
3360 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003361 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003362 /*
3363 * Migration does not charge the res_counter for the
3364 * replacement page, so leave it alone when phasing out the
3365 * page that is unused after the migration.
3366 */
3367 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003368 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003369
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003370 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003371
3372unlock_out:
3373 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003374 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003375}
3376
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003377void mem_cgroup_uncharge_page(struct page *page)
3378{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003379 /* early check. */
3380 if (page_mapped(page))
3381 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003382 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003383 if (PageSwapCache(page))
3384 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003385 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003386}
3387
3388void mem_cgroup_uncharge_cache_page(struct page *page)
3389{
3390 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003391 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003392 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003393}
3394
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003395/*
3396 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3397 * In that cases, pages are freed continuously and we can expect pages
3398 * are in the same memcg. All these calls itself limits the number of
3399 * pages freed at once, then uncharge_start/end() is called properly.
3400 * This may be called prural(2) times in a context,
3401 */
3402
3403void mem_cgroup_uncharge_start(void)
3404{
3405 current->memcg_batch.do_batch++;
3406 /* We can do nest. */
3407 if (current->memcg_batch.do_batch == 1) {
3408 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003409 current->memcg_batch.nr_pages = 0;
3410 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003411 }
3412}
3413
3414void mem_cgroup_uncharge_end(void)
3415{
3416 struct memcg_batch_info *batch = &current->memcg_batch;
3417
3418 if (!batch->do_batch)
3419 return;
3420
3421 batch->do_batch--;
3422 if (batch->do_batch) /* If stacked, do nothing. */
3423 return;
3424
3425 if (!batch->memcg)
3426 return;
3427 /*
3428 * This "batch->memcg" is valid without any css_get/put etc...
3429 * bacause we hide charges behind us.
3430 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003431 if (batch->nr_pages)
3432 res_counter_uncharge(&batch->memcg->res,
3433 batch->nr_pages * PAGE_SIZE);
3434 if (batch->memsw_nr_pages)
3435 res_counter_uncharge(&batch->memcg->memsw,
3436 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003437 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003438 /* forget this pointer (for sanity check) */
3439 batch->memcg = NULL;
3440}
3441
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003442#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003443/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003444 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003445 * memcg information is recorded to swap_cgroup of "ent"
3446 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003447void
3448mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003449{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003450 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003451 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003452
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003453 if (!swapout) /* this was a swap cache but the swap is unused ! */
3454 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3455
Johannes Weiner0030f532012-07-31 16:45:25 -07003456 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003457
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003458 /*
3459 * record memcg information, if swapout && memcg != NULL,
3460 * mem_cgroup_get() was called in uncharge().
3461 */
3462 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003463 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003464}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003465#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003466
Andrew Mortonc255a452012-07-31 16:43:02 -07003467#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003468/*
3469 * called from swap_entry_free(). remove record in swap_cgroup and
3470 * uncharge "memsw" account.
3471 */
3472void mem_cgroup_uncharge_swap(swp_entry_t ent)
3473{
3474 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003475 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003476
3477 if (!do_swap_account)
3478 return;
3479
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003480 id = swap_cgroup_record(ent, 0);
3481 rcu_read_lock();
3482 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003483 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003484 /*
3485 * We uncharge this because swap is freed.
3486 * This memcg can be obsolete one. We avoid calling css_tryget
3487 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003488 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003489 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003490 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003491 mem_cgroup_put(memcg);
3492 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003493 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003494}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003495
3496/**
3497 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3498 * @entry: swap entry to be moved
3499 * @from: mem_cgroup which the entry is moved from
3500 * @to: mem_cgroup which the entry is moved to
3501 *
3502 * It succeeds only when the swap_cgroup's record for this entry is the same
3503 * as the mem_cgroup's id of @from.
3504 *
3505 * Returns 0 on success, -EINVAL on failure.
3506 *
3507 * The caller must have charged to @to, IOW, called res_counter_charge() about
3508 * both res and memsw, and called css_get().
3509 */
3510static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003511 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003512{
3513 unsigned short old_id, new_id;
3514
3515 old_id = css_id(&from->css);
3516 new_id = css_id(&to->css);
3517
3518 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003519 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003520 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003521 /*
3522 * This function is only called from task migration context now.
3523 * It postpones res_counter and refcount handling till the end
3524 * of task migration(mem_cgroup_clear_mc()) for performance
3525 * improvement. But we cannot postpone mem_cgroup_get(to)
3526 * because if the process that has been moved to @to does
3527 * swap-in, the refcount of @to might be decreased to 0.
3528 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003529 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003530 return 0;
3531 }
3532 return -EINVAL;
3533}
3534#else
3535static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003536 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003537{
3538 return -EINVAL;
3539}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003540#endif
3541
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003542/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003543 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3544 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003545 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003546void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3547 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003548{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003549 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00003550 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003551 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003552 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003553
Johannes Weiner72835c82012-01-12 17:18:32 -08003554 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003555
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003556 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003557 return;
Balbir Singh40779602008-04-04 14:29:59 -07003558
Mel Gormanb32967f2012-11-19 12:35:47 +00003559 if (PageTransHuge(page))
3560 nr_pages <<= compound_order(page);
3561
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003562 pc = lookup_page_cgroup(page);
3563 lock_page_cgroup(pc);
3564 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003565 memcg = pc->mem_cgroup;
3566 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003567 /*
3568 * At migrating an anonymous page, its mapcount goes down
3569 * to 0 and uncharge() will be called. But, even if it's fully
3570 * unmapped, migration may fail and this page has to be
3571 * charged again. We set MIGRATION flag here and delay uncharge
3572 * until end_migration() is called
3573 *
3574 * Corner Case Thinking
3575 * A)
3576 * When the old page was mapped as Anon and it's unmap-and-freed
3577 * while migration was ongoing.
3578 * If unmap finds the old page, uncharge() of it will be delayed
3579 * until end_migration(). If unmap finds a new page, it's
3580 * uncharged when it make mapcount to be 1->0. If unmap code
3581 * finds swap_migration_entry, the new page will not be mapped
3582 * and end_migration() will find it(mapcount==0).
3583 *
3584 * B)
3585 * When the old page was mapped but migraion fails, the kernel
3586 * remaps it. A charge for it is kept by MIGRATION flag even
3587 * if mapcount goes down to 0. We can do remap successfully
3588 * without charging it again.
3589 *
3590 * C)
3591 * The "old" page is under lock_page() until the end of
3592 * migration, so, the old page itself will not be swapped-out.
3593 * If the new page is swapped out before end_migraton, our
3594 * hook to usual swap-out path will catch the event.
3595 */
3596 if (PageAnon(page))
3597 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003598 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003599 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003600 /*
3601 * If the page is not charged at this point,
3602 * we return here.
3603 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003604 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003605 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003606
Johannes Weiner72835c82012-01-12 17:18:32 -08003607 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003608 /*
3609 * We charge new page before it's used/mapped. So, even if unlock_page()
3610 * is called before end_migration, we can catch all events on this new
3611 * page. In the case new page is migrated but not remapped, new page's
3612 * mapcount will be finally 0 and we call uncharge in end_migration().
3613 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003614 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003615 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003616 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003617 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003618 /*
3619 * The page is committed to the memcg, but it's not actually
3620 * charged to the res_counter since we plan on replacing the
3621 * old one and only one page is going to be left afterwards.
3622 */
Mel Gormanb32967f2012-11-19 12:35:47 +00003623 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003624}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003625
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003626/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003627void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003628 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003629{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003630 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003631 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003632 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003633
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003634 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003635 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003636
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003637 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003638 used = oldpage;
3639 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003640 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003641 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003642 unused = oldpage;
3643 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003644 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003645 __mem_cgroup_uncharge_common(unused,
3646 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3647 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3648 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003649 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003650 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003651 * We disallowed uncharge of pages under migration because mapcount
3652 * of the page goes down to zero, temporarly.
3653 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003654 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003655 pc = lookup_page_cgroup(oldpage);
3656 lock_page_cgroup(pc);
3657 ClearPageCgroupMigration(pc);
3658 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003659
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003660 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003661 * If a page is a file cache, radix-tree replacement is very atomic
3662 * and we can skip this check. When it was an Anon page, its mapcount
3663 * goes down to 0. But because we added MIGRATION flage, it's not
3664 * uncharged yet. There are several case but page->mapcount check
3665 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3666 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003667 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003668 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003669 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003670}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003671
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003672/*
3673 * At replace page cache, newpage is not under any memcg but it's on
3674 * LRU. So, this function doesn't touch res_counter but handles LRU
3675 * in correct way. Both pages are locked so we cannot race with uncharge.
3676 */
3677void mem_cgroup_replace_page_cache(struct page *oldpage,
3678 struct page *newpage)
3679{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003680 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003681 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003682 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003683
3684 if (mem_cgroup_disabled())
3685 return;
3686
3687 pc = lookup_page_cgroup(oldpage);
3688 /* fix accounting on old pages */
3689 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003690 if (PageCgroupUsed(pc)) {
3691 memcg = pc->mem_cgroup;
3692 mem_cgroup_charge_statistics(memcg, false, -1);
3693 ClearPageCgroupUsed(pc);
3694 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003695 unlock_page_cgroup(pc);
3696
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003697 /*
3698 * When called from shmem_replace_page(), in some cases the
3699 * oldpage has already been charged, and in some cases not.
3700 */
3701 if (!memcg)
3702 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003703 /*
3704 * Even if newpage->mapping was NULL before starting replacement,
3705 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3706 * LRU while we overwrite pc->mem_cgroup.
3707 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003708 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003709}
3710
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003711#ifdef CONFIG_DEBUG_VM
3712static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3713{
3714 struct page_cgroup *pc;
3715
3716 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003717 /*
3718 * Can be NULL while feeding pages into the page allocator for
3719 * the first time, i.e. during boot or memory hotplug;
3720 * or when mem_cgroup_disabled().
3721 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003722 if (likely(pc) && PageCgroupUsed(pc))
3723 return pc;
3724 return NULL;
3725}
3726
3727bool mem_cgroup_bad_page_check(struct page *page)
3728{
3729 if (mem_cgroup_disabled())
3730 return false;
3731
3732 return lookup_page_cgroup_used(page) != NULL;
3733}
3734
3735void mem_cgroup_print_bad_page(struct page *page)
3736{
3737 struct page_cgroup *pc;
3738
3739 pc = lookup_page_cgroup_used(page);
3740 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003741 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003742 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003743 }
3744}
3745#endif
3746
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003747static DEFINE_MUTEX(set_limit_mutex);
3748
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003749static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003750 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003751{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003752 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003753 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003754 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003755 int children = mem_cgroup_count_children(memcg);
3756 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003757 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003758
3759 /*
3760 * For keeping hierarchical_reclaim simple, how long we should retry
3761 * is depends on callers. We set our retry-count to be function
3762 * of # of children which we should visit in this loop.
3763 */
3764 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3765
3766 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003767
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003768 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003769 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003770 if (signal_pending(current)) {
3771 ret = -EINTR;
3772 break;
3773 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003774 /*
3775 * Rather than hide all in some function, I do this in
3776 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003777 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003778 */
3779 mutex_lock(&set_limit_mutex);
3780 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3781 if (memswlimit < val) {
3782 ret = -EINVAL;
3783 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003784 break;
3785 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003786
3787 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3788 if (memlimit < val)
3789 enlarge = 1;
3790
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003791 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003792 if (!ret) {
3793 if (memswlimit == val)
3794 memcg->memsw_is_minimum = true;
3795 else
3796 memcg->memsw_is_minimum = false;
3797 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003798 mutex_unlock(&set_limit_mutex);
3799
3800 if (!ret)
3801 break;
3802
Johannes Weiner56600482012-01-12 17:17:59 -08003803 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3804 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003805 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3806 /* Usage is reduced ? */
3807 if (curusage >= oldusage)
3808 retry_count--;
3809 else
3810 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003811 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003812 if (!ret && enlarge)
3813 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003814
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003815 return ret;
3816}
3817
Li Zefan338c8432009-06-17 16:27:15 -07003818static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3819 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003820{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003821 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003822 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003823 int children = mem_cgroup_count_children(memcg);
3824 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003825 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003826
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003827 /* see mem_cgroup_resize_res_limit */
3828 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3829 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003830 while (retry_count) {
3831 if (signal_pending(current)) {
3832 ret = -EINTR;
3833 break;
3834 }
3835 /*
3836 * Rather than hide all in some function, I do this in
3837 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003838 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003839 */
3840 mutex_lock(&set_limit_mutex);
3841 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3842 if (memlimit > val) {
3843 ret = -EINVAL;
3844 mutex_unlock(&set_limit_mutex);
3845 break;
3846 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003847 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3848 if (memswlimit < val)
3849 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003850 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003851 if (!ret) {
3852 if (memlimit == val)
3853 memcg->memsw_is_minimum = true;
3854 else
3855 memcg->memsw_is_minimum = false;
3856 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003857 mutex_unlock(&set_limit_mutex);
3858
3859 if (!ret)
3860 break;
3861
Johannes Weiner56600482012-01-12 17:17:59 -08003862 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3863 MEM_CGROUP_RECLAIM_NOSWAP |
3864 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003865 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003866 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003867 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003868 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003869 else
3870 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003871 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003872 if (!ret && enlarge)
3873 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003874 return ret;
3875}
3876
Balbir Singh4e416952009-09-23 15:56:39 -07003877unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003878 gfp_t gfp_mask,
3879 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003880{
3881 unsigned long nr_reclaimed = 0;
3882 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3883 unsigned long reclaimed;
3884 int loop = 0;
3885 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003886 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003887 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003888
3889 if (order > 0)
3890 return 0;
3891
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003892 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003893 /*
3894 * This loop can run a while, specially if mem_cgroup's continuously
3895 * keep exceeding their soft limit and putting the system under
3896 * pressure
3897 */
3898 do {
3899 if (next_mz)
3900 mz = next_mz;
3901 else
3902 mz = mem_cgroup_largest_soft_limit_node(mctz);
3903 if (!mz)
3904 break;
3905
Ying Han0ae5e892011-05-26 16:25:25 -07003906 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003907 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003908 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003909 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003910 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003911 spin_lock(&mctz->lock);
3912
3913 /*
3914 * If we failed to reclaim anything from this memory cgroup
3915 * it is time to move on to the next cgroup
3916 */
3917 next_mz = NULL;
3918 if (!reclaimed) {
3919 do {
3920 /*
3921 * Loop until we find yet another one.
3922 *
3923 * By the time we get the soft_limit lock
3924 * again, someone might have aded the
3925 * group back on the RB tree. Iterate to
3926 * make sure we get a different mem.
3927 * mem_cgroup_largest_soft_limit_node returns
3928 * NULL if no other cgroup is present on
3929 * the tree
3930 */
3931 next_mz =
3932 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003933 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003934 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003935 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003936 break;
3937 } while (1);
3938 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003939 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3940 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003941 /*
3942 * One school of thought says that we should not add
3943 * back the node to the tree if reclaim returns 0.
3944 * But our reclaim could return 0, simply because due
3945 * to priority we are exposing a smaller subset of
3946 * memory to reclaim from. Consider this as a longer
3947 * term TODO.
3948 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003949 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003950 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003951 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003952 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003953 loop++;
3954 /*
3955 * Could not reclaim anything and there are no more
3956 * mem cgroups to try or we seem to be looping without
3957 * reclaiming anything.
3958 */
3959 if (!nr_reclaimed &&
3960 (next_mz == NULL ||
3961 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3962 break;
3963 } while (!nr_reclaimed);
3964 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003965 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003966 return nr_reclaimed;
3967}
3968
Michal Hocko2ef37d32012-10-26 13:37:30 +02003969/**
3970 * mem_cgroup_force_empty_list - clears LRU of a group
3971 * @memcg: group to clear
3972 * @node: NUMA node
3973 * @zid: zone id
3974 * @lru: lru to to clear
3975 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003976 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003977 * reclaim the pages page themselves - pages are moved to the parent (or root)
3978 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003979 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003980static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003981 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003982{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003983 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003984 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003985 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003986 struct page *busy;
3987 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003988
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003989 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003990 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3991 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003992
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003993 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003994 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003995 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003996 struct page *page;
3997
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003998 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003999 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004000 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004001 break;
4002 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004003 page = list_entry(list->prev, struct page, lru);
4004 if (busy == page) {
4005 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004006 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004007 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004008 continue;
4009 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004010 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004011
Johannes Weiner925b7672012-01-12 17:18:15 -08004012 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004013
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004014 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004015 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004016 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004017 cond_resched();
4018 } else
4019 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004020 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004021}
4022
4023/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004024 * make mem_cgroup's charge to be 0 if there is no task by moving
4025 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004026 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004027 *
4028 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004029 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004030static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004031{
Michal Hockoc26251f2012-10-26 13:37:28 +02004032 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004033 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004034
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004035 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004036 /* This is for making all *used* pages to be on LRU. */
4037 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004038 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004039 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004040 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004041 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004042 enum lru_list lru;
4043 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004044 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004045 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004046 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004047 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004048 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004049 mem_cgroup_end_move(memcg);
4050 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004051 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004052
Michal Hocko2ef37d32012-10-26 13:37:30 +02004053 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004054 * Kernel memory may not necessarily be trackable to a specific
4055 * process. So they are not migrated, and therefore we can't
4056 * expect their value to drop to 0 here.
4057 * Having res filled up with kmem only is enough.
4058 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004059 * This is a safety check because mem_cgroup_force_empty_list
4060 * could have raced with mem_cgroup_replace_page_cache callers
4061 * so the lru seemed empty but the page could have been added
4062 * right after the check. RES_USAGE should be safe as we always
4063 * charge before adding to the LRU.
4064 */
Glauber Costabea207c2012-12-18 14:22:11 -08004065 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4066 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4067 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004068}
4069
4070/*
4071 * Reclaims as many pages from the given memcg as possible and moves
4072 * the rest to the parent.
4073 *
4074 * Caller is responsible for holding css reference for memcg.
4075 */
4076static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4077{
4078 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4079 struct cgroup *cgrp = memcg->css.cgroup;
4080
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004081 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004082 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4083 return -EBUSY;
4084
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004085 /* we call try-to-free pages for make this cgroup empty */
4086 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004087 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004088 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004089 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004090
Michal Hockoc26251f2012-10-26 13:37:28 +02004091 if (signal_pending(current))
4092 return -EINTR;
4093
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004095 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004096 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004097 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004098 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004099 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004100 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004101
4102 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004103 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004104 mem_cgroup_reparent_charges(memcg);
4105
4106 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004107}
4108
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004109static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004110{
Michal Hockoc26251f2012-10-26 13:37:28 +02004111 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4112 int ret;
4113
Michal Hockod8423012012-10-26 13:37:29 +02004114 if (mem_cgroup_is_root(memcg))
4115 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004116 css_get(&memcg->css);
4117 ret = mem_cgroup_force_empty(memcg);
4118 css_put(&memcg->css);
4119
4120 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004121}
4122
4123
Balbir Singh18f59ea2009-01-07 18:08:07 -08004124static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4125{
4126 return mem_cgroup_from_cont(cont)->use_hierarchy;
4127}
4128
4129static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4130 u64 val)
4131{
4132 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004133 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004134 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004135 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004136
4137 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004138 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004139
4140 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004141
4142 if (memcg->use_hierarchy == val)
4143 goto out;
4144
Balbir Singh18f59ea2009-01-07 18:08:07 -08004145 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004146 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004147 * in the child subtrees. If it is unset, then the change can
4148 * occur, provided the current cgroup has no children.
4149 *
4150 * For the root cgroup, parent_mem is NULL, we allow value to be
4151 * set if there are no children.
4152 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004153 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004154 (val == 1 || val == 0)) {
4155 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004156 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004157 else
4158 retval = -EBUSY;
4159 } else
4160 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004161
4162out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004163 cgroup_unlock();
4164
4165 return retval;
4166}
4167
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004168
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004169static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004170 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004171{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004172 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004173 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004174
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004175 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004176 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004177 val += mem_cgroup_read_stat(iter, idx);
4178
4179 if (val < 0) /* race ? */
4180 val = 0;
4181 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004182}
4183
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004184static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004185{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004186 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004187
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004188 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004189 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004190 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004191 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004192 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004193 }
4194
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004195 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4196 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004197
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004198 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004199 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004200
4201 return val << PAGE_SHIFT;
4202}
4203
Tejun Heoaf36f902012-04-01 12:09:55 -07004204static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4205 struct file *file, char __user *buf,
4206 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004207{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004208 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004209 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004210 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004211 int name, len;
4212 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004213
4214 type = MEMFILE_TYPE(cft->private);
4215 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004216
4217 if (!do_swap_account && type == _MEMSWAP)
4218 return -EOPNOTSUPP;
4219
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004220 switch (type) {
4221 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004222 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004223 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004224 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004225 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004226 break;
4227 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004228 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004229 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004230 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004231 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004232 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004233 case _KMEM:
4234 val = res_counter_read_u64(&memcg->kmem, name);
4235 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004236 default:
4237 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004238 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004239
4240 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4241 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004242}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004243
4244static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4245{
4246 int ret = -EINVAL;
4247#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004248 bool must_inc_static_branch = false;
4249
Glauber Costa510fc4e2012-12-18 14:21:47 -08004250 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4251 /*
4252 * For simplicity, we won't allow this to be disabled. It also can't
4253 * be changed if the cgroup has children already, or if tasks had
4254 * already joined.
4255 *
4256 * If tasks join before we set the limit, a person looking at
4257 * kmem.usage_in_bytes will have no way to determine when it took
4258 * place, which makes the value quite meaningless.
4259 *
4260 * After it first became limited, changes in the value of the limit are
4261 * of course permitted.
4262 *
4263 * Taking the cgroup_lock is really offensive, but it is so far the only
4264 * way to guarantee that no children will appear. There are plenty of
4265 * other offenders, and they should all go away. Fine grained locking
4266 * is probably the way to go here. When we are fully hierarchical, we
4267 * can also get rid of the use_hierarchy check.
4268 */
4269 cgroup_lock();
4270 mutex_lock(&set_limit_mutex);
4271 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4272 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4273 !list_empty(&cont->children))) {
4274 ret = -EBUSY;
4275 goto out;
4276 }
4277 ret = res_counter_set_limit(&memcg->kmem, val);
4278 VM_BUG_ON(ret);
4279
Glauber Costaa8964b92012-12-18 14:22:09 -08004280 /*
4281 * After this point, kmem_accounted (that we test atomically in
4282 * the beginning of this conditional), is no longer 0. This
4283 * guarantees only one process will set the following boolean
4284 * to true. We don't need test_and_set because we're protected
4285 * by the set_limit_mutex anyway.
4286 */
4287 memcg_kmem_set_activated(memcg);
4288 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08004289 /*
4290 * kmem charges can outlive the cgroup. In the case of slab
4291 * pages, for instance, a page contain objects from various
4292 * processes, so it is unfeasible to migrate them away. We
4293 * need to reference count the memcg because of that.
4294 */
4295 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004296 } else
4297 ret = res_counter_set_limit(&memcg->kmem, val);
4298out:
4299 mutex_unlock(&set_limit_mutex);
4300 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08004301
4302 /*
4303 * We are by now familiar with the fact that we can't inc the static
4304 * branch inside cgroup_lock. See disarm functions for details. A
4305 * worker here is overkill, but also wrong: After the limit is set, we
4306 * must start accounting right away. Since this operation can't fail,
4307 * we can safely defer it to here - no rollback will be needed.
4308 *
4309 * The boolean used to control this is also safe, because
4310 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
4311 * able to set it to true;
4312 */
4313 if (must_inc_static_branch) {
4314 static_key_slow_inc(&memcg_kmem_enabled_key);
4315 /*
4316 * setting the active bit after the inc will guarantee no one
4317 * starts accounting before all call sites are patched
4318 */
4319 memcg_kmem_set_active(memcg);
4320 }
4321
Glauber Costa510fc4e2012-12-18 14:21:47 -08004322#endif
4323 return ret;
4324}
4325
4326static void memcg_propagate_kmem(struct mem_cgroup *memcg)
4327{
4328 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4329 if (!parent)
4330 return;
4331 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08004332#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004333 /*
4334 * When that happen, we need to disable the static branch only on those
4335 * memcgs that enabled it. To achieve this, we would be forced to
4336 * complicate the code by keeping track of which memcgs were the ones
4337 * that actually enabled limits, and which ones got it from its
4338 * parents.
4339 *
4340 * It is a lot simpler just to do static_key_slow_inc() on every child
4341 * that is accounted.
4342 */
4343 if (memcg_kmem_is_active(memcg)) {
Glauber Costa7de37682012-12-18 14:22:07 -08004344 mem_cgroup_get(memcg);
Glauber Costaa8964b92012-12-18 14:22:09 -08004345 static_key_slow_inc(&memcg_kmem_enabled_key);
4346 }
Glauber Costa7de37682012-12-18 14:22:07 -08004347#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004348}
4349
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004350/*
4351 * The user of this function is...
4352 * RES_LIMIT.
4353 */
Paul Menage856c13a2008-07-25 01:47:04 -07004354static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4355 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004356{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004357 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004358 enum res_type type;
4359 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004360 unsigned long long val;
4361 int ret;
4362
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004363 type = MEMFILE_TYPE(cft->private);
4364 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004365
4366 if (!do_swap_account && type == _MEMSWAP)
4367 return -EOPNOTSUPP;
4368
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004369 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004370 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004371 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4372 ret = -EINVAL;
4373 break;
4374 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004375 /* This function does all necessary parse...reuse it */
4376 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004377 if (ret)
4378 break;
4379 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004380 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004381 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004382 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004383 else if (type == _KMEM)
4384 ret = memcg_update_kmem_limit(cont, val);
4385 else
4386 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004387 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004388 case RES_SOFT_LIMIT:
4389 ret = res_counter_memparse_write_strategy(buffer, &val);
4390 if (ret)
4391 break;
4392 /*
4393 * For memsw, soft limits are hard to implement in terms
4394 * of semantics, for now, we support soft limits for
4395 * control without swap
4396 */
4397 if (type == _MEM)
4398 ret = res_counter_set_soft_limit(&memcg->res, val);
4399 else
4400 ret = -EINVAL;
4401 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004402 default:
4403 ret = -EINVAL; /* should be BUG() ? */
4404 break;
4405 }
4406 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004407}
4408
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004409static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4410 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4411{
4412 struct cgroup *cgroup;
4413 unsigned long long min_limit, min_memsw_limit, tmp;
4414
4415 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4416 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4417 cgroup = memcg->css.cgroup;
4418 if (!memcg->use_hierarchy)
4419 goto out;
4420
4421 while (cgroup->parent) {
4422 cgroup = cgroup->parent;
4423 memcg = mem_cgroup_from_cont(cgroup);
4424 if (!memcg->use_hierarchy)
4425 break;
4426 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4427 min_limit = min(min_limit, tmp);
4428 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4429 min_memsw_limit = min(min_memsw_limit, tmp);
4430 }
4431out:
4432 *mem_limit = min_limit;
4433 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004434}
4435
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004436static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004437{
Tejun Heoaf36f902012-04-01 12:09:55 -07004438 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004439 int name;
4440 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004441
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004442 type = MEMFILE_TYPE(event);
4443 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004444
4445 if (!do_swap_account && type == _MEMSWAP)
4446 return -EOPNOTSUPP;
4447
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004448 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004449 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004450 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004451 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004452 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004453 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004454 else if (type == _KMEM)
4455 res_counter_reset_max(&memcg->kmem);
4456 else
4457 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004458 break;
4459 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004460 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004461 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004462 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004463 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004464 else if (type == _KMEM)
4465 res_counter_reset_failcnt(&memcg->kmem);
4466 else
4467 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004468 break;
4469 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004470
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004471 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004472}
4473
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004474static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4475 struct cftype *cft)
4476{
4477 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4478}
4479
Daisuke Nishimura02491442010-03-10 15:22:17 -08004480#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004481static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4482 struct cftype *cft, u64 val)
4483{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004484 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004485
4486 if (val >= (1 << NR_MOVE_TYPE))
4487 return -EINVAL;
4488 /*
4489 * We check this value several times in both in can_attach() and
4490 * attach(), so we need cgroup lock to prevent this value from being
4491 * inconsistent.
4492 */
4493 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004494 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004495 cgroup_unlock();
4496
4497 return 0;
4498}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004499#else
4500static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4501 struct cftype *cft, u64 val)
4502{
4503 return -ENOSYS;
4504}
4505#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004506
Ying Han406eb0c2011-05-26 16:25:37 -07004507#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004508static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004509 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004510{
4511 int nid;
4512 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4513 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004514 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004515
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004516 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004517 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004518 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004519 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004520 seq_printf(m, " N%d=%lu", nid, node_nr);
4521 }
4522 seq_putc(m, '\n');
4523
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004524 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004525 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004526 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004527 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004528 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004529 seq_printf(m, " N%d=%lu", nid, node_nr);
4530 }
4531 seq_putc(m, '\n');
4532
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004533 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004534 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004535 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004536 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004537 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004538 seq_printf(m, " N%d=%lu", nid, node_nr);
4539 }
4540 seq_putc(m, '\n');
4541
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004542 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004543 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004544 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004545 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004546 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004547 seq_printf(m, " N%d=%lu", nid, node_nr);
4548 }
4549 seq_putc(m, '\n');
4550 return 0;
4551}
4552#endif /* CONFIG_NUMA */
4553
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004554static const char * const mem_cgroup_lru_names[] = {
4555 "inactive_anon",
4556 "active_anon",
4557 "inactive_file",
4558 "active_file",
4559 "unevictable",
4560};
4561
4562static inline void mem_cgroup_lru_names_not_uptodate(void)
4563{
4564 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4565}
4566
Wanpeng Liab215882012-07-31 16:43:09 -07004567static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004568 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004569{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004570 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004571 struct mem_cgroup *mi;
4572 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004573
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004574 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004575 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004576 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004577 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4578 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004579 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004580
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004581 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4582 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4583 mem_cgroup_read_events(memcg, i));
4584
4585 for (i = 0; i < NR_LRU_LISTS; i++)
4586 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4587 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4588
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004589 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004590 {
4591 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004592 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004593 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004594 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004595 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4596 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004597 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004598
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004599 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4600 long long val = 0;
4601
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004602 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004603 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004604 for_each_mem_cgroup_tree(mi, memcg)
4605 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4606 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4607 }
4608
4609 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4610 unsigned long long val = 0;
4611
4612 for_each_mem_cgroup_tree(mi, memcg)
4613 val += mem_cgroup_read_events(mi, i);
4614 seq_printf(m, "total_%s %llu\n",
4615 mem_cgroup_events_names[i], val);
4616 }
4617
4618 for (i = 0; i < NR_LRU_LISTS; i++) {
4619 unsigned long long val = 0;
4620
4621 for_each_mem_cgroup_tree(mi, memcg)
4622 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4623 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004624 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004625
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004626#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004627 {
4628 int nid, zid;
4629 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004630 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004631 unsigned long recent_rotated[2] = {0, 0};
4632 unsigned long recent_scanned[2] = {0, 0};
4633
4634 for_each_online_node(nid)
4635 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004636 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004637 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004638
Hugh Dickins89abfab2012-05-29 15:06:53 -07004639 recent_rotated[0] += rstat->recent_rotated[0];
4640 recent_rotated[1] += rstat->recent_rotated[1];
4641 recent_scanned[0] += rstat->recent_scanned[0];
4642 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004643 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004644 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4645 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4646 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4647 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004648 }
4649#endif
4650
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004651 return 0;
4652}
4653
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004654static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4655{
4656 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4657
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004658 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004659}
4660
4661static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4662 u64 val)
4663{
4664 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4665 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004666
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004667 if (val > 100)
4668 return -EINVAL;
4669
4670 if (cgrp->parent == NULL)
4671 return -EINVAL;
4672
4673 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004674
4675 cgroup_lock();
4676
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004677 /* If under hierarchy, only empty-root can set this value */
4678 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004679 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4680 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004681 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004682 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004683
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004684 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004685
Li Zefan068b38c2009-01-15 13:51:26 -08004686 cgroup_unlock();
4687
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004688 return 0;
4689}
4690
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004691static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4692{
4693 struct mem_cgroup_threshold_ary *t;
4694 u64 usage;
4695 int i;
4696
4697 rcu_read_lock();
4698 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004699 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004700 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004701 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004702
4703 if (!t)
4704 goto unlock;
4705
4706 usage = mem_cgroup_usage(memcg, swap);
4707
4708 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004709 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004710 * If it's not true, a threshold was crossed after last
4711 * call of __mem_cgroup_threshold().
4712 */
Phil Carmody5407a562010-05-26 14:42:42 -07004713 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004714
4715 /*
4716 * Iterate backward over array of thresholds starting from
4717 * current_threshold and check if a threshold is crossed.
4718 * If none of thresholds below usage is crossed, we read
4719 * only one element of the array here.
4720 */
4721 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4722 eventfd_signal(t->entries[i].eventfd, 1);
4723
4724 /* i = current_threshold + 1 */
4725 i++;
4726
4727 /*
4728 * Iterate forward over array of thresholds starting from
4729 * current_threshold+1 and check if a threshold is crossed.
4730 * If none of thresholds above usage is crossed, we read
4731 * only one element of the array here.
4732 */
4733 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4734 eventfd_signal(t->entries[i].eventfd, 1);
4735
4736 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004737 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004738unlock:
4739 rcu_read_unlock();
4740}
4741
4742static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4743{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004744 while (memcg) {
4745 __mem_cgroup_threshold(memcg, false);
4746 if (do_swap_account)
4747 __mem_cgroup_threshold(memcg, true);
4748
4749 memcg = parent_mem_cgroup(memcg);
4750 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004751}
4752
4753static int compare_thresholds(const void *a, const void *b)
4754{
4755 const struct mem_cgroup_threshold *_a = a;
4756 const struct mem_cgroup_threshold *_b = b;
4757
4758 return _a->threshold - _b->threshold;
4759}
4760
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004761static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004762{
4763 struct mem_cgroup_eventfd_list *ev;
4764
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004765 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004766 eventfd_signal(ev->eventfd, 1);
4767 return 0;
4768}
4769
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004770static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004771{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004772 struct mem_cgroup *iter;
4773
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004774 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004775 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004776}
4777
4778static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4779 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004780{
4781 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004782 struct mem_cgroup_thresholds *thresholds;
4783 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004784 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004785 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004786 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004787
4788 ret = res_counter_memparse_write_strategy(args, &threshold);
4789 if (ret)
4790 return ret;
4791
4792 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004793
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004794 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004795 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004796 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004797 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004798 else
4799 BUG();
4800
4801 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4802
4803 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004804 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004805 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4806
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004807 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004808
4809 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004810 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004811 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004812 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004813 ret = -ENOMEM;
4814 goto unlock;
4815 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004816 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004817
4818 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004819 if (thresholds->primary) {
4820 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004821 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004822 }
4823
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004824 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004825 new->entries[size - 1].eventfd = eventfd;
4826 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004827
4828 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004829 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004830 compare_thresholds, NULL);
4831
4832 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004833 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004834 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004835 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004836 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004837 * new->current_threshold will not be used until
4838 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004839 * it here.
4840 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004841 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004842 } else
4843 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004844 }
4845
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004846 /* Free old spare buffer and save old primary buffer as spare */
4847 kfree(thresholds->spare);
4848 thresholds->spare = thresholds->primary;
4849
4850 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004851
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004852 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004853 synchronize_rcu();
4854
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004855unlock:
4856 mutex_unlock(&memcg->thresholds_lock);
4857
4858 return ret;
4859}
4860
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004861static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004862 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004863{
4864 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004865 struct mem_cgroup_thresholds *thresholds;
4866 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004867 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004868 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004869 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004870
4871 mutex_lock(&memcg->thresholds_lock);
4872 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004873 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004874 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004875 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004876 else
4877 BUG();
4878
Anton Vorontsov371528c2012-02-24 05:14:46 +04004879 if (!thresholds->primary)
4880 goto unlock;
4881
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004882 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4883
4884 /* Check if a threshold crossed before removing */
4885 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4886
4887 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004888 size = 0;
4889 for (i = 0; i < thresholds->primary->size; i++) {
4890 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004891 size++;
4892 }
4893
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004894 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004895
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004896 /* Set thresholds array to NULL if we don't have thresholds */
4897 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004898 kfree(new);
4899 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004900 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004901 }
4902
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004903 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004904
4905 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004906 new->current_threshold = -1;
4907 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4908 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004909 continue;
4910
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004911 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004912 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004913 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004914 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004915 * until rcu_assign_pointer(), so it's safe to increment
4916 * it here.
4917 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004918 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004919 }
4920 j++;
4921 }
4922
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004923swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004924 /* Swap primary and spare array */
4925 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004926 /* If all events are unregistered, free the spare array */
4927 if (!new) {
4928 kfree(thresholds->spare);
4929 thresholds->spare = NULL;
4930 }
4931
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004932 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004933
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004934 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004935 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004936unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004937 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004938}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004939
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004940static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4941 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4942{
4943 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4944 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004945 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004946
4947 BUG_ON(type != _OOM_TYPE);
4948 event = kmalloc(sizeof(*event), GFP_KERNEL);
4949 if (!event)
4950 return -ENOMEM;
4951
Michal Hocko1af8efe2011-07-26 16:08:24 -07004952 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004953
4954 event->eventfd = eventfd;
4955 list_add(&event->list, &memcg->oom_notify);
4956
4957 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004958 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004959 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004960 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004961
4962 return 0;
4963}
4964
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004965static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004966 struct cftype *cft, struct eventfd_ctx *eventfd)
4967{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004968 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004969 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004970 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004971
4972 BUG_ON(type != _OOM_TYPE);
4973
Michal Hocko1af8efe2011-07-26 16:08:24 -07004974 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004975
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004976 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004977 if (ev->eventfd == eventfd) {
4978 list_del(&ev->list);
4979 kfree(ev);
4980 }
4981 }
4982
Michal Hocko1af8efe2011-07-26 16:08:24 -07004983 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004984}
4985
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004986static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4987 struct cftype *cft, struct cgroup_map_cb *cb)
4988{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004989 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004990
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004991 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004992
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004993 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004994 cb->fill(cb, "under_oom", 1);
4995 else
4996 cb->fill(cb, "under_oom", 0);
4997 return 0;
4998}
4999
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005000static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5001 struct cftype *cft, u64 val)
5002{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005003 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005004 struct mem_cgroup *parent;
5005
5006 /* cannot set to root cgroup and only 0 and 1 are allowed */
5007 if (!cgrp->parent || !((val == 0) || (val == 1)))
5008 return -EINVAL;
5009
5010 parent = mem_cgroup_from_cont(cgrp->parent);
5011
5012 cgroup_lock();
5013 /* oom-kill-disable is a flag for subhierarchy. */
5014 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005015 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005016 cgroup_unlock();
5017 return -EINVAL;
5018 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005019 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005020 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005021 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005022 cgroup_unlock();
5023 return 0;
5024}
5025
Andrew Mortonc255a452012-07-31 16:43:02 -07005026#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005027static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005028{
Glauber Costa510fc4e2012-12-18 14:21:47 -08005029 memcg_propagate_kmem(memcg);
Glauber Costa1d62e432012-04-09 19:36:33 -03005030 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005031};
5032
Glauber Costa1d62e432012-04-09 19:36:33 -03005033static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005034{
Glauber Costa1d62e432012-04-09 19:36:33 -03005035 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005036
5037 memcg_kmem_mark_dead(memcg);
5038
5039 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5040 return;
5041
5042 /*
5043 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5044 * path here, being careful not to race with memcg_uncharge_kmem: it is
5045 * possible that the charges went down to 0 between mark_dead and the
5046 * res_counter read, so in that case, we don't need the put
5047 */
5048 if (memcg_kmem_test_and_clear_dead(memcg))
5049 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005050}
Glauber Costae5671df2011-12-11 21:47:01 +00005051#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005052static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005053{
5054 return 0;
5055}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005056
Glauber Costa1d62e432012-04-09 19:36:33 -03005057static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005058{
5059}
Glauber Costae5671df2011-12-11 21:47:01 +00005060#endif
5061
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005062static struct cftype mem_cgroup_files[] = {
5063 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005064 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005065 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005066 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005067 .register_event = mem_cgroup_usage_register_event,
5068 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005069 },
5070 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005071 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005072 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005073 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005074 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005075 },
5076 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005077 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005078 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005079 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005080 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005081 },
5082 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005083 .name = "soft_limit_in_bytes",
5084 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5085 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005086 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005087 },
5088 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005089 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005090 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005091 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005092 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005093 },
Balbir Singh8697d332008-02-07 00:13:59 -08005094 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005095 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005096 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005097 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005098 {
5099 .name = "force_empty",
5100 .trigger = mem_cgroup_force_empty_write,
5101 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005102 {
5103 .name = "use_hierarchy",
5104 .write_u64 = mem_cgroup_hierarchy_write,
5105 .read_u64 = mem_cgroup_hierarchy_read,
5106 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005107 {
5108 .name = "swappiness",
5109 .read_u64 = mem_cgroup_swappiness_read,
5110 .write_u64 = mem_cgroup_swappiness_write,
5111 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005112 {
5113 .name = "move_charge_at_immigrate",
5114 .read_u64 = mem_cgroup_move_charge_read,
5115 .write_u64 = mem_cgroup_move_charge_write,
5116 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005117 {
5118 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005119 .read_map = mem_cgroup_oom_control_read,
5120 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005121 .register_event = mem_cgroup_oom_register_event,
5122 .unregister_event = mem_cgroup_oom_unregister_event,
5123 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5124 },
Ying Han406eb0c2011-05-26 16:25:37 -07005125#ifdef CONFIG_NUMA
5126 {
5127 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005128 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005129 },
5130#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07005131#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005132 {
5133 .name = "memsw.usage_in_bytes",
5134 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005135 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005136 .register_event = mem_cgroup_usage_register_event,
5137 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005138 },
5139 {
5140 .name = "memsw.max_usage_in_bytes",
5141 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5142 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005143 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005144 },
5145 {
5146 .name = "memsw.limit_in_bytes",
5147 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5148 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005149 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005150 },
5151 {
5152 .name = "memsw.failcnt",
5153 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5154 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005155 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005156 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005157#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005158#ifdef CONFIG_MEMCG_KMEM
5159 {
5160 .name = "kmem.limit_in_bytes",
5161 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5162 .write_string = mem_cgroup_write,
5163 .read = mem_cgroup_read,
5164 },
5165 {
5166 .name = "kmem.usage_in_bytes",
5167 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5168 .read = mem_cgroup_read,
5169 },
5170 {
5171 .name = "kmem.failcnt",
5172 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5173 .trigger = mem_cgroup_reset,
5174 .read = mem_cgroup_read,
5175 },
5176 {
5177 .name = "kmem.max_usage_in_bytes",
5178 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5179 .trigger = mem_cgroup_reset,
5180 .read = mem_cgroup_read,
5181 },
5182#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005183 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005184};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005185
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005186static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005187{
5188 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005189 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005190 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005191 /*
5192 * This routine is called against possible nodes.
5193 * But it's BUG to call kmalloc() against offline node.
5194 *
5195 * TODO: this routine can waste much memory for nodes which will
5196 * never be onlined. It's better to use memory hotplug callback
5197 * function.
5198 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005199 if (!node_state(node, N_NORMAL_MEMORY))
5200 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005201 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005202 if (!pn)
5203 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005204
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005205 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5206 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005207 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005208 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005209 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005210 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005211 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005212 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005213 return 0;
5214}
5215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005216static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005217{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005218 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005219}
5220
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005221static struct mem_cgroup *mem_cgroup_alloc(void)
5222{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005223 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005224 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005225
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005226 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005227 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005228 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005229 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005230 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005231
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005232 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005233 return NULL;
5234
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005235 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5236 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005237 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005238 spin_lock_init(&memcg->pcp_counter_lock);
5239 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005240
5241out_free:
5242 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005243 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005244 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005245 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005246 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005247}
5248
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005249/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07005250 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07005251 * but in process context. The work_freeing structure is overlaid
5252 * on the rcu_freeing structure, which itself is overlaid on memsw.
5253 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07005254static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005255{
5256 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005257 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005258
5259 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07005260 /*
5261 * We need to make sure that (at least for now), the jump label
5262 * destruction code runs outside of the cgroup lock. This is because
5263 * get_online_cpus(), which is called from the static_branch update,
5264 * can't be called inside the cgroup_lock. cpusets are the ones
5265 * enforcing this dependency, so if they ever change, we might as well.
5266 *
5267 * schedule_work() will guarantee this happens. Be careful if you need
5268 * to move this code around, and make sure it is outside
5269 * the cgroup_lock.
5270 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005271 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005272 if (size < PAGE_SIZE)
5273 kfree(memcg);
5274 else
5275 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005276}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005277
5278static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005279{
5280 struct mem_cgroup *memcg;
5281
5282 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005283 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005284 schedule_work(&memcg->work_freeing);
5285}
5286
5287/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005288 * At destroying mem_cgroup, references from swap_cgroup can remain.
5289 * (scanning all at force_empty is too costly...)
5290 *
5291 * Instead of clearing all references at force_empty, we remember
5292 * the number of reference from swap_cgroup and free mem_cgroup when
5293 * it goes down to 0.
5294 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005295 * Removal of cgroup itself succeeds regardless of refs from swap.
5296 */
5297
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005298static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005299{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005300 int node;
5301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005302 mem_cgroup_remove_from_trees(memcg);
5303 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005304
Bob Liu3ed28fa2012-01-12 17:19:04 -08005305 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005306 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005307
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005308 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005309 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005310}
5311
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005312static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005313{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005314 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005315}
5316
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005317static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005318{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005319 if (atomic_sub_and_test(count, &memcg->refcnt)) {
5320 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5321 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005322 if (parent)
5323 mem_cgroup_put(parent);
5324 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005325}
5326
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005327static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005328{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005329 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005330}
5331
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005332/*
5333 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5334 */
Glauber Costae1aab162011-12-11 21:47:03 +00005335struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005336{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005337 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005338 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005339 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005340}
Glauber Costae1aab162011-12-11 21:47:03 +00005341EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005342
Andrew Mortonc255a452012-07-31 16:43:02 -07005343#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005344static void __init enable_swap_cgroup(void)
5345{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08005346 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005347 do_swap_account = 1;
5348}
5349#else
5350static void __init enable_swap_cgroup(void)
5351{
5352}
5353#endif
5354
Balbir Singhf64c3f52009-09-23 15:56:37 -07005355static int mem_cgroup_soft_limit_tree_init(void)
5356{
5357 struct mem_cgroup_tree_per_node *rtpn;
5358 struct mem_cgroup_tree_per_zone *rtpz;
5359 int tmp, node, zone;
5360
Bob Liu3ed28fa2012-01-12 17:19:04 -08005361 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07005362 tmp = node;
5363 if (!node_state(node, N_NORMAL_MEMORY))
5364 tmp = -1;
5365 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5366 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08005367 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005368
5369 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5370
5371 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5372 rtpz = &rtpn->rb_tree_per_zone[zone];
5373 rtpz->rb_root = RB_ROOT;
5374 spin_lock_init(&rtpz->lock);
5375 }
5376 }
5377 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08005378
5379err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08005380 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08005381 if (!soft_limit_tree.rb_tree_per_node[node])
5382 break;
5383 kfree(soft_limit_tree.rb_tree_per_node[node]);
5384 soft_limit_tree.rb_tree_per_node[node] = NULL;
5385 }
5386 return 1;
5387
Balbir Singhf64c3f52009-09-23 15:56:37 -07005388}
5389
Li Zefan0eb253e2009-01-15 13:51:25 -08005390static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08005391mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005392{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005393 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005394 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005395 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005396
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005397 memcg = mem_cgroup_alloc();
5398 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005399 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005400
Bob Liu3ed28fa2012-01-12 17:19:04 -08005401 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005402 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005403 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005404
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005405 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005406 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005407 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005408 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005409 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005410 if (mem_cgroup_soft_limit_tree_init())
5411 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08005412 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005413 for_each_possible_cpu(cpu) {
5414 struct memcg_stock_pcp *stock =
5415 &per_cpu(memcg_stock, cpu);
5416 INIT_WORK(&stock->work, drain_local_stock);
5417 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005418 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005419 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005420 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005421 memcg->use_hierarchy = parent->use_hierarchy;
5422 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005423 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005424
Balbir Singh18f59ea2009-01-07 18:08:07 -08005425 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005426 res_counter_init(&memcg->res, &parent->res);
5427 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005428 res_counter_init(&memcg->kmem, &parent->kmem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005429 /*
5430 * We increment refcnt of the parent to ensure that we can
5431 * safely access it on res_counter_charge/uncharge.
5432 * This refcnt will be decremented when freeing this
5433 * mem_cgroup(see mem_cgroup_put).
5434 */
5435 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005436 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005437 res_counter_init(&memcg->res, NULL);
5438 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005439 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005440 /*
5441 * Deeper hierachy with use_hierarchy == false doesn't make
5442 * much sense so let cgroup subsystem know about this
5443 * unfortunate state in our controller.
5444 */
5445 if (parent && parent != root_mem_cgroup)
5446 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005447 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005448 memcg->last_scanned_node = MAX_NUMNODES;
5449 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005450
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005451 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005452 memcg->swappiness = mem_cgroup_swappiness(parent);
5453 atomic_set(&memcg->refcnt, 1);
5454 memcg->move_charge_at_immigrate = 0;
5455 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005456 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005457
5458 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5459 if (error) {
5460 /*
5461 * We call put now because our (and parent's) refcnts
5462 * are already in place. mem_cgroup_put() will internally
5463 * call __mem_cgroup_free, so return directly
5464 */
5465 mem_cgroup_put(memcg);
5466 return ERR_PTR(error);
5467 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005468 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005469free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005470 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005471 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005472}
5473
Tejun Heo92fb9742012-11-19 08:13:38 -08005474static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005475{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005476 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005477
Michal Hockoab5196c2012-10-26 13:37:32 +02005478 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005479}
5480
Tejun Heo92fb9742012-11-19 08:13:38 -08005481static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005482{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005483 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005484
Glauber Costa1d62e432012-04-09 19:36:33 -03005485 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005486
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005487 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005488}
5489
Daisuke Nishimura02491442010-03-10 15:22:17 -08005490#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005491/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005492#define PRECHARGE_COUNT_AT_ONCE 256
5493static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005494{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005495 int ret = 0;
5496 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005497 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005498
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005499 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005500 mc.precharge += count;
5501 /* we don't need css_get for root */
5502 return ret;
5503 }
5504 /* try to charge at once */
5505 if (count > 1) {
5506 struct res_counter *dummy;
5507 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005508 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005509 * by cgroup_lock_live_cgroup() that it is not removed and we
5510 * are still under the same cgroup_mutex. So we can postpone
5511 * css_get().
5512 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005513 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005514 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005515 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005516 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005517 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005518 goto one_by_one;
5519 }
5520 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005521 return ret;
5522 }
5523one_by_one:
5524 /* fall back to one by one charge */
5525 while (count--) {
5526 if (signal_pending(current)) {
5527 ret = -EINTR;
5528 break;
5529 }
5530 if (!batch_count--) {
5531 batch_count = PRECHARGE_COUNT_AT_ONCE;
5532 cond_resched();
5533 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005534 ret = __mem_cgroup_try_charge(NULL,
5535 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005536 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005537 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005538 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005539 mc.precharge++;
5540 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005541 return ret;
5542}
5543
5544/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005545 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005546 * @vma: the vma the pte to be checked belongs
5547 * @addr: the address corresponding to the pte to be checked
5548 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005549 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005550 *
5551 * Returns
5552 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5553 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5554 * move charge. if @target is not NULL, the page is stored in target->page
5555 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005556 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5557 * target for charge migration. if @target is not NULL, the entry is stored
5558 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005559 *
5560 * Called with pte lock held.
5561 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005562union mc_target {
5563 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005564 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005565};
5566
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005567enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005568 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005569 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005570 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005571};
5572
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005573static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5574 unsigned long addr, pte_t ptent)
5575{
5576 struct page *page = vm_normal_page(vma, addr, ptent);
5577
5578 if (!page || !page_mapped(page))
5579 return NULL;
5580 if (PageAnon(page)) {
5581 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005582 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005583 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005584 } else if (!move_file())
5585 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005586 return NULL;
5587 if (!get_page_unless_zero(page))
5588 return NULL;
5589
5590 return page;
5591}
5592
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005593#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005594static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5595 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5596{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005597 struct page *page = NULL;
5598 swp_entry_t ent = pte_to_swp_entry(ptent);
5599
5600 if (!move_anon() || non_swap_entry(ent))
5601 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005602 /*
5603 * Because lookup_swap_cache() updates some statistics counter,
5604 * we call find_get_page() with swapper_space directly.
5605 */
5606 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005607 if (do_swap_account)
5608 entry->val = ent.val;
5609
5610 return page;
5611}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005612#else
5613static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5614 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5615{
5616 return NULL;
5617}
5618#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005619
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005620static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5621 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5622{
5623 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005624 struct address_space *mapping;
5625 pgoff_t pgoff;
5626
5627 if (!vma->vm_file) /* anonymous vma */
5628 return NULL;
5629 if (!move_file())
5630 return NULL;
5631
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005632 mapping = vma->vm_file->f_mapping;
5633 if (pte_none(ptent))
5634 pgoff = linear_page_index(vma, addr);
5635 else /* pte_file(ptent) is true */
5636 pgoff = pte_to_pgoff(ptent);
5637
5638 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005639 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005640
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005641#ifdef CONFIG_SWAP
5642 /* shmem/tmpfs may report page out on swap: account for that too. */
5643 if (radix_tree_exceptional_entry(page)) {
5644 swp_entry_t swap = radix_to_swp_entry(page);
5645 if (do_swap_account)
5646 *entry = swap;
5647 page = find_get_page(&swapper_space, swap.val);
5648 }
5649#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005650 return page;
5651}
5652
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005653static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005654 unsigned long addr, pte_t ptent, union mc_target *target)
5655{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005656 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005657 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005658 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005659 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005660
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005661 if (pte_present(ptent))
5662 page = mc_handle_present_pte(vma, addr, ptent);
5663 else if (is_swap_pte(ptent))
5664 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005665 else if (pte_none(ptent) || pte_file(ptent))
5666 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005667
5668 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005669 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005670 if (page) {
5671 pc = lookup_page_cgroup(page);
5672 /*
5673 * Do only loose check w/o page_cgroup lock.
5674 * mem_cgroup_move_account() checks the pc is valid or not under
5675 * the lock.
5676 */
5677 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5678 ret = MC_TARGET_PAGE;
5679 if (target)
5680 target->page = page;
5681 }
5682 if (!ret || !target)
5683 put_page(page);
5684 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005685 /* There is a swap entry and a page doesn't exist or isn't charged */
5686 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005687 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005688 ret = MC_TARGET_SWAP;
5689 if (target)
5690 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005691 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005692 return ret;
5693}
5694
Naoya Horiguchi12724852012-03-21 16:34:28 -07005695#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5696/*
5697 * We don't consider swapping or file mapped pages because THP does not
5698 * support them for now.
5699 * Caller should make sure that pmd_trans_huge(pmd) is true.
5700 */
5701static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5702 unsigned long addr, pmd_t pmd, union mc_target *target)
5703{
5704 struct page *page = NULL;
5705 struct page_cgroup *pc;
5706 enum mc_target_type ret = MC_TARGET_NONE;
5707
5708 page = pmd_page(pmd);
5709 VM_BUG_ON(!page || !PageHead(page));
5710 if (!move_anon())
5711 return ret;
5712 pc = lookup_page_cgroup(page);
5713 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5714 ret = MC_TARGET_PAGE;
5715 if (target) {
5716 get_page(page);
5717 target->page = page;
5718 }
5719 }
5720 return ret;
5721}
5722#else
5723static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5724 unsigned long addr, pmd_t pmd, union mc_target *target)
5725{
5726 return MC_TARGET_NONE;
5727}
5728#endif
5729
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005730static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5731 unsigned long addr, unsigned long end,
5732 struct mm_walk *walk)
5733{
5734 struct vm_area_struct *vma = walk->private;
5735 pte_t *pte;
5736 spinlock_t *ptl;
5737
Naoya Horiguchi12724852012-03-21 16:34:28 -07005738 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5739 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5740 mc.precharge += HPAGE_PMD_NR;
5741 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005742 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005743 }
Dave Hansen03319322011-03-22 16:32:56 -07005744
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005745 if (pmd_trans_unstable(pmd))
5746 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005747 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5748 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005749 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005750 mc.precharge++; /* increment precharge temporarily */
5751 pte_unmap_unlock(pte - 1, ptl);
5752 cond_resched();
5753
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005754 return 0;
5755}
5756
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005757static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5758{
5759 unsigned long precharge;
5760 struct vm_area_struct *vma;
5761
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005762 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005763 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5764 struct mm_walk mem_cgroup_count_precharge_walk = {
5765 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5766 .mm = mm,
5767 .private = vma,
5768 };
5769 if (is_vm_hugetlb_page(vma))
5770 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005771 walk_page_range(vma->vm_start, vma->vm_end,
5772 &mem_cgroup_count_precharge_walk);
5773 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005774 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005775
5776 precharge = mc.precharge;
5777 mc.precharge = 0;
5778
5779 return precharge;
5780}
5781
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005782static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5783{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005784 unsigned long precharge = mem_cgroup_count_precharge(mm);
5785
5786 VM_BUG_ON(mc.moving_task);
5787 mc.moving_task = current;
5788 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005789}
5790
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005791/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5792static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005793{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005794 struct mem_cgroup *from = mc.from;
5795 struct mem_cgroup *to = mc.to;
5796
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005797 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005798 if (mc.precharge) {
5799 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5800 mc.precharge = 0;
5801 }
5802 /*
5803 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5804 * we must uncharge here.
5805 */
5806 if (mc.moved_charge) {
5807 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5808 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005809 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005810 /* we must fixup refcnts and charges */
5811 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005812 /* uncharge swap account from the old cgroup */
5813 if (!mem_cgroup_is_root(mc.from))
5814 res_counter_uncharge(&mc.from->memsw,
5815 PAGE_SIZE * mc.moved_swap);
5816 __mem_cgroup_put(mc.from, mc.moved_swap);
5817
5818 if (!mem_cgroup_is_root(mc.to)) {
5819 /*
5820 * we charged both to->res and to->memsw, so we should
5821 * uncharge to->res.
5822 */
5823 res_counter_uncharge(&mc.to->res,
5824 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005825 }
5826 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005827 mc.moved_swap = 0;
5828 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005829 memcg_oom_recover(from);
5830 memcg_oom_recover(to);
5831 wake_up_all(&mc.waitq);
5832}
5833
5834static void mem_cgroup_clear_mc(void)
5835{
5836 struct mem_cgroup *from = mc.from;
5837
5838 /*
5839 * we must clear moving_task before waking up waiters at the end of
5840 * task migration.
5841 */
5842 mc.moving_task = NULL;
5843 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005844 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005845 mc.from = NULL;
5846 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005847 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005848 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005849}
5850
Li Zefan761b3ef2012-01-31 13:47:36 +08005851static int mem_cgroup_can_attach(struct cgroup *cgroup,
5852 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005853{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005854 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005855 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005856 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005857
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005858 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005859 struct mm_struct *mm;
5860 struct mem_cgroup *from = mem_cgroup_from_task(p);
5861
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005862 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005863
5864 mm = get_task_mm(p);
5865 if (!mm)
5866 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005867 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005868 if (mm->owner == p) {
5869 VM_BUG_ON(mc.from);
5870 VM_BUG_ON(mc.to);
5871 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005872 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005873 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005874 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005875 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005876 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005877 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005878 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005879 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005880
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005881 ret = mem_cgroup_precharge_mc(mm);
5882 if (ret)
5883 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005884 }
5885 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005886 }
5887 return ret;
5888}
5889
Li Zefan761b3ef2012-01-31 13:47:36 +08005890static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5891 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005892{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005893 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005894}
5895
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005896static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5897 unsigned long addr, unsigned long end,
5898 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005899{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005900 int ret = 0;
5901 struct vm_area_struct *vma = walk->private;
5902 pte_t *pte;
5903 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005904 enum mc_target_type target_type;
5905 union mc_target target;
5906 struct page *page;
5907 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005908
Naoya Horiguchi12724852012-03-21 16:34:28 -07005909 /*
5910 * We don't take compound_lock() here but no race with splitting thp
5911 * happens because:
5912 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5913 * under splitting, which means there's no concurrent thp split,
5914 * - if another thread runs into split_huge_page() just after we
5915 * entered this if-block, the thread must wait for page table lock
5916 * to be unlocked in __split_huge_page_splitting(), where the main
5917 * part of thp split is not executed yet.
5918 */
5919 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005920 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005921 spin_unlock(&vma->vm_mm->page_table_lock);
5922 return 0;
5923 }
5924 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5925 if (target_type == MC_TARGET_PAGE) {
5926 page = target.page;
5927 if (!isolate_lru_page(page)) {
5928 pc = lookup_page_cgroup(page);
5929 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005930 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005931 mc.precharge -= HPAGE_PMD_NR;
5932 mc.moved_charge += HPAGE_PMD_NR;
5933 }
5934 putback_lru_page(page);
5935 }
5936 put_page(page);
5937 }
5938 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005939 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005940 }
5941
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005942 if (pmd_trans_unstable(pmd))
5943 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005944retry:
5945 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5946 for (; addr != end; addr += PAGE_SIZE) {
5947 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005948 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005949
5950 if (!mc.precharge)
5951 break;
5952
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005953 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005954 case MC_TARGET_PAGE:
5955 page = target.page;
5956 if (isolate_lru_page(page))
5957 goto put;
5958 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005959 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005960 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005961 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005962 /* we uncharge from mc.from later. */
5963 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005964 }
5965 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005966put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005967 put_page(page);
5968 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005969 case MC_TARGET_SWAP:
5970 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005971 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005972 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005973 /* we fixup refcnts and charges later. */
5974 mc.moved_swap++;
5975 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005976 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005977 default:
5978 break;
5979 }
5980 }
5981 pte_unmap_unlock(pte - 1, ptl);
5982 cond_resched();
5983
5984 if (addr != end) {
5985 /*
5986 * We have consumed all precharges we got in can_attach().
5987 * We try charge one by one, but don't do any additional
5988 * charges to mc.to if we have failed in charge once in attach()
5989 * phase.
5990 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005991 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005992 if (!ret)
5993 goto retry;
5994 }
5995
5996 return ret;
5997}
5998
5999static void mem_cgroup_move_charge(struct mm_struct *mm)
6000{
6001 struct vm_area_struct *vma;
6002
6003 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006004retry:
6005 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6006 /*
6007 * Someone who are holding the mmap_sem might be waiting in
6008 * waitq. So we cancel all extra charges, wake up all waiters,
6009 * and retry. Because we cancel precharges, we might not be able
6010 * to move enough charges, but moving charge is a best-effort
6011 * feature anyway, so it wouldn't be a big problem.
6012 */
6013 __mem_cgroup_clear_mc();
6014 cond_resched();
6015 goto retry;
6016 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006017 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6018 int ret;
6019 struct mm_walk mem_cgroup_move_charge_walk = {
6020 .pmd_entry = mem_cgroup_move_charge_pte_range,
6021 .mm = mm,
6022 .private = vma,
6023 };
6024 if (is_vm_hugetlb_page(vma))
6025 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006026 ret = walk_page_range(vma->vm_start, vma->vm_end,
6027 &mem_cgroup_move_charge_walk);
6028 if (ret)
6029 /*
6030 * means we have consumed all precharges and failed in
6031 * doing additional charge. Just abandon here.
6032 */
6033 break;
6034 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006035 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006036}
6037
Li Zefan761b3ef2012-01-31 13:47:36 +08006038static void mem_cgroup_move_task(struct cgroup *cont,
6039 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006040{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006041 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006042 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006043
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006044 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006045 if (mc.to)
6046 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006047 mmput(mm);
6048 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006049 if (mc.to)
6050 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006051}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006052#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006053static int mem_cgroup_can_attach(struct cgroup *cgroup,
6054 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006055{
6056 return 0;
6057}
Li Zefan761b3ef2012-01-31 13:47:36 +08006058static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6059 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006060{
6061}
Li Zefan761b3ef2012-01-31 13:47:36 +08006062static void mem_cgroup_move_task(struct cgroup *cont,
6063 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006064{
6065}
6066#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006067
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006068struct cgroup_subsys mem_cgroup_subsys = {
6069 .name = "memory",
6070 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006071 .css_alloc = mem_cgroup_css_alloc,
6072 .css_offline = mem_cgroup_css_offline,
6073 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006074 .can_attach = mem_cgroup_can_attach,
6075 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006076 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006077 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006078 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006079 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006080};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006081
Andrew Mortonc255a452012-07-31 16:43:02 -07006082#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006083static int __init enable_swap_account(char *s)
6084{
6085 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006086 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006087 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006088 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006089 really_do_swap_account = 0;
6090 return 1;
6091}
Michal Hockoa2c89902011-05-24 17:12:50 -07006092__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006093
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006094#endif