blob: 07bf3ec13a0710f7c9d03205b59b2e90fc82336a [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
1577 printk(KERN_INFO "Task in %s killed", memcg_name);
1578
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 */
1590 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1591done:
1592
1593 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1594 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));
1597 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1598 "failcnt %llu\n",
1599 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1600 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1601 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Glauber Costa510fc4e2012-12-18 14:21:47 -08001602 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1603 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1604 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1605 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001606
1607 for_each_mem_cgroup_tree(iter, memcg) {
1608 pr_info("Memory cgroup stats");
1609
1610 rcu_read_lock();
1611 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1612 if (!ret)
1613 pr_cont(" for %s", memcg_name);
1614 rcu_read_unlock();
1615 pr_cont(":");
1616
1617 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1618 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1619 continue;
1620 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1621 K(mem_cgroup_read_stat(iter, i)));
1622 }
1623
1624 for (i = 0; i < NR_LRU_LISTS; i++)
1625 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1626 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1627
1628 pr_cont("\n");
1629 }
Balbir Singhe2224322009-04-02 16:57:39 -07001630}
1631
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001632/*
1633 * This function returns the number of memcg under hierarchy tree. Returns
1634 * 1(self count) if no children.
1635 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001636static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001637{
1638 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001639 struct mem_cgroup *iter;
1640
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001641 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001642 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001643 return num;
1644}
1645
Balbir Singh6d61ef42009-01-07 18:08:06 -08001646/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001647 * Return the memory (and swap, if configured) limit for a memcg.
1648 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001649static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001650{
1651 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001652
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001653 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001654
David Rientjesa63d83f2010-08-09 17:19:46 -07001655 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001656 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001657 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001658 if (mem_cgroup_swappiness(memcg)) {
1659 u64 memsw;
1660
1661 limit += total_swap_pages << PAGE_SHIFT;
1662 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1663
1664 /*
1665 * If memsw is finite and limits the amount of swap space
1666 * available to this memcg, return that limit.
1667 */
1668 limit = min(limit, memsw);
1669 }
1670
1671 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001672}
1673
David Rientjes19965462012-12-11 16:00:26 -08001674static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1675 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001676{
1677 struct mem_cgroup *iter;
1678 unsigned long chosen_points = 0;
1679 unsigned long totalpages;
1680 unsigned int points = 0;
1681 struct task_struct *chosen = NULL;
1682
David Rientjes876aafb2012-07-31 16:43:48 -07001683 /*
1684 * If current has a pending SIGKILL, then automatically select it. The
1685 * goal is to allow it to allocate so that it may quickly exit and free
1686 * its memory.
1687 */
1688 if (fatal_signal_pending(current)) {
1689 set_thread_flag(TIF_MEMDIE);
1690 return;
1691 }
1692
1693 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001694 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1695 for_each_mem_cgroup_tree(iter, memcg) {
1696 struct cgroup *cgroup = iter->css.cgroup;
1697 struct cgroup_iter it;
1698 struct task_struct *task;
1699
1700 cgroup_iter_start(cgroup, &it);
1701 while ((task = cgroup_iter_next(cgroup, &it))) {
1702 switch (oom_scan_process_thread(task, totalpages, NULL,
1703 false)) {
1704 case OOM_SCAN_SELECT:
1705 if (chosen)
1706 put_task_struct(chosen);
1707 chosen = task;
1708 chosen_points = ULONG_MAX;
1709 get_task_struct(chosen);
1710 /* fall through */
1711 case OOM_SCAN_CONTINUE:
1712 continue;
1713 case OOM_SCAN_ABORT:
1714 cgroup_iter_end(cgroup, &it);
1715 mem_cgroup_iter_break(memcg, iter);
1716 if (chosen)
1717 put_task_struct(chosen);
1718 return;
1719 case OOM_SCAN_OK:
1720 break;
1721 };
1722 points = oom_badness(task, memcg, NULL, totalpages);
1723 if (points > chosen_points) {
1724 if (chosen)
1725 put_task_struct(chosen);
1726 chosen = task;
1727 chosen_points = points;
1728 get_task_struct(chosen);
1729 }
1730 }
1731 cgroup_iter_end(cgroup, &it);
1732 }
1733
1734 if (!chosen)
1735 return;
1736 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001737 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1738 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001739}
1740
Johannes Weiner56600482012-01-12 17:17:59 -08001741static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1742 gfp_t gfp_mask,
1743 unsigned long flags)
1744{
1745 unsigned long total = 0;
1746 bool noswap = false;
1747 int loop;
1748
1749 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1750 noswap = true;
1751 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1752 noswap = true;
1753
1754 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1755 if (loop)
1756 drain_all_stock_async(memcg);
1757 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1758 /*
1759 * Allow limit shrinkers, which are triggered directly
1760 * by userspace, to catch signals and stop reclaim
1761 * after minimal progress, regardless of the margin.
1762 */
1763 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1764 break;
1765 if (mem_cgroup_margin(memcg))
1766 break;
1767 /*
1768 * If nothing was reclaimed after two attempts, there
1769 * may be no reclaimable pages in this hierarchy.
1770 */
1771 if (loop && !total)
1772 break;
1773 }
1774 return total;
1775}
1776
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001777/**
1778 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001779 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001780 * @nid: the node ID to be checked.
1781 * @noswap : specify true here if the user wants flle only information.
1782 *
1783 * This function returns whether the specified memcg contains any
1784 * reclaimable pages on a node. Returns true if there are any reclaimable
1785 * pages in the node.
1786 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001787static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001788 int nid, bool noswap)
1789{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001790 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001791 return true;
1792 if (noswap || !total_swap_pages)
1793 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001794 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001795 return true;
1796 return false;
1797
1798}
Ying Han889976d2011-05-26 16:25:33 -07001799#if MAX_NUMNODES > 1
1800
1801/*
1802 * Always updating the nodemask is not very good - even if we have an empty
1803 * list or the wrong list here, we can start from some node and traverse all
1804 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1805 *
1806 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001807static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001808{
1809 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001810 /*
1811 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1812 * pagein/pageout changes since the last update.
1813 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001814 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001815 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001816 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001817 return;
1818
Ying Han889976d2011-05-26 16:25:33 -07001819 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001820 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001821
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001822 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001823
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001824 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1825 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001826 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001828 atomic_set(&memcg->numainfo_events, 0);
1829 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001830}
1831
1832/*
1833 * Selecting a node where we start reclaim from. Because what we need is just
1834 * reducing usage counter, start from anywhere is O,K. Considering
1835 * memory reclaim from current node, there are pros. and cons.
1836 *
1837 * Freeing memory from current node means freeing memory from a node which
1838 * we'll use or we've used. So, it may make LRU bad. And if several threads
1839 * hit limits, it will see a contention on a node. But freeing from remote
1840 * node means more costs for memory reclaim because of memory latency.
1841 *
1842 * Now, we use round-robin. Better algorithm is welcomed.
1843 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001844int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001845{
1846 int node;
1847
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001848 mem_cgroup_may_update_nodemask(memcg);
1849 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001850
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001851 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001852 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001854 /*
1855 * We call this when we hit limit, not when pages are added to LRU.
1856 * No LRU may hold pages because all pages are UNEVICTABLE or
1857 * memcg is too small and all pages are not on LRU. In that case,
1858 * we use curret node.
1859 */
1860 if (unlikely(node == MAX_NUMNODES))
1861 node = numa_node_id();
1862
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001863 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001864 return node;
1865}
1866
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001867/*
1868 * Check all nodes whether it contains reclaimable pages or not.
1869 * For quick scan, we make use of scan_nodes. This will allow us to skip
1870 * unused nodes. But scan_nodes is lazily updated and may not cotain
1871 * enough new information. We need to do double check.
1872 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001873static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001874{
1875 int nid;
1876
1877 /*
1878 * quick check...making use of scan_node.
1879 * We can skip unused nodes.
1880 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881 if (!nodes_empty(memcg->scan_nodes)) {
1882 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001883 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001884 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001885
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001886 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001887 return true;
1888 }
1889 }
1890 /*
1891 * Check rest of nodes.
1892 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001893 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001894 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001895 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001897 return true;
1898 }
1899 return false;
1900}
1901
Ying Han889976d2011-05-26 16:25:33 -07001902#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001903int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001904{
1905 return 0;
1906}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001907
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001908static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001909{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001910 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001911}
Ying Han889976d2011-05-26 16:25:33 -07001912#endif
1913
Johannes Weiner56600482012-01-12 17:17:59 -08001914static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1915 struct zone *zone,
1916 gfp_t gfp_mask,
1917 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001918{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001919 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001920 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001921 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001922 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001923 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001924 struct mem_cgroup_reclaim_cookie reclaim = {
1925 .zone = zone,
1926 .priority = 0,
1927 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001928
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001929 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001930
Balbir Singh4e416952009-09-23 15:56:39 -07001931 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001932 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001933 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001934 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001935 if (loop >= 2) {
1936 /*
1937 * If we have not been able to reclaim
1938 * anything, it might because there are
1939 * no reclaimable pages under this hierarchy
1940 */
Johannes Weiner56600482012-01-12 17:17:59 -08001941 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001942 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001943 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001944 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001945 * excess >> 2 is not to excessive so as to
1946 * reclaim too much, nor too less that we keep
1947 * coming back to reclaim from this cgroup
1948 */
1949 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001950 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001951 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001952 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001953 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001954 }
Johannes Weiner56600482012-01-12 17:17:59 -08001955 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001956 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001957 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1958 zone, &nr_scanned);
1959 *total_scanned += nr_scanned;
1960 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001961 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001962 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001963 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001964 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001965}
1966
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001967/*
1968 * Check OOM-Killer is already running under our hierarchy.
1969 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001970 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001971 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001972static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001973{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001974 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001975
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001976 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001977 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001978 /*
1979 * this subtree of our hierarchy is already locked
1980 * so we cannot give a lock.
1981 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001982 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001983 mem_cgroup_iter_break(memcg, iter);
1984 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001985 } else
1986 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001987 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001988
Michal Hocko79dfdac2011-07-26 16:08:23 -07001989 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001990 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001991
1992 /*
1993 * OK, we failed to lock the whole subtree so we have to clean up
1994 * what we set up to the failing subtree
1995 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001996 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001997 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001998 mem_cgroup_iter_break(memcg, iter);
1999 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002000 }
2001 iter->oom_lock = false;
2002 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002003 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002004}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002005
Michal Hocko79dfdac2011-07-26 16:08:23 -07002006/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002007 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002008 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002009static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002010{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002011 struct mem_cgroup *iter;
2012
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002013 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002014 iter->oom_lock = false;
2015 return 0;
2016}
2017
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002019{
2020 struct mem_cgroup *iter;
2021
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002022 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002023 atomic_inc(&iter->under_oom);
2024}
2025
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002026static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002027{
2028 struct mem_cgroup *iter;
2029
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002030 /*
2031 * When a new child is created while the hierarchy is under oom,
2032 * mem_cgroup_oom_lock() may not be called. We have to use
2033 * atomic_add_unless() here.
2034 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002035 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002036 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002037}
2038
Michal Hocko1af8efe2011-07-26 16:08:24 -07002039static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002040static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2041
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002042struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002043 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002044 wait_queue_t wait;
2045};
2046
2047static int memcg_oom_wake_function(wait_queue_t *wait,
2048 unsigned mode, int sync, void *arg)
2049{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002050 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2051 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002052 struct oom_wait_info *oom_wait_info;
2053
2054 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002055 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002056
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002057 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002058 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002059 * Then we can use css_is_ancestor without taking care of RCU.
2060 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002061 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2062 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002063 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002064 return autoremove_wake_function(wait, mode, sync, arg);
2065}
2066
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002067static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002068{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002069 /* for filtering, pass "memcg" as argument. */
2070 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002071}
2072
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002073static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002074{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002075 if (memcg && atomic_read(&memcg->under_oom))
2076 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002077}
2078
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002079/*
2080 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2081 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002082static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2083 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002084{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002085 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002086 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002087
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002088 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002089 owait.wait.flags = 0;
2090 owait.wait.func = memcg_oom_wake_function;
2091 owait.wait.private = current;
2092 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002093 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002094 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002095
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002096 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002097 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002098 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002099 /*
2100 * Even if signal_pending(), we can't quit charge() loop without
2101 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2102 * under OOM is always welcomed, use TASK_KILLABLE here.
2103 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002104 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002105 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002106 need_to_kill = false;
2107 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002108 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002109 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002110
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002111 if (need_to_kill) {
2112 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002113 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002114 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002115 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002116 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002117 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002118 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002119 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002120 mem_cgroup_oom_unlock(memcg);
2121 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002122 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002123
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002124 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002125
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002126 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2127 return false;
2128 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002129 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002130 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002131}
2132
Balbir Singhd69b0422009-06-17 16:26:34 -07002133/*
2134 * Currently used to update mapped file statistics, but the routine can be
2135 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002136 *
2137 * Notes: Race condition
2138 *
2139 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2140 * it tends to be costly. But considering some conditions, we doesn't need
2141 * to do so _always_.
2142 *
2143 * Considering "charge", lock_page_cgroup() is not required because all
2144 * file-stat operations happen after a page is attached to radix-tree. There
2145 * are no race with "charge".
2146 *
2147 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2148 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2149 * if there are race with "uncharge". Statistics itself is properly handled
2150 * by flags.
2151 *
2152 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002153 * small, we check mm->moving_account and detect there are possibility of race
2154 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002155 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002156
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002157void __mem_cgroup_begin_update_page_stat(struct page *page,
2158 bool *locked, unsigned long *flags)
2159{
2160 struct mem_cgroup *memcg;
2161 struct page_cgroup *pc;
2162
2163 pc = lookup_page_cgroup(page);
2164again:
2165 memcg = pc->mem_cgroup;
2166 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2167 return;
2168 /*
2169 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002170 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002171 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002172 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002173 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002174 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002175 return;
2176
2177 move_lock_mem_cgroup(memcg, flags);
2178 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2179 move_unlock_mem_cgroup(memcg, flags);
2180 goto again;
2181 }
2182 *locked = true;
2183}
2184
2185void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2186{
2187 struct page_cgroup *pc = lookup_page_cgroup(page);
2188
2189 /*
2190 * It's guaranteed that pc->mem_cgroup never changes while
2191 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002192 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002193 */
2194 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2195}
2196
Greg Thelen2a7106f2011-01-13 15:47:37 -08002197void mem_cgroup_update_page_stat(struct page *page,
2198 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002199{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002200 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002201 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002202 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002203
Johannes Weinercfa44942012-01-12 17:18:38 -08002204 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002205 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002206
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002207 memcg = pc->mem_cgroup;
2208 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002209 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002210
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002211 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002212 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002213 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002214 break;
2215 default:
2216 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002217 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002218
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002219 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002220}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002221
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002222/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002223 * size of first charge trial. "32" comes from vmscan.c's magic value.
2224 * TODO: maybe necessary to use big numbers in big irons.
2225 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002226#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002227struct memcg_stock_pcp {
2228 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002229 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002230 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002231 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002232#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002233};
2234static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002235static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002236
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002237/**
2238 * consume_stock: Try to consume stocked charge on this cpu.
2239 * @memcg: memcg to consume from.
2240 * @nr_pages: how many pages to charge.
2241 *
2242 * The charges will only happen if @memcg matches the current cpu's memcg
2243 * stock, and at least @nr_pages are available in that stock. Failure to
2244 * service an allocation will refill the stock.
2245 *
2246 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002247 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002248static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002249{
2250 struct memcg_stock_pcp *stock;
2251 bool ret = true;
2252
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002253 if (nr_pages > CHARGE_BATCH)
2254 return false;
2255
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002256 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002257 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2258 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002259 else /* need to call res_counter_charge */
2260 ret = false;
2261 put_cpu_var(memcg_stock);
2262 return ret;
2263}
2264
2265/*
2266 * Returns stocks cached in percpu to res_counter and reset cached information.
2267 */
2268static void drain_stock(struct memcg_stock_pcp *stock)
2269{
2270 struct mem_cgroup *old = stock->cached;
2271
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002272 if (stock->nr_pages) {
2273 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2274
2275 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002276 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002277 res_counter_uncharge(&old->memsw, bytes);
2278 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279 }
2280 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002281}
2282
2283/*
2284 * This must be called under preempt disabled or must be called by
2285 * a thread which is pinned to local cpu.
2286 */
2287static void drain_local_stock(struct work_struct *dummy)
2288{
2289 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2290 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002291 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002292}
2293
2294/*
2295 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002296 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002297 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002298static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002299{
2300 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002302 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002303 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002304 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002305 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002306 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002307 put_cpu_var(memcg_stock);
2308}
2309
2310/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002311 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002312 * of the hierarchy under it. sync flag says whether we should block
2313 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002314 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002315static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002316{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002317 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002318
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002319 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002320 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002321 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002322 for_each_online_cpu(cpu) {
2323 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002325
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002326 memcg = stock->cached;
2327 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002328 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002329 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002330 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002331 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2332 if (cpu == curcpu)
2333 drain_local_stock(&stock->work);
2334 else
2335 schedule_work_on(cpu, &stock->work);
2336 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002337 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002338 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002339
2340 if (!sync)
2341 goto out;
2342
2343 for_each_online_cpu(cpu) {
2344 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002345 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002346 flush_work(&stock->work);
2347 }
2348out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002349 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002350}
2351
2352/*
2353 * Tries to drain stocked charges in other cpus. This function is asynchronous
2354 * and just put a work per cpu for draining localy on each cpu. Caller can
2355 * expects some charges will be back to res_counter later but cannot wait for
2356 * it.
2357 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002359{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002360 /*
2361 * If someone calls draining, avoid adding more kworker runs.
2362 */
2363 if (!mutex_trylock(&percpu_charge_mutex))
2364 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002366 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002367}
2368
2369/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002370static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002371{
2372 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002373 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002375 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002376}
2377
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002378/*
2379 * This function drains percpu counter value from DEAD cpu and
2380 * move it to local cpu. Note that this function can be preempted.
2381 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002382static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002383{
2384 int i;
2385
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002387 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002388 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002389
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002390 per_cpu(memcg->stat->count[i], cpu) = 0;
2391 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002392 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002393 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002394 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002395
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002396 per_cpu(memcg->stat->events[i], cpu) = 0;
2397 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002398 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002399 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002400}
2401
2402static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002403 unsigned long action,
2404 void *hcpu)
2405{
2406 int cpu = (unsigned long)hcpu;
2407 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002408 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002409
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002410 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002411 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002412
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002413 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002414 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002415
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002416 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002417 mem_cgroup_drain_pcp_counter(iter, cpu);
2418
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002419 stock = &per_cpu(memcg_stock, cpu);
2420 drain_stock(stock);
2421 return NOTIFY_OK;
2422}
2423
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002424
2425/* See __mem_cgroup_try_charge() for details */
2426enum {
2427 CHARGE_OK, /* success */
2428 CHARGE_RETRY, /* need to retry but retry is not bad */
2429 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2430 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2431 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2432};
2433
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002434static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002435 unsigned int nr_pages, unsigned int min_pages,
2436 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002437{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002438 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002439 struct mem_cgroup *mem_over_limit;
2440 struct res_counter *fail_res;
2441 unsigned long flags = 0;
2442 int ret;
2443
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002444 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002445
2446 if (likely(!ret)) {
2447 if (!do_swap_account)
2448 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002449 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002450 if (likely(!ret))
2451 return CHARGE_OK;
2452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002453 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2455 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2456 } else
2457 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002458 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002459 * Never reclaim on behalf of optional batching, retry with a
2460 * single page instead.
2461 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002462 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002463 return CHARGE_RETRY;
2464
2465 if (!(gfp_mask & __GFP_WAIT))
2466 return CHARGE_WOULDBLOCK;
2467
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002468 if (gfp_mask & __GFP_NORETRY)
2469 return CHARGE_NOMEM;
2470
Johannes Weiner56600482012-01-12 17:17:59 -08002471 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002472 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002473 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002474 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002475 * Even though the limit is exceeded at this point, reclaim
2476 * may have been able to free some pages. Retry the charge
2477 * before killing the task.
2478 *
2479 * Only for regular pages, though: huge pages are rather
2480 * unlikely to succeed so close to the limit, and we fall back
2481 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002482 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002483 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002484 return CHARGE_RETRY;
2485
2486 /*
2487 * At task move, charge accounts can be doubly counted. So, it's
2488 * better to wait until the end of task_move if something is going on.
2489 */
2490 if (mem_cgroup_wait_acct_move(mem_over_limit))
2491 return CHARGE_RETRY;
2492
2493 /* If we don't need to call oom-killer at el, return immediately */
2494 if (!oom_check)
2495 return CHARGE_NOMEM;
2496 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002497 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002498 return CHARGE_OOM_DIE;
2499
2500 return CHARGE_RETRY;
2501}
2502
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002503/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002504 * __mem_cgroup_try_charge() does
2505 * 1. detect memcg to be charged against from passed *mm and *ptr,
2506 * 2. update res_counter
2507 * 3. call memory reclaim if necessary.
2508 *
2509 * In some special case, if the task is fatal, fatal_signal_pending() or
2510 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2511 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2512 * as possible without any hazards. 2: all pages should have a valid
2513 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2514 * pointer, that is treated as a charge to root_mem_cgroup.
2515 *
2516 * So __mem_cgroup_try_charge() will return
2517 * 0 ... on success, filling *ptr with a valid memcg pointer.
2518 * -ENOMEM ... charge failure because of resource limits.
2519 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2520 *
2521 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2522 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002523 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002524static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002525 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002526 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002527 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002528 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002529{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002530 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002531 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002532 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002533 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002534
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002535 /*
2536 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2537 * in system level. So, allow to go ahead dying process in addition to
2538 * MEMDIE process.
2539 */
2540 if (unlikely(test_thread_flag(TIF_MEMDIE)
2541 || fatal_signal_pending(current)))
2542 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002543
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002544 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002545 * We always charge the cgroup the mm_struct belongs to.
2546 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002547 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002548 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002549 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002550 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002551 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002552again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002553 if (*ptr) { /* css should be a valid one */
2554 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002555 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002556 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002557 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002558 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002559 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002560 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002561 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002562
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002563 rcu_read_lock();
2564 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002565 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002566 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002567 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002568 * race with swapoff. Then, we have small risk of mis-accouning.
2569 * But such kind of mis-account by race always happens because
2570 * we don't have cgroup_mutex(). It's overkill and we allo that
2571 * small race, here.
2572 * (*) swapoff at el will charge against mm-struct not against
2573 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002574 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002575 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002576 if (!memcg)
2577 memcg = root_mem_cgroup;
2578 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002579 rcu_read_unlock();
2580 goto done;
2581 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002582 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002583 /*
2584 * It seems dagerous to access memcg without css_get().
2585 * But considering how consume_stok works, it's not
2586 * necessary. If consume_stock success, some charges
2587 * from this memcg are cached on this cpu. So, we
2588 * don't need to call css_get()/css_tryget() before
2589 * calling consume_stock().
2590 */
2591 rcu_read_unlock();
2592 goto done;
2593 }
2594 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002595 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002596 rcu_read_unlock();
2597 goto again;
2598 }
2599 rcu_read_unlock();
2600 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002601
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002602 do {
2603 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002604
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002605 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002606 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002607 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002608 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002609 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002610
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002611 oom_check = false;
2612 if (oom && !nr_oom_retries) {
2613 oom_check = true;
2614 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2615 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002616
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002617 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2618 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002619 switch (ret) {
2620 case CHARGE_OK:
2621 break;
2622 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002623 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002624 css_put(&memcg->css);
2625 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002626 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002627 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002628 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002629 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002630 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002631 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002632 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002633 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002634 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002635 /* If oom, we never return -ENOMEM */
2636 nr_oom_retries--;
2637 break;
2638 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002639 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002640 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002641 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002642 } while (ret != CHARGE_OK);
2643
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002644 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002645 refill_stock(memcg, batch - nr_pages);
2646 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002647done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002648 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002649 return 0;
2650nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002651 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002652 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002653bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002654 *ptr = root_mem_cgroup;
2655 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002656}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002657
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002658/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002659 * Somemtimes we have to undo a charge we got by try_charge().
2660 * This function is for that and do uncharge, put css's refcnt.
2661 * gotten by try_charge().
2662 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002663static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002664 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002665{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002666 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002667 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002668
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002669 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002670 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002671 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002672 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002673}
2674
2675/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002676 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2677 * This is useful when moving usage to parent cgroup.
2678 */
2679static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2680 unsigned int nr_pages)
2681{
2682 unsigned long bytes = nr_pages * PAGE_SIZE;
2683
2684 if (mem_cgroup_is_root(memcg))
2685 return;
2686
2687 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2688 if (do_swap_account)
2689 res_counter_uncharge_until(&memcg->memsw,
2690 memcg->memsw.parent, bytes);
2691}
2692
2693/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002694 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002695 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2696 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2697 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002698 */
2699static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2700{
2701 struct cgroup_subsys_state *css;
2702
2703 /* ID 0 is unused ID */
2704 if (!id)
2705 return NULL;
2706 css = css_lookup(&mem_cgroup_subsys, id);
2707 if (!css)
2708 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002709 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002710}
2711
Wu Fengguange42d9d52009-12-16 12:19:59 +01002712struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002713{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002714 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002715 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002716 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002717 swp_entry_t ent;
2718
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002719 VM_BUG_ON(!PageLocked(page));
2720
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002721 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002722 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002723 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002724 memcg = pc->mem_cgroup;
2725 if (memcg && !css_tryget(&memcg->css))
2726 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002727 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002728 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002729 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002730 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002731 memcg = mem_cgroup_lookup(id);
2732 if (memcg && !css_tryget(&memcg->css))
2733 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002734 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002735 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002736 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002737 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002738}
2739
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002740static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002741 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002742 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002743 enum charge_type ctype,
2744 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002745{
Johannes Weinerce587e62012-04-24 20:22:33 +02002746 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002747 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002748 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002749 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002750 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002751
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002752 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002753 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002754 /*
2755 * we don't need page_cgroup_lock about tail pages, becase they are not
2756 * accessed by any other context at this point.
2757 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002758
2759 /*
2760 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2761 * may already be on some other mem_cgroup's LRU. Take care of it.
2762 */
2763 if (lrucare) {
2764 zone = page_zone(page);
2765 spin_lock_irq(&zone->lru_lock);
2766 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002767 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002768 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002769 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002770 was_on_lru = true;
2771 }
2772 }
2773
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002774 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002775 /*
2776 * We access a page_cgroup asynchronously without lock_page_cgroup().
2777 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2778 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2779 * before USED bit, we need memory barrier here.
2780 * See mem_cgroup_add_lru_list(), etc.
2781 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002782 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002783 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002784
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002785 if (lrucare) {
2786 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002787 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002788 VM_BUG_ON(PageLRU(page));
2789 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002790 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002791 }
2792 spin_unlock_irq(&zone->lru_lock);
2793 }
2794
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002795 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002796 anon = true;
2797 else
2798 anon = false;
2799
2800 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002801 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002802
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002803 /*
2804 * "charge_statistics" updated event counter. Then, check it.
2805 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2806 * if they exceeds softlimit.
2807 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002808 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002809}
2810
Glauber Costa7cf27982012-12-18 14:22:55 -08002811static DEFINE_MUTEX(set_limit_mutex);
2812
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002813#ifdef CONFIG_MEMCG_KMEM
2814static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2815{
2816 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2817 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2818}
2819
Glauber Costa1f458cb2012-12-18 14:22:50 -08002820/*
2821 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2822 * in the memcg_cache_params struct.
2823 */
2824static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2825{
2826 struct kmem_cache *cachep;
2827
2828 VM_BUG_ON(p->is_root_cache);
2829 cachep = p->root_cache;
2830 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2831}
2832
Glauber Costa749c5412012-12-18 14:23:01 -08002833#ifdef CONFIG_SLABINFO
2834static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2835 struct seq_file *m)
2836{
2837 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2838 struct memcg_cache_params *params;
2839
2840 if (!memcg_can_account_kmem(memcg))
2841 return -EIO;
2842
2843 print_slabinfo_header(m);
2844
2845 mutex_lock(&memcg->slab_caches_mutex);
2846 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2847 cache_show(memcg_params_to_cache(params), m);
2848 mutex_unlock(&memcg->slab_caches_mutex);
2849
2850 return 0;
2851}
2852#endif
2853
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002854static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2855{
2856 struct res_counter *fail_res;
2857 struct mem_cgroup *_memcg;
2858 int ret = 0;
2859 bool may_oom;
2860
2861 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2862 if (ret)
2863 return ret;
2864
2865 /*
2866 * Conditions under which we can wait for the oom_killer. Those are
2867 * the same conditions tested by the core page allocator
2868 */
2869 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2870
2871 _memcg = memcg;
2872 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2873 &_memcg, may_oom);
2874
2875 if (ret == -EINTR) {
2876 /*
2877 * __mem_cgroup_try_charge() chosed to bypass to root due to
2878 * OOM kill or fatal signal. Since our only options are to
2879 * either fail the allocation or charge it to this cgroup, do
2880 * it as a temporary condition. But we can't fail. From a
2881 * kmem/slab perspective, the cache has already been selected,
2882 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2883 * our minds.
2884 *
2885 * This condition will only trigger if the task entered
2886 * memcg_charge_kmem in a sane state, but was OOM-killed during
2887 * __mem_cgroup_try_charge() above. Tasks that were already
2888 * dying when the allocation triggers should have been already
2889 * directed to the root cgroup in memcontrol.h
2890 */
2891 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2892 if (do_swap_account)
2893 res_counter_charge_nofail(&memcg->memsw, size,
2894 &fail_res);
2895 ret = 0;
2896 } else if (ret)
2897 res_counter_uncharge(&memcg->kmem, size);
2898
2899 return ret;
2900}
2901
2902static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2903{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002904 res_counter_uncharge(&memcg->res, size);
2905 if (do_swap_account)
2906 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002907
2908 /* Not down to 0 */
2909 if (res_counter_uncharge(&memcg->kmem, size))
2910 return;
2911
2912 if (memcg_kmem_test_and_clear_dead(memcg))
2913 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002914}
2915
Glauber Costa2633d7a2012-12-18 14:22:34 -08002916void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2917{
2918 if (!memcg)
2919 return;
2920
2921 mutex_lock(&memcg->slab_caches_mutex);
2922 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2923 mutex_unlock(&memcg->slab_caches_mutex);
2924}
2925
2926/*
2927 * helper for acessing a memcg's index. It will be used as an index in the
2928 * child cache array in kmem_cache, and also to derive its name. This function
2929 * will return -1 when this is not a kmem-limited memcg.
2930 */
2931int memcg_cache_id(struct mem_cgroup *memcg)
2932{
2933 return memcg ? memcg->kmemcg_id : -1;
2934}
2935
Glauber Costa55007d82012-12-18 14:22:38 -08002936/*
2937 * This ends up being protected by the set_limit mutex, during normal
2938 * operation, because that is its main call site.
2939 *
2940 * But when we create a new cache, we can call this as well if its parent
2941 * is kmem-limited. That will have to hold set_limit_mutex as well.
2942 */
2943int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2944{
2945 int num, ret;
2946
2947 num = ida_simple_get(&kmem_limited_groups,
2948 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2949 if (num < 0)
2950 return num;
2951 /*
2952 * After this point, kmem_accounted (that we test atomically in
2953 * the beginning of this conditional), is no longer 0. This
2954 * guarantees only one process will set the following boolean
2955 * to true. We don't need test_and_set because we're protected
2956 * by the set_limit_mutex anyway.
2957 */
2958 memcg_kmem_set_activated(memcg);
2959
2960 ret = memcg_update_all_caches(num+1);
2961 if (ret) {
2962 ida_simple_remove(&kmem_limited_groups, num);
2963 memcg_kmem_clear_activated(memcg);
2964 return ret;
2965 }
2966
2967 memcg->kmemcg_id = num;
2968 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2969 mutex_init(&memcg->slab_caches_mutex);
2970 return 0;
2971}
2972
2973static size_t memcg_caches_array_size(int num_groups)
2974{
2975 ssize_t size;
2976 if (num_groups <= 0)
2977 return 0;
2978
2979 size = 2 * num_groups;
2980 if (size < MEMCG_CACHES_MIN_SIZE)
2981 size = MEMCG_CACHES_MIN_SIZE;
2982 else if (size > MEMCG_CACHES_MAX_SIZE)
2983 size = MEMCG_CACHES_MAX_SIZE;
2984
2985 return size;
2986}
2987
2988/*
2989 * We should update the current array size iff all caches updates succeed. This
2990 * can only be done from the slab side. The slab mutex needs to be held when
2991 * calling this.
2992 */
2993void memcg_update_array_size(int num)
2994{
2995 if (num > memcg_limited_groups_array_size)
2996 memcg_limited_groups_array_size = memcg_caches_array_size(num);
2997}
2998
2999int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3000{
3001 struct memcg_cache_params *cur_params = s->memcg_params;
3002
3003 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3004
3005 if (num_groups > memcg_limited_groups_array_size) {
3006 int i;
3007 ssize_t size = memcg_caches_array_size(num_groups);
3008
3009 size *= sizeof(void *);
3010 size += sizeof(struct memcg_cache_params);
3011
3012 s->memcg_params = kzalloc(size, GFP_KERNEL);
3013 if (!s->memcg_params) {
3014 s->memcg_params = cur_params;
3015 return -ENOMEM;
3016 }
3017
3018 s->memcg_params->is_root_cache = true;
3019
3020 /*
3021 * There is the chance it will be bigger than
3022 * memcg_limited_groups_array_size, if we failed an allocation
3023 * in a cache, in which case all caches updated before it, will
3024 * have a bigger array.
3025 *
3026 * But if that is the case, the data after
3027 * memcg_limited_groups_array_size is certainly unused
3028 */
3029 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3030 if (!cur_params->memcg_caches[i])
3031 continue;
3032 s->memcg_params->memcg_caches[i] =
3033 cur_params->memcg_caches[i];
3034 }
3035
3036 /*
3037 * Ideally, we would wait until all caches succeed, and only
3038 * then free the old one. But this is not worth the extra
3039 * pointer per-cache we'd have to have for this.
3040 *
3041 * It is not a big deal if some caches are left with a size
3042 * bigger than the others. And all updates will reset this
3043 * anyway.
3044 */
3045 kfree(cur_params);
3046 }
3047 return 0;
3048}
3049
Glauber Costa943a4512012-12-18 14:23:03 -08003050int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3051 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003052{
3053 size_t size = sizeof(struct memcg_cache_params);
3054
3055 if (!memcg_kmem_enabled())
3056 return 0;
3057
Glauber Costa55007d82012-12-18 14:22:38 -08003058 if (!memcg)
3059 size += memcg_limited_groups_array_size * sizeof(void *);
3060
Glauber Costa2633d7a2012-12-18 14:22:34 -08003061 s->memcg_params = kzalloc(size, GFP_KERNEL);
3062 if (!s->memcg_params)
3063 return -ENOMEM;
3064
Glauber Costa943a4512012-12-18 14:23:03 -08003065 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003066 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003067 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003068 } else
3069 s->memcg_params->is_root_cache = true;
3070
Glauber Costa2633d7a2012-12-18 14:22:34 -08003071 return 0;
3072}
3073
3074void memcg_release_cache(struct kmem_cache *s)
3075{
Glauber Costad7f25f82012-12-18 14:22:40 -08003076 struct kmem_cache *root;
3077 struct mem_cgroup *memcg;
3078 int id;
3079
3080 /*
3081 * This happens, for instance, when a root cache goes away before we
3082 * add any memcg.
3083 */
3084 if (!s->memcg_params)
3085 return;
3086
3087 if (s->memcg_params->is_root_cache)
3088 goto out;
3089
3090 memcg = s->memcg_params->memcg;
3091 id = memcg_cache_id(memcg);
3092
3093 root = s->memcg_params->root_cache;
3094 root->memcg_params->memcg_caches[id] = NULL;
3095 mem_cgroup_put(memcg);
3096
3097 mutex_lock(&memcg->slab_caches_mutex);
3098 list_del(&s->memcg_params->list);
3099 mutex_unlock(&memcg->slab_caches_mutex);
3100
3101out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003102 kfree(s->memcg_params);
3103}
3104
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003105/*
3106 * During the creation a new cache, we need to disable our accounting mechanism
3107 * altogether. This is true even if we are not creating, but rather just
3108 * enqueing new caches to be created.
3109 *
3110 * This is because that process will trigger allocations; some visible, like
3111 * explicit kmallocs to auxiliary data structures, name strings and internal
3112 * cache structures; some well concealed, like INIT_WORK() that can allocate
3113 * objects during debug.
3114 *
3115 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3116 * to it. This may not be a bounded recursion: since the first cache creation
3117 * failed to complete (waiting on the allocation), we'll just try to create the
3118 * cache again, failing at the same point.
3119 *
3120 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3121 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3122 * inside the following two functions.
3123 */
3124static inline void memcg_stop_kmem_account(void)
3125{
3126 VM_BUG_ON(!current->mm);
3127 current->memcg_kmem_skip_account++;
3128}
3129
3130static inline void memcg_resume_kmem_account(void)
3131{
3132 VM_BUG_ON(!current->mm);
3133 current->memcg_kmem_skip_account--;
3134}
3135
Glauber Costa1f458cb2012-12-18 14:22:50 -08003136static void kmem_cache_destroy_work_func(struct work_struct *w)
3137{
3138 struct kmem_cache *cachep;
3139 struct memcg_cache_params *p;
3140
3141 p = container_of(w, struct memcg_cache_params, destroy);
3142
3143 cachep = memcg_params_to_cache(p);
3144
Glauber Costa22933152012-12-18 14:22:59 -08003145 /*
3146 * If we get down to 0 after shrink, we could delete right away.
3147 * However, memcg_release_pages() already puts us back in the workqueue
3148 * in that case. If we proceed deleting, we'll get a dangling
3149 * reference, and removing the object from the workqueue in that case
3150 * is unnecessary complication. We are not a fast path.
3151 *
3152 * Note that this case is fundamentally different from racing with
3153 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3154 * kmem_cache_shrink, not only we would be reinserting a dead cache
3155 * into the queue, but doing so from inside the worker racing to
3156 * destroy it.
3157 *
3158 * So if we aren't down to zero, we'll just schedule a worker and try
3159 * again
3160 */
3161 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3162 kmem_cache_shrink(cachep);
3163 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3164 return;
3165 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003166 kmem_cache_destroy(cachep);
3167}
3168
3169void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3170{
3171 if (!cachep->memcg_params->dead)
3172 return;
3173
3174 /*
Glauber Costa22933152012-12-18 14:22:59 -08003175 * There are many ways in which we can get here.
3176 *
3177 * We can get to a memory-pressure situation while the delayed work is
3178 * still pending to run. The vmscan shrinkers can then release all
3179 * cache memory and get us to destruction. If this is the case, we'll
3180 * be executed twice, which is a bug (the second time will execute over
3181 * bogus data). In this case, cancelling the work should be fine.
3182 *
3183 * But we can also get here from the worker itself, if
3184 * kmem_cache_shrink is enough to shake all the remaining objects and
3185 * get the page count to 0. In this case, we'll deadlock if we try to
3186 * cancel the work (the worker runs with an internal lock held, which
3187 * is the same lock we would hold for cancel_work_sync().)
3188 *
3189 * Since we can't possibly know who got us here, just refrain from
3190 * running if there is already work pending
3191 */
3192 if (work_pending(&cachep->memcg_params->destroy))
3193 return;
3194 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003195 * We have to defer the actual destroying to a workqueue, because
3196 * we might currently be in a context that cannot sleep.
3197 */
3198 schedule_work(&cachep->memcg_params->destroy);
3199}
3200
Glauber Costad7f25f82012-12-18 14:22:40 -08003201static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3202{
3203 char *name;
3204 struct dentry *dentry;
3205
3206 rcu_read_lock();
3207 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3208 rcu_read_unlock();
3209
3210 BUG_ON(dentry == NULL);
3211
3212 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3213 memcg_cache_id(memcg), dentry->d_name.name);
3214
3215 return name;
3216}
3217
3218static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3219 struct kmem_cache *s)
3220{
3221 char *name;
3222 struct kmem_cache *new;
3223
3224 name = memcg_cache_name(memcg, s);
3225 if (!name)
3226 return NULL;
3227
3228 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003229 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003230
Glauber Costad79923f2012-12-18 14:22:48 -08003231 if (new)
3232 new->allocflags |= __GFP_KMEMCG;
3233
Glauber Costad7f25f82012-12-18 14:22:40 -08003234 kfree(name);
3235 return new;
3236}
3237
3238/*
3239 * This lock protects updaters, not readers. We want readers to be as fast as
3240 * they can, and they will either see NULL or a valid cache value. Our model
3241 * allow them to see NULL, in which case the root memcg will be selected.
3242 *
3243 * We need this lock because multiple allocations to the same cache from a non
3244 * will span more than one worker. Only one of them can create the cache.
3245 */
3246static DEFINE_MUTEX(memcg_cache_mutex);
3247static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3248 struct kmem_cache *cachep)
3249{
3250 struct kmem_cache *new_cachep;
3251 int idx;
3252
3253 BUG_ON(!memcg_can_account_kmem(memcg));
3254
3255 idx = memcg_cache_id(memcg);
3256
3257 mutex_lock(&memcg_cache_mutex);
3258 new_cachep = cachep->memcg_params->memcg_caches[idx];
3259 if (new_cachep)
3260 goto out;
3261
3262 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003263 if (new_cachep == NULL) {
3264 new_cachep = cachep;
3265 goto out;
3266 }
3267
3268 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003269 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003270
3271 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3272 /*
3273 * the readers won't lock, make sure everybody sees the updated value,
3274 * so they won't put stuff in the queue again for no reason
3275 */
3276 wmb();
3277out:
3278 mutex_unlock(&memcg_cache_mutex);
3279 return new_cachep;
3280}
3281
Glauber Costa7cf27982012-12-18 14:22:55 -08003282void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3283{
3284 struct kmem_cache *c;
3285 int i;
3286
3287 if (!s->memcg_params)
3288 return;
3289 if (!s->memcg_params->is_root_cache)
3290 return;
3291
3292 /*
3293 * If the cache is being destroyed, we trust that there is no one else
3294 * requesting objects from it. Even if there are, the sanity checks in
3295 * kmem_cache_destroy should caught this ill-case.
3296 *
3297 * Still, we don't want anyone else freeing memcg_caches under our
3298 * noses, which can happen if a new memcg comes to life. As usual,
3299 * we'll take the set_limit_mutex to protect ourselves against this.
3300 */
3301 mutex_lock(&set_limit_mutex);
3302 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3303 c = s->memcg_params->memcg_caches[i];
3304 if (!c)
3305 continue;
3306
3307 /*
3308 * We will now manually delete the caches, so to avoid races
3309 * we need to cancel all pending destruction workers and
3310 * proceed with destruction ourselves.
3311 *
3312 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3313 * and that could spawn the workers again: it is likely that
3314 * the cache still have active pages until this very moment.
3315 * This would lead us back to mem_cgroup_destroy_cache.
3316 *
3317 * But that will not execute at all if the "dead" flag is not
3318 * set, so flip it down to guarantee we are in control.
3319 */
3320 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003321 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003322 kmem_cache_destroy(c);
3323 }
3324 mutex_unlock(&set_limit_mutex);
3325}
3326
Glauber Costad7f25f82012-12-18 14:22:40 -08003327struct create_work {
3328 struct mem_cgroup *memcg;
3329 struct kmem_cache *cachep;
3330 struct work_struct work;
3331};
3332
Glauber Costa1f458cb2012-12-18 14:22:50 -08003333static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3334{
3335 struct kmem_cache *cachep;
3336 struct memcg_cache_params *params;
3337
3338 if (!memcg_kmem_is_active(memcg))
3339 return;
3340
3341 mutex_lock(&memcg->slab_caches_mutex);
3342 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3343 cachep = memcg_params_to_cache(params);
3344 cachep->memcg_params->dead = true;
3345 INIT_WORK(&cachep->memcg_params->destroy,
Glauber Costa22933152012-12-18 14:22:59 -08003346 kmem_cache_destroy_work_func);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003347 schedule_work(&cachep->memcg_params->destroy);
3348 }
3349 mutex_unlock(&memcg->slab_caches_mutex);
3350}
3351
Glauber Costad7f25f82012-12-18 14:22:40 -08003352static void memcg_create_cache_work_func(struct work_struct *w)
3353{
3354 struct create_work *cw;
3355
3356 cw = container_of(w, struct create_work, work);
3357 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3358 /* Drop the reference gotten when we enqueued. */
3359 css_put(&cw->memcg->css);
3360 kfree(cw);
3361}
3362
3363/*
3364 * Enqueue the creation of a per-memcg kmem_cache.
3365 * Called with rcu_read_lock.
3366 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003367static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3368 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003369{
3370 struct create_work *cw;
3371
3372 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3373 if (cw == NULL)
3374 return;
3375
3376 /* The corresponding put will be done in the workqueue. */
3377 if (!css_tryget(&memcg->css)) {
3378 kfree(cw);
3379 return;
3380 }
3381
3382 cw->memcg = memcg;
3383 cw->cachep = cachep;
3384
3385 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3386 schedule_work(&cw->work);
3387}
3388
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003389static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3390 struct kmem_cache *cachep)
3391{
3392 /*
3393 * We need to stop accounting when we kmalloc, because if the
3394 * corresponding kmalloc cache is not yet created, the first allocation
3395 * in __memcg_create_cache_enqueue will recurse.
3396 *
3397 * However, it is better to enclose the whole function. Depending on
3398 * the debugging options enabled, INIT_WORK(), for instance, can
3399 * trigger an allocation. This too, will make us recurse. Because at
3400 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3401 * the safest choice is to do it like this, wrapping the whole function.
3402 */
3403 memcg_stop_kmem_account();
3404 __memcg_create_cache_enqueue(memcg, cachep);
3405 memcg_resume_kmem_account();
3406}
Glauber Costad7f25f82012-12-18 14:22:40 -08003407/*
3408 * Return the kmem_cache we're supposed to use for a slab allocation.
3409 * We try to use the current memcg's version of the cache.
3410 *
3411 * If the cache does not exist yet, if we are the first user of it,
3412 * we either create it immediately, if possible, or create it asynchronously
3413 * in a workqueue.
3414 * In the latter case, we will let the current allocation go through with
3415 * the original cache.
3416 *
3417 * Can't be called in interrupt context or from kernel threads.
3418 * This function needs to be called with rcu_read_lock() held.
3419 */
3420struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3421 gfp_t gfp)
3422{
3423 struct mem_cgroup *memcg;
3424 int idx;
3425
3426 VM_BUG_ON(!cachep->memcg_params);
3427 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3428
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003429 if (!current->mm || current->memcg_kmem_skip_account)
3430 return cachep;
3431
Glauber Costad7f25f82012-12-18 14:22:40 -08003432 rcu_read_lock();
3433 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3434 rcu_read_unlock();
3435
3436 if (!memcg_can_account_kmem(memcg))
3437 return cachep;
3438
3439 idx = memcg_cache_id(memcg);
3440
3441 /*
3442 * barrier to mare sure we're always seeing the up to date value. The
3443 * code updating memcg_caches will issue a write barrier to match this.
3444 */
3445 read_barrier_depends();
3446 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3447 /*
3448 * If we are in a safe context (can wait, and not in interrupt
3449 * context), we could be be predictable and return right away.
3450 * This would guarantee that the allocation being performed
3451 * already belongs in the new cache.
3452 *
3453 * However, there are some clashes that can arrive from locking.
3454 * For instance, because we acquire the slab_mutex while doing
3455 * kmem_cache_dup, this means no further allocation could happen
3456 * with the slab_mutex held.
3457 *
3458 * Also, because cache creation issue get_online_cpus(), this
3459 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3460 * that ends up reversed during cpu hotplug. (cpuset allocates
3461 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3462 * better to defer everything.
3463 */
3464 memcg_create_cache_enqueue(memcg, cachep);
3465 return cachep;
3466 }
3467
3468 return cachep->memcg_params->memcg_caches[idx];
3469}
3470EXPORT_SYMBOL(__memcg_kmem_get_cache);
3471
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003472/*
3473 * We need to verify if the allocation against current->mm->owner's memcg is
3474 * possible for the given order. But the page is not allocated yet, so we'll
3475 * need a further commit step to do the final arrangements.
3476 *
3477 * It is possible for the task to switch cgroups in this mean time, so at
3478 * commit time, we can't rely on task conversion any longer. We'll then use
3479 * the handle argument to return to the caller which cgroup we should commit
3480 * against. We could also return the memcg directly and avoid the pointer
3481 * passing, but a boolean return value gives better semantics considering
3482 * the compiled-out case as well.
3483 *
3484 * Returning true means the allocation is possible.
3485 */
3486bool
3487__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3488{
3489 struct mem_cgroup *memcg;
3490 int ret;
3491
3492 *_memcg = NULL;
3493 memcg = try_get_mem_cgroup_from_mm(current->mm);
3494
3495 /*
3496 * very rare case described in mem_cgroup_from_task. Unfortunately there
3497 * isn't much we can do without complicating this too much, and it would
3498 * be gfp-dependent anyway. Just let it go
3499 */
3500 if (unlikely(!memcg))
3501 return true;
3502
3503 if (!memcg_can_account_kmem(memcg)) {
3504 css_put(&memcg->css);
3505 return true;
3506 }
3507
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003508 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3509 if (!ret)
3510 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003511
3512 css_put(&memcg->css);
3513 return (ret == 0);
3514}
3515
3516void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3517 int order)
3518{
3519 struct page_cgroup *pc;
3520
3521 VM_BUG_ON(mem_cgroup_is_root(memcg));
3522
3523 /* The page allocation failed. Revert */
3524 if (!page) {
3525 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003526 return;
3527 }
3528
3529 pc = lookup_page_cgroup(page);
3530 lock_page_cgroup(pc);
3531 pc->mem_cgroup = memcg;
3532 SetPageCgroupUsed(pc);
3533 unlock_page_cgroup(pc);
3534}
3535
3536void __memcg_kmem_uncharge_pages(struct page *page, int order)
3537{
3538 struct mem_cgroup *memcg = NULL;
3539 struct page_cgroup *pc;
3540
3541
3542 pc = lookup_page_cgroup(page);
3543 /*
3544 * Fast unlocked return. Theoretically might have changed, have to
3545 * check again after locking.
3546 */
3547 if (!PageCgroupUsed(pc))
3548 return;
3549
3550 lock_page_cgroup(pc);
3551 if (PageCgroupUsed(pc)) {
3552 memcg = pc->mem_cgroup;
3553 ClearPageCgroupUsed(pc);
3554 }
3555 unlock_page_cgroup(pc);
3556
3557 /*
3558 * We trust that only if there is a memcg associated with the page, it
3559 * is a valid allocation
3560 */
3561 if (!memcg)
3562 return;
3563
3564 VM_BUG_ON(mem_cgroup_is_root(memcg));
3565 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003566}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003567#else
3568static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3569{
3570}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003571#endif /* CONFIG_MEMCG_KMEM */
3572
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003573#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3574
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003575#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003576/*
3577 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003578 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3579 * charge/uncharge will be never happen and move_account() is done under
3580 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003581 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003582void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003583{
3584 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003585 struct page_cgroup *pc;
3586 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003587
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003588 if (mem_cgroup_disabled())
3589 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003590 for (i = 1; i < HPAGE_PMD_NR; i++) {
3591 pc = head_pc + i;
3592 pc->mem_cgroup = head_pc->mem_cgroup;
3593 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003594 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3595 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003596}
Hugh Dickins12d27102012-01-12 17:19:52 -08003597#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003598
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003599/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003600 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003601 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003602 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003603 * @pc: page_cgroup of the page.
3604 * @from: mem_cgroup which the page is moved from.
3605 * @to: mem_cgroup which the page is moved to. @from != @to.
3606 *
3607 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003608 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003609 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003610 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003611 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3612 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003613 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003614static int mem_cgroup_move_account(struct page *page,
3615 unsigned int nr_pages,
3616 struct page_cgroup *pc,
3617 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003618 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003619{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003620 unsigned long flags;
3621 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003622 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003623
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003624 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003625 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003626 /*
3627 * The page is isolated from LRU. So, collapse function
3628 * will not handle this page. But page splitting can happen.
3629 * Do this check under compound_page_lock(). The caller should
3630 * hold it.
3631 */
3632 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003633 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003634 goto out;
3635
3636 lock_page_cgroup(pc);
3637
3638 ret = -EINVAL;
3639 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3640 goto unlock;
3641
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003642 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003643
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003644 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003645 /* Update mapped_file data for mem_cgroup */
3646 preempt_disable();
3647 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3648 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3649 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003650 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003651 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003652
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003653 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003654 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003655 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003656 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003657 ret = 0;
3658unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003659 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003660 /*
3661 * check events
3662 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003663 memcg_check_events(to, page);
3664 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003665out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003666 return ret;
3667}
3668
Michal Hocko2ef37d32012-10-26 13:37:30 +02003669/**
3670 * mem_cgroup_move_parent - moves page to the parent group
3671 * @page: the page to move
3672 * @pc: page_cgroup of the page
3673 * @child: page's cgroup
3674 *
3675 * move charges to its parent or the root cgroup if the group has no
3676 * parent (aka use_hierarchy==0).
3677 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3678 * mem_cgroup_move_account fails) the failure is always temporary and
3679 * it signals a race with a page removal/uncharge or migration. In the
3680 * first case the page is on the way out and it will vanish from the LRU
3681 * on the next attempt and the call should be retried later.
3682 * Isolation from the LRU fails only if page has been isolated from
3683 * the LRU since we looked at it and that usually means either global
3684 * reclaim or migration going on. The page will either get back to the
3685 * LRU or vanish.
3686 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3687 * (!PageCgroupUsed) or moved to a different group. The page will
3688 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003689 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003690static int mem_cgroup_move_parent(struct page *page,
3691 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003692 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003693{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003694 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003695 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003696 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003697 int ret;
3698
Michal Hockod8423012012-10-26 13:37:29 +02003699 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003700
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003701 ret = -EBUSY;
3702 if (!get_page_unless_zero(page))
3703 goto out;
3704 if (isolate_lru_page(page))
3705 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003706
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003707 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003708
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003709 parent = parent_mem_cgroup(child);
3710 /*
3711 * If no parent, move charges to root cgroup.
3712 */
3713 if (!parent)
3714 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003715
Michal Hocko2ef37d32012-10-26 13:37:30 +02003716 if (nr_pages > 1) {
3717 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003718 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003719 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003720
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003721 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003722 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003723 if (!ret)
3724 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003725
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003726 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003727 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003728 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003729put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003730 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003731out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003732 return ret;
3733}
3734
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003735/*
3736 * Charge the memory controller for page usage.
3737 * Return
3738 * 0 if the charge was successful
3739 * < 0 if the cgroup is over its limit
3740 */
3741static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003742 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003743{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003744 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003745 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003746 bool oom = true;
3747 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003748
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003749 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003750 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003751 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003752 /*
3753 * Never OOM-kill a process for a huge page. The
3754 * fault handler will fall back to regular pages.
3755 */
3756 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003757 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003758
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003759 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003760 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003761 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003762 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003763 return 0;
3764}
3765
3766int mem_cgroup_newpage_charge(struct page *page,
3767 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003768{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003769 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003770 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003771 VM_BUG_ON(page_mapped(page));
3772 VM_BUG_ON(page->mapping && !PageAnon(page));
3773 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003774 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003775 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003776}
3777
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003778/*
3779 * While swap-in, try_charge -> commit or cancel, the page is locked.
3780 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003781 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003782 * "commit()" or removed by "cancel()"
3783 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003784static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3785 struct page *page,
3786 gfp_t mask,
3787 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003788{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003789 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003790 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003791 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003792
Johannes Weiner90deb782012-07-31 16:45:47 -07003793 pc = lookup_page_cgroup(page);
3794 /*
3795 * Every swap fault against a single page tries to charge the
3796 * page, bail as early as possible. shmem_unuse() encounters
3797 * already charged pages, too. The USED bit is protected by
3798 * the page lock, which serializes swap cache removal, which
3799 * in turn serializes uncharging.
3800 */
3801 if (PageCgroupUsed(pc))
3802 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003803 if (!do_swap_account)
3804 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003805 memcg = try_get_mem_cgroup_from_page(page);
3806 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003807 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003808 *memcgp = memcg;
3809 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003810 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003811 if (ret == -EINTR)
3812 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003813 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003814charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003815 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3816 if (ret == -EINTR)
3817 ret = 0;
3818 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003819}
3820
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003821int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3822 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3823{
3824 *memcgp = NULL;
3825 if (mem_cgroup_disabled())
3826 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003827 /*
3828 * A racing thread's fault, or swapoff, may have already
3829 * updated the pte, and even removed page from swap cache: in
3830 * those cases unuse_pte()'s pte_same() test will fail; but
3831 * there's also a KSM case which does need to charge the page.
3832 */
3833 if (!PageSwapCache(page)) {
3834 int ret;
3835
3836 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3837 if (ret == -EINTR)
3838 ret = 0;
3839 return ret;
3840 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003841 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3842}
3843
Johannes Weiner827a03d2012-07-31 16:45:36 -07003844void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3845{
3846 if (mem_cgroup_disabled())
3847 return;
3848 if (!memcg)
3849 return;
3850 __mem_cgroup_cancel_charge(memcg, 1);
3851}
3852
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003853static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003854__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003855 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003856{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003857 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003858 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003859 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003860 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003861
Johannes Weinerce587e62012-04-24 20:22:33 +02003862 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003863 /*
3864 * Now swap is on-memory. This means this page may be
3865 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003866 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3867 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3868 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003869 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003870 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003871 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003872 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003873 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003874}
3875
Johannes Weiner72835c82012-01-12 17:18:32 -08003876void mem_cgroup_commit_charge_swapin(struct page *page,
3877 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003878{
Johannes Weiner72835c82012-01-12 17:18:32 -08003879 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003880 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003881}
3882
Johannes Weiner827a03d2012-07-31 16:45:36 -07003883int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3884 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003885{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003886 struct mem_cgroup *memcg = NULL;
3887 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3888 int ret;
3889
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003890 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003891 return 0;
3892 if (PageCompound(page))
3893 return 0;
3894
Johannes Weiner827a03d2012-07-31 16:45:36 -07003895 if (!PageSwapCache(page))
3896 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3897 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003898 ret = __mem_cgroup_try_charge_swapin(mm, page,
3899 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003900 if (!ret)
3901 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3902 }
3903 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003904}
3905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003906static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003907 unsigned int nr_pages,
3908 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003909{
3910 struct memcg_batch_info *batch = NULL;
3911 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003912
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003913 /* If swapout, usage of swap doesn't decrease */
3914 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3915 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003916
3917 batch = &current->memcg_batch;
3918 /*
3919 * In usual, we do css_get() when we remember memcg pointer.
3920 * But in this case, we keep res->usage until end of a series of
3921 * uncharges. Then, it's ok to ignore memcg's refcnt.
3922 */
3923 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003924 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003925 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003926 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003927 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003928 * the same cgroup and we have chance to coalesce uncharges.
3929 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3930 * because we want to do uncharge as soon as possible.
3931 */
3932
3933 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3934 goto direct_uncharge;
3935
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003936 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003937 goto direct_uncharge;
3938
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003939 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003940 * In typical case, batch->memcg == mem. This means we can
3941 * merge a series of uncharges to an uncharge of res_counter.
3942 * If not, we uncharge res_counter ony by one.
3943 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003944 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003945 goto direct_uncharge;
3946 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003947 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003948 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003949 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003950 return;
3951direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003952 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003953 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003954 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3955 if (unlikely(batch->memcg != memcg))
3956 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003957}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003958
Balbir Singh8697d332008-02-07 00:13:59 -08003959/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003960 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003961 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003962static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003963__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3964 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003965{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003966 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003967 unsigned int nr_pages = 1;
3968 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003969 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003970
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003971 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003972 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003973
Johannes Weiner0c59b892012-07-31 16:45:31 -07003974 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003975
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003976 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003977 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003978 VM_BUG_ON(!PageTransHuge(page));
3979 }
Balbir Singh8697d332008-02-07 00:13:59 -08003980 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003981 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003982 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003983 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003984 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003985 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003986
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003987 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003988
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003989 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003990
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003991 if (!PageCgroupUsed(pc))
3992 goto unlock_out;
3993
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003994 anon = PageAnon(page);
3995
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003996 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003997 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003998 /*
3999 * Generally PageAnon tells if it's the anon statistics to be
4000 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4001 * used before page reached the stage of being marked PageAnon.
4002 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004003 anon = true;
4004 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004005 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004006 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004007 if (page_mapped(page))
4008 goto unlock_out;
4009 /*
4010 * Pages under migration may not be uncharged. But
4011 * end_migration() /must/ be the one uncharging the
4012 * unused post-migration page and so it has to call
4013 * here with the migration bit still set. See the
4014 * res_counter handling below.
4015 */
4016 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004017 goto unlock_out;
4018 break;
4019 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4020 if (!PageAnon(page)) { /* Shared memory */
4021 if (page->mapping && !page_is_file_cache(page))
4022 goto unlock_out;
4023 } else if (page_mapped(page)) /* Anon */
4024 goto unlock_out;
4025 break;
4026 default:
4027 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004028 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004029
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004030 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004031
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004032 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004033 /*
4034 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4035 * freed from LRU. This is safe because uncharged page is expected not
4036 * to be reused (freed soon). Exception is SwapCache, it's handled by
4037 * special functions.
4038 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004039
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004040 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004041 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004042 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004043 * will never be freed.
4044 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004045 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004046 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004047 mem_cgroup_swap_statistics(memcg, true);
4048 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004049 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004050 /*
4051 * Migration does not charge the res_counter for the
4052 * replacement page, so leave it alone when phasing out the
4053 * page that is unused after the migration.
4054 */
4055 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004056 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004057
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004058 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004059
4060unlock_out:
4061 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004062 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004063}
4064
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004065void mem_cgroup_uncharge_page(struct page *page)
4066{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004067 /* early check. */
4068 if (page_mapped(page))
4069 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004070 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004071 if (PageSwapCache(page))
4072 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004073 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004074}
4075
4076void mem_cgroup_uncharge_cache_page(struct page *page)
4077{
4078 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004079 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004080 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004081}
4082
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004083/*
4084 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4085 * In that cases, pages are freed continuously and we can expect pages
4086 * are in the same memcg. All these calls itself limits the number of
4087 * pages freed at once, then uncharge_start/end() is called properly.
4088 * This may be called prural(2) times in a context,
4089 */
4090
4091void mem_cgroup_uncharge_start(void)
4092{
4093 current->memcg_batch.do_batch++;
4094 /* We can do nest. */
4095 if (current->memcg_batch.do_batch == 1) {
4096 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004097 current->memcg_batch.nr_pages = 0;
4098 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004099 }
4100}
4101
4102void mem_cgroup_uncharge_end(void)
4103{
4104 struct memcg_batch_info *batch = &current->memcg_batch;
4105
4106 if (!batch->do_batch)
4107 return;
4108
4109 batch->do_batch--;
4110 if (batch->do_batch) /* If stacked, do nothing. */
4111 return;
4112
4113 if (!batch->memcg)
4114 return;
4115 /*
4116 * This "batch->memcg" is valid without any css_get/put etc...
4117 * bacause we hide charges behind us.
4118 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004119 if (batch->nr_pages)
4120 res_counter_uncharge(&batch->memcg->res,
4121 batch->nr_pages * PAGE_SIZE);
4122 if (batch->memsw_nr_pages)
4123 res_counter_uncharge(&batch->memcg->memsw,
4124 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004125 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004126 /* forget this pointer (for sanity check) */
4127 batch->memcg = NULL;
4128}
4129
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004130#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004131/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004132 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004133 * memcg information is recorded to swap_cgroup of "ent"
4134 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004135void
4136mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004137{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004138 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004139 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004140
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004141 if (!swapout) /* this was a swap cache but the swap is unused ! */
4142 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4143
Johannes Weiner0030f532012-07-31 16:45:25 -07004144 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004145
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004146 /*
4147 * record memcg information, if swapout && memcg != NULL,
4148 * mem_cgroup_get() was called in uncharge().
4149 */
4150 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004151 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004152}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004153#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004154
Andrew Mortonc255a452012-07-31 16:43:02 -07004155#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004156/*
4157 * called from swap_entry_free(). remove record in swap_cgroup and
4158 * uncharge "memsw" account.
4159 */
4160void mem_cgroup_uncharge_swap(swp_entry_t ent)
4161{
4162 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004163 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004164
4165 if (!do_swap_account)
4166 return;
4167
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004168 id = swap_cgroup_record(ent, 0);
4169 rcu_read_lock();
4170 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004171 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004172 /*
4173 * We uncharge this because swap is freed.
4174 * This memcg can be obsolete one. We avoid calling css_tryget
4175 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004176 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004177 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004178 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004179 mem_cgroup_put(memcg);
4180 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004181 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004182}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004183
4184/**
4185 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4186 * @entry: swap entry to be moved
4187 * @from: mem_cgroup which the entry is moved from
4188 * @to: mem_cgroup which the entry is moved to
4189 *
4190 * It succeeds only when the swap_cgroup's record for this entry is the same
4191 * as the mem_cgroup's id of @from.
4192 *
4193 * Returns 0 on success, -EINVAL on failure.
4194 *
4195 * The caller must have charged to @to, IOW, called res_counter_charge() about
4196 * both res and memsw, and called css_get().
4197 */
4198static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004199 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004200{
4201 unsigned short old_id, new_id;
4202
4203 old_id = css_id(&from->css);
4204 new_id = css_id(&to->css);
4205
4206 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004207 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004208 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004209 /*
4210 * This function is only called from task migration context now.
4211 * It postpones res_counter and refcount handling till the end
4212 * of task migration(mem_cgroup_clear_mc()) for performance
4213 * improvement. But we cannot postpone mem_cgroup_get(to)
4214 * because if the process that has been moved to @to does
4215 * swap-in, the refcount of @to might be decreased to 0.
4216 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004217 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004218 return 0;
4219 }
4220 return -EINVAL;
4221}
4222#else
4223static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004224 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004225{
4226 return -EINVAL;
4227}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004228#endif
4229
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004230/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004231 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4232 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004233 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004234void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4235 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004236{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004237 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004238 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004239 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004240 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004241
Johannes Weiner72835c82012-01-12 17:18:32 -08004242 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004243
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004244 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004245 return;
Balbir Singh40779602008-04-04 14:29:59 -07004246
Mel Gormanb32967f2012-11-19 12:35:47 +00004247 if (PageTransHuge(page))
4248 nr_pages <<= compound_order(page);
4249
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004250 pc = lookup_page_cgroup(page);
4251 lock_page_cgroup(pc);
4252 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004253 memcg = pc->mem_cgroup;
4254 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004255 /*
4256 * At migrating an anonymous page, its mapcount goes down
4257 * to 0 and uncharge() will be called. But, even if it's fully
4258 * unmapped, migration may fail and this page has to be
4259 * charged again. We set MIGRATION flag here and delay uncharge
4260 * until end_migration() is called
4261 *
4262 * Corner Case Thinking
4263 * A)
4264 * When the old page was mapped as Anon and it's unmap-and-freed
4265 * while migration was ongoing.
4266 * If unmap finds the old page, uncharge() of it will be delayed
4267 * until end_migration(). If unmap finds a new page, it's
4268 * uncharged when it make mapcount to be 1->0. If unmap code
4269 * finds swap_migration_entry, the new page will not be mapped
4270 * and end_migration() will find it(mapcount==0).
4271 *
4272 * B)
4273 * When the old page was mapped but migraion fails, the kernel
4274 * remaps it. A charge for it is kept by MIGRATION flag even
4275 * if mapcount goes down to 0. We can do remap successfully
4276 * without charging it again.
4277 *
4278 * C)
4279 * The "old" page is under lock_page() until the end of
4280 * migration, so, the old page itself will not be swapped-out.
4281 * If the new page is swapped out before end_migraton, our
4282 * hook to usual swap-out path will catch the event.
4283 */
4284 if (PageAnon(page))
4285 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004286 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004287 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004288 /*
4289 * If the page is not charged at this point,
4290 * we return here.
4291 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004292 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004293 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004294
Johannes Weiner72835c82012-01-12 17:18:32 -08004295 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004296 /*
4297 * We charge new page before it's used/mapped. So, even if unlock_page()
4298 * is called before end_migration, we can catch all events on this new
4299 * page. In the case new page is migrated but not remapped, new page's
4300 * mapcount will be finally 0 and we call uncharge in end_migration().
4301 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004302 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004303 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004304 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004305 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004306 /*
4307 * The page is committed to the memcg, but it's not actually
4308 * charged to the res_counter since we plan on replacing the
4309 * old one and only one page is going to be left afterwards.
4310 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004311 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004312}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004313
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004314/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004315void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004316 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004317{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004318 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004319 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004320 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004321
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004322 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004323 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004324
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004325 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004326 used = oldpage;
4327 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004328 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004329 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004330 unused = oldpage;
4331 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004332 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004333 __mem_cgroup_uncharge_common(unused,
4334 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4335 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4336 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004337 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004338 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004339 * We disallowed uncharge of pages under migration because mapcount
4340 * of the page goes down to zero, temporarly.
4341 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004342 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004343 pc = lookup_page_cgroup(oldpage);
4344 lock_page_cgroup(pc);
4345 ClearPageCgroupMigration(pc);
4346 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004347
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004348 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004349 * If a page is a file cache, radix-tree replacement is very atomic
4350 * and we can skip this check. When it was an Anon page, its mapcount
4351 * goes down to 0. But because we added MIGRATION flage, it's not
4352 * uncharged yet. There are several case but page->mapcount check
4353 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4354 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004355 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004356 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004357 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004358}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004359
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004360/*
4361 * At replace page cache, newpage is not under any memcg but it's on
4362 * LRU. So, this function doesn't touch res_counter but handles LRU
4363 * in correct way. Both pages are locked so we cannot race with uncharge.
4364 */
4365void mem_cgroup_replace_page_cache(struct page *oldpage,
4366 struct page *newpage)
4367{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004368 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004369 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004370 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004371
4372 if (mem_cgroup_disabled())
4373 return;
4374
4375 pc = lookup_page_cgroup(oldpage);
4376 /* fix accounting on old pages */
4377 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004378 if (PageCgroupUsed(pc)) {
4379 memcg = pc->mem_cgroup;
4380 mem_cgroup_charge_statistics(memcg, false, -1);
4381 ClearPageCgroupUsed(pc);
4382 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004383 unlock_page_cgroup(pc);
4384
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004385 /*
4386 * When called from shmem_replace_page(), in some cases the
4387 * oldpage has already been charged, and in some cases not.
4388 */
4389 if (!memcg)
4390 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004391 /*
4392 * Even if newpage->mapping was NULL before starting replacement,
4393 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4394 * LRU while we overwrite pc->mem_cgroup.
4395 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004396 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004397}
4398
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004399#ifdef CONFIG_DEBUG_VM
4400static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4401{
4402 struct page_cgroup *pc;
4403
4404 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004405 /*
4406 * Can be NULL while feeding pages into the page allocator for
4407 * the first time, i.e. during boot or memory hotplug;
4408 * or when mem_cgroup_disabled().
4409 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004410 if (likely(pc) && PageCgroupUsed(pc))
4411 return pc;
4412 return NULL;
4413}
4414
4415bool mem_cgroup_bad_page_check(struct page *page)
4416{
4417 if (mem_cgroup_disabled())
4418 return false;
4419
4420 return lookup_page_cgroup_used(page) != NULL;
4421}
4422
4423void mem_cgroup_print_bad_page(struct page *page)
4424{
4425 struct page_cgroup *pc;
4426
4427 pc = lookup_page_cgroup_used(page);
4428 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08004429 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004430 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004431 }
4432}
4433#endif
4434
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004435static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004436 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004437{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004438 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004439 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004440 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004441 int children = mem_cgroup_count_children(memcg);
4442 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004443 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004444
4445 /*
4446 * For keeping hierarchical_reclaim simple, how long we should retry
4447 * is depends on callers. We set our retry-count to be function
4448 * of # of children which we should visit in this loop.
4449 */
4450 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4451
4452 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004453
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004454 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004455 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004456 if (signal_pending(current)) {
4457 ret = -EINTR;
4458 break;
4459 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004460 /*
4461 * Rather than hide all in some function, I do this in
4462 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004463 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004464 */
4465 mutex_lock(&set_limit_mutex);
4466 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4467 if (memswlimit < val) {
4468 ret = -EINVAL;
4469 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004470 break;
4471 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004472
4473 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4474 if (memlimit < val)
4475 enlarge = 1;
4476
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004477 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004478 if (!ret) {
4479 if (memswlimit == val)
4480 memcg->memsw_is_minimum = true;
4481 else
4482 memcg->memsw_is_minimum = false;
4483 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004484 mutex_unlock(&set_limit_mutex);
4485
4486 if (!ret)
4487 break;
4488
Johannes Weiner56600482012-01-12 17:17:59 -08004489 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4490 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004491 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4492 /* Usage is reduced ? */
4493 if (curusage >= oldusage)
4494 retry_count--;
4495 else
4496 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004497 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004498 if (!ret && enlarge)
4499 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004500
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004501 return ret;
4502}
4503
Li Zefan338c8432009-06-17 16:27:15 -07004504static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4505 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004506{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004507 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004508 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004509 int children = mem_cgroup_count_children(memcg);
4510 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004511 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004512
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004513 /* see mem_cgroup_resize_res_limit */
4514 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4515 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004516 while (retry_count) {
4517 if (signal_pending(current)) {
4518 ret = -EINTR;
4519 break;
4520 }
4521 /*
4522 * Rather than hide all in some function, I do this in
4523 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004524 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004525 */
4526 mutex_lock(&set_limit_mutex);
4527 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4528 if (memlimit > val) {
4529 ret = -EINVAL;
4530 mutex_unlock(&set_limit_mutex);
4531 break;
4532 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004533 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4534 if (memswlimit < val)
4535 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004536 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004537 if (!ret) {
4538 if (memlimit == val)
4539 memcg->memsw_is_minimum = true;
4540 else
4541 memcg->memsw_is_minimum = false;
4542 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004543 mutex_unlock(&set_limit_mutex);
4544
4545 if (!ret)
4546 break;
4547
Johannes Weiner56600482012-01-12 17:17:59 -08004548 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4549 MEM_CGROUP_RECLAIM_NOSWAP |
4550 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004551 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004552 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004553 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004554 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004555 else
4556 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004557 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004558 if (!ret && enlarge)
4559 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004560 return ret;
4561}
4562
Balbir Singh4e416952009-09-23 15:56:39 -07004563unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004564 gfp_t gfp_mask,
4565 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004566{
4567 unsigned long nr_reclaimed = 0;
4568 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4569 unsigned long reclaimed;
4570 int loop = 0;
4571 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004572 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004573 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004574
4575 if (order > 0)
4576 return 0;
4577
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004578 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004579 /*
4580 * This loop can run a while, specially if mem_cgroup's continuously
4581 * keep exceeding their soft limit and putting the system under
4582 * pressure
4583 */
4584 do {
4585 if (next_mz)
4586 mz = next_mz;
4587 else
4588 mz = mem_cgroup_largest_soft_limit_node(mctz);
4589 if (!mz)
4590 break;
4591
Ying Han0ae5e892011-05-26 16:25:25 -07004592 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004593 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004594 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004595 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004596 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004597 spin_lock(&mctz->lock);
4598
4599 /*
4600 * If we failed to reclaim anything from this memory cgroup
4601 * it is time to move on to the next cgroup
4602 */
4603 next_mz = NULL;
4604 if (!reclaimed) {
4605 do {
4606 /*
4607 * Loop until we find yet another one.
4608 *
4609 * By the time we get the soft_limit lock
4610 * again, someone might have aded the
4611 * group back on the RB tree. Iterate to
4612 * make sure we get a different mem.
4613 * mem_cgroup_largest_soft_limit_node returns
4614 * NULL if no other cgroup is present on
4615 * the tree
4616 */
4617 next_mz =
4618 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004619 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004620 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004621 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004622 break;
4623 } while (1);
4624 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004625 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4626 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004627 /*
4628 * One school of thought says that we should not add
4629 * back the node to the tree if reclaim returns 0.
4630 * But our reclaim could return 0, simply because due
4631 * to priority we are exposing a smaller subset of
4632 * memory to reclaim from. Consider this as a longer
4633 * term TODO.
4634 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004635 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004636 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004637 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004638 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004639 loop++;
4640 /*
4641 * Could not reclaim anything and there are no more
4642 * mem cgroups to try or we seem to be looping without
4643 * reclaiming anything.
4644 */
4645 if (!nr_reclaimed &&
4646 (next_mz == NULL ||
4647 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4648 break;
4649 } while (!nr_reclaimed);
4650 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004651 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004652 return nr_reclaimed;
4653}
4654
Michal Hocko2ef37d32012-10-26 13:37:30 +02004655/**
4656 * mem_cgroup_force_empty_list - clears LRU of a group
4657 * @memcg: group to clear
4658 * @node: NUMA node
4659 * @zid: zone id
4660 * @lru: lru to to clear
4661 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004662 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004663 * reclaim the pages page themselves - pages are moved to the parent (or root)
4664 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004665 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004666static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004667 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004668{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004669 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004670 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004671 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004672 struct page *busy;
4673 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004674
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004675 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004676 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4677 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004678
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004679 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004680 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004681 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004682 struct page *page;
4683
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004684 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004685 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004686 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004687 break;
4688 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004689 page = list_entry(list->prev, struct page, lru);
4690 if (busy == page) {
4691 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004692 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004693 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004694 continue;
4695 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004696 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004697
Johannes Weiner925b7672012-01-12 17:18:15 -08004698 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004699
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004700 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004701 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004702 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004703 cond_resched();
4704 } else
4705 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004706 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004707}
4708
4709/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004710 * make mem_cgroup's charge to be 0 if there is no task by moving
4711 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004712 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004713 *
4714 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004715 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004716static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004717{
Michal Hockoc26251f2012-10-26 13:37:28 +02004718 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004719 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004720
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004721 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004722 /* This is for making all *used* pages to be on LRU. */
4723 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004724 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004725 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004726 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004727 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004728 enum lru_list lru;
4729 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004730 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004731 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004732 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004733 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004734 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004735 mem_cgroup_end_move(memcg);
4736 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004737 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004738
Michal Hocko2ef37d32012-10-26 13:37:30 +02004739 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004740 * Kernel memory may not necessarily be trackable to a specific
4741 * process. So they are not migrated, and therefore we can't
4742 * expect their value to drop to 0 here.
4743 * Having res filled up with kmem only is enough.
4744 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004745 * This is a safety check because mem_cgroup_force_empty_list
4746 * could have raced with mem_cgroup_replace_page_cache callers
4747 * so the lru seemed empty but the page could have been added
4748 * right after the check. RES_USAGE should be safe as we always
4749 * charge before adding to the LRU.
4750 */
Glauber Costabea207c2012-12-18 14:22:11 -08004751 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4752 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4753 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004754}
4755
4756/*
4757 * Reclaims as many pages from the given memcg as possible and moves
4758 * the rest to the parent.
4759 *
4760 * Caller is responsible for holding css reference for memcg.
4761 */
4762static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4763{
4764 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4765 struct cgroup *cgrp = memcg->css.cgroup;
4766
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004767 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004768 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4769 return -EBUSY;
4770
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004771 /* we call try-to-free pages for make this cgroup empty */
4772 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004773 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004774 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004775 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004776
Michal Hockoc26251f2012-10-26 13:37:28 +02004777 if (signal_pending(current))
4778 return -EINTR;
4779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004780 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004781 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004782 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004783 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004784 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004785 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004786 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004787
4788 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004789 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004790 mem_cgroup_reparent_charges(memcg);
4791
4792 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004793}
4794
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004795static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004796{
Michal Hockoc26251f2012-10-26 13:37:28 +02004797 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4798 int ret;
4799
Michal Hockod8423012012-10-26 13:37:29 +02004800 if (mem_cgroup_is_root(memcg))
4801 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004802 css_get(&memcg->css);
4803 ret = mem_cgroup_force_empty(memcg);
4804 css_put(&memcg->css);
4805
4806 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004807}
4808
4809
Balbir Singh18f59ea2009-01-07 18:08:07 -08004810static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4811{
4812 return mem_cgroup_from_cont(cont)->use_hierarchy;
4813}
4814
4815static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4816 u64 val)
4817{
4818 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004819 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004820 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004821 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004822
4823 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004824 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004825
4826 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004827
4828 if (memcg->use_hierarchy == val)
4829 goto out;
4830
Balbir Singh18f59ea2009-01-07 18:08:07 -08004831 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004832 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004833 * in the child subtrees. If it is unset, then the change can
4834 * occur, provided the current cgroup has no children.
4835 *
4836 * For the root cgroup, parent_mem is NULL, we allow value to be
4837 * set if there are no children.
4838 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004839 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004840 (val == 1 || val == 0)) {
4841 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004842 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004843 else
4844 retval = -EBUSY;
4845 } else
4846 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004847
4848out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004849 cgroup_unlock();
4850
4851 return retval;
4852}
4853
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004854
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004855static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004856 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004857{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004858 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004859 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004860
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004861 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004862 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004863 val += mem_cgroup_read_stat(iter, idx);
4864
4865 if (val < 0) /* race ? */
4866 val = 0;
4867 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004868}
4869
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004870static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004871{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004872 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004874 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004875 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004876 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004877 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004878 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004879 }
4880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004881 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4882 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004883
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004884 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004885 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004886
4887 return val << PAGE_SHIFT;
4888}
4889
Tejun Heoaf36f902012-04-01 12:09:55 -07004890static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4891 struct file *file, char __user *buf,
4892 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004893{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004894 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004895 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004896 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004897 int name, len;
4898 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004899
4900 type = MEMFILE_TYPE(cft->private);
4901 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004902
4903 if (!do_swap_account && type == _MEMSWAP)
4904 return -EOPNOTSUPP;
4905
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004906 switch (type) {
4907 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004908 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004909 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004910 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004911 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004912 break;
4913 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004914 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004915 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004916 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004917 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004918 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004919 case _KMEM:
4920 val = res_counter_read_u64(&memcg->kmem, name);
4921 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004922 default:
4923 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004924 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004925
4926 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4927 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004928}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004929
4930static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4931{
4932 int ret = -EINVAL;
4933#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004934 bool must_inc_static_branch = false;
4935
Glauber Costa510fc4e2012-12-18 14:21:47 -08004936 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4937 /*
4938 * For simplicity, we won't allow this to be disabled. It also can't
4939 * be changed if the cgroup has children already, or if tasks had
4940 * already joined.
4941 *
4942 * If tasks join before we set the limit, a person looking at
4943 * kmem.usage_in_bytes will have no way to determine when it took
4944 * place, which makes the value quite meaningless.
4945 *
4946 * After it first became limited, changes in the value of the limit are
4947 * of course permitted.
4948 *
4949 * Taking the cgroup_lock is really offensive, but it is so far the only
4950 * way to guarantee that no children will appear. There are plenty of
4951 * other offenders, and they should all go away. Fine grained locking
4952 * is probably the way to go here. When we are fully hierarchical, we
4953 * can also get rid of the use_hierarchy check.
4954 */
4955 cgroup_lock();
4956 mutex_lock(&set_limit_mutex);
4957 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4958 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4959 !list_empty(&cont->children))) {
4960 ret = -EBUSY;
4961 goto out;
4962 }
4963 ret = res_counter_set_limit(&memcg->kmem, val);
4964 VM_BUG_ON(ret);
4965
Glauber Costa55007d82012-12-18 14:22:38 -08004966 ret = memcg_update_cache_sizes(memcg);
4967 if (ret) {
4968 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
4969 goto out;
4970 }
Glauber Costaa8964b92012-12-18 14:22:09 -08004971 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08004972 /*
4973 * kmem charges can outlive the cgroup. In the case of slab
4974 * pages, for instance, a page contain objects from various
4975 * processes, so it is unfeasible to migrate them away. We
4976 * need to reference count the memcg because of that.
4977 */
4978 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004979 } else
4980 ret = res_counter_set_limit(&memcg->kmem, val);
4981out:
4982 mutex_unlock(&set_limit_mutex);
4983 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08004984
4985 /*
4986 * We are by now familiar with the fact that we can't inc the static
4987 * branch inside cgroup_lock. See disarm functions for details. A
4988 * worker here is overkill, but also wrong: After the limit is set, we
4989 * must start accounting right away. Since this operation can't fail,
4990 * we can safely defer it to here - no rollback will be needed.
4991 *
4992 * The boolean used to control this is also safe, because
4993 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
4994 * able to set it to true;
4995 */
4996 if (must_inc_static_branch) {
4997 static_key_slow_inc(&memcg_kmem_enabled_key);
4998 /*
4999 * setting the active bit after the inc will guarantee no one
5000 * starts accounting before all call sites are patched
5001 */
5002 memcg_kmem_set_active(memcg);
5003 }
5004
Glauber Costa510fc4e2012-12-18 14:21:47 -08005005#endif
5006 return ret;
5007}
5008
Glauber Costa55007d82012-12-18 14:22:38 -08005009static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005010{
Glauber Costa55007d82012-12-18 14:22:38 -08005011 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005012 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5013 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005014 goto out;
5015
Glauber Costa510fc4e2012-12-18 14:21:47 -08005016 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08005017#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08005018 /*
5019 * When that happen, we need to disable the static branch only on those
5020 * memcgs that enabled it. To achieve this, we would be forced to
5021 * complicate the code by keeping track of which memcgs were the ones
5022 * that actually enabled limits, and which ones got it from its
5023 * parents.
5024 *
5025 * It is a lot simpler just to do static_key_slow_inc() on every child
5026 * that is accounted.
5027 */
Glauber Costa55007d82012-12-18 14:22:38 -08005028 if (!memcg_kmem_is_active(memcg))
5029 goto out;
5030
5031 /*
5032 * destroy(), called if we fail, will issue static_key_slow_inc() and
5033 * mem_cgroup_put() if kmem is enabled. We have to either call them
5034 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5035 * this more consistent, since it always leads to the same destroy path
5036 */
5037 mem_cgroup_get(memcg);
5038 static_key_slow_inc(&memcg_kmem_enabled_key);
5039
5040 mutex_lock(&set_limit_mutex);
5041 ret = memcg_update_cache_sizes(memcg);
5042 mutex_unlock(&set_limit_mutex);
Glauber Costa7de37682012-12-18 14:22:07 -08005043#endif
Glauber Costa55007d82012-12-18 14:22:38 -08005044out:
5045 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005046}
5047
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005048/*
5049 * The user of this function is...
5050 * RES_LIMIT.
5051 */
Paul Menage856c13a2008-07-25 01:47:04 -07005052static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5053 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005054{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005055 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005056 enum res_type type;
5057 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005058 unsigned long long val;
5059 int ret;
5060
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005061 type = MEMFILE_TYPE(cft->private);
5062 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005063
5064 if (!do_swap_account && type == _MEMSWAP)
5065 return -EOPNOTSUPP;
5066
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005067 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005068 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005069 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5070 ret = -EINVAL;
5071 break;
5072 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005073 /* This function does all necessary parse...reuse it */
5074 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005075 if (ret)
5076 break;
5077 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005078 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005079 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005080 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005081 else if (type == _KMEM)
5082 ret = memcg_update_kmem_limit(cont, val);
5083 else
5084 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005085 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005086 case RES_SOFT_LIMIT:
5087 ret = res_counter_memparse_write_strategy(buffer, &val);
5088 if (ret)
5089 break;
5090 /*
5091 * For memsw, soft limits are hard to implement in terms
5092 * of semantics, for now, we support soft limits for
5093 * control without swap
5094 */
5095 if (type == _MEM)
5096 ret = res_counter_set_soft_limit(&memcg->res, val);
5097 else
5098 ret = -EINVAL;
5099 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005100 default:
5101 ret = -EINVAL; /* should be BUG() ? */
5102 break;
5103 }
5104 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005105}
5106
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005107static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5108 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5109{
5110 struct cgroup *cgroup;
5111 unsigned long long min_limit, min_memsw_limit, tmp;
5112
5113 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5114 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5115 cgroup = memcg->css.cgroup;
5116 if (!memcg->use_hierarchy)
5117 goto out;
5118
5119 while (cgroup->parent) {
5120 cgroup = cgroup->parent;
5121 memcg = mem_cgroup_from_cont(cgroup);
5122 if (!memcg->use_hierarchy)
5123 break;
5124 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5125 min_limit = min(min_limit, tmp);
5126 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5127 min_memsw_limit = min(min_memsw_limit, tmp);
5128 }
5129out:
5130 *mem_limit = min_limit;
5131 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005132}
5133
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005134static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005135{
Tejun Heoaf36f902012-04-01 12:09:55 -07005136 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005137 int name;
5138 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005139
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005140 type = MEMFILE_TYPE(event);
5141 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005142
5143 if (!do_swap_account && type == _MEMSWAP)
5144 return -EOPNOTSUPP;
5145
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005146 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005147 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005148 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005149 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005150 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005151 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005152 else if (type == _KMEM)
5153 res_counter_reset_max(&memcg->kmem);
5154 else
5155 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005156 break;
5157 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005158 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005159 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005160 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005161 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005162 else if (type == _KMEM)
5163 res_counter_reset_failcnt(&memcg->kmem);
5164 else
5165 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005166 break;
5167 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005168
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005169 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005170}
5171
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005172static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5173 struct cftype *cft)
5174{
5175 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5176}
5177
Daisuke Nishimura02491442010-03-10 15:22:17 -08005178#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005179static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5180 struct cftype *cft, u64 val)
5181{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005182 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005183
5184 if (val >= (1 << NR_MOVE_TYPE))
5185 return -EINVAL;
5186 /*
5187 * We check this value several times in both in can_attach() and
5188 * attach(), so we need cgroup lock to prevent this value from being
5189 * inconsistent.
5190 */
5191 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005192 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005193 cgroup_unlock();
5194
5195 return 0;
5196}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005197#else
5198static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5199 struct cftype *cft, u64 val)
5200{
5201 return -ENOSYS;
5202}
5203#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005204
Ying Han406eb0c2011-05-26 16:25:37 -07005205#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005206static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005207 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005208{
5209 int nid;
5210 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5211 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005212 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005213
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005214 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005215 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005216 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005217 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005218 seq_printf(m, " N%d=%lu", nid, node_nr);
5219 }
5220 seq_putc(m, '\n');
5221
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005222 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005223 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005224 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005225 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005226 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005227 seq_printf(m, " N%d=%lu", nid, node_nr);
5228 }
5229 seq_putc(m, '\n');
5230
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005231 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005232 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005233 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005234 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005235 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005236 seq_printf(m, " N%d=%lu", nid, node_nr);
5237 }
5238 seq_putc(m, '\n');
5239
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005240 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005241 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005242 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005243 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005244 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005245 seq_printf(m, " N%d=%lu", nid, node_nr);
5246 }
5247 seq_putc(m, '\n');
5248 return 0;
5249}
5250#endif /* CONFIG_NUMA */
5251
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005252static inline void mem_cgroup_lru_names_not_uptodate(void)
5253{
5254 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5255}
5256
Wanpeng Liab215882012-07-31 16:43:09 -07005257static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005258 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005259{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005260 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005261 struct mem_cgroup *mi;
5262 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005263
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005264 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005265 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005266 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005267 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5268 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005269 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005270
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005271 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5272 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5273 mem_cgroup_read_events(memcg, i));
5274
5275 for (i = 0; i < NR_LRU_LISTS; i++)
5276 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5277 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5278
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005279 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005280 {
5281 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005282 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005283 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005284 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005285 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5286 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005287 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005288
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005289 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5290 long long val = 0;
5291
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005292 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005293 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005294 for_each_mem_cgroup_tree(mi, memcg)
5295 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5296 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5297 }
5298
5299 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5300 unsigned long long val = 0;
5301
5302 for_each_mem_cgroup_tree(mi, memcg)
5303 val += mem_cgroup_read_events(mi, i);
5304 seq_printf(m, "total_%s %llu\n",
5305 mem_cgroup_events_names[i], val);
5306 }
5307
5308 for (i = 0; i < NR_LRU_LISTS; i++) {
5309 unsigned long long val = 0;
5310
5311 for_each_mem_cgroup_tree(mi, memcg)
5312 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5313 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005314 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005315
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005316#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005317 {
5318 int nid, zid;
5319 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005320 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005321 unsigned long recent_rotated[2] = {0, 0};
5322 unsigned long recent_scanned[2] = {0, 0};
5323
5324 for_each_online_node(nid)
5325 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005326 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005327 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005328
Hugh Dickins89abfab2012-05-29 15:06:53 -07005329 recent_rotated[0] += rstat->recent_rotated[0];
5330 recent_rotated[1] += rstat->recent_rotated[1];
5331 recent_scanned[0] += rstat->recent_scanned[0];
5332 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005333 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005334 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5335 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5336 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5337 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005338 }
5339#endif
5340
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005341 return 0;
5342}
5343
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005344static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5345{
5346 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5347
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005348 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005349}
5350
5351static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5352 u64 val)
5353{
5354 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5355 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005356
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005357 if (val > 100)
5358 return -EINVAL;
5359
5360 if (cgrp->parent == NULL)
5361 return -EINVAL;
5362
5363 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005364
5365 cgroup_lock();
5366
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005367 /* If under hierarchy, only empty-root can set this value */
5368 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08005369 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
5370 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005371 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005372 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005373
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005374 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005375
Li Zefan068b38c2009-01-15 13:51:26 -08005376 cgroup_unlock();
5377
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005378 return 0;
5379}
5380
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005381static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5382{
5383 struct mem_cgroup_threshold_ary *t;
5384 u64 usage;
5385 int i;
5386
5387 rcu_read_lock();
5388 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005389 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005390 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005391 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005392
5393 if (!t)
5394 goto unlock;
5395
5396 usage = mem_cgroup_usage(memcg, swap);
5397
5398 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005399 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005400 * If it's not true, a threshold was crossed after last
5401 * call of __mem_cgroup_threshold().
5402 */
Phil Carmody5407a562010-05-26 14:42:42 -07005403 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005404
5405 /*
5406 * Iterate backward over array of thresholds starting from
5407 * current_threshold and check if a threshold is crossed.
5408 * If none of thresholds below usage is crossed, we read
5409 * only one element of the array here.
5410 */
5411 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5412 eventfd_signal(t->entries[i].eventfd, 1);
5413
5414 /* i = current_threshold + 1 */
5415 i++;
5416
5417 /*
5418 * Iterate forward over array of thresholds starting from
5419 * current_threshold+1 and check if a threshold is crossed.
5420 * If none of thresholds above usage is crossed, we read
5421 * only one element of the array here.
5422 */
5423 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5424 eventfd_signal(t->entries[i].eventfd, 1);
5425
5426 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005427 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005428unlock:
5429 rcu_read_unlock();
5430}
5431
5432static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5433{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005434 while (memcg) {
5435 __mem_cgroup_threshold(memcg, false);
5436 if (do_swap_account)
5437 __mem_cgroup_threshold(memcg, true);
5438
5439 memcg = parent_mem_cgroup(memcg);
5440 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005441}
5442
5443static int compare_thresholds(const void *a, const void *b)
5444{
5445 const struct mem_cgroup_threshold *_a = a;
5446 const struct mem_cgroup_threshold *_b = b;
5447
5448 return _a->threshold - _b->threshold;
5449}
5450
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005451static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005452{
5453 struct mem_cgroup_eventfd_list *ev;
5454
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005455 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005456 eventfd_signal(ev->eventfd, 1);
5457 return 0;
5458}
5459
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005460static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005461{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005462 struct mem_cgroup *iter;
5463
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005464 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005465 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005466}
5467
5468static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5469 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005470{
5471 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005472 struct mem_cgroup_thresholds *thresholds;
5473 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005474 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005475 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005476 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005477
5478 ret = res_counter_memparse_write_strategy(args, &threshold);
5479 if (ret)
5480 return ret;
5481
5482 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005483
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005484 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005485 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005486 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005487 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005488 else
5489 BUG();
5490
5491 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5492
5493 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005494 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005495 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5496
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005497 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005498
5499 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005500 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005501 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005502 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005503 ret = -ENOMEM;
5504 goto unlock;
5505 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005506 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005507
5508 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005509 if (thresholds->primary) {
5510 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005511 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005512 }
5513
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005514 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005515 new->entries[size - 1].eventfd = eventfd;
5516 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005517
5518 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005519 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005520 compare_thresholds, NULL);
5521
5522 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005523 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005524 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005525 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005526 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005527 * new->current_threshold will not be used until
5528 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529 * it here.
5530 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005531 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005532 } else
5533 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005534 }
5535
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005536 /* Free old spare buffer and save old primary buffer as spare */
5537 kfree(thresholds->spare);
5538 thresholds->spare = thresholds->primary;
5539
5540 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005541
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005542 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005543 synchronize_rcu();
5544
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005545unlock:
5546 mutex_unlock(&memcg->thresholds_lock);
5547
5548 return ret;
5549}
5550
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005551static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005552 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005553{
5554 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005555 struct mem_cgroup_thresholds *thresholds;
5556 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005557 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005558 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005559 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005560
5561 mutex_lock(&memcg->thresholds_lock);
5562 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005563 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005564 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005565 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005566 else
5567 BUG();
5568
Anton Vorontsov371528c2012-02-24 05:14:46 +04005569 if (!thresholds->primary)
5570 goto unlock;
5571
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005572 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5573
5574 /* Check if a threshold crossed before removing */
5575 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5576
5577 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005578 size = 0;
5579 for (i = 0; i < thresholds->primary->size; i++) {
5580 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005581 size++;
5582 }
5583
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005584 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005585
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005586 /* Set thresholds array to NULL if we don't have thresholds */
5587 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005588 kfree(new);
5589 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005590 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005591 }
5592
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005593 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005594
5595 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005596 new->current_threshold = -1;
5597 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5598 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005599 continue;
5600
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005601 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005602 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005603 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005604 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005605 * until rcu_assign_pointer(), so it's safe to increment
5606 * it here.
5607 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005608 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005609 }
5610 j++;
5611 }
5612
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005613swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005614 /* Swap primary and spare array */
5615 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005616 /* If all events are unregistered, free the spare array */
5617 if (!new) {
5618 kfree(thresholds->spare);
5619 thresholds->spare = NULL;
5620 }
5621
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005622 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005623
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005624 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005625 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005626unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005627 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005628}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005629
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005630static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5631 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5632{
5633 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5634 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005635 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005636
5637 BUG_ON(type != _OOM_TYPE);
5638 event = kmalloc(sizeof(*event), GFP_KERNEL);
5639 if (!event)
5640 return -ENOMEM;
5641
Michal Hocko1af8efe2011-07-26 16:08:24 -07005642 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005643
5644 event->eventfd = eventfd;
5645 list_add(&event->list, &memcg->oom_notify);
5646
5647 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005648 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005649 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005650 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005651
5652 return 0;
5653}
5654
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005655static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005656 struct cftype *cft, struct eventfd_ctx *eventfd)
5657{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005658 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005659 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005660 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005661
5662 BUG_ON(type != _OOM_TYPE);
5663
Michal Hocko1af8efe2011-07-26 16:08:24 -07005664 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005665
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005666 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005667 if (ev->eventfd == eventfd) {
5668 list_del(&ev->list);
5669 kfree(ev);
5670 }
5671 }
5672
Michal Hocko1af8efe2011-07-26 16:08:24 -07005673 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005674}
5675
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005676static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5677 struct cftype *cft, struct cgroup_map_cb *cb)
5678{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005679 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005680
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005681 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005682
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005683 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005684 cb->fill(cb, "under_oom", 1);
5685 else
5686 cb->fill(cb, "under_oom", 0);
5687 return 0;
5688}
5689
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005690static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5691 struct cftype *cft, u64 val)
5692{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005693 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005694 struct mem_cgroup *parent;
5695
5696 /* cannot set to root cgroup and only 0 and 1 are allowed */
5697 if (!cgrp->parent || !((val == 0) || (val == 1)))
5698 return -EINVAL;
5699
5700 parent = mem_cgroup_from_cont(cgrp->parent);
5701
5702 cgroup_lock();
5703 /* oom-kill-disable is a flag for subhierarchy. */
5704 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005705 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005706 cgroup_unlock();
5707 return -EINVAL;
5708 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005709 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005710 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005711 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005712 cgroup_unlock();
5713 return 0;
5714}
5715
Andrew Mortonc255a452012-07-31 16:43:02 -07005716#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005717static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005718{
Glauber Costa55007d82012-12-18 14:22:38 -08005719 int ret;
5720
Glauber Costa2633d7a2012-12-18 14:22:34 -08005721 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005722 ret = memcg_propagate_kmem(memcg);
5723 if (ret)
5724 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005725
Glauber Costa1d62e432012-04-09 19:36:33 -03005726 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005727};
5728
Glauber Costa1d62e432012-04-09 19:36:33 -03005729static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005730{
Glauber Costa1d62e432012-04-09 19:36:33 -03005731 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005732
5733 memcg_kmem_mark_dead(memcg);
5734
5735 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5736 return;
5737
5738 /*
5739 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5740 * path here, being careful not to race with memcg_uncharge_kmem: it is
5741 * possible that the charges went down to 0 between mark_dead and the
5742 * res_counter read, so in that case, we don't need the put
5743 */
5744 if (memcg_kmem_test_and_clear_dead(memcg))
5745 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005746}
Glauber Costae5671df2011-12-11 21:47:01 +00005747#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005748static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005749{
5750 return 0;
5751}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005752
Glauber Costa1d62e432012-04-09 19:36:33 -03005753static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005754{
5755}
Glauber Costae5671df2011-12-11 21:47:01 +00005756#endif
5757
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005758static struct cftype mem_cgroup_files[] = {
5759 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005760 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005761 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005762 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005763 .register_event = mem_cgroup_usage_register_event,
5764 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005765 },
5766 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005767 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005768 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005769 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005770 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005771 },
5772 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005773 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005774 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005775 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005776 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005777 },
5778 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005779 .name = "soft_limit_in_bytes",
5780 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5781 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005782 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005783 },
5784 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005785 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005786 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005787 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005788 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005789 },
Balbir Singh8697d332008-02-07 00:13:59 -08005790 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005791 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005792 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005793 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005794 {
5795 .name = "force_empty",
5796 .trigger = mem_cgroup_force_empty_write,
5797 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005798 {
5799 .name = "use_hierarchy",
5800 .write_u64 = mem_cgroup_hierarchy_write,
5801 .read_u64 = mem_cgroup_hierarchy_read,
5802 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005803 {
5804 .name = "swappiness",
5805 .read_u64 = mem_cgroup_swappiness_read,
5806 .write_u64 = mem_cgroup_swappiness_write,
5807 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005808 {
5809 .name = "move_charge_at_immigrate",
5810 .read_u64 = mem_cgroup_move_charge_read,
5811 .write_u64 = mem_cgroup_move_charge_write,
5812 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005813 {
5814 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005815 .read_map = mem_cgroup_oom_control_read,
5816 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005817 .register_event = mem_cgroup_oom_register_event,
5818 .unregister_event = mem_cgroup_oom_unregister_event,
5819 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5820 },
Ying Han406eb0c2011-05-26 16:25:37 -07005821#ifdef CONFIG_NUMA
5822 {
5823 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005824 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005825 },
5826#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07005827#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005828 {
5829 .name = "memsw.usage_in_bytes",
5830 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005831 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005832 .register_event = mem_cgroup_usage_register_event,
5833 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005834 },
5835 {
5836 .name = "memsw.max_usage_in_bytes",
5837 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5838 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005839 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005840 },
5841 {
5842 .name = "memsw.limit_in_bytes",
5843 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5844 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005845 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005846 },
5847 {
5848 .name = "memsw.failcnt",
5849 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5850 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005851 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005852 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005853#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005854#ifdef CONFIG_MEMCG_KMEM
5855 {
5856 .name = "kmem.limit_in_bytes",
5857 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5858 .write_string = mem_cgroup_write,
5859 .read = mem_cgroup_read,
5860 },
5861 {
5862 .name = "kmem.usage_in_bytes",
5863 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5864 .read = mem_cgroup_read,
5865 },
5866 {
5867 .name = "kmem.failcnt",
5868 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5869 .trigger = mem_cgroup_reset,
5870 .read = mem_cgroup_read,
5871 },
5872 {
5873 .name = "kmem.max_usage_in_bytes",
5874 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5875 .trigger = mem_cgroup_reset,
5876 .read = mem_cgroup_read,
5877 },
Glauber Costa749c5412012-12-18 14:23:01 -08005878#ifdef CONFIG_SLABINFO
5879 {
5880 .name = "kmem.slabinfo",
5881 .read_seq_string = mem_cgroup_slabinfo_read,
5882 },
5883#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005884#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005885 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005886};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005887
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005888static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005889{
5890 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005891 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005892 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005893 /*
5894 * This routine is called against possible nodes.
5895 * But it's BUG to call kmalloc() against offline node.
5896 *
5897 * TODO: this routine can waste much memory for nodes which will
5898 * never be onlined. It's better to use memory hotplug callback
5899 * function.
5900 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005901 if (!node_state(node, N_NORMAL_MEMORY))
5902 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005903 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005904 if (!pn)
5905 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005906
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005907 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5908 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005909 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005910 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005911 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005912 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005913 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005914 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005915 return 0;
5916}
5917
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005918static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005919{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005920 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005921}
5922
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005923static struct mem_cgroup *mem_cgroup_alloc(void)
5924{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005925 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005926 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005927
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005928 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005929 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005930 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005931 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005932 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005933
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005934 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005935 return NULL;
5936
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005937 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5938 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005939 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005940 spin_lock_init(&memcg->pcp_counter_lock);
5941 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005942
5943out_free:
5944 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005945 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005946 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005947 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005948 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005949}
5950
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005951/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005952 * At destroying mem_cgroup, references from swap_cgroup can remain.
5953 * (scanning all at force_empty is too costly...)
5954 *
5955 * Instead of clearing all references at force_empty, we remember
5956 * the number of reference from swap_cgroup and free mem_cgroup when
5957 * it goes down to 0.
5958 *
5959 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005960 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005961
5962static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005963{
Glauber Costac8b2a362012-12-18 14:22:13 -08005964 int node;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005965 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005966
Glauber Costac8b2a362012-12-18 14:22:13 -08005967 mem_cgroup_remove_from_trees(memcg);
5968 free_css_id(&mem_cgroup_subsys, &memcg->css);
5969
5970 for_each_node(node)
5971 free_mem_cgroup_per_zone_info(memcg, node);
5972
5973 free_percpu(memcg->stat);
5974
Glauber Costa3f134612012-05-29 15:07:11 -07005975 /*
5976 * We need to make sure that (at least for now), the jump label
5977 * destruction code runs outside of the cgroup lock. This is because
5978 * get_online_cpus(), which is called from the static_branch update,
5979 * can't be called inside the cgroup_lock. cpusets are the ones
5980 * enforcing this dependency, so if they ever change, we might as well.
5981 *
5982 * schedule_work() will guarantee this happens. Be careful if you need
5983 * to move this code around, and make sure it is outside
5984 * the cgroup_lock.
5985 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005986 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005987 if (size < PAGE_SIZE)
5988 kfree(memcg);
5989 else
5990 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005991}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005992
Glauber Costac8b2a362012-12-18 14:22:13 -08005993
5994/*
5995 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
5996 * but in process context. The work_freeing structure is overlaid
5997 * on the rcu_freeing structure, which itself is overlaid on memsw.
5998 */
5999static void free_work(struct work_struct *work)
6000{
6001 struct mem_cgroup *memcg;
6002
6003 memcg = container_of(work, struct mem_cgroup, work_freeing);
6004 __mem_cgroup_free(memcg);
6005}
6006
Glauber Costa3afe36b2012-05-29 15:07:10 -07006007static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006008{
6009 struct mem_cgroup *memcg;
6010
6011 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006012 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006013 schedule_work(&memcg->work_freeing);
6014}
6015
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006016static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006017{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006018 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006019}
6020
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006021static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006022{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006023 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6024 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006025 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006026 if (parent)
6027 mem_cgroup_put(parent);
6028 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006029}
6030
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006031static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006032{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006033 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006034}
6035
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006036/*
6037 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6038 */
Glauber Costae1aab162011-12-11 21:47:03 +00006039struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006040{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006041 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006042 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006043 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006044}
Glauber Costae1aab162011-12-11 21:47:03 +00006045EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006046
Andrew Mortonc255a452012-07-31 16:43:02 -07006047#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006048static void __init enable_swap_cgroup(void)
6049{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08006050 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006051 do_swap_account = 1;
6052}
6053#else
6054static void __init enable_swap_cgroup(void)
6055{
6056}
6057#endif
6058
Balbir Singhf64c3f52009-09-23 15:56:37 -07006059static int mem_cgroup_soft_limit_tree_init(void)
6060{
6061 struct mem_cgroup_tree_per_node *rtpn;
6062 struct mem_cgroup_tree_per_zone *rtpz;
6063 int tmp, node, zone;
6064
Bob Liu3ed28fa2012-01-12 17:19:04 -08006065 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006066 tmp = node;
6067 if (!node_state(node, N_NORMAL_MEMORY))
6068 tmp = -1;
6069 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6070 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08006071 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006072
6073 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6074
6075 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6076 rtpz = &rtpn->rb_tree_per_zone[zone];
6077 rtpz->rb_root = RB_ROOT;
6078 spin_lock_init(&rtpz->lock);
6079 }
6080 }
6081 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08006082
6083err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08006084 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08006085 if (!soft_limit_tree.rb_tree_per_node[node])
6086 break;
6087 kfree(soft_limit_tree.rb_tree_per_node[node]);
6088 soft_limit_tree.rb_tree_per_node[node] = NULL;
6089 }
6090 return 1;
6091
Balbir Singhf64c3f52009-09-23 15:56:37 -07006092}
6093
Li Zefan0eb253e2009-01-15 13:51:25 -08006094static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006095mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006096{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006097 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006098 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006099 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006100
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006101 memcg = mem_cgroup_alloc();
6102 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006103 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006104
Bob Liu3ed28fa2012-01-12 17:19:04 -08006105 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006106 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006107 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006108
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006109 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006110 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006111 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006112 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006113 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006114 if (mem_cgroup_soft_limit_tree_init())
6115 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08006116 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006117 for_each_possible_cpu(cpu) {
6118 struct memcg_stock_pcp *stock =
6119 &per_cpu(memcg_stock, cpu);
6120 INIT_WORK(&stock->work, drain_local_stock);
6121 }
Balbir Singh18f59ea2009-01-07 18:08:07 -08006122 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006123 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006124 memcg->use_hierarchy = parent->use_hierarchy;
6125 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006126 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006127
Balbir Singh18f59ea2009-01-07 18:08:07 -08006128 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006129 res_counter_init(&memcg->res, &parent->res);
6130 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006131 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006132
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006133 /*
6134 * We increment refcnt of the parent to ensure that we can
6135 * safely access it on res_counter_charge/uncharge.
6136 * This refcnt will be decremented when freeing this
6137 * mem_cgroup(see mem_cgroup_put).
6138 */
6139 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006140 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006141 res_counter_init(&memcg->res, NULL);
6142 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006143 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006144 /*
6145 * Deeper hierachy with use_hierarchy == false doesn't make
6146 * much sense so let cgroup subsystem know about this
6147 * unfortunate state in our controller.
6148 */
6149 if (parent && parent != root_mem_cgroup)
6150 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006151 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006152 memcg->last_scanned_node = MAX_NUMNODES;
6153 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08006154
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006155 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006156 memcg->swappiness = mem_cgroup_swappiness(parent);
6157 atomic_set(&memcg->refcnt, 1);
6158 memcg->move_charge_at_immigrate = 0;
6159 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07006160 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03006161
6162 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
6163 if (error) {
6164 /*
6165 * We call put now because our (and parent's) refcnts
6166 * are already in place. mem_cgroup_put() will internally
6167 * call __mem_cgroup_free, so return directly
6168 */
6169 mem_cgroup_put(memcg);
6170 return ERR_PTR(error);
6171 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006172 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006173free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006174 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006175 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006176}
6177
Tejun Heo92fb9742012-11-19 08:13:38 -08006178static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006179{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006180 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006181
Michal Hockoab5196c2012-10-26 13:37:32 +02006182 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006183 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006184}
6185
Tejun Heo92fb9742012-11-19 08:13:38 -08006186static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006187{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006188 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006189
Glauber Costa1d62e432012-04-09 19:36:33 -03006190 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006191
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006192 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006193}
6194
Daisuke Nishimura02491442010-03-10 15:22:17 -08006195#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006196/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006197#define PRECHARGE_COUNT_AT_ONCE 256
6198static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006199{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006200 int ret = 0;
6201 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006202 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006204 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006205 mc.precharge += count;
6206 /* we don't need css_get for root */
6207 return ret;
6208 }
6209 /* try to charge at once */
6210 if (count > 1) {
6211 struct res_counter *dummy;
6212 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006213 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006214 * by cgroup_lock_live_cgroup() that it is not removed and we
6215 * are still under the same cgroup_mutex. So we can postpone
6216 * css_get().
6217 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006218 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006219 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006220 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006221 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006222 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006223 goto one_by_one;
6224 }
6225 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006226 return ret;
6227 }
6228one_by_one:
6229 /* fall back to one by one charge */
6230 while (count--) {
6231 if (signal_pending(current)) {
6232 ret = -EINTR;
6233 break;
6234 }
6235 if (!batch_count--) {
6236 batch_count = PRECHARGE_COUNT_AT_ONCE;
6237 cond_resched();
6238 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006239 ret = __mem_cgroup_try_charge(NULL,
6240 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006241 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006242 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006243 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006244 mc.precharge++;
6245 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006246 return ret;
6247}
6248
6249/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006250 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006251 * @vma: the vma the pte to be checked belongs
6252 * @addr: the address corresponding to the pte to be checked
6253 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006254 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006255 *
6256 * Returns
6257 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6258 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6259 * move charge. if @target is not NULL, the page is stored in target->page
6260 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006261 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6262 * target for charge migration. if @target is not NULL, the entry is stored
6263 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006264 *
6265 * Called with pte lock held.
6266 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006267union mc_target {
6268 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006269 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006270};
6271
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006272enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006273 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006274 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006275 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006276};
6277
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006278static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6279 unsigned long addr, pte_t ptent)
6280{
6281 struct page *page = vm_normal_page(vma, addr, ptent);
6282
6283 if (!page || !page_mapped(page))
6284 return NULL;
6285 if (PageAnon(page)) {
6286 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006287 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006288 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006289 } else if (!move_file())
6290 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006291 return NULL;
6292 if (!get_page_unless_zero(page))
6293 return NULL;
6294
6295 return page;
6296}
6297
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006298#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006299static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6300 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6301{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006302 struct page *page = NULL;
6303 swp_entry_t ent = pte_to_swp_entry(ptent);
6304
6305 if (!move_anon() || non_swap_entry(ent))
6306 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006307 /*
6308 * Because lookup_swap_cache() updates some statistics counter,
6309 * we call find_get_page() with swapper_space directly.
6310 */
6311 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006312 if (do_swap_account)
6313 entry->val = ent.val;
6314
6315 return page;
6316}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006317#else
6318static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6319 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6320{
6321 return NULL;
6322}
6323#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006324
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006325static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6326 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6327{
6328 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006329 struct address_space *mapping;
6330 pgoff_t pgoff;
6331
6332 if (!vma->vm_file) /* anonymous vma */
6333 return NULL;
6334 if (!move_file())
6335 return NULL;
6336
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006337 mapping = vma->vm_file->f_mapping;
6338 if (pte_none(ptent))
6339 pgoff = linear_page_index(vma, addr);
6340 else /* pte_file(ptent) is true */
6341 pgoff = pte_to_pgoff(ptent);
6342
6343 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006344 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006345
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006346#ifdef CONFIG_SWAP
6347 /* shmem/tmpfs may report page out on swap: account for that too. */
6348 if (radix_tree_exceptional_entry(page)) {
6349 swp_entry_t swap = radix_to_swp_entry(page);
6350 if (do_swap_account)
6351 *entry = swap;
6352 page = find_get_page(&swapper_space, swap.val);
6353 }
6354#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006355 return page;
6356}
6357
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006358static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006359 unsigned long addr, pte_t ptent, union mc_target *target)
6360{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006361 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006362 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006363 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006364 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006365
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006366 if (pte_present(ptent))
6367 page = mc_handle_present_pte(vma, addr, ptent);
6368 else if (is_swap_pte(ptent))
6369 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006370 else if (pte_none(ptent) || pte_file(ptent))
6371 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006372
6373 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006374 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006375 if (page) {
6376 pc = lookup_page_cgroup(page);
6377 /*
6378 * Do only loose check w/o page_cgroup lock.
6379 * mem_cgroup_move_account() checks the pc is valid or not under
6380 * the lock.
6381 */
6382 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6383 ret = MC_TARGET_PAGE;
6384 if (target)
6385 target->page = page;
6386 }
6387 if (!ret || !target)
6388 put_page(page);
6389 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006390 /* There is a swap entry and a page doesn't exist or isn't charged */
6391 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006392 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006393 ret = MC_TARGET_SWAP;
6394 if (target)
6395 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006396 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006397 return ret;
6398}
6399
Naoya Horiguchi12724852012-03-21 16:34:28 -07006400#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6401/*
6402 * We don't consider swapping or file mapped pages because THP does not
6403 * support them for now.
6404 * Caller should make sure that pmd_trans_huge(pmd) is true.
6405 */
6406static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6407 unsigned long addr, pmd_t pmd, union mc_target *target)
6408{
6409 struct page *page = NULL;
6410 struct page_cgroup *pc;
6411 enum mc_target_type ret = MC_TARGET_NONE;
6412
6413 page = pmd_page(pmd);
6414 VM_BUG_ON(!page || !PageHead(page));
6415 if (!move_anon())
6416 return ret;
6417 pc = lookup_page_cgroup(page);
6418 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6419 ret = MC_TARGET_PAGE;
6420 if (target) {
6421 get_page(page);
6422 target->page = page;
6423 }
6424 }
6425 return ret;
6426}
6427#else
6428static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6429 unsigned long addr, pmd_t pmd, union mc_target *target)
6430{
6431 return MC_TARGET_NONE;
6432}
6433#endif
6434
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6436 unsigned long addr, unsigned long end,
6437 struct mm_walk *walk)
6438{
6439 struct vm_area_struct *vma = walk->private;
6440 pte_t *pte;
6441 spinlock_t *ptl;
6442
Naoya Horiguchi12724852012-03-21 16:34:28 -07006443 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6444 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6445 mc.precharge += HPAGE_PMD_NR;
6446 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006447 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006448 }
Dave Hansen03319322011-03-22 16:32:56 -07006449
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006450 if (pmd_trans_unstable(pmd))
6451 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006452 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6453 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006454 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006455 mc.precharge++; /* increment precharge temporarily */
6456 pte_unmap_unlock(pte - 1, ptl);
6457 cond_resched();
6458
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006459 return 0;
6460}
6461
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006462static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6463{
6464 unsigned long precharge;
6465 struct vm_area_struct *vma;
6466
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006467 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006468 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6469 struct mm_walk mem_cgroup_count_precharge_walk = {
6470 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6471 .mm = mm,
6472 .private = vma,
6473 };
6474 if (is_vm_hugetlb_page(vma))
6475 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006476 walk_page_range(vma->vm_start, vma->vm_end,
6477 &mem_cgroup_count_precharge_walk);
6478 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006479 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006480
6481 precharge = mc.precharge;
6482 mc.precharge = 0;
6483
6484 return precharge;
6485}
6486
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006487static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6488{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006489 unsigned long precharge = mem_cgroup_count_precharge(mm);
6490
6491 VM_BUG_ON(mc.moving_task);
6492 mc.moving_task = current;
6493 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006494}
6495
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006496/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6497static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006498{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006499 struct mem_cgroup *from = mc.from;
6500 struct mem_cgroup *to = mc.to;
6501
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006502 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006503 if (mc.precharge) {
6504 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6505 mc.precharge = 0;
6506 }
6507 /*
6508 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6509 * we must uncharge here.
6510 */
6511 if (mc.moved_charge) {
6512 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6513 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006514 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006515 /* we must fixup refcnts and charges */
6516 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006517 /* uncharge swap account from the old cgroup */
6518 if (!mem_cgroup_is_root(mc.from))
6519 res_counter_uncharge(&mc.from->memsw,
6520 PAGE_SIZE * mc.moved_swap);
6521 __mem_cgroup_put(mc.from, mc.moved_swap);
6522
6523 if (!mem_cgroup_is_root(mc.to)) {
6524 /*
6525 * we charged both to->res and to->memsw, so we should
6526 * uncharge to->res.
6527 */
6528 res_counter_uncharge(&mc.to->res,
6529 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006530 }
6531 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006532 mc.moved_swap = 0;
6533 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006534 memcg_oom_recover(from);
6535 memcg_oom_recover(to);
6536 wake_up_all(&mc.waitq);
6537}
6538
6539static void mem_cgroup_clear_mc(void)
6540{
6541 struct mem_cgroup *from = mc.from;
6542
6543 /*
6544 * we must clear moving_task before waking up waiters at the end of
6545 * task migration.
6546 */
6547 mc.moving_task = NULL;
6548 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006549 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006550 mc.from = NULL;
6551 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006552 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006553 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006554}
6555
Li Zefan761b3ef2012-01-31 13:47:36 +08006556static int mem_cgroup_can_attach(struct cgroup *cgroup,
6557 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006558{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006559 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006560 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006561 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006562
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006563 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006564 struct mm_struct *mm;
6565 struct mem_cgroup *from = mem_cgroup_from_task(p);
6566
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006567 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006568
6569 mm = get_task_mm(p);
6570 if (!mm)
6571 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006572 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006573 if (mm->owner == p) {
6574 VM_BUG_ON(mc.from);
6575 VM_BUG_ON(mc.to);
6576 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006577 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006578 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006579 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006580 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006581 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006582 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006583 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006584 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006585
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006586 ret = mem_cgroup_precharge_mc(mm);
6587 if (ret)
6588 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006589 }
6590 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006591 }
6592 return ret;
6593}
6594
Li Zefan761b3ef2012-01-31 13:47:36 +08006595static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6596 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006597{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006598 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006599}
6600
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006601static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6602 unsigned long addr, unsigned long end,
6603 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006604{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006605 int ret = 0;
6606 struct vm_area_struct *vma = walk->private;
6607 pte_t *pte;
6608 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006609 enum mc_target_type target_type;
6610 union mc_target target;
6611 struct page *page;
6612 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006613
Naoya Horiguchi12724852012-03-21 16:34:28 -07006614 /*
6615 * We don't take compound_lock() here but no race with splitting thp
6616 * happens because:
6617 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6618 * under splitting, which means there's no concurrent thp split,
6619 * - if another thread runs into split_huge_page() just after we
6620 * entered this if-block, the thread must wait for page table lock
6621 * to be unlocked in __split_huge_page_splitting(), where the main
6622 * part of thp split is not executed yet.
6623 */
6624 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006625 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006626 spin_unlock(&vma->vm_mm->page_table_lock);
6627 return 0;
6628 }
6629 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6630 if (target_type == MC_TARGET_PAGE) {
6631 page = target.page;
6632 if (!isolate_lru_page(page)) {
6633 pc = lookup_page_cgroup(page);
6634 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006635 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006636 mc.precharge -= HPAGE_PMD_NR;
6637 mc.moved_charge += HPAGE_PMD_NR;
6638 }
6639 putback_lru_page(page);
6640 }
6641 put_page(page);
6642 }
6643 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006644 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006645 }
6646
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006647 if (pmd_trans_unstable(pmd))
6648 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006649retry:
6650 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6651 for (; addr != end; addr += PAGE_SIZE) {
6652 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006653 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006654
6655 if (!mc.precharge)
6656 break;
6657
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006658 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006659 case MC_TARGET_PAGE:
6660 page = target.page;
6661 if (isolate_lru_page(page))
6662 goto put;
6663 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006664 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006665 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006666 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006667 /* we uncharge from mc.from later. */
6668 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006669 }
6670 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006671put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006672 put_page(page);
6673 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006674 case MC_TARGET_SWAP:
6675 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006676 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006677 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006678 /* we fixup refcnts and charges later. */
6679 mc.moved_swap++;
6680 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006681 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006682 default:
6683 break;
6684 }
6685 }
6686 pte_unmap_unlock(pte - 1, ptl);
6687 cond_resched();
6688
6689 if (addr != end) {
6690 /*
6691 * We have consumed all precharges we got in can_attach().
6692 * We try charge one by one, but don't do any additional
6693 * charges to mc.to if we have failed in charge once in attach()
6694 * phase.
6695 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006696 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006697 if (!ret)
6698 goto retry;
6699 }
6700
6701 return ret;
6702}
6703
6704static void mem_cgroup_move_charge(struct mm_struct *mm)
6705{
6706 struct vm_area_struct *vma;
6707
6708 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006709retry:
6710 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6711 /*
6712 * Someone who are holding the mmap_sem might be waiting in
6713 * waitq. So we cancel all extra charges, wake up all waiters,
6714 * and retry. Because we cancel precharges, we might not be able
6715 * to move enough charges, but moving charge is a best-effort
6716 * feature anyway, so it wouldn't be a big problem.
6717 */
6718 __mem_cgroup_clear_mc();
6719 cond_resched();
6720 goto retry;
6721 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006722 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6723 int ret;
6724 struct mm_walk mem_cgroup_move_charge_walk = {
6725 .pmd_entry = mem_cgroup_move_charge_pte_range,
6726 .mm = mm,
6727 .private = vma,
6728 };
6729 if (is_vm_hugetlb_page(vma))
6730 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006731 ret = walk_page_range(vma->vm_start, vma->vm_end,
6732 &mem_cgroup_move_charge_walk);
6733 if (ret)
6734 /*
6735 * means we have consumed all precharges and failed in
6736 * doing additional charge. Just abandon here.
6737 */
6738 break;
6739 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006740 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006741}
6742
Li Zefan761b3ef2012-01-31 13:47:36 +08006743static void mem_cgroup_move_task(struct cgroup *cont,
6744 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006745{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006746 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006747 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006748
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006749 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006750 if (mc.to)
6751 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006752 mmput(mm);
6753 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006754 if (mc.to)
6755 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006756}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006757#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006758static int mem_cgroup_can_attach(struct cgroup *cgroup,
6759 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006760{
6761 return 0;
6762}
Li Zefan761b3ef2012-01-31 13:47:36 +08006763static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6764 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006765{
6766}
Li Zefan761b3ef2012-01-31 13:47:36 +08006767static void mem_cgroup_move_task(struct cgroup *cont,
6768 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006769{
6770}
6771#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006772
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006773struct cgroup_subsys mem_cgroup_subsys = {
6774 .name = "memory",
6775 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006776 .css_alloc = mem_cgroup_css_alloc,
6777 .css_offline = mem_cgroup_css_offline,
6778 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006779 .can_attach = mem_cgroup_can_attach,
6780 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006781 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006782 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006783 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006784 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006785};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006786
Tejun Heo154b4542012-12-20 15:05:40 -08006787/*
6788 * The rest of init is performed during ->css_alloc() for root css which
6789 * happens before initcalls. hotcpu_notifier() can't be done together as
6790 * it would introduce circular locking by adding cgroup_lock -> cpu hotplug
6791 * dependency. Do it from a subsys_initcall().
6792 */
6793static int __init mem_cgroup_init(void)
6794{
6795 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
6796 return 0;
6797}
6798subsys_initcall(mem_cgroup_init);
6799
Andrew Mortonc255a452012-07-31 16:43:02 -07006800#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006801static int __init enable_swap_account(char *s)
6802{
6803 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006804 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006805 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006806 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006807 really_do_swap_account = 0;
6808 return 1;
6809}
Michal Hockoa2c89902011-05-24 17:12:50 -07006810__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006811
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006812#endif