blob: 00ce03e8a277de7fb346855c6ab5843d7202df1d [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
Sha Zhengju58cf1882013-02-22 16:32:05 -0800123static const char * const mem_cgroup_lru_names[] = {
124 "inactive_anon",
125 "active_anon",
126 "inactive_file",
127 "active_file",
128 "unevictable",
129};
130
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700131/*
132 * Per memcg event counter is incremented at every pagein/pageout. With THP,
133 * it will be incremated by the number of pages. This counter is used for
134 * for trigger some periodic events. This is straightforward and better
135 * than using jiffies etc. to handle periodic memcg event.
136 */
137enum mem_cgroup_events_target {
138 MEM_CGROUP_TARGET_THRESH,
139 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700140 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700141 MEM_CGROUP_NTARGETS,
142};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700143#define THRESHOLDS_EVENTS_TARGET 128
144#define SOFTLIMIT_EVENTS_TARGET 1024
145#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700146
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800147struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700148 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700149 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700150 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700151 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800152};
153
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800154struct mem_cgroup_reclaim_iter {
155 /* css_id of the last scanned hierarchy member */
156 int position;
157 /* scan generation, increased every round-trip */
158 unsigned int generation;
159};
160
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800161/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800162 * per-zone information in memory controller.
163 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800165 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700166 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800167
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800168 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
169
Balbir Singhf64c3f52009-09-23 15:56:37 -0700170 struct rb_node tree_node; /* RB tree node */
171 unsigned long long usage_in_excess;/* Set to the value by which */
172 /* the soft limit is exceeded*/
173 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700174 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700175 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800176};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800177
178struct mem_cgroup_per_node {
179 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
180};
181
182struct mem_cgroup_lru_info {
183 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
184};
185
186/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700187 * Cgroups above their limits are maintained in a RB-Tree, independent of
188 * their hierarchy representation
189 */
190
191struct mem_cgroup_tree_per_zone {
192 struct rb_root rb_root;
193 spinlock_t lock;
194};
195
196struct mem_cgroup_tree_per_node {
197 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
198};
199
200struct mem_cgroup_tree {
201 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
202};
203
204static struct mem_cgroup_tree soft_limit_tree __read_mostly;
205
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206struct mem_cgroup_threshold {
207 struct eventfd_ctx *eventfd;
208 u64 threshold;
209};
210
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700211/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800212struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700213 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700214 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215 /* Size of entries[] */
216 unsigned int size;
217 /* Array of thresholds */
218 struct mem_cgroup_threshold entries[0];
219};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700220
221struct mem_cgroup_thresholds {
222 /* Primary thresholds array */
223 struct mem_cgroup_threshold_ary *primary;
224 /*
225 * Spare threshold array.
226 * This is needed to make mem_cgroup_unregister_event() "never fail".
227 * It must be able to store at least primary->size - 1 entries.
228 */
229 struct mem_cgroup_threshold_ary *spare;
230};
231
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700232/* for OOM */
233struct mem_cgroup_eventfd_list {
234 struct list_head list;
235 struct eventfd_ctx *eventfd;
236};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800237
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700238static void mem_cgroup_threshold(struct mem_cgroup *memcg);
239static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800240
Balbir Singhf64c3f52009-09-23 15:56:37 -0700241/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800242 * The memory controller data structure. The memory controller controls both
243 * page cache and RSS per cgroup. We would eventually like to provide
244 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
245 * to help the administrator determine what knobs to tune.
246 *
247 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800248 * we hit the water mark. May be even add a low water mark, such that
249 * no reclaim occurs from a cgroup at it's low water mark, this is
250 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800251 */
252struct mem_cgroup {
253 struct cgroup_subsys_state css;
254 /*
255 * the counter to account for memory usage
256 */
257 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700258
259 union {
260 /*
261 * the counter to account for mem+swap usage.
262 */
263 struct res_counter memsw;
264
265 /*
266 * rcu_freeing is used only when freeing struct mem_cgroup,
267 * so put it into a union to avoid wasting more memory.
268 * It must be disjoint from the css field. It could be
269 * in a union with the res field, but res plays a much
270 * larger part in mem_cgroup life than memsw, and might
271 * be of interest, even at time of free, when debugging.
272 * So share rcu_head with the less interesting memsw.
273 */
274 struct rcu_head rcu_freeing;
275 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700276 * We also need some space for a worker in deferred freeing.
277 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700278 */
279 struct work_struct work_freeing;
280 };
281
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800282 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800283 * the counter to account for kernel memory usage.
284 */
285 struct res_counter kmem;
286 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800287 * Per cgroup active and inactive list, similar to the
288 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800289 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800290 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700291 int last_scanned_node;
292#if MAX_NUMNODES > 1
293 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700294 atomic_t numainfo_events;
295 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700296#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800297 /*
298 * Should the accounting and control be hierarchical, per subtree?
299 */
300 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800301 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700302
303 bool oom_lock;
304 atomic_t under_oom;
305
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800306 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800307
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700308 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700309 /* OOM-Killer disable */
310 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800311
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700312 /* set when res.limit == memsw.limit */
313 bool memsw_is_minimum;
314
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800315 /* protect arrays of thresholds */
316 struct mutex thresholds_lock;
317
318 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700319 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700320
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800321 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700322 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700323
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700324 /* For oom notifier event fd */
325 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700326
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800327 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800328 * Should we move charges of a task when a task is moved into this
329 * mem_cgroup ? And what type of charges should we move ?
330 */
331 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800332 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700333 * set > 0 if pages under this cgroup are moving to other cgroup.
334 */
335 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700336 /* taken only while moving_account > 0 */
337 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700338 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800339 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800340 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700341 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700342 /*
343 * used when a cpu is offlined or other synchronizations
344 * See mem_cgroup_read_stat().
345 */
346 struct mem_cgroup_stat_cpu nocpu_base;
347 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000348
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700349#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000350 struct tcp_memcontrol tcp_mem;
351#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800352#if defined(CONFIG_MEMCG_KMEM)
353 /* analogous to slab_common's slab_caches list. per-memcg */
354 struct list_head memcg_slab_caches;
355 /* Not a spinlock, we can take a lot of time walking the list */
356 struct mutex slab_caches_mutex;
357 /* Index in the kmem_cache->memcg_params->memcg_caches array */
358 int kmemcg_id;
359#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800360};
361
Glauber Costa510fc4e2012-12-18 14:21:47 -0800362/* internal only representation about the status of kmem accounting. */
363enum {
364 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800365 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800366 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800367};
368
Glauber Costaa8964b92012-12-18 14:22:09 -0800369/* We account when limit is on, but only after call sites are patched */
370#define KMEM_ACCOUNTED_MASK \
371 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800372
373#ifdef CONFIG_MEMCG_KMEM
374static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
375{
376 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
377}
Glauber Costa7de37682012-12-18 14:22:07 -0800378
379static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
380{
381 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
382}
383
Glauber Costaa8964b92012-12-18 14:22:09 -0800384static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
385{
386 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
387}
388
Glauber Costa55007d82012-12-18 14:22:38 -0800389static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
390{
391 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
392}
393
Glauber Costa7de37682012-12-18 14:22:07 -0800394static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
395{
396 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
397 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
398}
399
400static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
401{
402 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
403 &memcg->kmem_account_flags);
404}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800405#endif
406
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800407/* Stuffs for move charges at task migration. */
408/*
409 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
410 * left-shifted bitmap of these types.
411 */
412enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800413 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700414 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800415 NR_MOVE_TYPE,
416};
417
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800418/* "mc" and its members are protected by cgroup_mutex */
419static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800420 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800421 struct mem_cgroup *from;
422 struct mem_cgroup *to;
423 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800424 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800425 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800426 struct task_struct *moving_task; /* a task moving charges */
427 wait_queue_head_t waitq; /* a waitq for other context */
428} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700429 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800430 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
431};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800432
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700433static bool move_anon(void)
434{
435 return test_bit(MOVE_CHARGE_TYPE_ANON,
436 &mc.to->move_charge_at_immigrate);
437}
438
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700439static bool move_file(void)
440{
441 return test_bit(MOVE_CHARGE_TYPE_FILE,
442 &mc.to->move_charge_at_immigrate);
443}
444
Balbir Singh4e416952009-09-23 15:56:39 -0700445/*
446 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
447 * limit reclaim to prevent infinite loops, if they ever occur.
448 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700449#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
450#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700451
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800452enum charge_type {
453 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700454 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800455 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700456 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700457 NR_CHARGE_TYPE,
458};
459
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800460/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800461enum res_type {
462 _MEM,
463 _MEMSWAP,
464 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800465 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800466};
467
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700468#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
469#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800470#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700471/* Used for OOM nofiier */
472#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800473
Balbir Singh75822b42009-09-23 15:56:38 -0700474/*
475 * Reclaim flags for mem_cgroup_hierarchical_reclaim
476 */
477#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
478#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
479#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
480#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
481
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700482static void mem_cgroup_get(struct mem_cgroup *memcg);
483static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000484
Wanpeng Lib2145142012-07-31 16:46:01 -0700485static inline
486struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
487{
488 return container_of(s, struct mem_cgroup, css);
489}
490
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700491static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
492{
493 return (memcg == root_mem_cgroup);
494}
495
Glauber Costae1aab162011-12-11 21:47:03 +0000496/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700497#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000498
Glauber Costae1aab162011-12-11 21:47:03 +0000499void sock_update_memcg(struct sock *sk)
500{
Glauber Costa376be5f2012-01-20 04:57:14 +0000501 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000502 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700503 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000504
505 BUG_ON(!sk->sk_prot->proto_cgroup);
506
Glauber Costaf3f511e2012-01-05 20:16:39 +0000507 /* Socket cloning can throw us here with sk_cgrp already
508 * filled. It won't however, necessarily happen from
509 * process context. So the test for root memcg given
510 * the current task's memcg won't help us in this case.
511 *
512 * Respecting the original socket's memcg is a better
513 * decision in this case.
514 */
515 if (sk->sk_cgrp) {
516 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
517 mem_cgroup_get(sk->sk_cgrp->memcg);
518 return;
519 }
520
Glauber Costae1aab162011-12-11 21:47:03 +0000521 rcu_read_lock();
522 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700523 cg_proto = sk->sk_prot->proto_cgroup(memcg);
524 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000525 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700526 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000527 }
528 rcu_read_unlock();
529 }
530}
531EXPORT_SYMBOL(sock_update_memcg);
532
533void sock_release_memcg(struct sock *sk)
534{
Glauber Costa376be5f2012-01-20 04:57:14 +0000535 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000536 struct mem_cgroup *memcg;
537 WARN_ON(!sk->sk_cgrp->memcg);
538 memcg = sk->sk_cgrp->memcg;
539 mem_cgroup_put(memcg);
540 }
541}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000542
543struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
544{
545 if (!memcg || mem_cgroup_is_root(memcg))
546 return NULL;
547
548 return &memcg->tcp_mem.cg_proto;
549}
550EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000551
Glauber Costa3f134612012-05-29 15:07:11 -0700552static void disarm_sock_keys(struct mem_cgroup *memcg)
553{
554 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
555 return;
556 static_key_slow_dec(&memcg_socket_limit_enabled);
557}
558#else
559static void disarm_sock_keys(struct mem_cgroup *memcg)
560{
561}
562#endif
563
Glauber Costaa8964b92012-12-18 14:22:09 -0800564#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800565/*
566 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
567 * There are two main reasons for not using the css_id for this:
568 * 1) this works better in sparse environments, where we have a lot of memcgs,
569 * but only a few kmem-limited. Or also, if we have, for instance, 200
570 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
571 * 200 entry array for that.
572 *
573 * 2) In order not to violate the cgroup API, we would like to do all memory
574 * allocation in ->create(). At that point, we haven't yet allocated the
575 * css_id. Having a separate index prevents us from messing with the cgroup
576 * core for this
577 *
578 * The current size of the caches array is stored in
579 * memcg_limited_groups_array_size. It will double each time we have to
580 * increase it.
581 */
582static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800583int memcg_limited_groups_array_size;
584
Glauber Costa55007d82012-12-18 14:22:38 -0800585/*
586 * MIN_SIZE is different than 1, because we would like to avoid going through
587 * the alloc/free process all the time. In a small machine, 4 kmem-limited
588 * cgroups is a reasonable guess. In the future, it could be a parameter or
589 * tunable, but that is strictly not necessary.
590 *
591 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
592 * this constant directly from cgroup, but it is understandable that this is
593 * better kept as an internal representation in cgroup.c. In any case, the
594 * css_id space is not getting any smaller, and we don't have to necessarily
595 * increase ours as well if it increases.
596 */
597#define MEMCG_CACHES_MIN_SIZE 4
598#define MEMCG_CACHES_MAX_SIZE 65535
599
Glauber Costad7f25f82012-12-18 14:22:40 -0800600/*
601 * A lot of the calls to the cache allocation functions are expected to be
602 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
603 * conditional to this static branch, we'll have to allow modules that does
604 * kmem_cache_alloc and the such to see this symbol as well
605 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800606struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800607EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800608
609static void disarm_kmem_keys(struct mem_cgroup *memcg)
610{
Glauber Costa55007d82012-12-18 14:22:38 -0800611 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800612 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800613 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
614 }
Glauber Costabea207c2012-12-18 14:22:11 -0800615 /*
616 * This check can't live in kmem destruction function,
617 * since the charges will outlive the cgroup
618 */
619 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800620}
621#else
622static void disarm_kmem_keys(struct mem_cgroup *memcg)
623{
624}
625#endif /* CONFIG_MEMCG_KMEM */
626
627static void disarm_static_keys(struct mem_cgroup *memcg)
628{
629 disarm_sock_keys(memcg);
630 disarm_kmem_keys(memcg);
631}
632
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700633static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800634
Balbir Singhf64c3f52009-09-23 15:56:37 -0700635static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700636mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700637{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700638 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700639}
640
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700641struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100642{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700643 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100644}
645
Balbir Singhf64c3f52009-09-23 15:56:37 -0700646static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700647page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700648{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700649 int nid = page_to_nid(page);
650 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653}
654
655static struct mem_cgroup_tree_per_zone *
656soft_limit_tree_node_zone(int nid, int zid)
657{
658 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
659}
660
661static struct mem_cgroup_tree_per_zone *
662soft_limit_tree_from_page(struct page *page)
663{
664 int nid = page_to_nid(page);
665 int zid = page_zonenum(page);
666
667 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
668}
669
670static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700671__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700672 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700673 struct mem_cgroup_tree_per_zone *mctz,
674 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700675{
676 struct rb_node **p = &mctz->rb_root.rb_node;
677 struct rb_node *parent = NULL;
678 struct mem_cgroup_per_zone *mz_node;
679
680 if (mz->on_tree)
681 return;
682
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700683 mz->usage_in_excess = new_usage_in_excess;
684 if (!mz->usage_in_excess)
685 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700686 while (*p) {
687 parent = *p;
688 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
689 tree_node);
690 if (mz->usage_in_excess < mz_node->usage_in_excess)
691 p = &(*p)->rb_left;
692 /*
693 * We can't avoid mem cgroups that are over their soft
694 * limit by the same amount
695 */
696 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
697 p = &(*p)->rb_right;
698 }
699 rb_link_node(&mz->tree_node, parent, p);
700 rb_insert_color(&mz->tree_node, &mctz->rb_root);
701 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700702}
703
704static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700705__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700706 struct mem_cgroup_per_zone *mz,
707 struct mem_cgroup_tree_per_zone *mctz)
708{
709 if (!mz->on_tree)
710 return;
711 rb_erase(&mz->tree_node, &mctz->rb_root);
712 mz->on_tree = false;
713}
714
715static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700716mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700717 struct mem_cgroup_per_zone *mz,
718 struct mem_cgroup_tree_per_zone *mctz)
719{
720 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700721 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700722 spin_unlock(&mctz->lock);
723}
724
Balbir Singhf64c3f52009-09-23 15:56:37 -0700725
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700726static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700727{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700728 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700729 struct mem_cgroup_per_zone *mz;
730 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700731 int nid = page_to_nid(page);
732 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700733 mctz = soft_limit_tree_from_page(page);
734
735 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700736 * Necessary to update all ancestors when hierarchy is used.
737 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700738 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700739 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
740 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
741 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700742 /*
743 * We have to update the tree if mz is on RB-tree or
744 * mem is over its softlimit.
745 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700746 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700747 spin_lock(&mctz->lock);
748 /* if on-tree, remove it */
749 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700750 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700751 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700752 * Insert again. mz->usage_in_excess will be updated.
753 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700754 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700755 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700756 spin_unlock(&mctz->lock);
757 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700758 }
759}
760
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700761static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700762{
763 int node, zone;
764 struct mem_cgroup_per_zone *mz;
765 struct mem_cgroup_tree_per_zone *mctz;
766
Bob Liu3ed28fa2012-01-12 17:19:04 -0800767 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700768 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700769 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700770 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700771 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700772 }
773 }
774}
775
Balbir Singh4e416952009-09-23 15:56:39 -0700776static struct mem_cgroup_per_zone *
777__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
778{
779 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700780 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700781
782retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700783 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700784 rightmost = rb_last(&mctz->rb_root);
785 if (!rightmost)
786 goto done; /* Nothing to reclaim from */
787
788 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
789 /*
790 * Remove the node now but someone else can add it back,
791 * we will to add it back at the end of reclaim to its correct
792 * position in the tree.
793 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700794 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
795 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
796 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700797 goto retry;
798done:
799 return mz;
800}
801
802static struct mem_cgroup_per_zone *
803mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
804{
805 struct mem_cgroup_per_zone *mz;
806
807 spin_lock(&mctz->lock);
808 mz = __mem_cgroup_largest_soft_limit_node(mctz);
809 spin_unlock(&mctz->lock);
810 return mz;
811}
812
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700813/*
814 * Implementation Note: reading percpu statistics for memcg.
815 *
816 * Both of vmstat[] and percpu_counter has threshold and do periodic
817 * synchronization to implement "quick" read. There are trade-off between
818 * reading cost and precision of value. Then, we may have a chance to implement
819 * a periodic synchronizion of counter in memcg's counter.
820 *
821 * But this _read() function is used for user interface now. The user accounts
822 * memory usage by memory cgroup and he _always_ requires exact value because
823 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
824 * have to visit all online cpus and make sum. So, for now, unnecessary
825 * synchronization is not implemented. (just implemented for cpu hotplug)
826 *
827 * If there are kernel internal actions which can make use of some not-exact
828 * value, and reading all cpu value can be performance bottleneck in some
829 * common workload, threashold and synchonization as vmstat[] should be
830 * implemented.
831 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700832static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700833 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800834{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700835 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800836 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800837
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700838 get_online_cpus();
839 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700840 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700841#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842 spin_lock(&memcg->pcp_counter_lock);
843 val += memcg->nocpu_base.count[idx];
844 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700845#endif
846 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800847 return val;
848}
849
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700850static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700851 bool charge)
852{
853 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700854 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700855}
856
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700857static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700858 enum mem_cgroup_events_index idx)
859{
860 unsigned long val = 0;
861 int cpu;
862
863 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700864 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700865#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700866 spin_lock(&memcg->pcp_counter_lock);
867 val += memcg->nocpu_base.events[idx];
868 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700869#endif
870 return val;
871}
872
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700873static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700874 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800875{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800876 preempt_disable();
877
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700878 /*
879 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
880 * counted as CACHE even if it's on ANON LRU.
881 */
882 if (anon)
883 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800885 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700886 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700887 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700888
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800889 /* pagein of a big page is an event. So, ignore page size */
890 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700891 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800892 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700893 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800894 nr_pages = -nr_pages; /* for event */
895 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800896
Johannes Weiner13114712012-05-29 15:07:07 -0700897 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800898
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800899 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800900}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800901
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700902unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700903mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700904{
905 struct mem_cgroup_per_zone *mz;
906
907 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
908 return mz->lru_size[lru];
909}
910
911static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700912mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700913 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700914{
915 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700916 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700917 unsigned long ret = 0;
918
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700919 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700920
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700921 for_each_lru(lru) {
922 if (BIT(lru) & lru_mask)
923 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700924 }
925 return ret;
926}
927
928static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700930 int nid, unsigned int lru_mask)
931{
Ying Han889976d2011-05-26 16:25:33 -0700932 u64 total = 0;
933 int zid;
934
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700935 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700936 total += mem_cgroup_zone_nr_lru_pages(memcg,
937 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700938
Ying Han889976d2011-05-26 16:25:33 -0700939 return total;
940}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700941
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700942static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700943 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800944{
Ying Han889976d2011-05-26 16:25:33 -0700945 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800946 u64 total = 0;
947
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800948 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700949 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800950 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800951}
952
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800953static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
954 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800955{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700956 unsigned long val, next;
957
Johannes Weiner13114712012-05-29 15:07:07 -0700958 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700959 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700960 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800961 if ((long)next - (long)val < 0) {
962 switch (target) {
963 case MEM_CGROUP_TARGET_THRESH:
964 next = val + THRESHOLDS_EVENTS_TARGET;
965 break;
966 case MEM_CGROUP_TARGET_SOFTLIMIT:
967 next = val + SOFTLIMIT_EVENTS_TARGET;
968 break;
969 case MEM_CGROUP_TARGET_NUMAINFO:
970 next = val + NUMAINFO_EVENTS_TARGET;
971 break;
972 default:
973 break;
974 }
975 __this_cpu_write(memcg->stat->targets[target], next);
976 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700977 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800978 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800979}
980
981/*
982 * Check events in order.
983 *
984 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700985static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800986{
Steven Rostedt47994012011-11-02 13:38:33 -0700987 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800988 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800989 if (unlikely(mem_cgroup_event_ratelimit(memcg,
990 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800991 bool do_softlimit;
992 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800993
994 do_softlimit = mem_cgroup_event_ratelimit(memcg,
995 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700996#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800997 do_numainfo = mem_cgroup_event_ratelimit(memcg,
998 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700999#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001000 preempt_enable();
1001
1002 mem_cgroup_threshold(memcg);
1003 if (unlikely(do_softlimit))
1004 mem_cgroup_update_tree(memcg, page);
1005#if MAX_NUMNODES > 1
1006 if (unlikely(do_numainfo))
1007 atomic_inc(&memcg->numainfo_events);
1008#endif
1009 } else
1010 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001011}
1012
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001013struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001014{
Wanpeng Lib2145142012-07-31 16:46:01 -07001015 return mem_cgroup_from_css(
1016 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001017}
1018
Balbir Singhcf475ad2008-04-29 01:00:16 -07001019struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001020{
Balbir Singh31a78f22008-09-28 23:09:31 +01001021 /*
1022 * mm_update_next_owner() may clear mm->owner to NULL
1023 * if it races with swapoff, page migration, etc.
1024 * So this can be called with p == NULL.
1025 */
1026 if (unlikely(!p))
1027 return NULL;
1028
Wanpeng Lib2145142012-07-31 16:46:01 -07001029 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001030}
1031
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001032struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001033{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001034 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001035
1036 if (!mm)
1037 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001038 /*
1039 * Because we have no locks, mm->owner's may be being moved to other
1040 * cgroup. We use css_tryget() here even if this looks
1041 * pessimistic (rather than adding locks here).
1042 */
1043 rcu_read_lock();
1044 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001045 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1046 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001047 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001048 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001049 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001050 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001051}
1052
Johannes Weiner56600482012-01-12 17:17:59 -08001053/**
1054 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1055 * @root: hierarchy root
1056 * @prev: previously returned memcg, NULL on first invocation
1057 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1058 *
1059 * Returns references to children of the hierarchy below @root, or
1060 * @root itself, or %NULL after a full round-trip.
1061 *
1062 * Caller must pass the return value in @prev on subsequent
1063 * invocations for reference counting, or use mem_cgroup_iter_break()
1064 * to cancel a hierarchy walk before the round-trip is complete.
1065 *
1066 * Reclaimers can specify a zone and a priority level in @reclaim to
1067 * divide up the memcgs in the hierarchy among all concurrent
1068 * reclaimers operating on the same zone and priority.
1069 */
1070struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1071 struct mem_cgroup *prev,
1072 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001073{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001074 struct mem_cgroup *memcg = NULL;
1075 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001076
Johannes Weiner56600482012-01-12 17:17:59 -08001077 if (mem_cgroup_disabled())
1078 return NULL;
1079
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001080 if (!root)
1081 root = root_mem_cgroup;
1082
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001083 if (prev && !reclaim)
1084 id = css_id(&prev->css);
1085
1086 if (prev && prev != root)
1087 css_put(&prev->css);
1088
1089 if (!root->use_hierarchy && root != root_mem_cgroup) {
1090 if (prev)
1091 return NULL;
1092 return root;
1093 }
1094
1095 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001096 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001097 struct cgroup_subsys_state *css;
1098
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001099 if (reclaim) {
1100 int nid = zone_to_nid(reclaim->zone);
1101 int zid = zone_idx(reclaim->zone);
1102 struct mem_cgroup_per_zone *mz;
1103
1104 mz = mem_cgroup_zoneinfo(root, nid, zid);
1105 iter = &mz->reclaim_iter[reclaim->priority];
1106 if (prev && reclaim->generation != iter->generation)
1107 return NULL;
1108 id = iter->position;
1109 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001110
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001111 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001112 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1113 if (css) {
1114 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001115 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001116 } else
1117 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001118 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001119
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001120 if (reclaim) {
1121 iter->position = id;
1122 if (!css)
1123 iter->generation++;
1124 else if (!prev && memcg)
1125 reclaim->generation = iter->generation;
1126 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001127
1128 if (prev && !css)
1129 return NULL;
1130 }
1131 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001132}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001133
Johannes Weiner56600482012-01-12 17:17:59 -08001134/**
1135 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1136 * @root: hierarchy root
1137 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1138 */
1139void mem_cgroup_iter_break(struct mem_cgroup *root,
1140 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001141{
1142 if (!root)
1143 root = root_mem_cgroup;
1144 if (prev && prev != root)
1145 css_put(&prev->css);
1146}
1147
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001148/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001149 * Iteration constructs for visiting all cgroups (under a tree). If
1150 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1151 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001152 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001153#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001154 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001155 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001156 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001157
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001158#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001159 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001160 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001161 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001162
David Rientjes68ae5642012-12-12 13:51:57 -08001163void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001164{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001165 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001166
Ying Han456f9982011-05-26 16:25:38 -07001167 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001168 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1169 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001170 goto out;
1171
1172 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001173 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001174 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1175 break;
1176 case PGMAJFAULT:
1177 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001178 break;
1179 default:
1180 BUG();
1181 }
1182out:
1183 rcu_read_unlock();
1184}
David Rientjes68ae5642012-12-12 13:51:57 -08001185EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001186
Johannes Weiner925b7672012-01-12 17:18:15 -08001187/**
1188 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1189 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001190 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001191 *
1192 * Returns the lru list vector holding pages for the given @zone and
1193 * @mem. This can be the global zone lruvec, if the memory controller
1194 * is disabled.
1195 */
1196struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1197 struct mem_cgroup *memcg)
1198{
1199 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001200 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001201
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001202 if (mem_cgroup_disabled()) {
1203 lruvec = &zone->lruvec;
1204 goto out;
1205 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001206
1207 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001208 lruvec = &mz->lruvec;
1209out:
1210 /*
1211 * Since a node can be onlined after the mem_cgroup was created,
1212 * we have to be prepared to initialize lruvec->zone here;
1213 * and if offlined then reonlined, we need to reinitialize it.
1214 */
1215 if (unlikely(lruvec->zone != zone))
1216 lruvec->zone = zone;
1217 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001218}
1219
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001220/*
1221 * Following LRU functions are allowed to be used without PCG_LOCK.
1222 * Operations are called by routine of global LRU independently from memcg.
1223 * What we have to take care of here is validness of pc->mem_cgroup.
1224 *
1225 * Changes to pc->mem_cgroup happens when
1226 * 1. charge
1227 * 2. moving account
1228 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1229 * It is added to LRU before charge.
1230 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1231 * When moving account, the page is not on LRU. It's isolated.
1232 */
1233
Johannes Weiner925b7672012-01-12 17:18:15 -08001234/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001235 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001236 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001237 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001238 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001239struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001240{
1241 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001242 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001243 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001244 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001245
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001246 if (mem_cgroup_disabled()) {
1247 lruvec = &zone->lruvec;
1248 goto out;
1249 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001250
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001251 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001252 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001253
1254 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001255 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001256 * an uncharged page off lru does nothing to secure
1257 * its former mem_cgroup from sudden removal.
1258 *
1259 * Our caller holds lru_lock, and PageCgroupUsed is updated
1260 * under page_cgroup lock: between them, they make all uses
1261 * of pc->mem_cgroup safe.
1262 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001263 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001264 pc->mem_cgroup = memcg = root_mem_cgroup;
1265
Johannes Weiner925b7672012-01-12 17:18:15 -08001266 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001267 lruvec = &mz->lruvec;
1268out:
1269 /*
1270 * Since a node can be onlined after the mem_cgroup was created,
1271 * we have to be prepared to initialize lruvec->zone here;
1272 * and if offlined then reonlined, we need to reinitialize it.
1273 */
1274 if (unlikely(lruvec->zone != zone))
1275 lruvec->zone = zone;
1276 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001277}
1278
1279/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001280 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1281 * @lruvec: mem_cgroup per zone lru vector
1282 * @lru: index of lru list the page is sitting on
1283 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001284 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001285 * This function must be called when a page is added to or removed from an
1286 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001287 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001288void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1289 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001290{
1291 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001292 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001293
1294 if (mem_cgroup_disabled())
1295 return;
1296
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001297 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1298 lru_size = mz->lru_size + lru;
1299 *lru_size += nr_pages;
1300 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001301}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001302
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001303/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001304 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001305 * hierarchy subtree
1306 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001307bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1308 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001309{
Johannes Weiner91c637342012-05-29 15:06:24 -07001310 if (root_memcg == memcg)
1311 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001312 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001313 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001314 return css_is_ancestor(&memcg->css, &root_memcg->css);
1315}
1316
1317static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1318 struct mem_cgroup *memcg)
1319{
1320 bool ret;
1321
Johannes Weiner91c637342012-05-29 15:06:24 -07001322 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001323 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001324 rcu_read_unlock();
1325 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001326}
1327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001328int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001329{
1330 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001331 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001332 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001333
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001334 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001335 if (p) {
1336 curr = try_get_mem_cgroup_from_mm(p->mm);
1337 task_unlock(p);
1338 } else {
1339 /*
1340 * All threads may have already detached their mm's, but the oom
1341 * killer still needs to detect if they have already been oom
1342 * killed to prevent needlessly killing additional tasks.
1343 */
1344 task_lock(task);
1345 curr = mem_cgroup_from_task(task);
1346 if (curr)
1347 css_get(&curr->css);
1348 task_unlock(task);
1349 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001350 if (!curr)
1351 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001352 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001353 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001354 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001355 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1356 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001357 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001358 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001359 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001360 return ret;
1361}
1362
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001363int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001364{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001365 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001366 unsigned long inactive;
1367 unsigned long active;
1368 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001369
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001370 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1371 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001372
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001373 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1374 if (gb)
1375 inactive_ratio = int_sqrt(10 * gb);
1376 else
1377 inactive_ratio = 1;
1378
Johannes Weiner9b272972011-11-02 13:38:23 -07001379 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001380}
1381
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001382int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001383{
1384 unsigned long active;
1385 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001386
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001387 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1388 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001389
1390 return (active > inactive);
1391}
1392
Balbir Singh6d61ef42009-01-07 18:08:06 -08001393#define mem_cgroup_from_res_counter(counter, member) \
1394 container_of(counter, struct mem_cgroup, member)
1395
Johannes Weiner19942822011-02-01 15:52:43 -08001396/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001397 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001398 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001399 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001400 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001401 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001402 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001403static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001404{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001405 unsigned long long margin;
1406
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001407 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001408 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001409 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001410 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001411}
1412
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001413int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001414{
1415 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001416
1417 /* root ? */
1418 if (cgrp->parent == NULL)
1419 return vm_swappiness;
1420
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001421 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001422}
1423
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001424/*
1425 * memcg->moving_account is used for checking possibility that some thread is
1426 * calling move_account(). When a thread on CPU-A starts moving pages under
1427 * a memcg, other threads should check memcg->moving_account under
1428 * rcu_read_lock(), like this:
1429 *
1430 * CPU-A CPU-B
1431 * rcu_read_lock()
1432 * memcg->moving_account+1 if (memcg->mocing_account)
1433 * take heavy locks.
1434 * synchronize_rcu() update something.
1435 * rcu_read_unlock()
1436 * start move here.
1437 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001438
1439/* for quick checking without looking up memcg */
1440atomic_t memcg_moving __read_mostly;
1441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001442static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001443{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001444 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001445 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001446 synchronize_rcu();
1447}
1448
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001449static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001450{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001451 /*
1452 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1453 * We check NULL in callee rather than caller.
1454 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001455 if (memcg) {
1456 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001457 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001458 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001459}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001460
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001461/*
1462 * 2 routines for checking "mem" is under move_account() or not.
1463 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001464 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1465 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001466 * pc->mem_cgroup may be overwritten.
1467 *
1468 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1469 * under hierarchy of moving cgroups. This is for
1470 * waiting at hith-memory prressure caused by "move".
1471 */
1472
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001473static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001474{
1475 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001476 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001477}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001478
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001479static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001480{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001481 struct mem_cgroup *from;
1482 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001483 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001484 /*
1485 * Unlike task_move routines, we access mc.to, mc.from not under
1486 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1487 */
1488 spin_lock(&mc.lock);
1489 from = mc.from;
1490 to = mc.to;
1491 if (!from)
1492 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001493
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001494 ret = mem_cgroup_same_or_subtree(memcg, from)
1495 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001496unlock:
1497 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001498 return ret;
1499}
1500
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001501static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001502{
1503 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001504 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001505 DEFINE_WAIT(wait);
1506 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1507 /* moving charge context might have finished. */
1508 if (mc.moving_task)
1509 schedule();
1510 finish_wait(&mc.waitq, &wait);
1511 return true;
1512 }
1513 }
1514 return false;
1515}
1516
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001517/*
1518 * Take this lock when
1519 * - a code tries to modify page's memcg while it's USED.
1520 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001521 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001522 */
1523static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1524 unsigned long *flags)
1525{
1526 spin_lock_irqsave(&memcg->move_lock, *flags);
1527}
1528
1529static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1530 unsigned long *flags)
1531{
1532 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1533}
1534
Sha Zhengju58cf1882013-02-22 16:32:05 -08001535#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001536/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001537 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001538 * @memcg: The memory cgroup that went over limit
1539 * @p: Task that is going to be killed
1540 *
1541 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1542 * enabled
1543 */
1544void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1545{
1546 struct cgroup *task_cgrp;
1547 struct cgroup *mem_cgrp;
1548 /*
1549 * Need a buffer in BSS, can't rely on allocations. The code relies
1550 * on the assumption that OOM is serialized for memory controller.
1551 * If this assumption is broken, revisit this code.
1552 */
1553 static char memcg_name[PATH_MAX];
1554 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001555 struct mem_cgroup *iter;
1556 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001557
Sha Zhengju58cf1882013-02-22 16:32:05 -08001558 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001559 return;
1560
Balbir Singhe2224322009-04-02 16:57:39 -07001561 rcu_read_lock();
1562
1563 mem_cgrp = memcg->css.cgroup;
1564 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1565
1566 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1567 if (ret < 0) {
1568 /*
1569 * Unfortunately, we are unable to convert to a useful name
1570 * But we'll still print out the usage information
1571 */
1572 rcu_read_unlock();
1573 goto done;
1574 }
1575 rcu_read_unlock();
1576
Andrew Mortond0451972013-02-22 16:32:06 -08001577 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001578
1579 rcu_read_lock();
1580 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1581 if (ret < 0) {
1582 rcu_read_unlock();
1583 goto done;
1584 }
1585 rcu_read_unlock();
1586
1587 /*
1588 * Continues from above, so we don't need an KERN_ level
1589 */
Andrew Mortond0451972013-02-22 16:32:06 -08001590 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001591done:
1592
Andrew Mortond0451972013-02-22 16:32:06 -08001593 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001594 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1595 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1596 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001597 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001598 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1599 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1600 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001601 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001602 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1603 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1604 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001605
1606 for_each_mem_cgroup_tree(iter, memcg) {
1607 pr_info("Memory cgroup stats");
1608
1609 rcu_read_lock();
1610 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1611 if (!ret)
1612 pr_cont(" for %s", memcg_name);
1613 rcu_read_unlock();
1614 pr_cont(":");
1615
1616 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1617 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1618 continue;
1619 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1620 K(mem_cgroup_read_stat(iter, i)));
1621 }
1622
1623 for (i = 0; i < NR_LRU_LISTS; i++)
1624 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1625 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1626
1627 pr_cont("\n");
1628 }
Balbir Singhe2224322009-04-02 16:57:39 -07001629}
1630
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001631/*
1632 * This function returns the number of memcg under hierarchy tree. Returns
1633 * 1(self count) if no children.
1634 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001635static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001636{
1637 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001638 struct mem_cgroup *iter;
1639
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001640 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001641 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001642 return num;
1643}
1644
Balbir Singh6d61ef42009-01-07 18:08:06 -08001645/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001646 * Return the memory (and swap, if configured) limit for a memcg.
1647 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001648static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001649{
1650 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001651
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001652 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001653
David Rientjesa63d83f2010-08-09 17:19:46 -07001654 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001655 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001656 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001657 if (mem_cgroup_swappiness(memcg)) {
1658 u64 memsw;
1659
1660 limit += total_swap_pages << PAGE_SHIFT;
1661 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1662
1663 /*
1664 * If memsw is finite and limits the amount of swap space
1665 * available to this memcg, return that limit.
1666 */
1667 limit = min(limit, memsw);
1668 }
1669
1670 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001671}
1672
David Rientjes19965462012-12-11 16:00:26 -08001673static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1674 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001675{
1676 struct mem_cgroup *iter;
1677 unsigned long chosen_points = 0;
1678 unsigned long totalpages;
1679 unsigned int points = 0;
1680 struct task_struct *chosen = NULL;
1681
David Rientjes876aafb2012-07-31 16:43:48 -07001682 /*
1683 * If current has a pending SIGKILL, then automatically select it. The
1684 * goal is to allow it to allocate so that it may quickly exit and free
1685 * its memory.
1686 */
1687 if (fatal_signal_pending(current)) {
1688 set_thread_flag(TIF_MEMDIE);
1689 return;
1690 }
1691
1692 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001693 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1694 for_each_mem_cgroup_tree(iter, memcg) {
1695 struct cgroup *cgroup = iter->css.cgroup;
1696 struct cgroup_iter it;
1697 struct task_struct *task;
1698
1699 cgroup_iter_start(cgroup, &it);
1700 while ((task = cgroup_iter_next(cgroup, &it))) {
1701 switch (oom_scan_process_thread(task, totalpages, NULL,
1702 false)) {
1703 case OOM_SCAN_SELECT:
1704 if (chosen)
1705 put_task_struct(chosen);
1706 chosen = task;
1707 chosen_points = ULONG_MAX;
1708 get_task_struct(chosen);
1709 /* fall through */
1710 case OOM_SCAN_CONTINUE:
1711 continue;
1712 case OOM_SCAN_ABORT:
1713 cgroup_iter_end(cgroup, &it);
1714 mem_cgroup_iter_break(memcg, iter);
1715 if (chosen)
1716 put_task_struct(chosen);
1717 return;
1718 case OOM_SCAN_OK:
1719 break;
1720 };
1721 points = oom_badness(task, memcg, NULL, totalpages);
1722 if (points > chosen_points) {
1723 if (chosen)
1724 put_task_struct(chosen);
1725 chosen = task;
1726 chosen_points = points;
1727 get_task_struct(chosen);
1728 }
1729 }
1730 cgroup_iter_end(cgroup, &it);
1731 }
1732
1733 if (!chosen)
1734 return;
1735 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001736 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1737 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001738}
1739
Johannes Weiner56600482012-01-12 17:17:59 -08001740static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1741 gfp_t gfp_mask,
1742 unsigned long flags)
1743{
1744 unsigned long total = 0;
1745 bool noswap = false;
1746 int loop;
1747
1748 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1749 noswap = true;
1750 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1751 noswap = true;
1752
1753 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1754 if (loop)
1755 drain_all_stock_async(memcg);
1756 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1757 /*
1758 * Allow limit shrinkers, which are triggered directly
1759 * by userspace, to catch signals and stop reclaim
1760 * after minimal progress, regardless of the margin.
1761 */
1762 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1763 break;
1764 if (mem_cgroup_margin(memcg))
1765 break;
1766 /*
1767 * If nothing was reclaimed after two attempts, there
1768 * may be no reclaimable pages in this hierarchy.
1769 */
1770 if (loop && !total)
1771 break;
1772 }
1773 return total;
1774}
1775
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001776/**
1777 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001778 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001779 * @nid: the node ID to be checked.
1780 * @noswap : specify true here if the user wants flle only information.
1781 *
1782 * This function returns whether the specified memcg contains any
1783 * reclaimable pages on a node. Returns true if there are any reclaimable
1784 * pages in the node.
1785 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001786static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001787 int nid, bool noswap)
1788{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001789 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001790 return true;
1791 if (noswap || !total_swap_pages)
1792 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001793 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001794 return true;
1795 return false;
1796
1797}
Ying Han889976d2011-05-26 16:25:33 -07001798#if MAX_NUMNODES > 1
1799
1800/*
1801 * Always updating the nodemask is not very good - even if we have an empty
1802 * list or the wrong list here, we can start from some node and traverse all
1803 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1804 *
1805 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001806static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001807{
1808 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001809 /*
1810 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1811 * pagein/pageout changes since the last update.
1812 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001814 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001815 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001816 return;
1817
Ying Han889976d2011-05-26 16:25:33 -07001818 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001819 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001820
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001821 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001822
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001823 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1824 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001825 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001826
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001827 atomic_set(&memcg->numainfo_events, 0);
1828 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001829}
1830
1831/*
1832 * Selecting a node where we start reclaim from. Because what we need is just
1833 * reducing usage counter, start from anywhere is O,K. Considering
1834 * memory reclaim from current node, there are pros. and cons.
1835 *
1836 * Freeing memory from current node means freeing memory from a node which
1837 * we'll use or we've used. So, it may make LRU bad. And if several threads
1838 * hit limits, it will see a contention on a node. But freeing from remote
1839 * node means more costs for memory reclaim because of memory latency.
1840 *
1841 * Now, we use round-robin. Better algorithm is welcomed.
1842 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001843int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001844{
1845 int node;
1846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001847 mem_cgroup_may_update_nodemask(memcg);
1848 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001849
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001850 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001851 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001852 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001853 /*
1854 * We call this when we hit limit, not when pages are added to LRU.
1855 * No LRU may hold pages because all pages are UNEVICTABLE or
1856 * memcg is too small and all pages are not on LRU. In that case,
1857 * we use curret node.
1858 */
1859 if (unlikely(node == MAX_NUMNODES))
1860 node = numa_node_id();
1861
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001862 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001863 return node;
1864}
1865
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001866/*
1867 * Check all nodes whether it contains reclaimable pages or not.
1868 * For quick scan, we make use of scan_nodes. This will allow us to skip
1869 * unused nodes. But scan_nodes is lazily updated and may not cotain
1870 * enough new information. We need to do double check.
1871 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001872static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001873{
1874 int nid;
1875
1876 /*
1877 * quick check...making use of scan_node.
1878 * We can skip unused nodes.
1879 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001880 if (!nodes_empty(memcg->scan_nodes)) {
1881 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001882 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001883 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001884
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001885 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001886 return true;
1887 }
1888 }
1889 /*
1890 * Check rest of nodes.
1891 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001892 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001893 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001894 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001895 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001896 return true;
1897 }
1898 return false;
1899}
1900
Ying Han889976d2011-05-26 16:25:33 -07001901#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001903{
1904 return 0;
1905}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001906
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001907static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001908{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001909 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001910}
Ying Han889976d2011-05-26 16:25:33 -07001911#endif
1912
Johannes Weiner56600482012-01-12 17:17:59 -08001913static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1914 struct zone *zone,
1915 gfp_t gfp_mask,
1916 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001917{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001918 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001919 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001920 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001921 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001922 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001923 struct mem_cgroup_reclaim_cookie reclaim = {
1924 .zone = zone,
1925 .priority = 0,
1926 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001927
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001928 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001929
Balbir Singh4e416952009-09-23 15:56:39 -07001930 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001931 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001932 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001933 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001934 if (loop >= 2) {
1935 /*
1936 * If we have not been able to reclaim
1937 * anything, it might because there are
1938 * no reclaimable pages under this hierarchy
1939 */
Johannes Weiner56600482012-01-12 17:17:59 -08001940 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001941 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001942 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001943 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001944 * excess >> 2 is not to excessive so as to
1945 * reclaim too much, nor too less that we keep
1946 * coming back to reclaim from this cgroup
1947 */
1948 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001949 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001950 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001951 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001952 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001953 }
Johannes Weiner56600482012-01-12 17:17:59 -08001954 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001955 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001956 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1957 zone, &nr_scanned);
1958 *total_scanned += nr_scanned;
1959 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001960 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001961 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001962 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001963 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001964}
1965
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001966/*
1967 * Check OOM-Killer is already running under our hierarchy.
1968 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001969 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001970 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001971static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001972{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001973 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001974
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001975 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001976 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001977 /*
1978 * this subtree of our hierarchy is already locked
1979 * so we cannot give a lock.
1980 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001981 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001982 mem_cgroup_iter_break(memcg, iter);
1983 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001984 } else
1985 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001986 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001987
Michal Hocko79dfdac2011-07-26 16:08:23 -07001988 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001989 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001990
1991 /*
1992 * OK, we failed to lock the whole subtree so we have to clean up
1993 * what we set up to the failing subtree
1994 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001995 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001996 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001997 mem_cgroup_iter_break(memcg, iter);
1998 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001999 }
2000 iter->oom_lock = false;
2001 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002002 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002003}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002004
Michal Hocko79dfdac2011-07-26 16:08:23 -07002005/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002006 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002007 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002008static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002009{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002010 struct mem_cgroup *iter;
2011
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002012 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002013 iter->oom_lock = false;
2014 return 0;
2015}
2016
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002017static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002018{
2019 struct mem_cgroup *iter;
2020
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002021 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002022 atomic_inc(&iter->under_oom);
2023}
2024
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002025static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002026{
2027 struct mem_cgroup *iter;
2028
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002029 /*
2030 * When a new child is created while the hierarchy is under oom,
2031 * mem_cgroup_oom_lock() may not be called. We have to use
2032 * atomic_add_unless() here.
2033 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002034 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002035 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002036}
2037
Michal Hocko1af8efe2011-07-26 16:08:24 -07002038static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002039static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2040
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002041struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002042 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002043 wait_queue_t wait;
2044};
2045
2046static int memcg_oom_wake_function(wait_queue_t *wait,
2047 unsigned mode, int sync, void *arg)
2048{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002049 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2050 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002051 struct oom_wait_info *oom_wait_info;
2052
2053 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002054 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002055
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002056 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002057 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002058 * Then we can use css_is_ancestor without taking care of RCU.
2059 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002060 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2061 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002062 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002063 return autoremove_wake_function(wait, mode, sync, arg);
2064}
2065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002066static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002067{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002068 /* for filtering, pass "memcg" as argument. */
2069 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002070}
2071
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002072static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002073{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002074 if (memcg && atomic_read(&memcg->under_oom))
2075 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002076}
2077
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002078/*
2079 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2080 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002081static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2082 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002083{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002084 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002085 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002086
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002087 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002088 owait.wait.flags = 0;
2089 owait.wait.func = memcg_oom_wake_function;
2090 owait.wait.private = current;
2091 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002092 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002093 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002094
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002095 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002096 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002097 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002098 /*
2099 * Even if signal_pending(), we can't quit charge() loop without
2100 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2101 * under OOM is always welcomed, use TASK_KILLABLE here.
2102 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002103 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002104 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002105 need_to_kill = false;
2106 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002107 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002108 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002109
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002110 if (need_to_kill) {
2111 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002112 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002113 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002114 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002115 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002116 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002117 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002118 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002119 mem_cgroup_oom_unlock(memcg);
2120 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002121 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002122
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002123 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002124
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002125 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2126 return false;
2127 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002128 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002129 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002130}
2131
Balbir Singhd69b0422009-06-17 16:26:34 -07002132/*
2133 * Currently used to update mapped file statistics, but the routine can be
2134 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002135 *
2136 * Notes: Race condition
2137 *
2138 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2139 * it tends to be costly. But considering some conditions, we doesn't need
2140 * to do so _always_.
2141 *
2142 * Considering "charge", lock_page_cgroup() is not required because all
2143 * file-stat operations happen after a page is attached to radix-tree. There
2144 * are no race with "charge".
2145 *
2146 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2147 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2148 * if there are race with "uncharge". Statistics itself is properly handled
2149 * by flags.
2150 *
2151 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002152 * small, we check mm->moving_account and detect there are possibility of race
2153 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002154 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002155
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002156void __mem_cgroup_begin_update_page_stat(struct page *page,
2157 bool *locked, unsigned long *flags)
2158{
2159 struct mem_cgroup *memcg;
2160 struct page_cgroup *pc;
2161
2162 pc = lookup_page_cgroup(page);
2163again:
2164 memcg = pc->mem_cgroup;
2165 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2166 return;
2167 /*
2168 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002169 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002170 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002171 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002172 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002173 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002174 return;
2175
2176 move_lock_mem_cgroup(memcg, flags);
2177 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2178 move_unlock_mem_cgroup(memcg, flags);
2179 goto again;
2180 }
2181 *locked = true;
2182}
2183
2184void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2185{
2186 struct page_cgroup *pc = lookup_page_cgroup(page);
2187
2188 /*
2189 * It's guaranteed that pc->mem_cgroup never changes while
2190 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002191 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002192 */
2193 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2194}
2195
Greg Thelen2a7106f2011-01-13 15:47:37 -08002196void mem_cgroup_update_page_stat(struct page *page,
2197 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002198{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002199 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002200 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002201 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002202
Johannes Weinercfa44942012-01-12 17:18:38 -08002203 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002204 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002205
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002206 memcg = pc->mem_cgroup;
2207 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002208 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002209
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002210 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002211 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002212 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002213 break;
2214 default:
2215 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002216 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002217
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002218 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002219}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002220
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002221/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002222 * size of first charge trial. "32" comes from vmscan.c's magic value.
2223 * TODO: maybe necessary to use big numbers in big irons.
2224 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002225#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002226struct memcg_stock_pcp {
2227 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002228 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002229 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002230 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002231#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002232};
2233static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002234static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002235
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002236/**
2237 * consume_stock: Try to consume stocked charge on this cpu.
2238 * @memcg: memcg to consume from.
2239 * @nr_pages: how many pages to charge.
2240 *
2241 * The charges will only happen if @memcg matches the current cpu's memcg
2242 * stock, and at least @nr_pages are available in that stock. Failure to
2243 * service an allocation will refill the stock.
2244 *
2245 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002246 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002247static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002248{
2249 struct memcg_stock_pcp *stock;
2250 bool ret = true;
2251
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002252 if (nr_pages > CHARGE_BATCH)
2253 return false;
2254
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002255 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002256 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2257 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002258 else /* need to call res_counter_charge */
2259 ret = false;
2260 put_cpu_var(memcg_stock);
2261 return ret;
2262}
2263
2264/*
2265 * Returns stocks cached in percpu to res_counter and reset cached information.
2266 */
2267static void drain_stock(struct memcg_stock_pcp *stock)
2268{
2269 struct mem_cgroup *old = stock->cached;
2270
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002271 if (stock->nr_pages) {
2272 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2273
2274 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002275 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002276 res_counter_uncharge(&old->memsw, bytes);
2277 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002278 }
2279 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002280}
2281
2282/*
2283 * This must be called under preempt disabled or must be called by
2284 * a thread which is pinned to local cpu.
2285 */
2286static void drain_local_stock(struct work_struct *dummy)
2287{
2288 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2289 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002290 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002291}
2292
2293/*
2294 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002295 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002296 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002297static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002298{
2299 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2300
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002301 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002302 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002303 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002304 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002305 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002306 put_cpu_var(memcg_stock);
2307}
2308
2309/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002310 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002311 * of the hierarchy under it. sync flag says whether we should block
2312 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002314static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002315{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002316 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002317
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002318 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002319 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002320 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002321 for_each_online_cpu(cpu) {
2322 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002323 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002324
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002325 memcg = stock->cached;
2326 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002327 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002328 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002329 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002330 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2331 if (cpu == curcpu)
2332 drain_local_stock(&stock->work);
2333 else
2334 schedule_work_on(cpu, &stock->work);
2335 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002336 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002337 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002338
2339 if (!sync)
2340 goto out;
2341
2342 for_each_online_cpu(cpu) {
2343 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002344 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002345 flush_work(&stock->work);
2346 }
2347out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002348 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002349}
2350
2351/*
2352 * Tries to drain stocked charges in other cpus. This function is asynchronous
2353 * and just put a work per cpu for draining localy on each cpu. Caller can
2354 * expects some charges will be back to res_counter later but cannot wait for
2355 * it.
2356 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002357static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002358{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002359 /*
2360 * If someone calls draining, avoid adding more kworker runs.
2361 */
2362 if (!mutex_trylock(&percpu_charge_mutex))
2363 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002364 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002365 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002366}
2367
2368/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002369static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002370{
2371 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002372 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002374 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002375}
2376
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002377/*
2378 * This function drains percpu counter value from DEAD cpu and
2379 * move it to local cpu. Note that this function can be preempted.
2380 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002381static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002382{
2383 int i;
2384
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002385 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002386 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002388
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002389 per_cpu(memcg->stat->count[i], cpu) = 0;
2390 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002391 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002392 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002393 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002394
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 per_cpu(memcg->stat->events[i], cpu) = 0;
2396 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002397 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002399}
2400
2401static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002402 unsigned long action,
2403 void *hcpu)
2404{
2405 int cpu = (unsigned long)hcpu;
2406 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002407 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002408
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002409 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002410 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002411
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002412 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002413 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002414
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002415 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002416 mem_cgroup_drain_pcp_counter(iter, cpu);
2417
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002418 stock = &per_cpu(memcg_stock, cpu);
2419 drain_stock(stock);
2420 return NOTIFY_OK;
2421}
2422
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002423
2424/* See __mem_cgroup_try_charge() for details */
2425enum {
2426 CHARGE_OK, /* success */
2427 CHARGE_RETRY, /* need to retry but retry is not bad */
2428 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2429 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2430 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2431};
2432
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002433static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002434 unsigned int nr_pages, unsigned int min_pages,
2435 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002436{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002437 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002438 struct mem_cgroup *mem_over_limit;
2439 struct res_counter *fail_res;
2440 unsigned long flags = 0;
2441 int ret;
2442
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002444
2445 if (likely(!ret)) {
2446 if (!do_swap_account)
2447 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002448 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002449 if (likely(!ret))
2450 return CHARGE_OK;
2451
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002452 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002453 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2454 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2455 } else
2456 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002457 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002458 * Never reclaim on behalf of optional batching, retry with a
2459 * single page instead.
2460 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002461 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002462 return CHARGE_RETRY;
2463
2464 if (!(gfp_mask & __GFP_WAIT))
2465 return CHARGE_WOULDBLOCK;
2466
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002467 if (gfp_mask & __GFP_NORETRY)
2468 return CHARGE_NOMEM;
2469
Johannes Weiner56600482012-01-12 17:17:59 -08002470 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002471 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002472 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002473 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002474 * Even though the limit is exceeded at this point, reclaim
2475 * may have been able to free some pages. Retry the charge
2476 * before killing the task.
2477 *
2478 * Only for regular pages, though: huge pages are rather
2479 * unlikely to succeed so close to the limit, and we fall back
2480 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002481 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002482 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002483 return CHARGE_RETRY;
2484
2485 /*
2486 * At task move, charge accounts can be doubly counted. So, it's
2487 * better to wait until the end of task_move if something is going on.
2488 */
2489 if (mem_cgroup_wait_acct_move(mem_over_limit))
2490 return CHARGE_RETRY;
2491
2492 /* If we don't need to call oom-killer at el, return immediately */
2493 if (!oom_check)
2494 return CHARGE_NOMEM;
2495 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002496 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002497 return CHARGE_OOM_DIE;
2498
2499 return CHARGE_RETRY;
2500}
2501
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002502/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002503 * __mem_cgroup_try_charge() does
2504 * 1. detect memcg to be charged against from passed *mm and *ptr,
2505 * 2. update res_counter
2506 * 3. call memory reclaim if necessary.
2507 *
2508 * In some special case, if the task is fatal, fatal_signal_pending() or
2509 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2510 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2511 * as possible without any hazards. 2: all pages should have a valid
2512 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2513 * pointer, that is treated as a charge to root_mem_cgroup.
2514 *
2515 * So __mem_cgroup_try_charge() will return
2516 * 0 ... on success, filling *ptr with a valid memcg pointer.
2517 * -ENOMEM ... charge failure because of resource limits.
2518 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2519 *
2520 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2521 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002522 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002523static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002524 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002525 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002526 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002527 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002528{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002529 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002530 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002531 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002532 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002533
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002534 /*
2535 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2536 * in system level. So, allow to go ahead dying process in addition to
2537 * MEMDIE process.
2538 */
2539 if (unlikely(test_thread_flag(TIF_MEMDIE)
2540 || fatal_signal_pending(current)))
2541 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002542
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002543 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002544 * We always charge the cgroup the mm_struct belongs to.
2545 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002546 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002547 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002548 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002549 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002550 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002551again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002552 if (*ptr) { /* css should be a valid one */
2553 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002554 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002555 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002556 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002557 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002558 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002559 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002560 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002561
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002562 rcu_read_lock();
2563 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002564 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002565 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002567 * race with swapoff. Then, we have small risk of mis-accouning.
2568 * But such kind of mis-account by race always happens because
2569 * we don't have cgroup_mutex(). It's overkill and we allo that
2570 * small race, here.
2571 * (*) swapoff at el will charge against mm-struct not against
2572 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002573 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002574 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002575 if (!memcg)
2576 memcg = root_mem_cgroup;
2577 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002578 rcu_read_unlock();
2579 goto done;
2580 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002581 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002582 /*
2583 * It seems dagerous to access memcg without css_get().
2584 * But considering how consume_stok works, it's not
2585 * necessary. If consume_stock success, some charges
2586 * from this memcg are cached on this cpu. So, we
2587 * don't need to call css_get()/css_tryget() before
2588 * calling consume_stock().
2589 */
2590 rcu_read_unlock();
2591 goto done;
2592 }
2593 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002594 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002595 rcu_read_unlock();
2596 goto again;
2597 }
2598 rcu_read_unlock();
2599 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002600
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002601 do {
2602 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002603
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002604 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002605 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002606 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002607 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002608 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002609
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002610 oom_check = false;
2611 if (oom && !nr_oom_retries) {
2612 oom_check = true;
2613 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2614 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002615
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002616 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2617 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002618 switch (ret) {
2619 case CHARGE_OK:
2620 break;
2621 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002622 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002623 css_put(&memcg->css);
2624 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002625 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002626 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002627 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002628 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002629 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002630 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002631 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002632 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002633 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002634 /* If oom, we never return -ENOMEM */
2635 nr_oom_retries--;
2636 break;
2637 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002638 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002639 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002640 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002641 } while (ret != CHARGE_OK);
2642
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002643 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002644 refill_stock(memcg, batch - nr_pages);
2645 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002646done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002647 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002648 return 0;
2649nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002650 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002651 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002652bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002653 *ptr = root_mem_cgroup;
2654 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002655}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002656
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002657/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002658 * Somemtimes we have to undo a charge we got by try_charge().
2659 * This function is for that and do uncharge, put css's refcnt.
2660 * gotten by try_charge().
2661 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002662static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002663 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002664{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002665 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002666 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002667
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002668 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002669 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002670 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002671 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002672}
2673
2674/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002675 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2676 * This is useful when moving usage to parent cgroup.
2677 */
2678static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2679 unsigned int nr_pages)
2680{
2681 unsigned long bytes = nr_pages * PAGE_SIZE;
2682
2683 if (mem_cgroup_is_root(memcg))
2684 return;
2685
2686 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2687 if (do_swap_account)
2688 res_counter_uncharge_until(&memcg->memsw,
2689 memcg->memsw.parent, bytes);
2690}
2691
2692/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002693 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002694 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2695 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2696 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002697 */
2698static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2699{
2700 struct cgroup_subsys_state *css;
2701
2702 /* ID 0 is unused ID */
2703 if (!id)
2704 return NULL;
2705 css = css_lookup(&mem_cgroup_subsys, id);
2706 if (!css)
2707 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002708 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002709}
2710
Wu Fengguange42d9d52009-12-16 12:19:59 +01002711struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002712{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002713 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002714 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002715 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002716 swp_entry_t ent;
2717
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002718 VM_BUG_ON(!PageLocked(page));
2719
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002720 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002721 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002722 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002723 memcg = pc->mem_cgroup;
2724 if (memcg && !css_tryget(&memcg->css))
2725 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002726 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002727 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002728 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002729 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002730 memcg = mem_cgroup_lookup(id);
2731 if (memcg && !css_tryget(&memcg->css))
2732 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002733 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002734 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002735 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002736 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002737}
2738
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002739static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002740 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002741 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002742 enum charge_type ctype,
2743 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002744{
Johannes Weinerce587e62012-04-24 20:22:33 +02002745 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002746 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002747 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002748 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002749 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002750
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002751 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002752 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002753 /*
2754 * we don't need page_cgroup_lock about tail pages, becase they are not
2755 * accessed by any other context at this point.
2756 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002757
2758 /*
2759 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2760 * may already be on some other mem_cgroup's LRU. Take care of it.
2761 */
2762 if (lrucare) {
2763 zone = page_zone(page);
2764 spin_lock_irq(&zone->lru_lock);
2765 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002766 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002767 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002768 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002769 was_on_lru = true;
2770 }
2771 }
2772
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002773 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002774 /*
2775 * We access a page_cgroup asynchronously without lock_page_cgroup().
2776 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2777 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2778 * before USED bit, we need memory barrier here.
2779 * See mem_cgroup_add_lru_list(), etc.
2780 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002781 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002782 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002783
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002784 if (lrucare) {
2785 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002786 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002787 VM_BUG_ON(PageLRU(page));
2788 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002789 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002790 }
2791 spin_unlock_irq(&zone->lru_lock);
2792 }
2793
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002794 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002795 anon = true;
2796 else
2797 anon = false;
2798
2799 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002800 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002801
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002802 /*
2803 * "charge_statistics" updated event counter. Then, check it.
2804 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2805 * if they exceeds softlimit.
2806 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002807 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002808}
2809
Glauber Costa7cf27982012-12-18 14:22:55 -08002810static DEFINE_MUTEX(set_limit_mutex);
2811
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002812#ifdef CONFIG_MEMCG_KMEM
2813static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2814{
2815 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2816 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2817}
2818
Glauber Costa1f458cb2012-12-18 14:22:50 -08002819/*
2820 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2821 * in the memcg_cache_params struct.
2822 */
2823static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2824{
2825 struct kmem_cache *cachep;
2826
2827 VM_BUG_ON(p->is_root_cache);
2828 cachep = p->root_cache;
2829 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2830}
2831
Glauber Costa749c5412012-12-18 14:23:01 -08002832#ifdef CONFIG_SLABINFO
2833static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2834 struct seq_file *m)
2835{
2836 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2837 struct memcg_cache_params *params;
2838
2839 if (!memcg_can_account_kmem(memcg))
2840 return -EIO;
2841
2842 print_slabinfo_header(m);
2843
2844 mutex_lock(&memcg->slab_caches_mutex);
2845 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2846 cache_show(memcg_params_to_cache(params), m);
2847 mutex_unlock(&memcg->slab_caches_mutex);
2848
2849 return 0;
2850}
2851#endif
2852
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002853static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2854{
2855 struct res_counter *fail_res;
2856 struct mem_cgroup *_memcg;
2857 int ret = 0;
2858 bool may_oom;
2859
2860 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2861 if (ret)
2862 return ret;
2863
2864 /*
2865 * Conditions under which we can wait for the oom_killer. Those are
2866 * the same conditions tested by the core page allocator
2867 */
2868 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2869
2870 _memcg = memcg;
2871 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2872 &_memcg, may_oom);
2873
2874 if (ret == -EINTR) {
2875 /*
2876 * __mem_cgroup_try_charge() chosed to bypass to root due to
2877 * OOM kill or fatal signal. Since our only options are to
2878 * either fail the allocation or charge it to this cgroup, do
2879 * it as a temporary condition. But we can't fail. From a
2880 * kmem/slab perspective, the cache has already been selected,
2881 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2882 * our minds.
2883 *
2884 * This condition will only trigger if the task entered
2885 * memcg_charge_kmem in a sane state, but was OOM-killed during
2886 * __mem_cgroup_try_charge() above. Tasks that were already
2887 * dying when the allocation triggers should have been already
2888 * directed to the root cgroup in memcontrol.h
2889 */
2890 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2891 if (do_swap_account)
2892 res_counter_charge_nofail(&memcg->memsw, size,
2893 &fail_res);
2894 ret = 0;
2895 } else if (ret)
2896 res_counter_uncharge(&memcg->kmem, size);
2897
2898 return ret;
2899}
2900
2901static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2902{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002903 res_counter_uncharge(&memcg->res, size);
2904 if (do_swap_account)
2905 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002906
2907 /* Not down to 0 */
2908 if (res_counter_uncharge(&memcg->kmem, size))
2909 return;
2910
2911 if (memcg_kmem_test_and_clear_dead(memcg))
2912 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002913}
2914
Glauber Costa2633d7a2012-12-18 14:22:34 -08002915void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2916{
2917 if (!memcg)
2918 return;
2919
2920 mutex_lock(&memcg->slab_caches_mutex);
2921 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2922 mutex_unlock(&memcg->slab_caches_mutex);
2923}
2924
2925/*
2926 * helper for acessing a memcg's index. It will be used as an index in the
2927 * child cache array in kmem_cache, and also to derive its name. This function
2928 * will return -1 when this is not a kmem-limited memcg.
2929 */
2930int memcg_cache_id(struct mem_cgroup *memcg)
2931{
2932 return memcg ? memcg->kmemcg_id : -1;
2933}
2934
Glauber Costa55007d82012-12-18 14:22:38 -08002935/*
2936 * This ends up being protected by the set_limit mutex, during normal
2937 * operation, because that is its main call site.
2938 *
2939 * But when we create a new cache, we can call this as well if its parent
2940 * is kmem-limited. That will have to hold set_limit_mutex as well.
2941 */
2942int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2943{
2944 int num, ret;
2945
2946 num = ida_simple_get(&kmem_limited_groups,
2947 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2948 if (num < 0)
2949 return num;
2950 /*
2951 * After this point, kmem_accounted (that we test atomically in
2952 * the beginning of this conditional), is no longer 0. This
2953 * guarantees only one process will set the following boolean
2954 * to true. We don't need test_and_set because we're protected
2955 * by the set_limit_mutex anyway.
2956 */
2957 memcg_kmem_set_activated(memcg);
2958
2959 ret = memcg_update_all_caches(num+1);
2960 if (ret) {
2961 ida_simple_remove(&kmem_limited_groups, num);
2962 memcg_kmem_clear_activated(memcg);
2963 return ret;
2964 }
2965
2966 memcg->kmemcg_id = num;
2967 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2968 mutex_init(&memcg->slab_caches_mutex);
2969 return 0;
2970}
2971
2972static size_t memcg_caches_array_size(int num_groups)
2973{
2974 ssize_t size;
2975 if (num_groups <= 0)
2976 return 0;
2977
2978 size = 2 * num_groups;
2979 if (size < MEMCG_CACHES_MIN_SIZE)
2980 size = MEMCG_CACHES_MIN_SIZE;
2981 else if (size > MEMCG_CACHES_MAX_SIZE)
2982 size = MEMCG_CACHES_MAX_SIZE;
2983
2984 return size;
2985}
2986
2987/*
2988 * We should update the current array size iff all caches updates succeed. This
2989 * can only be done from the slab side. The slab mutex needs to be held when
2990 * calling this.
2991 */
2992void memcg_update_array_size(int num)
2993{
2994 if (num > memcg_limited_groups_array_size)
2995 memcg_limited_groups_array_size = memcg_caches_array_size(num);
2996}
2997
2998int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
2999{
3000 struct memcg_cache_params *cur_params = s->memcg_params;
3001
3002 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3003
3004 if (num_groups > memcg_limited_groups_array_size) {
3005 int i;
3006 ssize_t size = memcg_caches_array_size(num_groups);
3007
3008 size *= sizeof(void *);
3009 size += sizeof(struct memcg_cache_params);
3010
3011 s->memcg_params = kzalloc(size, GFP_KERNEL);
3012 if (!s->memcg_params) {
3013 s->memcg_params = cur_params;
3014 return -ENOMEM;
3015 }
3016
3017 s->memcg_params->is_root_cache = true;
3018
3019 /*
3020 * There is the chance it will be bigger than
3021 * memcg_limited_groups_array_size, if we failed an allocation
3022 * in a cache, in which case all caches updated before it, will
3023 * have a bigger array.
3024 *
3025 * But if that is the case, the data after
3026 * memcg_limited_groups_array_size is certainly unused
3027 */
3028 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3029 if (!cur_params->memcg_caches[i])
3030 continue;
3031 s->memcg_params->memcg_caches[i] =
3032 cur_params->memcg_caches[i];
3033 }
3034
3035 /*
3036 * Ideally, we would wait until all caches succeed, and only
3037 * then free the old one. But this is not worth the extra
3038 * pointer per-cache we'd have to have for this.
3039 *
3040 * It is not a big deal if some caches are left with a size
3041 * bigger than the others. And all updates will reset this
3042 * anyway.
3043 */
3044 kfree(cur_params);
3045 }
3046 return 0;
3047}
3048
Glauber Costa943a4512012-12-18 14:23:03 -08003049int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3050 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003051{
3052 size_t size = sizeof(struct memcg_cache_params);
3053
3054 if (!memcg_kmem_enabled())
3055 return 0;
3056
Glauber Costa55007d82012-12-18 14:22:38 -08003057 if (!memcg)
3058 size += memcg_limited_groups_array_size * sizeof(void *);
3059
Glauber Costa2633d7a2012-12-18 14:22:34 -08003060 s->memcg_params = kzalloc(size, GFP_KERNEL);
3061 if (!s->memcg_params)
3062 return -ENOMEM;
3063
Glauber Costa943a4512012-12-18 14:23:03 -08003064 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003065 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003066 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003067 } else
3068 s->memcg_params->is_root_cache = true;
3069
Glauber Costa2633d7a2012-12-18 14:22:34 -08003070 return 0;
3071}
3072
3073void memcg_release_cache(struct kmem_cache *s)
3074{
Glauber Costad7f25f82012-12-18 14:22:40 -08003075 struct kmem_cache *root;
3076 struct mem_cgroup *memcg;
3077 int id;
3078
3079 /*
3080 * This happens, for instance, when a root cache goes away before we
3081 * add any memcg.
3082 */
3083 if (!s->memcg_params)
3084 return;
3085
3086 if (s->memcg_params->is_root_cache)
3087 goto out;
3088
3089 memcg = s->memcg_params->memcg;
3090 id = memcg_cache_id(memcg);
3091
3092 root = s->memcg_params->root_cache;
3093 root->memcg_params->memcg_caches[id] = NULL;
3094 mem_cgroup_put(memcg);
3095
3096 mutex_lock(&memcg->slab_caches_mutex);
3097 list_del(&s->memcg_params->list);
3098 mutex_unlock(&memcg->slab_caches_mutex);
3099
3100out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003101 kfree(s->memcg_params);
3102}
3103
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003104/*
3105 * During the creation a new cache, we need to disable our accounting mechanism
3106 * altogether. This is true even if we are not creating, but rather just
3107 * enqueing new caches to be created.
3108 *
3109 * This is because that process will trigger allocations; some visible, like
3110 * explicit kmallocs to auxiliary data structures, name strings and internal
3111 * cache structures; some well concealed, like INIT_WORK() that can allocate
3112 * objects during debug.
3113 *
3114 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3115 * to it. This may not be a bounded recursion: since the first cache creation
3116 * failed to complete (waiting on the allocation), we'll just try to create the
3117 * cache again, failing at the same point.
3118 *
3119 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3120 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3121 * inside the following two functions.
3122 */
3123static inline void memcg_stop_kmem_account(void)
3124{
3125 VM_BUG_ON(!current->mm);
3126 current->memcg_kmem_skip_account++;
3127}
3128
3129static inline void memcg_resume_kmem_account(void)
3130{
3131 VM_BUG_ON(!current->mm);
3132 current->memcg_kmem_skip_account--;
3133}
3134
Glauber Costa1f458cb2012-12-18 14:22:50 -08003135static void kmem_cache_destroy_work_func(struct work_struct *w)
3136{
3137 struct kmem_cache *cachep;
3138 struct memcg_cache_params *p;
3139
3140 p = container_of(w, struct memcg_cache_params, destroy);
3141
3142 cachep = memcg_params_to_cache(p);
3143
Glauber Costa22933152012-12-18 14:22:59 -08003144 /*
3145 * If we get down to 0 after shrink, we could delete right away.
3146 * However, memcg_release_pages() already puts us back in the workqueue
3147 * in that case. If we proceed deleting, we'll get a dangling
3148 * reference, and removing the object from the workqueue in that case
3149 * is unnecessary complication. We are not a fast path.
3150 *
3151 * Note that this case is fundamentally different from racing with
3152 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3153 * kmem_cache_shrink, not only we would be reinserting a dead cache
3154 * into the queue, but doing so from inside the worker racing to
3155 * destroy it.
3156 *
3157 * So if we aren't down to zero, we'll just schedule a worker and try
3158 * again
3159 */
3160 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3161 kmem_cache_shrink(cachep);
3162 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3163 return;
3164 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003165 kmem_cache_destroy(cachep);
3166}
3167
3168void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3169{
3170 if (!cachep->memcg_params->dead)
3171 return;
3172
3173 /*
Glauber Costa22933152012-12-18 14:22:59 -08003174 * There are many ways in which we can get here.
3175 *
3176 * We can get to a memory-pressure situation while the delayed work is
3177 * still pending to run. The vmscan shrinkers can then release all
3178 * cache memory and get us to destruction. If this is the case, we'll
3179 * be executed twice, which is a bug (the second time will execute over
3180 * bogus data). In this case, cancelling the work should be fine.
3181 *
3182 * But we can also get here from the worker itself, if
3183 * kmem_cache_shrink is enough to shake all the remaining objects and
3184 * get the page count to 0. In this case, we'll deadlock if we try to
3185 * cancel the work (the worker runs with an internal lock held, which
3186 * is the same lock we would hold for cancel_work_sync().)
3187 *
3188 * Since we can't possibly know who got us here, just refrain from
3189 * running if there is already work pending
3190 */
3191 if (work_pending(&cachep->memcg_params->destroy))
3192 return;
3193 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003194 * We have to defer the actual destroying to a workqueue, because
3195 * we might currently be in a context that cannot sleep.
3196 */
3197 schedule_work(&cachep->memcg_params->destroy);
3198}
3199
Glauber Costad7f25f82012-12-18 14:22:40 -08003200static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3201{
3202 char *name;
3203 struct dentry *dentry;
3204
3205 rcu_read_lock();
3206 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3207 rcu_read_unlock();
3208
3209 BUG_ON(dentry == NULL);
3210
3211 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3212 memcg_cache_id(memcg), dentry->d_name.name);
3213
3214 return name;
3215}
3216
3217static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3218 struct kmem_cache *s)
3219{
3220 char *name;
3221 struct kmem_cache *new;
3222
3223 name = memcg_cache_name(memcg, s);
3224 if (!name)
3225 return NULL;
3226
3227 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003228 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003229
Glauber Costad79923f2012-12-18 14:22:48 -08003230 if (new)
3231 new->allocflags |= __GFP_KMEMCG;
3232
Glauber Costad7f25f82012-12-18 14:22:40 -08003233 kfree(name);
3234 return new;
3235}
3236
3237/*
3238 * This lock protects updaters, not readers. We want readers to be as fast as
3239 * they can, and they will either see NULL or a valid cache value. Our model
3240 * allow them to see NULL, in which case the root memcg will be selected.
3241 *
3242 * We need this lock because multiple allocations to the same cache from a non
3243 * will span more than one worker. Only one of them can create the cache.
3244 */
3245static DEFINE_MUTEX(memcg_cache_mutex);
3246static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3247 struct kmem_cache *cachep)
3248{
3249 struct kmem_cache *new_cachep;
3250 int idx;
3251
3252 BUG_ON(!memcg_can_account_kmem(memcg));
3253
3254 idx = memcg_cache_id(memcg);
3255
3256 mutex_lock(&memcg_cache_mutex);
3257 new_cachep = cachep->memcg_params->memcg_caches[idx];
3258 if (new_cachep)
3259 goto out;
3260
3261 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003262 if (new_cachep == NULL) {
3263 new_cachep = cachep;
3264 goto out;
3265 }
3266
3267 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003268 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003269
3270 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3271 /*
3272 * the readers won't lock, make sure everybody sees the updated value,
3273 * so they won't put stuff in the queue again for no reason
3274 */
3275 wmb();
3276out:
3277 mutex_unlock(&memcg_cache_mutex);
3278 return new_cachep;
3279}
3280
Glauber Costa7cf27982012-12-18 14:22:55 -08003281void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3282{
3283 struct kmem_cache *c;
3284 int i;
3285
3286 if (!s->memcg_params)
3287 return;
3288 if (!s->memcg_params->is_root_cache)
3289 return;
3290
3291 /*
3292 * If the cache is being destroyed, we trust that there is no one else
3293 * requesting objects from it. Even if there are, the sanity checks in
3294 * kmem_cache_destroy should caught this ill-case.
3295 *
3296 * Still, we don't want anyone else freeing memcg_caches under our
3297 * noses, which can happen if a new memcg comes to life. As usual,
3298 * we'll take the set_limit_mutex to protect ourselves against this.
3299 */
3300 mutex_lock(&set_limit_mutex);
3301 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3302 c = s->memcg_params->memcg_caches[i];
3303 if (!c)
3304 continue;
3305
3306 /*
3307 * We will now manually delete the caches, so to avoid races
3308 * we need to cancel all pending destruction workers and
3309 * proceed with destruction ourselves.
3310 *
3311 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3312 * and that could spawn the workers again: it is likely that
3313 * the cache still have active pages until this very moment.
3314 * This would lead us back to mem_cgroup_destroy_cache.
3315 *
3316 * But that will not execute at all if the "dead" flag is not
3317 * set, so flip it down to guarantee we are in control.
3318 */
3319 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003320 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003321 kmem_cache_destroy(c);
3322 }
3323 mutex_unlock(&set_limit_mutex);
3324}
3325
Glauber Costad7f25f82012-12-18 14:22:40 -08003326struct create_work {
3327 struct mem_cgroup *memcg;
3328 struct kmem_cache *cachep;
3329 struct work_struct work;
3330};
3331
Glauber Costa1f458cb2012-12-18 14:22:50 -08003332static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3333{
3334 struct kmem_cache *cachep;
3335 struct memcg_cache_params *params;
3336
3337 if (!memcg_kmem_is_active(memcg))
3338 return;
3339
3340 mutex_lock(&memcg->slab_caches_mutex);
3341 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3342 cachep = memcg_params_to_cache(params);
3343 cachep->memcg_params->dead = true;
3344 INIT_WORK(&cachep->memcg_params->destroy,
Glauber Costa22933152012-12-18 14:22:59 -08003345 kmem_cache_destroy_work_func);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003346 schedule_work(&cachep->memcg_params->destroy);
3347 }
3348 mutex_unlock(&memcg->slab_caches_mutex);
3349}
3350
Glauber Costad7f25f82012-12-18 14:22:40 -08003351static void memcg_create_cache_work_func(struct work_struct *w)
3352{
3353 struct create_work *cw;
3354
3355 cw = container_of(w, struct create_work, work);
3356 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3357 /* Drop the reference gotten when we enqueued. */
3358 css_put(&cw->memcg->css);
3359 kfree(cw);
3360}
3361
3362/*
3363 * Enqueue the creation of a per-memcg kmem_cache.
3364 * Called with rcu_read_lock.
3365 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003366static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3367 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003368{
3369 struct create_work *cw;
3370
3371 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3372 if (cw == NULL)
3373 return;
3374
3375 /* The corresponding put will be done in the workqueue. */
3376 if (!css_tryget(&memcg->css)) {
3377 kfree(cw);
3378 return;
3379 }
3380
3381 cw->memcg = memcg;
3382 cw->cachep = cachep;
3383
3384 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3385 schedule_work(&cw->work);
3386}
3387
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003388static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3389 struct kmem_cache *cachep)
3390{
3391 /*
3392 * We need to stop accounting when we kmalloc, because if the
3393 * corresponding kmalloc cache is not yet created, the first allocation
3394 * in __memcg_create_cache_enqueue will recurse.
3395 *
3396 * However, it is better to enclose the whole function. Depending on
3397 * the debugging options enabled, INIT_WORK(), for instance, can
3398 * trigger an allocation. This too, will make us recurse. Because at
3399 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3400 * the safest choice is to do it like this, wrapping the whole function.
3401 */
3402 memcg_stop_kmem_account();
3403 __memcg_create_cache_enqueue(memcg, cachep);
3404 memcg_resume_kmem_account();
3405}
Glauber Costad7f25f82012-12-18 14:22:40 -08003406/*
3407 * Return the kmem_cache we're supposed to use for a slab allocation.
3408 * We try to use the current memcg's version of the cache.
3409 *
3410 * If the cache does not exist yet, if we are the first user of it,
3411 * we either create it immediately, if possible, or create it asynchronously
3412 * in a workqueue.
3413 * In the latter case, we will let the current allocation go through with
3414 * the original cache.
3415 *
3416 * Can't be called in interrupt context or from kernel threads.
3417 * This function needs to be called with rcu_read_lock() held.
3418 */
3419struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3420 gfp_t gfp)
3421{
3422 struct mem_cgroup *memcg;
3423 int idx;
3424
3425 VM_BUG_ON(!cachep->memcg_params);
3426 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3427
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003428 if (!current->mm || current->memcg_kmem_skip_account)
3429 return cachep;
3430
Glauber Costad7f25f82012-12-18 14:22:40 -08003431 rcu_read_lock();
3432 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3433 rcu_read_unlock();
3434
3435 if (!memcg_can_account_kmem(memcg))
3436 return cachep;
3437
3438 idx = memcg_cache_id(memcg);
3439
3440 /*
3441 * barrier to mare sure we're always seeing the up to date value. The
3442 * code updating memcg_caches will issue a write barrier to match this.
3443 */
3444 read_barrier_depends();
3445 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3446 /*
3447 * If we are in a safe context (can wait, and not in interrupt
3448 * context), we could be be predictable and return right away.
3449 * This would guarantee that the allocation being performed
3450 * already belongs in the new cache.
3451 *
3452 * However, there are some clashes that can arrive from locking.
3453 * For instance, because we acquire the slab_mutex while doing
3454 * kmem_cache_dup, this means no further allocation could happen
3455 * with the slab_mutex held.
3456 *
3457 * Also, because cache creation issue get_online_cpus(), this
3458 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3459 * that ends up reversed during cpu hotplug. (cpuset allocates
3460 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3461 * better to defer everything.
3462 */
3463 memcg_create_cache_enqueue(memcg, cachep);
3464 return cachep;
3465 }
3466
3467 return cachep->memcg_params->memcg_caches[idx];
3468}
3469EXPORT_SYMBOL(__memcg_kmem_get_cache);
3470
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003471/*
3472 * We need to verify if the allocation against current->mm->owner's memcg is
3473 * possible for the given order. But the page is not allocated yet, so we'll
3474 * need a further commit step to do the final arrangements.
3475 *
3476 * It is possible for the task to switch cgroups in this mean time, so at
3477 * commit time, we can't rely on task conversion any longer. We'll then use
3478 * the handle argument to return to the caller which cgroup we should commit
3479 * against. We could also return the memcg directly and avoid the pointer
3480 * passing, but a boolean return value gives better semantics considering
3481 * the compiled-out case as well.
3482 *
3483 * Returning true means the allocation is possible.
3484 */
3485bool
3486__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3487{
3488 struct mem_cgroup *memcg;
3489 int ret;
3490
3491 *_memcg = NULL;
3492 memcg = try_get_mem_cgroup_from_mm(current->mm);
3493
3494 /*
3495 * very rare case described in mem_cgroup_from_task. Unfortunately there
3496 * isn't much we can do without complicating this too much, and it would
3497 * be gfp-dependent anyway. Just let it go
3498 */
3499 if (unlikely(!memcg))
3500 return true;
3501
3502 if (!memcg_can_account_kmem(memcg)) {
3503 css_put(&memcg->css);
3504 return true;
3505 }
3506
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003507 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3508 if (!ret)
3509 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003510
3511 css_put(&memcg->css);
3512 return (ret == 0);
3513}
3514
3515void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3516 int order)
3517{
3518 struct page_cgroup *pc;
3519
3520 VM_BUG_ON(mem_cgroup_is_root(memcg));
3521
3522 /* The page allocation failed. Revert */
3523 if (!page) {
3524 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003525 return;
3526 }
3527
3528 pc = lookup_page_cgroup(page);
3529 lock_page_cgroup(pc);
3530 pc->mem_cgroup = memcg;
3531 SetPageCgroupUsed(pc);
3532 unlock_page_cgroup(pc);
3533}
3534
3535void __memcg_kmem_uncharge_pages(struct page *page, int order)
3536{
3537 struct mem_cgroup *memcg = NULL;
3538 struct page_cgroup *pc;
3539
3540
3541 pc = lookup_page_cgroup(page);
3542 /*
3543 * Fast unlocked return. Theoretically might have changed, have to
3544 * check again after locking.
3545 */
3546 if (!PageCgroupUsed(pc))
3547 return;
3548
3549 lock_page_cgroup(pc);
3550 if (PageCgroupUsed(pc)) {
3551 memcg = pc->mem_cgroup;
3552 ClearPageCgroupUsed(pc);
3553 }
3554 unlock_page_cgroup(pc);
3555
3556 /*
3557 * We trust that only if there is a memcg associated with the page, it
3558 * is a valid allocation
3559 */
3560 if (!memcg)
3561 return;
3562
3563 VM_BUG_ON(mem_cgroup_is_root(memcg));
3564 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003565}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003566#else
3567static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3568{
3569}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003570#endif /* CONFIG_MEMCG_KMEM */
3571
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003572#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3573
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003574#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003575/*
3576 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003577 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3578 * charge/uncharge will be never happen and move_account() is done under
3579 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003580 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003581void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003582{
3583 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003584 struct page_cgroup *pc;
3585 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003586
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003587 if (mem_cgroup_disabled())
3588 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003589 for (i = 1; i < HPAGE_PMD_NR; i++) {
3590 pc = head_pc + i;
3591 pc->mem_cgroup = head_pc->mem_cgroup;
3592 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003593 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3594 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003595}
Hugh Dickins12d27102012-01-12 17:19:52 -08003596#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003597
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003598/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003599 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003600 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003601 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003602 * @pc: page_cgroup of the page.
3603 * @from: mem_cgroup which the page is moved from.
3604 * @to: mem_cgroup which the page is moved to. @from != @to.
3605 *
3606 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003607 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003608 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003609 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003610 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3611 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003612 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003613static int mem_cgroup_move_account(struct page *page,
3614 unsigned int nr_pages,
3615 struct page_cgroup *pc,
3616 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003617 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003618{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003619 unsigned long flags;
3620 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003621 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003622
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003623 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003624 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003625 /*
3626 * The page is isolated from LRU. So, collapse function
3627 * will not handle this page. But page splitting can happen.
3628 * Do this check under compound_page_lock(). The caller should
3629 * hold it.
3630 */
3631 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003632 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003633 goto out;
3634
3635 lock_page_cgroup(pc);
3636
3637 ret = -EINVAL;
3638 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3639 goto unlock;
3640
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003641 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003642
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003643 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003644 /* Update mapped_file data for mem_cgroup */
3645 preempt_disable();
3646 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3647 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3648 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003649 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003650 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003651
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003652 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003653 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003654 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003655 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003656 ret = 0;
3657unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003658 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003659 /*
3660 * check events
3661 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003662 memcg_check_events(to, page);
3663 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003664out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003665 return ret;
3666}
3667
Michal Hocko2ef37d32012-10-26 13:37:30 +02003668/**
3669 * mem_cgroup_move_parent - moves page to the parent group
3670 * @page: the page to move
3671 * @pc: page_cgroup of the page
3672 * @child: page's cgroup
3673 *
3674 * move charges to its parent or the root cgroup if the group has no
3675 * parent (aka use_hierarchy==0).
3676 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3677 * mem_cgroup_move_account fails) the failure is always temporary and
3678 * it signals a race with a page removal/uncharge or migration. In the
3679 * first case the page is on the way out and it will vanish from the LRU
3680 * on the next attempt and the call should be retried later.
3681 * Isolation from the LRU fails only if page has been isolated from
3682 * the LRU since we looked at it and that usually means either global
3683 * reclaim or migration going on. The page will either get back to the
3684 * LRU or vanish.
3685 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3686 * (!PageCgroupUsed) or moved to a different group. The page will
3687 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003688 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003689static int mem_cgroup_move_parent(struct page *page,
3690 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003691 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003692{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003693 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003694 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003695 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003696 int ret;
3697
Michal Hockod8423012012-10-26 13:37:29 +02003698 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003699
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003700 ret = -EBUSY;
3701 if (!get_page_unless_zero(page))
3702 goto out;
3703 if (isolate_lru_page(page))
3704 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003705
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003706 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003707
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003708 parent = parent_mem_cgroup(child);
3709 /*
3710 * If no parent, move charges to root cgroup.
3711 */
3712 if (!parent)
3713 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003714
Michal Hocko2ef37d32012-10-26 13:37:30 +02003715 if (nr_pages > 1) {
3716 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003717 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003718 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003719
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003720 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003721 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003722 if (!ret)
3723 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003724
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003725 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003726 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003727 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003728put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003729 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003730out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003731 return ret;
3732}
3733
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003734/*
3735 * Charge the memory controller for page usage.
3736 * Return
3737 * 0 if the charge was successful
3738 * < 0 if the cgroup is over its limit
3739 */
3740static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003741 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003742{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003743 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003744 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003745 bool oom = true;
3746 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003747
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003748 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003749 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003750 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003751 /*
3752 * Never OOM-kill a process for a huge page. The
3753 * fault handler will fall back to regular pages.
3754 */
3755 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003756 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003757
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003758 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003759 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003760 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003761 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003762 return 0;
3763}
3764
3765int mem_cgroup_newpage_charge(struct page *page,
3766 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003767{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003768 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003769 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003770 VM_BUG_ON(page_mapped(page));
3771 VM_BUG_ON(page->mapping && !PageAnon(page));
3772 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003773 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003774 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003775}
3776
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003777/*
3778 * While swap-in, try_charge -> commit or cancel, the page is locked.
3779 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003780 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003781 * "commit()" or removed by "cancel()"
3782 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003783static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3784 struct page *page,
3785 gfp_t mask,
3786 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003787{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003788 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003789 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003790 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003791
Johannes Weiner90deb782012-07-31 16:45:47 -07003792 pc = lookup_page_cgroup(page);
3793 /*
3794 * Every swap fault against a single page tries to charge the
3795 * page, bail as early as possible. shmem_unuse() encounters
3796 * already charged pages, too. The USED bit is protected by
3797 * the page lock, which serializes swap cache removal, which
3798 * in turn serializes uncharging.
3799 */
3800 if (PageCgroupUsed(pc))
3801 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003802 if (!do_swap_account)
3803 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003804 memcg = try_get_mem_cgroup_from_page(page);
3805 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003806 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003807 *memcgp = memcg;
3808 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003809 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003810 if (ret == -EINTR)
3811 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003812 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003813charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003814 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3815 if (ret == -EINTR)
3816 ret = 0;
3817 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003818}
3819
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003820int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3821 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3822{
3823 *memcgp = NULL;
3824 if (mem_cgroup_disabled())
3825 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003826 /*
3827 * A racing thread's fault, or swapoff, may have already
3828 * updated the pte, and even removed page from swap cache: in
3829 * those cases unuse_pte()'s pte_same() test will fail; but
3830 * there's also a KSM case which does need to charge the page.
3831 */
3832 if (!PageSwapCache(page)) {
3833 int ret;
3834
3835 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3836 if (ret == -EINTR)
3837 ret = 0;
3838 return ret;
3839 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003840 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3841}
3842
Johannes Weiner827a03d2012-07-31 16:45:36 -07003843void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3844{
3845 if (mem_cgroup_disabled())
3846 return;
3847 if (!memcg)
3848 return;
3849 __mem_cgroup_cancel_charge(memcg, 1);
3850}
3851
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003852static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003853__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003854 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003855{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003856 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003857 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003858 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003859 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003860
Johannes Weinerce587e62012-04-24 20:22:33 +02003861 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003862 /*
3863 * Now swap is on-memory. This means this page may be
3864 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003865 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3866 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3867 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003868 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003869 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003870 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003871 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003872 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003873}
3874
Johannes Weiner72835c82012-01-12 17:18:32 -08003875void mem_cgroup_commit_charge_swapin(struct page *page,
3876 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003877{
Johannes Weiner72835c82012-01-12 17:18:32 -08003878 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003879 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003880}
3881
Johannes Weiner827a03d2012-07-31 16:45:36 -07003882int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3883 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003884{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003885 struct mem_cgroup *memcg = NULL;
3886 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3887 int ret;
3888
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003889 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003890 return 0;
3891 if (PageCompound(page))
3892 return 0;
3893
Johannes Weiner827a03d2012-07-31 16:45:36 -07003894 if (!PageSwapCache(page))
3895 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3896 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003897 ret = __mem_cgroup_try_charge_swapin(mm, page,
3898 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003899 if (!ret)
3900 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3901 }
3902 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003903}
3904
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003905static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003906 unsigned int nr_pages,
3907 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003908{
3909 struct memcg_batch_info *batch = NULL;
3910 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003911
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003912 /* If swapout, usage of swap doesn't decrease */
3913 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3914 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003915
3916 batch = &current->memcg_batch;
3917 /*
3918 * In usual, we do css_get() when we remember memcg pointer.
3919 * But in this case, we keep res->usage until end of a series of
3920 * uncharges. Then, it's ok to ignore memcg's refcnt.
3921 */
3922 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003923 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003924 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003925 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003926 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003927 * the same cgroup and we have chance to coalesce uncharges.
3928 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3929 * because we want to do uncharge as soon as possible.
3930 */
3931
3932 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3933 goto direct_uncharge;
3934
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003935 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003936 goto direct_uncharge;
3937
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003938 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003939 * In typical case, batch->memcg == mem. This means we can
3940 * merge a series of uncharges to an uncharge of res_counter.
3941 * If not, we uncharge res_counter ony by one.
3942 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003943 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003944 goto direct_uncharge;
3945 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003946 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003947 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003948 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003949 return;
3950direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003951 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003952 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003953 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3954 if (unlikely(batch->memcg != memcg))
3955 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003956}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003957
Balbir Singh8697d332008-02-07 00:13:59 -08003958/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003959 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003960 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003961static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003962__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3963 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003964{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003965 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003966 unsigned int nr_pages = 1;
3967 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003968 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003969
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003970 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003972
Johannes Weiner0c59b892012-07-31 16:45:31 -07003973 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003974
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003975 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003976 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003977 VM_BUG_ON(!PageTransHuge(page));
3978 }
Balbir Singh8697d332008-02-07 00:13:59 -08003979 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003980 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003981 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003982 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003983 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003984 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003985
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003986 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003987
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003988 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003989
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003990 if (!PageCgroupUsed(pc))
3991 goto unlock_out;
3992
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003993 anon = PageAnon(page);
3994
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003995 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003996 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003997 /*
3998 * Generally PageAnon tells if it's the anon statistics to be
3999 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4000 * used before page reached the stage of being marked PageAnon.
4001 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004002 anon = true;
4003 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004004 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004005 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004006 if (page_mapped(page))
4007 goto unlock_out;
4008 /*
4009 * Pages under migration may not be uncharged. But
4010 * end_migration() /must/ be the one uncharging the
4011 * unused post-migration page and so it has to call
4012 * here with the migration bit still set. See the
4013 * res_counter handling below.
4014 */
4015 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004016 goto unlock_out;
4017 break;
4018 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4019 if (!PageAnon(page)) { /* Shared memory */
4020 if (page->mapping && !page_is_file_cache(page))
4021 goto unlock_out;
4022 } else if (page_mapped(page)) /* Anon */
4023 goto unlock_out;
4024 break;
4025 default:
4026 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004027 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004028
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004029 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004030
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004031 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004032 /*
4033 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4034 * freed from LRU. This is safe because uncharged page is expected not
4035 * to be reused (freed soon). Exception is SwapCache, it's handled by
4036 * special functions.
4037 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004038
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004039 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004040 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004041 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004042 * will never be freed.
4043 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004044 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004045 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004046 mem_cgroup_swap_statistics(memcg, true);
4047 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004048 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004049 /*
4050 * Migration does not charge the res_counter for the
4051 * replacement page, so leave it alone when phasing out the
4052 * page that is unused after the migration.
4053 */
4054 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004055 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004056
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004057 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004058
4059unlock_out:
4060 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004061 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004062}
4063
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004064void mem_cgroup_uncharge_page(struct page *page)
4065{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004066 /* early check. */
4067 if (page_mapped(page))
4068 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004069 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004070 if (PageSwapCache(page))
4071 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004072 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004073}
4074
4075void mem_cgroup_uncharge_cache_page(struct page *page)
4076{
4077 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004078 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004079 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004080}
4081
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004082/*
4083 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4084 * In that cases, pages are freed continuously and we can expect pages
4085 * are in the same memcg. All these calls itself limits the number of
4086 * pages freed at once, then uncharge_start/end() is called properly.
4087 * This may be called prural(2) times in a context,
4088 */
4089
4090void mem_cgroup_uncharge_start(void)
4091{
4092 current->memcg_batch.do_batch++;
4093 /* We can do nest. */
4094 if (current->memcg_batch.do_batch == 1) {
4095 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004096 current->memcg_batch.nr_pages = 0;
4097 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004098 }
4099}
4100
4101void mem_cgroup_uncharge_end(void)
4102{
4103 struct memcg_batch_info *batch = &current->memcg_batch;
4104
4105 if (!batch->do_batch)
4106 return;
4107
4108 batch->do_batch--;
4109 if (batch->do_batch) /* If stacked, do nothing. */
4110 return;
4111
4112 if (!batch->memcg)
4113 return;
4114 /*
4115 * This "batch->memcg" is valid without any css_get/put etc...
4116 * bacause we hide charges behind us.
4117 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004118 if (batch->nr_pages)
4119 res_counter_uncharge(&batch->memcg->res,
4120 batch->nr_pages * PAGE_SIZE);
4121 if (batch->memsw_nr_pages)
4122 res_counter_uncharge(&batch->memcg->memsw,
4123 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004124 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004125 /* forget this pointer (for sanity check) */
4126 batch->memcg = NULL;
4127}
4128
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004129#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004130/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004131 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004132 * memcg information is recorded to swap_cgroup of "ent"
4133 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004134void
4135mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004136{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004137 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004138 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004139
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004140 if (!swapout) /* this was a swap cache but the swap is unused ! */
4141 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4142
Johannes Weiner0030f532012-07-31 16:45:25 -07004143 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004144
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004145 /*
4146 * record memcg information, if swapout && memcg != NULL,
4147 * mem_cgroup_get() was called in uncharge().
4148 */
4149 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004150 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004151}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004152#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004153
Andrew Mortonc255a452012-07-31 16:43:02 -07004154#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004155/*
4156 * called from swap_entry_free(). remove record in swap_cgroup and
4157 * uncharge "memsw" account.
4158 */
4159void mem_cgroup_uncharge_swap(swp_entry_t ent)
4160{
4161 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004162 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004163
4164 if (!do_swap_account)
4165 return;
4166
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004167 id = swap_cgroup_record(ent, 0);
4168 rcu_read_lock();
4169 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004170 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004171 /*
4172 * We uncharge this because swap is freed.
4173 * This memcg can be obsolete one. We avoid calling css_tryget
4174 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004175 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004176 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004177 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004178 mem_cgroup_put(memcg);
4179 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004180 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004181}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004182
4183/**
4184 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4185 * @entry: swap entry to be moved
4186 * @from: mem_cgroup which the entry is moved from
4187 * @to: mem_cgroup which the entry is moved to
4188 *
4189 * It succeeds only when the swap_cgroup's record for this entry is the same
4190 * as the mem_cgroup's id of @from.
4191 *
4192 * Returns 0 on success, -EINVAL on failure.
4193 *
4194 * The caller must have charged to @to, IOW, called res_counter_charge() about
4195 * both res and memsw, and called css_get().
4196 */
4197static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004198 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004199{
4200 unsigned short old_id, new_id;
4201
4202 old_id = css_id(&from->css);
4203 new_id = css_id(&to->css);
4204
4205 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004206 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004207 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004208 /*
4209 * This function is only called from task migration context now.
4210 * It postpones res_counter and refcount handling till the end
4211 * of task migration(mem_cgroup_clear_mc()) for performance
4212 * improvement. But we cannot postpone mem_cgroup_get(to)
4213 * because if the process that has been moved to @to does
4214 * swap-in, the refcount of @to might be decreased to 0.
4215 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004216 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004217 return 0;
4218 }
4219 return -EINVAL;
4220}
4221#else
4222static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004223 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004224{
4225 return -EINVAL;
4226}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004227#endif
4228
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004229/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004230 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4231 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004232 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004233void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4234 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004235{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004236 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004237 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004238 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004239 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004240
Johannes Weiner72835c82012-01-12 17:18:32 -08004241 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004242
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004243 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004244 return;
Balbir Singh40779602008-04-04 14:29:59 -07004245
Mel Gormanb32967f2012-11-19 12:35:47 +00004246 if (PageTransHuge(page))
4247 nr_pages <<= compound_order(page);
4248
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004249 pc = lookup_page_cgroup(page);
4250 lock_page_cgroup(pc);
4251 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004252 memcg = pc->mem_cgroup;
4253 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004254 /*
4255 * At migrating an anonymous page, its mapcount goes down
4256 * to 0 and uncharge() will be called. But, even if it's fully
4257 * unmapped, migration may fail and this page has to be
4258 * charged again. We set MIGRATION flag here and delay uncharge
4259 * until end_migration() is called
4260 *
4261 * Corner Case Thinking
4262 * A)
4263 * When the old page was mapped as Anon and it's unmap-and-freed
4264 * while migration was ongoing.
4265 * If unmap finds the old page, uncharge() of it will be delayed
4266 * until end_migration(). If unmap finds a new page, it's
4267 * uncharged when it make mapcount to be 1->0. If unmap code
4268 * finds swap_migration_entry, the new page will not be mapped
4269 * and end_migration() will find it(mapcount==0).
4270 *
4271 * B)
4272 * When the old page was mapped but migraion fails, the kernel
4273 * remaps it. A charge for it is kept by MIGRATION flag even
4274 * if mapcount goes down to 0. We can do remap successfully
4275 * without charging it again.
4276 *
4277 * C)
4278 * The "old" page is under lock_page() until the end of
4279 * migration, so, the old page itself will not be swapped-out.
4280 * If the new page is swapped out before end_migraton, our
4281 * hook to usual swap-out path will catch the event.
4282 */
4283 if (PageAnon(page))
4284 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004285 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004286 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004287 /*
4288 * If the page is not charged at this point,
4289 * we return here.
4290 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004291 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004292 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004293
Johannes Weiner72835c82012-01-12 17:18:32 -08004294 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004295 /*
4296 * We charge new page before it's used/mapped. So, even if unlock_page()
4297 * is called before end_migration, we can catch all events on this new
4298 * page. In the case new page is migrated but not remapped, new page's
4299 * mapcount will be finally 0 and we call uncharge in end_migration().
4300 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004301 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004302 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004303 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004304 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004305 /*
4306 * The page is committed to the memcg, but it's not actually
4307 * charged to the res_counter since we plan on replacing the
4308 * old one and only one page is going to be left afterwards.
4309 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004310 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004311}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004312
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004313/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004314void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004315 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004316{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004317 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004318 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004319 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004320
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004321 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004322 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004323
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004324 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004325 used = oldpage;
4326 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004327 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004328 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004329 unused = oldpage;
4330 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004331 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004332 __mem_cgroup_uncharge_common(unused,
4333 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4334 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4335 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004336 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004337 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004338 * We disallowed uncharge of pages under migration because mapcount
4339 * of the page goes down to zero, temporarly.
4340 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004341 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004342 pc = lookup_page_cgroup(oldpage);
4343 lock_page_cgroup(pc);
4344 ClearPageCgroupMigration(pc);
4345 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004346
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004347 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004348 * If a page is a file cache, radix-tree replacement is very atomic
4349 * and we can skip this check. When it was an Anon page, its mapcount
4350 * goes down to 0. But because we added MIGRATION flage, it's not
4351 * uncharged yet. There are several case but page->mapcount check
4352 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4353 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004354 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004355 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004356 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004357}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004358
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004359/*
4360 * At replace page cache, newpage is not under any memcg but it's on
4361 * LRU. So, this function doesn't touch res_counter but handles LRU
4362 * in correct way. Both pages are locked so we cannot race with uncharge.
4363 */
4364void mem_cgroup_replace_page_cache(struct page *oldpage,
4365 struct page *newpage)
4366{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004367 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004368 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004369 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004370
4371 if (mem_cgroup_disabled())
4372 return;
4373
4374 pc = lookup_page_cgroup(oldpage);
4375 /* fix accounting on old pages */
4376 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004377 if (PageCgroupUsed(pc)) {
4378 memcg = pc->mem_cgroup;
4379 mem_cgroup_charge_statistics(memcg, false, -1);
4380 ClearPageCgroupUsed(pc);
4381 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004382 unlock_page_cgroup(pc);
4383
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004384 /*
4385 * When called from shmem_replace_page(), in some cases the
4386 * oldpage has already been charged, and in some cases not.
4387 */
4388 if (!memcg)
4389 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004390 /*
4391 * Even if newpage->mapping was NULL before starting replacement,
4392 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4393 * LRU while we overwrite pc->mem_cgroup.
4394 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004395 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004396}
4397
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004398#ifdef CONFIG_DEBUG_VM
4399static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4400{
4401 struct page_cgroup *pc;
4402
4403 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004404 /*
4405 * Can be NULL while feeding pages into the page allocator for
4406 * the first time, i.e. during boot or memory hotplug;
4407 * or when mem_cgroup_disabled().
4408 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004409 if (likely(pc) && PageCgroupUsed(pc))
4410 return pc;
4411 return NULL;
4412}
4413
4414bool mem_cgroup_bad_page_check(struct page *page)
4415{
4416 if (mem_cgroup_disabled())
4417 return false;
4418
4419 return lookup_page_cgroup_used(page) != NULL;
4420}
4421
4422void mem_cgroup_print_bad_page(struct page *page)
4423{
4424 struct page_cgroup *pc;
4425
4426 pc = lookup_page_cgroup_used(page);
4427 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004428 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4429 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004430 }
4431}
4432#endif
4433
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004434static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004435 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004436{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004437 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004438 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004439 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004440 int children = mem_cgroup_count_children(memcg);
4441 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004442 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004443
4444 /*
4445 * For keeping hierarchical_reclaim simple, how long we should retry
4446 * is depends on callers. We set our retry-count to be function
4447 * of # of children which we should visit in this loop.
4448 */
4449 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4450
4451 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004452
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004453 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004454 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004455 if (signal_pending(current)) {
4456 ret = -EINTR;
4457 break;
4458 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004459 /*
4460 * Rather than hide all in some function, I do this in
4461 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004462 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004463 */
4464 mutex_lock(&set_limit_mutex);
4465 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4466 if (memswlimit < val) {
4467 ret = -EINVAL;
4468 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004469 break;
4470 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004471
4472 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4473 if (memlimit < val)
4474 enlarge = 1;
4475
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004476 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004477 if (!ret) {
4478 if (memswlimit == val)
4479 memcg->memsw_is_minimum = true;
4480 else
4481 memcg->memsw_is_minimum = false;
4482 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004483 mutex_unlock(&set_limit_mutex);
4484
4485 if (!ret)
4486 break;
4487
Johannes Weiner56600482012-01-12 17:17:59 -08004488 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4489 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004490 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4491 /* Usage is reduced ? */
4492 if (curusage >= oldusage)
4493 retry_count--;
4494 else
4495 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004496 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004497 if (!ret && enlarge)
4498 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004499
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004500 return ret;
4501}
4502
Li Zefan338c8432009-06-17 16:27:15 -07004503static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4504 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004505{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004506 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004507 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004508 int children = mem_cgroup_count_children(memcg);
4509 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004510 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004511
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004512 /* see mem_cgroup_resize_res_limit */
4513 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4514 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004515 while (retry_count) {
4516 if (signal_pending(current)) {
4517 ret = -EINTR;
4518 break;
4519 }
4520 /*
4521 * Rather than hide all in some function, I do this in
4522 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004523 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004524 */
4525 mutex_lock(&set_limit_mutex);
4526 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4527 if (memlimit > val) {
4528 ret = -EINVAL;
4529 mutex_unlock(&set_limit_mutex);
4530 break;
4531 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004532 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4533 if (memswlimit < val)
4534 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004535 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004536 if (!ret) {
4537 if (memlimit == val)
4538 memcg->memsw_is_minimum = true;
4539 else
4540 memcg->memsw_is_minimum = false;
4541 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004542 mutex_unlock(&set_limit_mutex);
4543
4544 if (!ret)
4545 break;
4546
Johannes Weiner56600482012-01-12 17:17:59 -08004547 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4548 MEM_CGROUP_RECLAIM_NOSWAP |
4549 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004550 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004551 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004552 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004553 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004554 else
4555 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004556 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004557 if (!ret && enlarge)
4558 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004559 return ret;
4560}
4561
Balbir Singh4e416952009-09-23 15:56:39 -07004562unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004563 gfp_t gfp_mask,
4564 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004565{
4566 unsigned long nr_reclaimed = 0;
4567 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4568 unsigned long reclaimed;
4569 int loop = 0;
4570 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004571 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004572 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004573
4574 if (order > 0)
4575 return 0;
4576
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004577 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004578 /*
4579 * This loop can run a while, specially if mem_cgroup's continuously
4580 * keep exceeding their soft limit and putting the system under
4581 * pressure
4582 */
4583 do {
4584 if (next_mz)
4585 mz = next_mz;
4586 else
4587 mz = mem_cgroup_largest_soft_limit_node(mctz);
4588 if (!mz)
4589 break;
4590
Ying Han0ae5e892011-05-26 16:25:25 -07004591 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004592 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004593 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004594 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004595 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004596 spin_lock(&mctz->lock);
4597
4598 /*
4599 * If we failed to reclaim anything from this memory cgroup
4600 * it is time to move on to the next cgroup
4601 */
4602 next_mz = NULL;
4603 if (!reclaimed) {
4604 do {
4605 /*
4606 * Loop until we find yet another one.
4607 *
4608 * By the time we get the soft_limit lock
4609 * again, someone might have aded the
4610 * group back on the RB tree. Iterate to
4611 * make sure we get a different mem.
4612 * mem_cgroup_largest_soft_limit_node returns
4613 * NULL if no other cgroup is present on
4614 * the tree
4615 */
4616 next_mz =
4617 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004618 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004619 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004620 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004621 break;
4622 } while (1);
4623 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004624 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4625 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004626 /*
4627 * One school of thought says that we should not add
4628 * back the node to the tree if reclaim returns 0.
4629 * But our reclaim could return 0, simply because due
4630 * to priority we are exposing a smaller subset of
4631 * memory to reclaim from. Consider this as a longer
4632 * term TODO.
4633 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004634 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004635 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004636 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004637 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004638 loop++;
4639 /*
4640 * Could not reclaim anything and there are no more
4641 * mem cgroups to try or we seem to be looping without
4642 * reclaiming anything.
4643 */
4644 if (!nr_reclaimed &&
4645 (next_mz == NULL ||
4646 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4647 break;
4648 } while (!nr_reclaimed);
4649 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004650 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004651 return nr_reclaimed;
4652}
4653
Michal Hocko2ef37d32012-10-26 13:37:30 +02004654/**
4655 * mem_cgroup_force_empty_list - clears LRU of a group
4656 * @memcg: group to clear
4657 * @node: NUMA node
4658 * @zid: zone id
4659 * @lru: lru to to clear
4660 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004661 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004662 * reclaim the pages page themselves - pages are moved to the parent (or root)
4663 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004664 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004665static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004666 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004667{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004668 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004669 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004670 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004671 struct page *busy;
4672 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004673
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004674 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004675 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4676 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004677
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004678 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004679 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004680 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004681 struct page *page;
4682
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004683 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004684 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004685 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004686 break;
4687 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004688 page = list_entry(list->prev, struct page, lru);
4689 if (busy == page) {
4690 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004691 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004692 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004693 continue;
4694 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004695 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004696
Johannes Weiner925b7672012-01-12 17:18:15 -08004697 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004698
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004699 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004700 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004701 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004702 cond_resched();
4703 } else
4704 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004705 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004706}
4707
4708/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004709 * make mem_cgroup's charge to be 0 if there is no task by moving
4710 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004711 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004712 *
4713 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004714 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004715static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004716{
Michal Hockoc26251f2012-10-26 13:37:28 +02004717 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004718 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004719
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004720 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004721 /* This is for making all *used* pages to be on LRU. */
4722 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004723 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004724 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004725 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004726 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004727 enum lru_list lru;
4728 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004729 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004730 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004731 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004732 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004733 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004734 mem_cgroup_end_move(memcg);
4735 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004736 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004737
Michal Hocko2ef37d32012-10-26 13:37:30 +02004738 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004739 * Kernel memory may not necessarily be trackable to a specific
4740 * process. So they are not migrated, and therefore we can't
4741 * expect their value to drop to 0 here.
4742 * Having res filled up with kmem only is enough.
4743 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004744 * This is a safety check because mem_cgroup_force_empty_list
4745 * could have raced with mem_cgroup_replace_page_cache callers
4746 * so the lru seemed empty but the page could have been added
4747 * right after the check. RES_USAGE should be safe as we always
4748 * charge before adding to the LRU.
4749 */
Glauber Costabea207c2012-12-18 14:22:11 -08004750 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4751 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4752 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004753}
4754
4755/*
4756 * Reclaims as many pages from the given memcg as possible and moves
4757 * the rest to the parent.
4758 *
4759 * Caller is responsible for holding css reference for memcg.
4760 */
4761static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4762{
4763 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4764 struct cgroup *cgrp = memcg->css.cgroup;
4765
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004766 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004767 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4768 return -EBUSY;
4769
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004770 /* we call try-to-free pages for make this cgroup empty */
4771 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004772 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004773 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004774 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004775
Michal Hockoc26251f2012-10-26 13:37:28 +02004776 if (signal_pending(current))
4777 return -EINTR;
4778
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004779 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004780 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004781 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004782 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004783 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004784 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004785 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004786
4787 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004788 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004789 mem_cgroup_reparent_charges(memcg);
4790
4791 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004792}
4793
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004794static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004795{
Michal Hockoc26251f2012-10-26 13:37:28 +02004796 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4797 int ret;
4798
Michal Hockod8423012012-10-26 13:37:29 +02004799 if (mem_cgroup_is_root(memcg))
4800 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004801 css_get(&memcg->css);
4802 ret = mem_cgroup_force_empty(memcg);
4803 css_put(&memcg->css);
4804
4805 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004806}
4807
4808
Balbir Singh18f59ea2009-01-07 18:08:07 -08004809static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4810{
4811 return mem_cgroup_from_cont(cont)->use_hierarchy;
4812}
4813
4814static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4815 u64 val)
4816{
4817 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004818 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004819 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004820 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004821
4822 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004823 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004824
4825 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004826
4827 if (memcg->use_hierarchy == val)
4828 goto out;
4829
Balbir Singh18f59ea2009-01-07 18:08:07 -08004830 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004831 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004832 * in the child subtrees. If it is unset, then the change can
4833 * occur, provided the current cgroup has no children.
4834 *
4835 * For the root cgroup, parent_mem is NULL, we allow value to be
4836 * set if there are no children.
4837 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004838 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004839 (val == 1 || val == 0)) {
4840 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004841 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004842 else
4843 retval = -EBUSY;
4844 } else
4845 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004846
4847out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004848 cgroup_unlock();
4849
4850 return retval;
4851}
4852
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004853
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004854static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004855 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004856{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004857 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004858 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004859
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004860 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004861 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004862 val += mem_cgroup_read_stat(iter, idx);
4863
4864 if (val < 0) /* race ? */
4865 val = 0;
4866 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004867}
4868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004869static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004870{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004871 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004872
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004873 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004874 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004875 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004876 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004877 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004878 }
4879
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004880 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4881 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004882
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004883 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004884 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004885
4886 return val << PAGE_SHIFT;
4887}
4888
Tejun Heoaf36f902012-04-01 12:09:55 -07004889static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4890 struct file *file, char __user *buf,
4891 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004892{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004893 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004894 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004895 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004896 int name, len;
4897 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004898
4899 type = MEMFILE_TYPE(cft->private);
4900 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004901
4902 if (!do_swap_account && type == _MEMSWAP)
4903 return -EOPNOTSUPP;
4904
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004905 switch (type) {
4906 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004907 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004908 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004909 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004910 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004911 break;
4912 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004913 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004914 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004915 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004916 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004917 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004918 case _KMEM:
4919 val = res_counter_read_u64(&memcg->kmem, name);
4920 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004921 default:
4922 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004923 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004924
4925 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4926 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004927}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004928
4929static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4930{
4931 int ret = -EINVAL;
4932#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004933 bool must_inc_static_branch = false;
4934
Glauber Costa510fc4e2012-12-18 14:21:47 -08004935 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4936 /*
4937 * For simplicity, we won't allow this to be disabled. It also can't
4938 * be changed if the cgroup has children already, or if tasks had
4939 * already joined.
4940 *
4941 * If tasks join before we set the limit, a person looking at
4942 * kmem.usage_in_bytes will have no way to determine when it took
4943 * place, which makes the value quite meaningless.
4944 *
4945 * After it first became limited, changes in the value of the limit are
4946 * of course permitted.
4947 *
4948 * Taking the cgroup_lock is really offensive, but it is so far the only
4949 * way to guarantee that no children will appear. There are plenty of
4950 * other offenders, and they should all go away. Fine grained locking
4951 * is probably the way to go here. When we are fully hierarchical, we
4952 * can also get rid of the use_hierarchy check.
4953 */
4954 cgroup_lock();
4955 mutex_lock(&set_limit_mutex);
4956 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4957 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4958 !list_empty(&cont->children))) {
4959 ret = -EBUSY;
4960 goto out;
4961 }
4962 ret = res_counter_set_limit(&memcg->kmem, val);
4963 VM_BUG_ON(ret);
4964
Glauber Costa55007d82012-12-18 14:22:38 -08004965 ret = memcg_update_cache_sizes(memcg);
4966 if (ret) {
4967 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
4968 goto out;
4969 }
Glauber Costaa8964b92012-12-18 14:22:09 -08004970 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08004971 /*
4972 * kmem charges can outlive the cgroup. In the case of slab
4973 * pages, for instance, a page contain objects from various
4974 * processes, so it is unfeasible to migrate them away. We
4975 * need to reference count the memcg because of that.
4976 */
4977 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004978 } else
4979 ret = res_counter_set_limit(&memcg->kmem, val);
4980out:
4981 mutex_unlock(&set_limit_mutex);
4982 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08004983
4984 /*
4985 * We are by now familiar with the fact that we can't inc the static
4986 * branch inside cgroup_lock. See disarm functions for details. A
4987 * worker here is overkill, but also wrong: After the limit is set, we
4988 * must start accounting right away. Since this operation can't fail,
4989 * we can safely defer it to here - no rollback will be needed.
4990 *
4991 * The boolean used to control this is also safe, because
4992 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
4993 * able to set it to true;
4994 */
4995 if (must_inc_static_branch) {
4996 static_key_slow_inc(&memcg_kmem_enabled_key);
4997 /*
4998 * setting the active bit after the inc will guarantee no one
4999 * starts accounting before all call sites are patched
5000 */
5001 memcg_kmem_set_active(memcg);
5002 }
5003
Glauber Costa510fc4e2012-12-18 14:21:47 -08005004#endif
5005 return ret;
5006}
5007
Glauber Costa55007d82012-12-18 14:22:38 -08005008static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005009{
Glauber Costa55007d82012-12-18 14:22:38 -08005010 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005011 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5012 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005013 goto out;
5014
Glauber Costa510fc4e2012-12-18 14:21:47 -08005015 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08005016#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08005017 /*
5018 * When that happen, we need to disable the static branch only on those
5019 * memcgs that enabled it. To achieve this, we would be forced to
5020 * complicate the code by keeping track of which memcgs were the ones
5021 * that actually enabled limits, and which ones got it from its
5022 * parents.
5023 *
5024 * It is a lot simpler just to do static_key_slow_inc() on every child
5025 * that is accounted.
5026 */
Glauber Costa55007d82012-12-18 14:22:38 -08005027 if (!memcg_kmem_is_active(memcg))
5028 goto out;
5029
5030 /*
5031 * destroy(), called if we fail, will issue static_key_slow_inc() and
5032 * mem_cgroup_put() if kmem is enabled. We have to either call them
5033 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5034 * this more consistent, since it always leads to the same destroy path
5035 */
5036 mem_cgroup_get(memcg);
5037 static_key_slow_inc(&memcg_kmem_enabled_key);
5038
5039 mutex_lock(&set_limit_mutex);
5040 ret = memcg_update_cache_sizes(memcg);
5041 mutex_unlock(&set_limit_mutex);
Glauber Costa7de37682012-12-18 14:22:07 -08005042#endif
Glauber Costa55007d82012-12-18 14:22:38 -08005043out:
5044 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005045}
5046
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005047/*
5048 * The user of this function is...
5049 * RES_LIMIT.
5050 */
Paul Menage856c13a2008-07-25 01:47:04 -07005051static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5052 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005053{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005054 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005055 enum res_type type;
5056 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005057 unsigned long long val;
5058 int ret;
5059
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005060 type = MEMFILE_TYPE(cft->private);
5061 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005062
5063 if (!do_swap_account && type == _MEMSWAP)
5064 return -EOPNOTSUPP;
5065
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005066 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005067 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005068 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5069 ret = -EINVAL;
5070 break;
5071 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005072 /* This function does all necessary parse...reuse it */
5073 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005074 if (ret)
5075 break;
5076 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005077 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005078 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005079 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005080 else if (type == _KMEM)
5081 ret = memcg_update_kmem_limit(cont, val);
5082 else
5083 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005084 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005085 case RES_SOFT_LIMIT:
5086 ret = res_counter_memparse_write_strategy(buffer, &val);
5087 if (ret)
5088 break;
5089 /*
5090 * For memsw, soft limits are hard to implement in terms
5091 * of semantics, for now, we support soft limits for
5092 * control without swap
5093 */
5094 if (type == _MEM)
5095 ret = res_counter_set_soft_limit(&memcg->res, val);
5096 else
5097 ret = -EINVAL;
5098 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005099 default:
5100 ret = -EINVAL; /* should be BUG() ? */
5101 break;
5102 }
5103 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005104}
5105
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005106static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5107 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5108{
5109 struct cgroup *cgroup;
5110 unsigned long long min_limit, min_memsw_limit, tmp;
5111
5112 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5113 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5114 cgroup = memcg->css.cgroup;
5115 if (!memcg->use_hierarchy)
5116 goto out;
5117
5118 while (cgroup->parent) {
5119 cgroup = cgroup->parent;
5120 memcg = mem_cgroup_from_cont(cgroup);
5121 if (!memcg->use_hierarchy)
5122 break;
5123 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5124 min_limit = min(min_limit, tmp);
5125 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5126 min_memsw_limit = min(min_memsw_limit, tmp);
5127 }
5128out:
5129 *mem_limit = min_limit;
5130 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005131}
5132
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005133static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005134{
Tejun Heoaf36f902012-04-01 12:09:55 -07005135 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005136 int name;
5137 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005138
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005139 type = MEMFILE_TYPE(event);
5140 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005141
5142 if (!do_swap_account && type == _MEMSWAP)
5143 return -EOPNOTSUPP;
5144
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005145 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005146 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005147 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005148 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005149 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005150 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005151 else if (type == _KMEM)
5152 res_counter_reset_max(&memcg->kmem);
5153 else
5154 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005155 break;
5156 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005157 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005158 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005159 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005160 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005161 else if (type == _KMEM)
5162 res_counter_reset_failcnt(&memcg->kmem);
5163 else
5164 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005165 break;
5166 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005167
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005168 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005169}
5170
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005171static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5172 struct cftype *cft)
5173{
5174 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5175}
5176
Daisuke Nishimura02491442010-03-10 15:22:17 -08005177#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005178static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5179 struct cftype *cft, u64 val)
5180{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005181 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005182
5183 if (val >= (1 << NR_MOVE_TYPE))
5184 return -EINVAL;
5185 /*
5186 * We check this value several times in both in can_attach() and
5187 * attach(), so we need cgroup lock to prevent this value from being
5188 * inconsistent.
5189 */
5190 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005191 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005192 cgroup_unlock();
5193
5194 return 0;
5195}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005196#else
5197static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5198 struct cftype *cft, u64 val)
5199{
5200 return -ENOSYS;
5201}
5202#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005203
Ying Han406eb0c2011-05-26 16:25:37 -07005204#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005205static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005206 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005207{
5208 int nid;
5209 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5210 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005211 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005212
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005213 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005214 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005215 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005216 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005217 seq_printf(m, " N%d=%lu", nid, node_nr);
5218 }
5219 seq_putc(m, '\n');
5220
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005221 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005222 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005223 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005224 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005225 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005226 seq_printf(m, " N%d=%lu", nid, node_nr);
5227 }
5228 seq_putc(m, '\n');
5229
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005230 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005231 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005232 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005233 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005234 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005235 seq_printf(m, " N%d=%lu", nid, node_nr);
5236 }
5237 seq_putc(m, '\n');
5238
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005239 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005240 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005241 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005242 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005243 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005244 seq_printf(m, " N%d=%lu", nid, node_nr);
5245 }
5246 seq_putc(m, '\n');
5247 return 0;
5248}
5249#endif /* CONFIG_NUMA */
5250
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005251static inline void mem_cgroup_lru_names_not_uptodate(void)
5252{
5253 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5254}
5255
Wanpeng Liab215882012-07-31 16:43:09 -07005256static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005257 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005258{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005259 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005260 struct mem_cgroup *mi;
5261 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005262
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005263 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005264 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005265 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005266 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5267 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005268 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005269
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005270 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5271 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5272 mem_cgroup_read_events(memcg, i));
5273
5274 for (i = 0; i < NR_LRU_LISTS; i++)
5275 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5276 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5277
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005278 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005279 {
5280 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005281 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005282 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005283 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005284 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5285 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005286 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005287
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005288 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5289 long long val = 0;
5290
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005291 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005292 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005293 for_each_mem_cgroup_tree(mi, memcg)
5294 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5295 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5296 }
5297
5298 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5299 unsigned long long val = 0;
5300
5301 for_each_mem_cgroup_tree(mi, memcg)
5302 val += mem_cgroup_read_events(mi, i);
5303 seq_printf(m, "total_%s %llu\n",
5304 mem_cgroup_events_names[i], val);
5305 }
5306
5307 for (i = 0; i < NR_LRU_LISTS; i++) {
5308 unsigned long long val = 0;
5309
5310 for_each_mem_cgroup_tree(mi, memcg)
5311 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5312 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005313 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005314
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005315#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005316 {
5317 int nid, zid;
5318 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005319 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005320 unsigned long recent_rotated[2] = {0, 0};
5321 unsigned long recent_scanned[2] = {0, 0};
5322
5323 for_each_online_node(nid)
5324 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005325 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005326 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005327
Hugh Dickins89abfab2012-05-29 15:06:53 -07005328 recent_rotated[0] += rstat->recent_rotated[0];
5329 recent_rotated[1] += rstat->recent_rotated[1];
5330 recent_scanned[0] += rstat->recent_scanned[0];
5331 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005332 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005333 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5334 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5335 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5336 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005337 }
5338#endif
5339
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005340 return 0;
5341}
5342
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005343static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5344{
5345 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5346
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005347 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005348}
5349
5350static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5351 u64 val)
5352{
5353 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5354 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005355
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005356 if (val > 100)
5357 return -EINVAL;
5358
5359 if (cgrp->parent == NULL)
5360 return -EINVAL;
5361
5362 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005363
5364 cgroup_lock();
5365
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005366 /* If under hierarchy, only empty-root can set this value */
5367 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08005368 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
5369 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005370 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005371 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005372
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005373 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005374
Li Zefan068b38c2009-01-15 13:51:26 -08005375 cgroup_unlock();
5376
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005377 return 0;
5378}
5379
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005380static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5381{
5382 struct mem_cgroup_threshold_ary *t;
5383 u64 usage;
5384 int i;
5385
5386 rcu_read_lock();
5387 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005388 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005389 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005390 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005391
5392 if (!t)
5393 goto unlock;
5394
5395 usage = mem_cgroup_usage(memcg, swap);
5396
5397 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005398 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005399 * If it's not true, a threshold was crossed after last
5400 * call of __mem_cgroup_threshold().
5401 */
Phil Carmody5407a562010-05-26 14:42:42 -07005402 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005403
5404 /*
5405 * Iterate backward over array of thresholds starting from
5406 * current_threshold and check if a threshold is crossed.
5407 * If none of thresholds below usage is crossed, we read
5408 * only one element of the array here.
5409 */
5410 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5411 eventfd_signal(t->entries[i].eventfd, 1);
5412
5413 /* i = current_threshold + 1 */
5414 i++;
5415
5416 /*
5417 * Iterate forward over array of thresholds starting from
5418 * current_threshold+1 and check if a threshold is crossed.
5419 * If none of thresholds above usage is crossed, we read
5420 * only one element of the array here.
5421 */
5422 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5423 eventfd_signal(t->entries[i].eventfd, 1);
5424
5425 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005426 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005427unlock:
5428 rcu_read_unlock();
5429}
5430
5431static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5432{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005433 while (memcg) {
5434 __mem_cgroup_threshold(memcg, false);
5435 if (do_swap_account)
5436 __mem_cgroup_threshold(memcg, true);
5437
5438 memcg = parent_mem_cgroup(memcg);
5439 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005440}
5441
5442static int compare_thresholds(const void *a, const void *b)
5443{
5444 const struct mem_cgroup_threshold *_a = a;
5445 const struct mem_cgroup_threshold *_b = b;
5446
5447 return _a->threshold - _b->threshold;
5448}
5449
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005450static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005451{
5452 struct mem_cgroup_eventfd_list *ev;
5453
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005454 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005455 eventfd_signal(ev->eventfd, 1);
5456 return 0;
5457}
5458
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005459static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005460{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005461 struct mem_cgroup *iter;
5462
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005463 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005464 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005465}
5466
5467static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5468 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005469{
5470 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005471 struct mem_cgroup_thresholds *thresholds;
5472 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005473 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005474 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005475 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005476
5477 ret = res_counter_memparse_write_strategy(args, &threshold);
5478 if (ret)
5479 return ret;
5480
5481 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005482
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005483 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005484 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005485 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005486 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005487 else
5488 BUG();
5489
5490 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5491
5492 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005493 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005494 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5495
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005496 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005497
5498 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005499 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005500 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005501 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005502 ret = -ENOMEM;
5503 goto unlock;
5504 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506
5507 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005508 if (thresholds->primary) {
5509 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005510 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005511 }
5512
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005513 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005514 new->entries[size - 1].eventfd = eventfd;
5515 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005516
5517 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005518 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005519 compare_thresholds, NULL);
5520
5521 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005522 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005523 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005524 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005525 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005526 * new->current_threshold will not be used until
5527 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005528 * it here.
5529 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005530 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005531 } else
5532 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005533 }
5534
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005535 /* Free old spare buffer and save old primary buffer as spare */
5536 kfree(thresholds->spare);
5537 thresholds->spare = thresholds->primary;
5538
5539 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005540
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005541 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005542 synchronize_rcu();
5543
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005544unlock:
5545 mutex_unlock(&memcg->thresholds_lock);
5546
5547 return ret;
5548}
5549
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005550static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005551 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005552{
5553 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005554 struct mem_cgroup_thresholds *thresholds;
5555 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005556 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005557 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005558 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005559
5560 mutex_lock(&memcg->thresholds_lock);
5561 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005562 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005564 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005565 else
5566 BUG();
5567
Anton Vorontsov371528c2012-02-24 05:14:46 +04005568 if (!thresholds->primary)
5569 goto unlock;
5570
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005571 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5572
5573 /* Check if a threshold crossed before removing */
5574 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5575
5576 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005577 size = 0;
5578 for (i = 0; i < thresholds->primary->size; i++) {
5579 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005580 size++;
5581 }
5582
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005583 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005584
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005585 /* Set thresholds array to NULL if we don't have thresholds */
5586 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005587 kfree(new);
5588 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005589 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005590 }
5591
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005592 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005593
5594 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005595 new->current_threshold = -1;
5596 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5597 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005598 continue;
5599
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005600 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005601 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005602 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005603 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005604 * until rcu_assign_pointer(), so it's safe to increment
5605 * it here.
5606 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005607 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005608 }
5609 j++;
5610 }
5611
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005612swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005613 /* Swap primary and spare array */
5614 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005615 /* If all events are unregistered, free the spare array */
5616 if (!new) {
5617 kfree(thresholds->spare);
5618 thresholds->spare = NULL;
5619 }
5620
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005621 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005622
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005623 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005624 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005625unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005626 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005627}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005628
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005629static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5630 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5631{
5632 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5633 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005634 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005635
5636 BUG_ON(type != _OOM_TYPE);
5637 event = kmalloc(sizeof(*event), GFP_KERNEL);
5638 if (!event)
5639 return -ENOMEM;
5640
Michal Hocko1af8efe2011-07-26 16:08:24 -07005641 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005642
5643 event->eventfd = eventfd;
5644 list_add(&event->list, &memcg->oom_notify);
5645
5646 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005647 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005648 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005649 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005650
5651 return 0;
5652}
5653
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005654static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005655 struct cftype *cft, struct eventfd_ctx *eventfd)
5656{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005657 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005658 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005659 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005660
5661 BUG_ON(type != _OOM_TYPE);
5662
Michal Hocko1af8efe2011-07-26 16:08:24 -07005663 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005664
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005665 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005666 if (ev->eventfd == eventfd) {
5667 list_del(&ev->list);
5668 kfree(ev);
5669 }
5670 }
5671
Michal Hocko1af8efe2011-07-26 16:08:24 -07005672 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005673}
5674
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005675static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5676 struct cftype *cft, struct cgroup_map_cb *cb)
5677{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005678 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005679
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005680 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005681
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005682 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005683 cb->fill(cb, "under_oom", 1);
5684 else
5685 cb->fill(cb, "under_oom", 0);
5686 return 0;
5687}
5688
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005689static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5690 struct cftype *cft, u64 val)
5691{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005692 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005693 struct mem_cgroup *parent;
5694
5695 /* cannot set to root cgroup and only 0 and 1 are allowed */
5696 if (!cgrp->parent || !((val == 0) || (val == 1)))
5697 return -EINVAL;
5698
5699 parent = mem_cgroup_from_cont(cgrp->parent);
5700
5701 cgroup_lock();
5702 /* oom-kill-disable is a flag for subhierarchy. */
5703 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005704 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005705 cgroup_unlock();
5706 return -EINVAL;
5707 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005708 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005709 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005710 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005711 cgroup_unlock();
5712 return 0;
5713}
5714
Andrew Mortonc255a452012-07-31 16:43:02 -07005715#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005716static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005717{
Glauber Costa55007d82012-12-18 14:22:38 -08005718 int ret;
5719
Glauber Costa2633d7a2012-12-18 14:22:34 -08005720 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005721 ret = memcg_propagate_kmem(memcg);
5722 if (ret)
5723 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005724
Glauber Costa1d62e432012-04-09 19:36:33 -03005725 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005726};
5727
Glauber Costa1d62e432012-04-09 19:36:33 -03005728static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005729{
Glauber Costa1d62e432012-04-09 19:36:33 -03005730 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005731
5732 memcg_kmem_mark_dead(memcg);
5733
5734 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5735 return;
5736
5737 /*
5738 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5739 * path here, being careful not to race with memcg_uncharge_kmem: it is
5740 * possible that the charges went down to 0 between mark_dead and the
5741 * res_counter read, so in that case, we don't need the put
5742 */
5743 if (memcg_kmem_test_and_clear_dead(memcg))
5744 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005745}
Glauber Costae5671df2011-12-11 21:47:01 +00005746#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005747static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005748{
5749 return 0;
5750}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005751
Glauber Costa1d62e432012-04-09 19:36:33 -03005752static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005753{
5754}
Glauber Costae5671df2011-12-11 21:47:01 +00005755#endif
5756
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005757static struct cftype mem_cgroup_files[] = {
5758 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005759 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005760 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005761 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005762 .register_event = mem_cgroup_usage_register_event,
5763 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005764 },
5765 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005766 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005767 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005768 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005769 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005770 },
5771 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005772 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005773 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005774 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005775 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005776 },
5777 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005778 .name = "soft_limit_in_bytes",
5779 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5780 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005781 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005782 },
5783 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005784 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005785 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005786 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005787 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005788 },
Balbir Singh8697d332008-02-07 00:13:59 -08005789 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005790 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005791 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005792 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005793 {
5794 .name = "force_empty",
5795 .trigger = mem_cgroup_force_empty_write,
5796 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005797 {
5798 .name = "use_hierarchy",
5799 .write_u64 = mem_cgroup_hierarchy_write,
5800 .read_u64 = mem_cgroup_hierarchy_read,
5801 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005802 {
5803 .name = "swappiness",
5804 .read_u64 = mem_cgroup_swappiness_read,
5805 .write_u64 = mem_cgroup_swappiness_write,
5806 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005807 {
5808 .name = "move_charge_at_immigrate",
5809 .read_u64 = mem_cgroup_move_charge_read,
5810 .write_u64 = mem_cgroup_move_charge_write,
5811 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005812 {
5813 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005814 .read_map = mem_cgroup_oom_control_read,
5815 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005816 .register_event = mem_cgroup_oom_register_event,
5817 .unregister_event = mem_cgroup_oom_unregister_event,
5818 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5819 },
Ying Han406eb0c2011-05-26 16:25:37 -07005820#ifdef CONFIG_NUMA
5821 {
5822 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005823 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005824 },
5825#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005826#ifdef CONFIG_MEMCG_KMEM
5827 {
5828 .name = "kmem.limit_in_bytes",
5829 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5830 .write_string = mem_cgroup_write,
5831 .read = mem_cgroup_read,
5832 },
5833 {
5834 .name = "kmem.usage_in_bytes",
5835 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5836 .read = mem_cgroup_read,
5837 },
5838 {
5839 .name = "kmem.failcnt",
5840 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5841 .trigger = mem_cgroup_reset,
5842 .read = mem_cgroup_read,
5843 },
5844 {
5845 .name = "kmem.max_usage_in_bytes",
5846 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5847 .trigger = mem_cgroup_reset,
5848 .read = mem_cgroup_read,
5849 },
Glauber Costa749c5412012-12-18 14:23:01 -08005850#ifdef CONFIG_SLABINFO
5851 {
5852 .name = "kmem.slabinfo",
5853 .read_seq_string = mem_cgroup_slabinfo_read,
5854 },
5855#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005856#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005857 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005858};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005859
Michal Hocko2d110852013-02-22 16:34:43 -08005860#ifdef CONFIG_MEMCG_SWAP
5861static struct cftype memsw_cgroup_files[] = {
5862 {
5863 .name = "memsw.usage_in_bytes",
5864 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5865 .read = mem_cgroup_read,
5866 .register_event = mem_cgroup_usage_register_event,
5867 .unregister_event = mem_cgroup_usage_unregister_event,
5868 },
5869 {
5870 .name = "memsw.max_usage_in_bytes",
5871 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5872 .trigger = mem_cgroup_reset,
5873 .read = mem_cgroup_read,
5874 },
5875 {
5876 .name = "memsw.limit_in_bytes",
5877 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5878 .write_string = mem_cgroup_write,
5879 .read = mem_cgroup_read,
5880 },
5881 {
5882 .name = "memsw.failcnt",
5883 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5884 .trigger = mem_cgroup_reset,
5885 .read = mem_cgroup_read,
5886 },
5887 { }, /* terminate */
5888};
5889#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005890static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005891{
5892 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005893 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005894 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005895 /*
5896 * This routine is called against possible nodes.
5897 * But it's BUG to call kmalloc() against offline node.
5898 *
5899 * TODO: this routine can waste much memory for nodes which will
5900 * never be onlined. It's better to use memory hotplug callback
5901 * function.
5902 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005903 if (!node_state(node, N_NORMAL_MEMORY))
5904 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005905 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005906 if (!pn)
5907 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005908
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005909 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5910 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005911 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005912 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005913 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005914 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005915 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005916 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005917 return 0;
5918}
5919
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005920static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005921{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005922 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005923}
5924
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005925static struct mem_cgroup *mem_cgroup_alloc(void)
5926{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005927 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005928 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005929
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005930 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005931 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005932 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005933 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005934 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005935
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005936 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005937 return NULL;
5938
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005939 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5940 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005941 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005942 spin_lock_init(&memcg->pcp_counter_lock);
5943 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005944
5945out_free:
5946 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005947 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005948 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005949 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005950 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005951}
5952
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005953/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005954 * At destroying mem_cgroup, references from swap_cgroup can remain.
5955 * (scanning all at force_empty is too costly...)
5956 *
5957 * Instead of clearing all references at force_empty, we remember
5958 * the number of reference from swap_cgroup and free mem_cgroup when
5959 * it goes down to 0.
5960 *
5961 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005962 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005963
5964static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005965{
Glauber Costac8b2a362012-12-18 14:22:13 -08005966 int node;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005967 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005968
Glauber Costac8b2a362012-12-18 14:22:13 -08005969 mem_cgroup_remove_from_trees(memcg);
5970 free_css_id(&mem_cgroup_subsys, &memcg->css);
5971
5972 for_each_node(node)
5973 free_mem_cgroup_per_zone_info(memcg, node);
5974
5975 free_percpu(memcg->stat);
5976
Glauber Costa3f134612012-05-29 15:07:11 -07005977 /*
5978 * We need to make sure that (at least for now), the jump label
5979 * destruction code runs outside of the cgroup lock. This is because
5980 * get_online_cpus(), which is called from the static_branch update,
5981 * can't be called inside the cgroup_lock. cpusets are the ones
5982 * enforcing this dependency, so if they ever change, we might as well.
5983 *
5984 * schedule_work() will guarantee this happens. Be careful if you need
5985 * to move this code around, and make sure it is outside
5986 * the cgroup_lock.
5987 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005988 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005989 if (size < PAGE_SIZE)
5990 kfree(memcg);
5991 else
5992 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005993}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005994
Glauber Costac8b2a362012-12-18 14:22:13 -08005995
5996/*
5997 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
5998 * but in process context. The work_freeing structure is overlaid
5999 * on the rcu_freeing structure, which itself is overlaid on memsw.
6000 */
6001static void free_work(struct work_struct *work)
6002{
6003 struct mem_cgroup *memcg;
6004
6005 memcg = container_of(work, struct mem_cgroup, work_freeing);
6006 __mem_cgroup_free(memcg);
6007}
6008
Glauber Costa3afe36b2012-05-29 15:07:10 -07006009static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006010{
6011 struct mem_cgroup *memcg;
6012
6013 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006014 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006015 schedule_work(&memcg->work_freeing);
6016}
6017
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006018static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006019{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006020 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006021}
6022
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006023static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006024{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006025 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6026 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006027 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006028 if (parent)
6029 mem_cgroup_put(parent);
6030 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006031}
6032
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006033static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006034{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006035 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006036}
6037
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006038/*
6039 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6040 */
Glauber Costae1aab162011-12-11 21:47:03 +00006041struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006042{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006043 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006044 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006045 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006046}
Glauber Costae1aab162011-12-11 21:47:03 +00006047EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006048
Andrew Mortonc255a452012-07-31 16:43:02 -07006049#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006050static void __init enable_swap_cgroup(void)
6051{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08006052 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006053 do_swap_account = 1;
6054}
6055#else
6056static void __init enable_swap_cgroup(void)
6057{
6058}
6059#endif
6060
Balbir Singhf64c3f52009-09-23 15:56:37 -07006061static int mem_cgroup_soft_limit_tree_init(void)
6062{
6063 struct mem_cgroup_tree_per_node *rtpn;
6064 struct mem_cgroup_tree_per_zone *rtpz;
6065 int tmp, node, zone;
6066
Bob Liu3ed28fa2012-01-12 17:19:04 -08006067 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006068 tmp = node;
6069 if (!node_state(node, N_NORMAL_MEMORY))
6070 tmp = -1;
6071 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6072 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08006073 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006074
6075 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6076
6077 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6078 rtpz = &rtpn->rb_tree_per_zone[zone];
6079 rtpz->rb_root = RB_ROOT;
6080 spin_lock_init(&rtpz->lock);
6081 }
6082 }
6083 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08006084
6085err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08006086 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08006087 if (!soft_limit_tree.rb_tree_per_node[node])
6088 break;
6089 kfree(soft_limit_tree.rb_tree_per_node[node]);
6090 soft_limit_tree.rb_tree_per_node[node] = NULL;
6091 }
6092 return 1;
6093
Balbir Singhf64c3f52009-09-23 15:56:37 -07006094}
6095
Li Zefan0eb253e2009-01-15 13:51:25 -08006096static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006097mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006098{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006099 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006100 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006101 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006102
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006103 memcg = mem_cgroup_alloc();
6104 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006105 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006106
Bob Liu3ed28fa2012-01-12 17:19:04 -08006107 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006108 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006109 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006110
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006111 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006112 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006113 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006114 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006115 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006116 if (mem_cgroup_soft_limit_tree_init())
6117 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08006118 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006119 for_each_possible_cpu(cpu) {
6120 struct memcg_stock_pcp *stock =
6121 &per_cpu(memcg_stock, cpu);
6122 INIT_WORK(&stock->work, drain_local_stock);
6123 }
Balbir Singh18f59ea2009-01-07 18:08:07 -08006124 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006125 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006126 memcg->use_hierarchy = parent->use_hierarchy;
6127 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006128 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006129
Balbir Singh18f59ea2009-01-07 18:08:07 -08006130 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006131 res_counter_init(&memcg->res, &parent->res);
6132 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006133 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006134
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006135 /*
6136 * We increment refcnt of the parent to ensure that we can
6137 * safely access it on res_counter_charge/uncharge.
6138 * This refcnt will be decremented when freeing this
6139 * mem_cgroup(see mem_cgroup_put).
6140 */
6141 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006142 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006143 res_counter_init(&memcg->res, NULL);
6144 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006145 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006146 /*
6147 * Deeper hierachy with use_hierarchy == false doesn't make
6148 * much sense so let cgroup subsystem know about this
6149 * unfortunate state in our controller.
6150 */
6151 if (parent && parent != root_mem_cgroup)
6152 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006153 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006154 memcg->last_scanned_node = MAX_NUMNODES;
6155 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08006156
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006157 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006158 memcg->swappiness = mem_cgroup_swappiness(parent);
6159 atomic_set(&memcg->refcnt, 1);
6160 memcg->move_charge_at_immigrate = 0;
6161 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07006162 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03006163
6164 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
6165 if (error) {
6166 /*
6167 * We call put now because our (and parent's) refcnts
6168 * are already in place. mem_cgroup_put() will internally
6169 * call __mem_cgroup_free, so return directly
6170 */
6171 mem_cgroup_put(memcg);
6172 return ERR_PTR(error);
6173 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006174 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006175free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006176 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006177 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006178}
6179
Tejun Heo92fb9742012-11-19 08:13:38 -08006180static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006181{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006182 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006183
Michal Hockoab5196c2012-10-26 13:37:32 +02006184 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006185 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006186}
6187
Tejun Heo92fb9742012-11-19 08:13:38 -08006188static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006189{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006190 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006191
Glauber Costa1d62e432012-04-09 19:36:33 -03006192 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006193
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006194 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006195}
6196
Daisuke Nishimura02491442010-03-10 15:22:17 -08006197#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006198/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006199#define PRECHARGE_COUNT_AT_ONCE 256
6200static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006201{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006202 int ret = 0;
6203 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006204 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006205
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006206 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006207 mc.precharge += count;
6208 /* we don't need css_get for root */
6209 return ret;
6210 }
6211 /* try to charge at once */
6212 if (count > 1) {
6213 struct res_counter *dummy;
6214 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006215 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006216 * by cgroup_lock_live_cgroup() that it is not removed and we
6217 * are still under the same cgroup_mutex. So we can postpone
6218 * css_get().
6219 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006220 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006221 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006222 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006223 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006224 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006225 goto one_by_one;
6226 }
6227 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006228 return ret;
6229 }
6230one_by_one:
6231 /* fall back to one by one charge */
6232 while (count--) {
6233 if (signal_pending(current)) {
6234 ret = -EINTR;
6235 break;
6236 }
6237 if (!batch_count--) {
6238 batch_count = PRECHARGE_COUNT_AT_ONCE;
6239 cond_resched();
6240 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006241 ret = __mem_cgroup_try_charge(NULL,
6242 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006243 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006244 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006245 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006246 mc.precharge++;
6247 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006248 return ret;
6249}
6250
6251/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006252 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006253 * @vma: the vma the pte to be checked belongs
6254 * @addr: the address corresponding to the pte to be checked
6255 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006256 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006257 *
6258 * Returns
6259 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6260 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6261 * move charge. if @target is not NULL, the page is stored in target->page
6262 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006263 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6264 * target for charge migration. if @target is not NULL, the entry is stored
6265 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006266 *
6267 * Called with pte lock held.
6268 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006269union mc_target {
6270 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006271 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006272};
6273
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006274enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006275 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006276 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006277 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006278};
6279
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006280static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6281 unsigned long addr, pte_t ptent)
6282{
6283 struct page *page = vm_normal_page(vma, addr, ptent);
6284
6285 if (!page || !page_mapped(page))
6286 return NULL;
6287 if (PageAnon(page)) {
6288 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006289 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006290 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006291 } else if (!move_file())
6292 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006293 return NULL;
6294 if (!get_page_unless_zero(page))
6295 return NULL;
6296
6297 return page;
6298}
6299
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006300#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006301static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6302 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6303{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006304 struct page *page = NULL;
6305 swp_entry_t ent = pte_to_swp_entry(ptent);
6306
6307 if (!move_anon() || non_swap_entry(ent))
6308 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006309 /*
6310 * Because lookup_swap_cache() updates some statistics counter,
6311 * we call find_get_page() with swapper_space directly.
6312 */
Shaohua Li33806f02013-02-22 16:34:37 -08006313 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006314 if (do_swap_account)
6315 entry->val = ent.val;
6316
6317 return page;
6318}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006319#else
6320static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6321 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6322{
6323 return NULL;
6324}
6325#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006326
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006327static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6328 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6329{
6330 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006331 struct address_space *mapping;
6332 pgoff_t pgoff;
6333
6334 if (!vma->vm_file) /* anonymous vma */
6335 return NULL;
6336 if (!move_file())
6337 return NULL;
6338
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006339 mapping = vma->vm_file->f_mapping;
6340 if (pte_none(ptent))
6341 pgoff = linear_page_index(vma, addr);
6342 else /* pte_file(ptent) is true */
6343 pgoff = pte_to_pgoff(ptent);
6344
6345 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006346 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006347
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006348#ifdef CONFIG_SWAP
6349 /* shmem/tmpfs may report page out on swap: account for that too. */
6350 if (radix_tree_exceptional_entry(page)) {
6351 swp_entry_t swap = radix_to_swp_entry(page);
6352 if (do_swap_account)
6353 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006354 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006355 }
6356#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006357 return page;
6358}
6359
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006360static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006361 unsigned long addr, pte_t ptent, union mc_target *target)
6362{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006363 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006364 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006365 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006366 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006367
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006368 if (pte_present(ptent))
6369 page = mc_handle_present_pte(vma, addr, ptent);
6370 else if (is_swap_pte(ptent))
6371 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006372 else if (pte_none(ptent) || pte_file(ptent))
6373 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006374
6375 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006376 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006377 if (page) {
6378 pc = lookup_page_cgroup(page);
6379 /*
6380 * Do only loose check w/o page_cgroup lock.
6381 * mem_cgroup_move_account() checks the pc is valid or not under
6382 * the lock.
6383 */
6384 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6385 ret = MC_TARGET_PAGE;
6386 if (target)
6387 target->page = page;
6388 }
6389 if (!ret || !target)
6390 put_page(page);
6391 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006392 /* There is a swap entry and a page doesn't exist or isn't charged */
6393 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006394 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006395 ret = MC_TARGET_SWAP;
6396 if (target)
6397 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006398 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006399 return ret;
6400}
6401
Naoya Horiguchi12724852012-03-21 16:34:28 -07006402#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6403/*
6404 * We don't consider swapping or file mapped pages because THP does not
6405 * support them for now.
6406 * Caller should make sure that pmd_trans_huge(pmd) is true.
6407 */
6408static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6409 unsigned long addr, pmd_t pmd, union mc_target *target)
6410{
6411 struct page *page = NULL;
6412 struct page_cgroup *pc;
6413 enum mc_target_type ret = MC_TARGET_NONE;
6414
6415 page = pmd_page(pmd);
6416 VM_BUG_ON(!page || !PageHead(page));
6417 if (!move_anon())
6418 return ret;
6419 pc = lookup_page_cgroup(page);
6420 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6421 ret = MC_TARGET_PAGE;
6422 if (target) {
6423 get_page(page);
6424 target->page = page;
6425 }
6426 }
6427 return ret;
6428}
6429#else
6430static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6431 unsigned long addr, pmd_t pmd, union mc_target *target)
6432{
6433 return MC_TARGET_NONE;
6434}
6435#endif
6436
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006437static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6438 unsigned long addr, unsigned long end,
6439 struct mm_walk *walk)
6440{
6441 struct vm_area_struct *vma = walk->private;
6442 pte_t *pte;
6443 spinlock_t *ptl;
6444
Naoya Horiguchi12724852012-03-21 16:34:28 -07006445 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6446 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6447 mc.precharge += HPAGE_PMD_NR;
6448 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006449 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006450 }
Dave Hansen03319322011-03-22 16:32:56 -07006451
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006452 if (pmd_trans_unstable(pmd))
6453 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006454 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6455 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006456 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006457 mc.precharge++; /* increment precharge temporarily */
6458 pte_unmap_unlock(pte - 1, ptl);
6459 cond_resched();
6460
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006461 return 0;
6462}
6463
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006464static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6465{
6466 unsigned long precharge;
6467 struct vm_area_struct *vma;
6468
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006469 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006470 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6471 struct mm_walk mem_cgroup_count_precharge_walk = {
6472 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6473 .mm = mm,
6474 .private = vma,
6475 };
6476 if (is_vm_hugetlb_page(vma))
6477 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006478 walk_page_range(vma->vm_start, vma->vm_end,
6479 &mem_cgroup_count_precharge_walk);
6480 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006481 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006482
6483 precharge = mc.precharge;
6484 mc.precharge = 0;
6485
6486 return precharge;
6487}
6488
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006489static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6490{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006491 unsigned long precharge = mem_cgroup_count_precharge(mm);
6492
6493 VM_BUG_ON(mc.moving_task);
6494 mc.moving_task = current;
6495 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006496}
6497
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006498/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6499static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006500{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006501 struct mem_cgroup *from = mc.from;
6502 struct mem_cgroup *to = mc.to;
6503
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006504 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006505 if (mc.precharge) {
6506 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6507 mc.precharge = 0;
6508 }
6509 /*
6510 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6511 * we must uncharge here.
6512 */
6513 if (mc.moved_charge) {
6514 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6515 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006516 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006517 /* we must fixup refcnts and charges */
6518 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006519 /* uncharge swap account from the old cgroup */
6520 if (!mem_cgroup_is_root(mc.from))
6521 res_counter_uncharge(&mc.from->memsw,
6522 PAGE_SIZE * mc.moved_swap);
6523 __mem_cgroup_put(mc.from, mc.moved_swap);
6524
6525 if (!mem_cgroup_is_root(mc.to)) {
6526 /*
6527 * we charged both to->res and to->memsw, so we should
6528 * uncharge to->res.
6529 */
6530 res_counter_uncharge(&mc.to->res,
6531 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006532 }
6533 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006534 mc.moved_swap = 0;
6535 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006536 memcg_oom_recover(from);
6537 memcg_oom_recover(to);
6538 wake_up_all(&mc.waitq);
6539}
6540
6541static void mem_cgroup_clear_mc(void)
6542{
6543 struct mem_cgroup *from = mc.from;
6544
6545 /*
6546 * we must clear moving_task before waking up waiters at the end of
6547 * task migration.
6548 */
6549 mc.moving_task = NULL;
6550 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006551 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006552 mc.from = NULL;
6553 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006554 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006555 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006556}
6557
Li Zefan761b3ef2012-01-31 13:47:36 +08006558static int mem_cgroup_can_attach(struct cgroup *cgroup,
6559 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006560{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006561 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006562 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006563 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006564
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006565 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006566 struct mm_struct *mm;
6567 struct mem_cgroup *from = mem_cgroup_from_task(p);
6568
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006569 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006570
6571 mm = get_task_mm(p);
6572 if (!mm)
6573 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006574 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006575 if (mm->owner == p) {
6576 VM_BUG_ON(mc.from);
6577 VM_BUG_ON(mc.to);
6578 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006579 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006580 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006581 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006582 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006583 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006584 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006585 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006586 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006587
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006588 ret = mem_cgroup_precharge_mc(mm);
6589 if (ret)
6590 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006591 }
6592 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006593 }
6594 return ret;
6595}
6596
Li Zefan761b3ef2012-01-31 13:47:36 +08006597static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6598 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006599{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006600 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006601}
6602
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006603static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6604 unsigned long addr, unsigned long end,
6605 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006606{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006607 int ret = 0;
6608 struct vm_area_struct *vma = walk->private;
6609 pte_t *pte;
6610 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006611 enum mc_target_type target_type;
6612 union mc_target target;
6613 struct page *page;
6614 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006615
Naoya Horiguchi12724852012-03-21 16:34:28 -07006616 /*
6617 * We don't take compound_lock() here but no race with splitting thp
6618 * happens because:
6619 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6620 * under splitting, which means there's no concurrent thp split,
6621 * - if another thread runs into split_huge_page() just after we
6622 * entered this if-block, the thread must wait for page table lock
6623 * to be unlocked in __split_huge_page_splitting(), where the main
6624 * part of thp split is not executed yet.
6625 */
6626 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006627 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006628 spin_unlock(&vma->vm_mm->page_table_lock);
6629 return 0;
6630 }
6631 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6632 if (target_type == MC_TARGET_PAGE) {
6633 page = target.page;
6634 if (!isolate_lru_page(page)) {
6635 pc = lookup_page_cgroup(page);
6636 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006637 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006638 mc.precharge -= HPAGE_PMD_NR;
6639 mc.moved_charge += HPAGE_PMD_NR;
6640 }
6641 putback_lru_page(page);
6642 }
6643 put_page(page);
6644 }
6645 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006646 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006647 }
6648
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006649 if (pmd_trans_unstable(pmd))
6650 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006651retry:
6652 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6653 for (; addr != end; addr += PAGE_SIZE) {
6654 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006655 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006656
6657 if (!mc.precharge)
6658 break;
6659
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006660 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006661 case MC_TARGET_PAGE:
6662 page = target.page;
6663 if (isolate_lru_page(page))
6664 goto put;
6665 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006666 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006667 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006668 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006669 /* we uncharge from mc.from later. */
6670 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006671 }
6672 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006673put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006674 put_page(page);
6675 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006676 case MC_TARGET_SWAP:
6677 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006678 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006679 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006680 /* we fixup refcnts and charges later. */
6681 mc.moved_swap++;
6682 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006683 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006684 default:
6685 break;
6686 }
6687 }
6688 pte_unmap_unlock(pte - 1, ptl);
6689 cond_resched();
6690
6691 if (addr != end) {
6692 /*
6693 * We have consumed all precharges we got in can_attach().
6694 * We try charge one by one, but don't do any additional
6695 * charges to mc.to if we have failed in charge once in attach()
6696 * phase.
6697 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006698 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006699 if (!ret)
6700 goto retry;
6701 }
6702
6703 return ret;
6704}
6705
6706static void mem_cgroup_move_charge(struct mm_struct *mm)
6707{
6708 struct vm_area_struct *vma;
6709
6710 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006711retry:
6712 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6713 /*
6714 * Someone who are holding the mmap_sem might be waiting in
6715 * waitq. So we cancel all extra charges, wake up all waiters,
6716 * and retry. Because we cancel precharges, we might not be able
6717 * to move enough charges, but moving charge is a best-effort
6718 * feature anyway, so it wouldn't be a big problem.
6719 */
6720 __mem_cgroup_clear_mc();
6721 cond_resched();
6722 goto retry;
6723 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006724 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6725 int ret;
6726 struct mm_walk mem_cgroup_move_charge_walk = {
6727 .pmd_entry = mem_cgroup_move_charge_pte_range,
6728 .mm = mm,
6729 .private = vma,
6730 };
6731 if (is_vm_hugetlb_page(vma))
6732 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006733 ret = walk_page_range(vma->vm_start, vma->vm_end,
6734 &mem_cgroup_move_charge_walk);
6735 if (ret)
6736 /*
6737 * means we have consumed all precharges and failed in
6738 * doing additional charge. Just abandon here.
6739 */
6740 break;
6741 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006742 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006743}
6744
Li Zefan761b3ef2012-01-31 13:47:36 +08006745static void mem_cgroup_move_task(struct cgroup *cont,
6746 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006747{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006748 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006749 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006750
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006751 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006752 if (mc.to)
6753 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006754 mmput(mm);
6755 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006756 if (mc.to)
6757 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006758}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006759#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006760static int mem_cgroup_can_attach(struct cgroup *cgroup,
6761 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006762{
6763 return 0;
6764}
Li Zefan761b3ef2012-01-31 13:47:36 +08006765static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6766 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006767{
6768}
Li Zefan761b3ef2012-01-31 13:47:36 +08006769static void mem_cgroup_move_task(struct cgroup *cont,
6770 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006771{
6772}
6773#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006774
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006775struct cgroup_subsys mem_cgroup_subsys = {
6776 .name = "memory",
6777 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006778 .css_alloc = mem_cgroup_css_alloc,
6779 .css_offline = mem_cgroup_css_offline,
6780 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006781 .can_attach = mem_cgroup_can_attach,
6782 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006783 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006784 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006785 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006786 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006787};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006788
Andrew Mortonc255a452012-07-31 16:43:02 -07006789#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006790static int __init enable_swap_account(char *s)
6791{
6792 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006793 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006794 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006795 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006796 really_do_swap_account = 0;
6797 return 1;
6798}
Michal Hockoa2c89902011-05-24 17:12:50 -07006799__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006800
Michal Hocko2d110852013-02-22 16:34:43 -08006801static void __init memsw_file_init(void)
6802{
6803 if (really_do_swap_account)
6804 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys,
6805 memsw_cgroup_files));
6806}
6807#else
6808static void __init memsw_file_init(void)
6809{
6810}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006811#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006812
6813/*
6814 * The rest of init is performed during ->css_alloc() for root css which
6815 * happens before initcalls. hotcpu_notifier() can't be done together as
6816 * it would introduce circular locking by adding cgroup_lock -> cpu hotplug
6817 * dependency. Do it from a subsys_initcall().
6818 */
6819static int __init mem_cgroup_init(void)
6820{
6821 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
6822 memsw_file_init();
6823 return 0;
6824}
6825subsys_initcall(mem_cgroup_init);