blob: 26a38b7c7739d36c5ed734ec17f0dfcaecc2bd2e [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 {
Michal Hocko542f85f2013-04-29 15:07:15 -0700155 /* last scanned hierarchy member with elevated css ref count */
156 struct mem_cgroup *last_visited;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800157 /* scan generation, increased every round-trip */
158 unsigned int generation;
Michal Hocko542f85f2013-04-29 15:07:15 -0700159 /* lock to protect the position and generation */
160 spinlock_t iter_lock;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800161};
162
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800163/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164 * per-zone information in memory controller.
165 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800166struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800167 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700168 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800169
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800170 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
171
Balbir Singhf64c3f52009-09-23 15:56:37 -0700172 struct rb_node tree_node; /* RB tree node */
173 unsigned long long usage_in_excess;/* Set to the value by which */
174 /* the soft limit is exceeded*/
175 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700176 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700177 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800178};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800179
180struct mem_cgroup_per_node {
181 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
182};
183
184struct mem_cgroup_lru_info {
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800185 struct mem_cgroup_per_node *nodeinfo[0];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800186};
187
188/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700189 * Cgroups above their limits are maintained in a RB-Tree, independent of
190 * their hierarchy representation
191 */
192
193struct mem_cgroup_tree_per_zone {
194 struct rb_root rb_root;
195 spinlock_t lock;
196};
197
198struct mem_cgroup_tree_per_node {
199 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
200};
201
202struct mem_cgroup_tree {
203 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
204};
205
206static struct mem_cgroup_tree soft_limit_tree __read_mostly;
207
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800208struct mem_cgroup_threshold {
209 struct eventfd_ctx *eventfd;
210 u64 threshold;
211};
212
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700213/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800214struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700215 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700216 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800217 /* Size of entries[] */
218 unsigned int size;
219 /* Array of thresholds */
220 struct mem_cgroup_threshold entries[0];
221};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700222
223struct mem_cgroup_thresholds {
224 /* Primary thresholds array */
225 struct mem_cgroup_threshold_ary *primary;
226 /*
227 * Spare threshold array.
228 * This is needed to make mem_cgroup_unregister_event() "never fail".
229 * It must be able to store at least primary->size - 1 entries.
230 */
231 struct mem_cgroup_threshold_ary *spare;
232};
233
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700234/* for OOM */
235struct mem_cgroup_eventfd_list {
236 struct list_head list;
237 struct eventfd_ctx *eventfd;
238};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800239
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700240static void mem_cgroup_threshold(struct mem_cgroup *memcg);
241static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800242
Balbir Singhf64c3f52009-09-23 15:56:37 -0700243/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800244 * The memory controller data structure. The memory controller controls both
245 * page cache and RSS per cgroup. We would eventually like to provide
246 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
247 * to help the administrator determine what knobs to tune.
248 *
249 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800250 * we hit the water mark. May be even add a low water mark, such that
251 * no reclaim occurs from a cgroup at it's low water mark, this is
252 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800253 */
254struct mem_cgroup {
255 struct cgroup_subsys_state css;
256 /*
257 * the counter to account for memory usage
258 */
259 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700260
261 union {
262 /*
263 * the counter to account for mem+swap usage.
264 */
265 struct res_counter memsw;
266
267 /*
268 * rcu_freeing is used only when freeing struct mem_cgroup,
269 * so put it into a union to avoid wasting more memory.
270 * It must be disjoint from the css field. It could be
271 * in a union with the res field, but res plays a much
272 * larger part in mem_cgroup life than memsw, and might
273 * be of interest, even at time of free, when debugging.
274 * So share rcu_head with the less interesting memsw.
275 */
276 struct rcu_head rcu_freeing;
277 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700278 * We also need some space for a worker in deferred freeing.
279 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700280 */
281 struct work_struct work_freeing;
282 };
283
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800284 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800285 * the counter to account for kernel memory usage.
286 */
287 struct res_counter kmem;
288 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800289 * Should the accounting and control be hierarchical, per subtree?
290 */
291 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800292 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700293
294 bool oom_lock;
295 atomic_t under_oom;
296
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800297 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800298
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700299 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700300 /* OOM-Killer disable */
301 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800302
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700303 /* set when res.limit == memsw.limit */
304 bool memsw_is_minimum;
305
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800306 /* protect arrays of thresholds */
307 struct mutex thresholds_lock;
308
309 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700310 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700311
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800312 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700313 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700314
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700315 /* For oom notifier event fd */
316 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700317
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800318 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800319 * Should we move charges of a task when a task is moved into this
320 * mem_cgroup ? And what type of charges should we move ?
321 */
322 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800323 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700324 * set > 0 if pages under this cgroup are moving to other cgroup.
325 */
326 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700327 /* taken only while moving_account > 0 */
328 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700329 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800330 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800331 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700332 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700333 /*
334 * used when a cpu is offlined or other synchronizations
335 * See mem_cgroup_read_stat().
336 */
337 struct mem_cgroup_stat_cpu nocpu_base;
338 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000339
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700340#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000341 struct tcp_memcontrol tcp_mem;
342#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800343#if defined(CONFIG_MEMCG_KMEM)
344 /* analogous to slab_common's slab_caches list. per-memcg */
345 struct list_head memcg_slab_caches;
346 /* Not a spinlock, we can take a lot of time walking the list */
347 struct mutex slab_caches_mutex;
348 /* Index in the kmem_cache->memcg_params->memcg_caches array */
349 int kmemcg_id;
350#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800351
352 int last_scanned_node;
353#if MAX_NUMNODES > 1
354 nodemask_t scan_nodes;
355 atomic_t numainfo_events;
356 atomic_t numainfo_updating;
357#endif
358 /*
359 * Per cgroup active and inactive list, similar to the
360 * per zone LRU lists.
361 *
362 * WARNING: This has to be the last element of the struct. Don't
363 * add new fields after this point.
364 */
365 struct mem_cgroup_lru_info info;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800366};
367
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800368static size_t memcg_size(void)
369{
370 return sizeof(struct mem_cgroup) +
371 nr_node_ids * sizeof(struct mem_cgroup_per_node);
372}
373
Glauber Costa510fc4e2012-12-18 14:21:47 -0800374/* internal only representation about the status of kmem accounting. */
375enum {
376 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800377 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800378 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800379};
380
Glauber Costaa8964b92012-12-18 14:22:09 -0800381/* We account when limit is on, but only after call sites are patched */
382#define KMEM_ACCOUNTED_MASK \
383 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800384
385#ifdef CONFIG_MEMCG_KMEM
386static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
387{
388 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
389}
Glauber Costa7de37682012-12-18 14:22:07 -0800390
391static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
392{
393 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
394}
395
Glauber Costaa8964b92012-12-18 14:22:09 -0800396static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
397{
398 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
399}
400
Glauber Costa55007d82012-12-18 14:22:38 -0800401static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
402{
403 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
404}
405
Glauber Costa7de37682012-12-18 14:22:07 -0800406static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
407{
408 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
409 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
410}
411
412static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
413{
414 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
415 &memcg->kmem_account_flags);
416}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800417#endif
418
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800419/* Stuffs for move charges at task migration. */
420/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800421 * Types of charges to be moved. "move_charge_at_immitgrate" and
422 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800423 */
424enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800425 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700426 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800427 NR_MOVE_TYPE,
428};
429
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800430/* "mc" and its members are protected by cgroup_mutex */
431static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800432 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800433 struct mem_cgroup *from;
434 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800435 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800436 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800437 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800438 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800439 struct task_struct *moving_task; /* a task moving charges */
440 wait_queue_head_t waitq; /* a waitq for other context */
441} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700442 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800443 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
444};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800445
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700446static bool move_anon(void)
447{
Glauber Costaee5e8472013-02-22 16:34:50 -0800448 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700449}
450
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700451static bool move_file(void)
452{
Glauber Costaee5e8472013-02-22 16:34:50 -0800453 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700454}
455
Balbir Singh4e416952009-09-23 15:56:39 -0700456/*
457 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
458 * limit reclaim to prevent infinite loops, if they ever occur.
459 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700460#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
461#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700462
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800463enum charge_type {
464 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700465 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800466 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700467 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700468 NR_CHARGE_TYPE,
469};
470
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800471/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800472enum res_type {
473 _MEM,
474 _MEMSWAP,
475 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800476 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800477};
478
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700479#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
480#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800481#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700482/* Used for OOM nofiier */
483#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800484
Balbir Singh75822b42009-09-23 15:56:38 -0700485/*
486 * Reclaim flags for mem_cgroup_hierarchical_reclaim
487 */
488#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
489#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
490#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
491#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
492
Glauber Costa09998212013-02-22 16:34:55 -0800493/*
494 * The memcg_create_mutex will be held whenever a new cgroup is created.
495 * As a consequence, any change that needs to protect against new child cgroups
496 * appearing has to hold it as well.
497 */
498static DEFINE_MUTEX(memcg_create_mutex);
499
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700500static void mem_cgroup_get(struct mem_cgroup *memcg);
501static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000502
Wanpeng Lib2145142012-07-31 16:46:01 -0700503static inline
504struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
505{
506 return container_of(s, struct mem_cgroup, css);
507}
508
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700509static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
510{
511 return (memcg == root_mem_cgroup);
512}
513
Glauber Costae1aab162011-12-11 21:47:03 +0000514/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700515#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000516
Glauber Costae1aab162011-12-11 21:47:03 +0000517void sock_update_memcg(struct sock *sk)
518{
Glauber Costa376be5f2012-01-20 04:57:14 +0000519 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000520 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700521 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000522
523 BUG_ON(!sk->sk_prot->proto_cgroup);
524
Glauber Costaf3f511e2012-01-05 20:16:39 +0000525 /* Socket cloning can throw us here with sk_cgrp already
526 * filled. It won't however, necessarily happen from
527 * process context. So the test for root memcg given
528 * the current task's memcg won't help us in this case.
529 *
530 * Respecting the original socket's memcg is a better
531 * decision in this case.
532 */
533 if (sk->sk_cgrp) {
534 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
535 mem_cgroup_get(sk->sk_cgrp->memcg);
536 return;
537 }
538
Glauber Costae1aab162011-12-11 21:47:03 +0000539 rcu_read_lock();
540 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700541 cg_proto = sk->sk_prot->proto_cgroup(memcg);
542 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000543 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700544 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000545 }
546 rcu_read_unlock();
547 }
548}
549EXPORT_SYMBOL(sock_update_memcg);
550
551void sock_release_memcg(struct sock *sk)
552{
Glauber Costa376be5f2012-01-20 04:57:14 +0000553 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000554 struct mem_cgroup *memcg;
555 WARN_ON(!sk->sk_cgrp->memcg);
556 memcg = sk->sk_cgrp->memcg;
557 mem_cgroup_put(memcg);
558 }
559}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000560
561struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
562{
563 if (!memcg || mem_cgroup_is_root(memcg))
564 return NULL;
565
566 return &memcg->tcp_mem.cg_proto;
567}
568EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000569
Glauber Costa3f134612012-05-29 15:07:11 -0700570static void disarm_sock_keys(struct mem_cgroup *memcg)
571{
572 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
573 return;
574 static_key_slow_dec(&memcg_socket_limit_enabled);
575}
576#else
577static void disarm_sock_keys(struct mem_cgroup *memcg)
578{
579}
580#endif
581
Glauber Costaa8964b92012-12-18 14:22:09 -0800582#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800583/*
584 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
585 * There are two main reasons for not using the css_id for this:
586 * 1) this works better in sparse environments, where we have a lot of memcgs,
587 * but only a few kmem-limited. Or also, if we have, for instance, 200
588 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
589 * 200 entry array for that.
590 *
591 * 2) In order not to violate the cgroup API, we would like to do all memory
592 * allocation in ->create(). At that point, we haven't yet allocated the
593 * css_id. Having a separate index prevents us from messing with the cgroup
594 * core for this
595 *
596 * The current size of the caches array is stored in
597 * memcg_limited_groups_array_size. It will double each time we have to
598 * increase it.
599 */
600static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800601int memcg_limited_groups_array_size;
602
Glauber Costa55007d82012-12-18 14:22:38 -0800603/*
604 * MIN_SIZE is different than 1, because we would like to avoid going through
605 * the alloc/free process all the time. In a small machine, 4 kmem-limited
606 * cgroups is a reasonable guess. In the future, it could be a parameter or
607 * tunable, but that is strictly not necessary.
608 *
609 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
610 * this constant directly from cgroup, but it is understandable that this is
611 * better kept as an internal representation in cgroup.c. In any case, the
612 * css_id space is not getting any smaller, and we don't have to necessarily
613 * increase ours as well if it increases.
614 */
615#define MEMCG_CACHES_MIN_SIZE 4
616#define MEMCG_CACHES_MAX_SIZE 65535
617
Glauber Costad7f25f82012-12-18 14:22:40 -0800618/*
619 * A lot of the calls to the cache allocation functions are expected to be
620 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
621 * conditional to this static branch, we'll have to allow modules that does
622 * kmem_cache_alloc and the such to see this symbol as well
623 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800624struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800625EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800626
627static void disarm_kmem_keys(struct mem_cgroup *memcg)
628{
Glauber Costa55007d82012-12-18 14:22:38 -0800629 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800630 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800631 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
632 }
Glauber Costabea207c2012-12-18 14:22:11 -0800633 /*
634 * This check can't live in kmem destruction function,
635 * since the charges will outlive the cgroup
636 */
637 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800638}
639#else
640static void disarm_kmem_keys(struct mem_cgroup *memcg)
641{
642}
643#endif /* CONFIG_MEMCG_KMEM */
644
645static void disarm_static_keys(struct mem_cgroup *memcg)
646{
647 disarm_sock_keys(memcg);
648 disarm_kmem_keys(memcg);
649}
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800652
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700654mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700655{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800656 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700657 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700658}
659
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700660struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100661{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700662 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100663}
664
Balbir Singhf64c3f52009-09-23 15:56:37 -0700665static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700666page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700667{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700668 int nid = page_to_nid(page);
669 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700670
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700671 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700672}
673
674static struct mem_cgroup_tree_per_zone *
675soft_limit_tree_node_zone(int nid, int zid)
676{
677 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
678}
679
680static struct mem_cgroup_tree_per_zone *
681soft_limit_tree_from_page(struct page *page)
682{
683 int nid = page_to_nid(page);
684 int zid = page_zonenum(page);
685
686 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
687}
688
689static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700690__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700691 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700692 struct mem_cgroup_tree_per_zone *mctz,
693 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700694{
695 struct rb_node **p = &mctz->rb_root.rb_node;
696 struct rb_node *parent = NULL;
697 struct mem_cgroup_per_zone *mz_node;
698
699 if (mz->on_tree)
700 return;
701
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700702 mz->usage_in_excess = new_usage_in_excess;
703 if (!mz->usage_in_excess)
704 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700705 while (*p) {
706 parent = *p;
707 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
708 tree_node);
709 if (mz->usage_in_excess < mz_node->usage_in_excess)
710 p = &(*p)->rb_left;
711 /*
712 * We can't avoid mem cgroups that are over their soft
713 * limit by the same amount
714 */
715 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
716 p = &(*p)->rb_right;
717 }
718 rb_link_node(&mz->tree_node, parent, p);
719 rb_insert_color(&mz->tree_node, &mctz->rb_root);
720 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700721}
722
723static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700724__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700725 struct mem_cgroup_per_zone *mz,
726 struct mem_cgroup_tree_per_zone *mctz)
727{
728 if (!mz->on_tree)
729 return;
730 rb_erase(&mz->tree_node, &mctz->rb_root);
731 mz->on_tree = false;
732}
733
734static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700735mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700736 struct mem_cgroup_per_zone *mz,
737 struct mem_cgroup_tree_per_zone *mctz)
738{
739 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700740 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700741 spin_unlock(&mctz->lock);
742}
743
Balbir Singhf64c3f52009-09-23 15:56:37 -0700744
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700745static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700746{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700747 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700748 struct mem_cgroup_per_zone *mz;
749 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700750 int nid = page_to_nid(page);
751 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700752 mctz = soft_limit_tree_from_page(page);
753
754 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700755 * Necessary to update all ancestors when hierarchy is used.
756 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700757 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700758 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
759 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
760 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700761 /*
762 * We have to update the tree if mz is on RB-tree or
763 * mem is over its softlimit.
764 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700765 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700766 spin_lock(&mctz->lock);
767 /* if on-tree, remove it */
768 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700769 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700770 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700771 * Insert again. mz->usage_in_excess will be updated.
772 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700773 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700774 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700775 spin_unlock(&mctz->lock);
776 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700777 }
778}
779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700780static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700781{
782 int node, zone;
783 struct mem_cgroup_per_zone *mz;
784 struct mem_cgroup_tree_per_zone *mctz;
785
Bob Liu3ed28fa2012-01-12 17:19:04 -0800786 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700787 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700789 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700790 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700791 }
792 }
793}
794
Balbir Singh4e416952009-09-23 15:56:39 -0700795static struct mem_cgroup_per_zone *
796__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
797{
798 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700799 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700800
801retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700802 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700803 rightmost = rb_last(&mctz->rb_root);
804 if (!rightmost)
805 goto done; /* Nothing to reclaim from */
806
807 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
808 /*
809 * Remove the node now but someone else can add it back,
810 * we will to add it back at the end of reclaim to its correct
811 * position in the tree.
812 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700813 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
814 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
815 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700816 goto retry;
817done:
818 return mz;
819}
820
821static struct mem_cgroup_per_zone *
822mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
823{
824 struct mem_cgroup_per_zone *mz;
825
826 spin_lock(&mctz->lock);
827 mz = __mem_cgroup_largest_soft_limit_node(mctz);
828 spin_unlock(&mctz->lock);
829 return mz;
830}
831
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700832/*
833 * Implementation Note: reading percpu statistics for memcg.
834 *
835 * Both of vmstat[] and percpu_counter has threshold and do periodic
836 * synchronization to implement "quick" read. There are trade-off between
837 * reading cost and precision of value. Then, we may have a chance to implement
838 * a periodic synchronizion of counter in memcg's counter.
839 *
840 * But this _read() function is used for user interface now. The user accounts
841 * memory usage by memory cgroup and he _always_ requires exact value because
842 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
843 * have to visit all online cpus and make sum. So, for now, unnecessary
844 * synchronization is not implemented. (just implemented for cpu hotplug)
845 *
846 * If there are kernel internal actions which can make use of some not-exact
847 * value, and reading all cpu value can be performance bottleneck in some
848 * common workload, threashold and synchonization as vmstat[] should be
849 * implemented.
850 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700851static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700852 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800853{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700854 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800855 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800856
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700857 get_online_cpus();
858 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700859 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700860#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700861 spin_lock(&memcg->pcp_counter_lock);
862 val += memcg->nocpu_base.count[idx];
863 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700864#endif
865 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800866 return val;
867}
868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700869static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700870 bool charge)
871{
872 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700873 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700874}
875
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700876static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700877 enum mem_cgroup_events_index idx)
878{
879 unsigned long val = 0;
880 int cpu;
881
882 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700884#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700885 spin_lock(&memcg->pcp_counter_lock);
886 val += memcg->nocpu_base.events[idx];
887 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700888#endif
889 return val;
890}
891
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700892static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700893 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800894{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800895 preempt_disable();
896
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700897 /*
898 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
899 * counted as CACHE even if it's on ANON LRU.
900 */
901 if (anon)
902 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700903 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800904 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700905 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700906 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700907
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800908 /* pagein of a big page is an event. So, ignore page size */
909 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700910 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800911 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700912 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800913 nr_pages = -nr_pages; /* for event */
914 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800915
Johannes Weiner13114712012-05-29 15:07:07 -0700916 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800917
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800918 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800919}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800920
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700921unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700922mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700923{
924 struct mem_cgroup_per_zone *mz;
925
926 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
927 return mz->lru_size[lru];
928}
929
930static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700931mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700932 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700933{
934 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700935 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700936 unsigned long ret = 0;
937
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700938 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700939
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700940 for_each_lru(lru) {
941 if (BIT(lru) & lru_mask)
942 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700943 }
944 return ret;
945}
946
947static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700948mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700949 int nid, unsigned int lru_mask)
950{
Ying Han889976d2011-05-26 16:25:33 -0700951 u64 total = 0;
952 int zid;
953
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700954 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700955 total += mem_cgroup_zone_nr_lru_pages(memcg,
956 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700957
Ying Han889976d2011-05-26 16:25:33 -0700958 return total;
959}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700960
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700961static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700962 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800963{
Ying Han889976d2011-05-26 16:25:33 -0700964 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800965 u64 total = 0;
966
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800967 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700968 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800969 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800970}
971
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800972static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
973 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800974{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700975 unsigned long val, next;
976
Johannes Weiner13114712012-05-29 15:07:07 -0700977 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700978 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700979 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800980 if ((long)next - (long)val < 0) {
981 switch (target) {
982 case MEM_CGROUP_TARGET_THRESH:
983 next = val + THRESHOLDS_EVENTS_TARGET;
984 break;
985 case MEM_CGROUP_TARGET_SOFTLIMIT:
986 next = val + SOFTLIMIT_EVENTS_TARGET;
987 break;
988 case MEM_CGROUP_TARGET_NUMAINFO:
989 next = val + NUMAINFO_EVENTS_TARGET;
990 break;
991 default:
992 break;
993 }
994 __this_cpu_write(memcg->stat->targets[target], next);
995 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700996 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800997 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800998}
999
1000/*
1001 * Check events in order.
1002 *
1003 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001004static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001005{
Steven Rostedt47994012011-11-02 13:38:33 -07001006 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001007 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001008 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1009 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001010 bool do_softlimit;
1011 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001012
1013 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1014 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001015#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001016 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1017 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001018#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001019 preempt_enable();
1020
1021 mem_cgroup_threshold(memcg);
1022 if (unlikely(do_softlimit))
1023 mem_cgroup_update_tree(memcg, page);
1024#if MAX_NUMNODES > 1
1025 if (unlikely(do_numainfo))
1026 atomic_inc(&memcg->numainfo_events);
1027#endif
1028 } else
1029 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001030}
1031
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001032struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001033{
Wanpeng Lib2145142012-07-31 16:46:01 -07001034 return mem_cgroup_from_css(
1035 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001036}
1037
Balbir Singhcf475ad2008-04-29 01:00:16 -07001038struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001039{
Balbir Singh31a78f22008-09-28 23:09:31 +01001040 /*
1041 * mm_update_next_owner() may clear mm->owner to NULL
1042 * if it races with swapoff, page migration, etc.
1043 * So this can be called with p == NULL.
1044 */
1045 if (unlikely(!p))
1046 return NULL;
1047
Wanpeng Lib2145142012-07-31 16:46:01 -07001048 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001049}
1050
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001051struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001052{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001053 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001054
1055 if (!mm)
1056 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001057 /*
1058 * Because we have no locks, mm->owner's may be being moved to other
1059 * cgroup. We use css_tryget() here even if this looks
1060 * pessimistic (rather than adding locks here).
1061 */
1062 rcu_read_lock();
1063 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001064 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1065 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001066 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001067 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001068 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001069 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001070}
1071
Johannes Weiner56600482012-01-12 17:17:59 -08001072/**
1073 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1074 * @root: hierarchy root
1075 * @prev: previously returned memcg, NULL on first invocation
1076 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1077 *
1078 * Returns references to children of the hierarchy below @root, or
1079 * @root itself, or %NULL after a full round-trip.
1080 *
1081 * Caller must pass the return value in @prev on subsequent
1082 * invocations for reference counting, or use mem_cgroup_iter_break()
1083 * to cancel a hierarchy walk before the round-trip is complete.
1084 *
1085 * Reclaimers can specify a zone and a priority level in @reclaim to
1086 * divide up the memcgs in the hierarchy among all concurrent
1087 * reclaimers operating on the same zone and priority.
1088 */
1089struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1090 struct mem_cgroup *prev,
1091 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001092{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001093 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001094 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001095
Johannes Weiner56600482012-01-12 17:17:59 -08001096 if (mem_cgroup_disabled())
1097 return NULL;
1098
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001099 if (!root)
1100 root = root_mem_cgroup;
1101
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001102 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001103 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001104
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001105 if (!root->use_hierarchy && root != root_mem_cgroup) {
1106 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001107 goto out_css_put;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001108 return root;
1109 }
1110
Michal Hocko542f85f2013-04-29 15:07:15 -07001111 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001112 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001113 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Michal Hocko542f85f2013-04-29 15:07:15 -07001114 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001115
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001116 if (reclaim) {
1117 int nid = zone_to_nid(reclaim->zone);
1118 int zid = zone_idx(reclaim->zone);
1119 struct mem_cgroup_per_zone *mz;
1120
1121 mz = mem_cgroup_zoneinfo(root, nid, zid);
1122 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001123 spin_lock(&iter->iter_lock);
1124 last_visited = iter->last_visited;
1125 if (prev && reclaim->generation != iter->generation) {
1126 if (last_visited) {
1127 css_put(&last_visited->css);
1128 iter->last_visited = NULL;
1129 }
1130 spin_unlock(&iter->iter_lock);
1131 goto out_unlock;
1132 }
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001133 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001134
Michal Hocko542f85f2013-04-29 15:07:15 -07001135 /*
1136 * Root is not visited by cgroup iterators so it needs an
1137 * explicit visit.
1138 */
1139 if (!last_visited) {
1140 css = &root->css;
1141 } else {
1142 struct cgroup *prev_cgroup, *next_cgroup;
1143
1144 prev_cgroup = (last_visited == root) ? NULL
1145 : last_visited->css.cgroup;
1146 next_cgroup = cgroup_next_descendant_pre(prev_cgroup,
1147 root->css.cgroup);
1148 if (next_cgroup)
1149 css = cgroup_subsys_state(next_cgroup,
1150 mem_cgroup_subsys_id);
1151 }
1152
1153 /*
1154 * Even if we found a group we have to make sure it is alive.
1155 * css && !memcg means that the groups should be skipped and
1156 * we should continue the tree walk.
1157 * last_visited css is safe to use because it is protected by
1158 * css_get and the tree walk is rcu safe.
1159 */
1160 if (css == &root->css || (css && css_tryget(css)))
1161 memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001162
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001163 if (reclaim) {
Michal Hocko542f85f2013-04-29 15:07:15 -07001164 struct mem_cgroup *curr = memcg;
1165
1166 if (last_visited)
1167 css_put(&last_visited->css);
1168
1169 if (css && !memcg)
1170 curr = mem_cgroup_from_css(css);
1171
1172 /* make sure that the cached memcg is not removed */
1173 if (curr)
1174 css_get(&curr->css);
1175 iter->last_visited = curr;
1176
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001177 if (!css)
1178 iter->generation++;
1179 else if (!prev && memcg)
1180 reclaim->generation = iter->generation;
Michal Hocko542f85f2013-04-29 15:07:15 -07001181 spin_unlock(&iter->iter_lock);
1182 } else if (css && !memcg) {
1183 last_visited = mem_cgroup_from_css(css);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001184 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001185
1186 if (prev && !css)
Michal Hocko542f85f2013-04-29 15:07:15 -07001187 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001188 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001189out_unlock:
1190 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001191out_css_put:
1192 if (prev && prev != root)
1193 css_put(&prev->css);
1194
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001195 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001196}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001197
Johannes Weiner56600482012-01-12 17:17:59 -08001198/**
1199 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1200 * @root: hierarchy root
1201 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1202 */
1203void mem_cgroup_iter_break(struct mem_cgroup *root,
1204 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001205{
1206 if (!root)
1207 root = root_mem_cgroup;
1208 if (prev && prev != root)
1209 css_put(&prev->css);
1210}
1211
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001212/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001213 * Iteration constructs for visiting all cgroups (under a tree). If
1214 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1215 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001216 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001217#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001218 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001219 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001220 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001221
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001222#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001223 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001224 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001225 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001226
David Rientjes68ae5642012-12-12 13:51:57 -08001227void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001228{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001229 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001230
Ying Han456f9982011-05-26 16:25:38 -07001231 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001232 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1233 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001234 goto out;
1235
1236 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001237 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001238 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1239 break;
1240 case PGMAJFAULT:
1241 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001242 break;
1243 default:
1244 BUG();
1245 }
1246out:
1247 rcu_read_unlock();
1248}
David Rientjes68ae5642012-12-12 13:51:57 -08001249EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001250
Johannes Weiner925b7672012-01-12 17:18:15 -08001251/**
1252 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1253 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001254 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001255 *
1256 * Returns the lru list vector holding pages for the given @zone and
1257 * @mem. This can be the global zone lruvec, if the memory controller
1258 * is disabled.
1259 */
1260struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1261 struct mem_cgroup *memcg)
1262{
1263 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001264 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001265
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001266 if (mem_cgroup_disabled()) {
1267 lruvec = &zone->lruvec;
1268 goto out;
1269 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001270
1271 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001272 lruvec = &mz->lruvec;
1273out:
1274 /*
1275 * Since a node can be onlined after the mem_cgroup was created,
1276 * we have to be prepared to initialize lruvec->zone here;
1277 * and if offlined then reonlined, we need to reinitialize it.
1278 */
1279 if (unlikely(lruvec->zone != zone))
1280 lruvec->zone = zone;
1281 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001282}
1283
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001284/*
1285 * Following LRU functions are allowed to be used without PCG_LOCK.
1286 * Operations are called by routine of global LRU independently from memcg.
1287 * What we have to take care of here is validness of pc->mem_cgroup.
1288 *
1289 * Changes to pc->mem_cgroup happens when
1290 * 1. charge
1291 * 2. moving account
1292 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1293 * It is added to LRU before charge.
1294 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1295 * When moving account, the page is not on LRU. It's isolated.
1296 */
1297
Johannes Weiner925b7672012-01-12 17:18:15 -08001298/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001299 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001300 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001301 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001302 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001303struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001304{
1305 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001306 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001307 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001308 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001309
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001310 if (mem_cgroup_disabled()) {
1311 lruvec = &zone->lruvec;
1312 goto out;
1313 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001314
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001315 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001316 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001317
1318 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001319 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001320 * an uncharged page off lru does nothing to secure
1321 * its former mem_cgroup from sudden removal.
1322 *
1323 * Our caller holds lru_lock, and PageCgroupUsed is updated
1324 * under page_cgroup lock: between them, they make all uses
1325 * of pc->mem_cgroup safe.
1326 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001327 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001328 pc->mem_cgroup = memcg = root_mem_cgroup;
1329
Johannes Weiner925b7672012-01-12 17:18:15 -08001330 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001331 lruvec = &mz->lruvec;
1332out:
1333 /*
1334 * Since a node can be onlined after the mem_cgroup was created,
1335 * we have to be prepared to initialize lruvec->zone here;
1336 * and if offlined then reonlined, we need to reinitialize it.
1337 */
1338 if (unlikely(lruvec->zone != zone))
1339 lruvec->zone = zone;
1340 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001341}
1342
1343/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001344 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1345 * @lruvec: mem_cgroup per zone lru vector
1346 * @lru: index of lru list the page is sitting on
1347 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001348 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001349 * This function must be called when a page is added to or removed from an
1350 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001351 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001352void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1353 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001354{
1355 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001356 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001357
1358 if (mem_cgroup_disabled())
1359 return;
1360
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001361 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1362 lru_size = mz->lru_size + lru;
1363 *lru_size += nr_pages;
1364 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001365}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001366
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001367/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001368 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001369 * hierarchy subtree
1370 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001371bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1372 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001373{
Johannes Weiner91c637342012-05-29 15:06:24 -07001374 if (root_memcg == memcg)
1375 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001376 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001377 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001378 return css_is_ancestor(&memcg->css, &root_memcg->css);
1379}
1380
1381static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1382 struct mem_cgroup *memcg)
1383{
1384 bool ret;
1385
Johannes Weiner91c637342012-05-29 15:06:24 -07001386 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001387 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001388 rcu_read_unlock();
1389 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001390}
1391
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001392int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001393{
1394 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001395 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001396 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001397
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001398 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001399 if (p) {
1400 curr = try_get_mem_cgroup_from_mm(p->mm);
1401 task_unlock(p);
1402 } else {
1403 /*
1404 * All threads may have already detached their mm's, but the oom
1405 * killer still needs to detect if they have already been oom
1406 * killed to prevent needlessly killing additional tasks.
1407 */
1408 task_lock(task);
1409 curr = mem_cgroup_from_task(task);
1410 if (curr)
1411 css_get(&curr->css);
1412 task_unlock(task);
1413 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001414 if (!curr)
1415 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001416 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001418 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001419 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1420 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001421 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001422 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001423 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001424 return ret;
1425}
1426
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001427int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001428{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001429 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001430 unsigned long inactive;
1431 unsigned long active;
1432 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001433
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001434 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1435 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001436
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001437 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1438 if (gb)
1439 inactive_ratio = int_sqrt(10 * gb);
1440 else
1441 inactive_ratio = 1;
1442
Johannes Weiner9b272972011-11-02 13:38:23 -07001443 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001444}
1445
Balbir Singh6d61ef42009-01-07 18:08:06 -08001446#define mem_cgroup_from_res_counter(counter, member) \
1447 container_of(counter, struct mem_cgroup, member)
1448
Johannes Weiner19942822011-02-01 15:52:43 -08001449/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001450 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001451 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001452 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001453 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001454 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001455 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001456static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001457{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001458 unsigned long long margin;
1459
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001460 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001461 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001462 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001463 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001464}
1465
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001466int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001467{
1468 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001469
1470 /* root ? */
1471 if (cgrp->parent == NULL)
1472 return vm_swappiness;
1473
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001474 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001475}
1476
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001477/*
1478 * memcg->moving_account is used for checking possibility that some thread is
1479 * calling move_account(). When a thread on CPU-A starts moving pages under
1480 * a memcg, other threads should check memcg->moving_account under
1481 * rcu_read_lock(), like this:
1482 *
1483 * CPU-A CPU-B
1484 * rcu_read_lock()
1485 * memcg->moving_account+1 if (memcg->mocing_account)
1486 * take heavy locks.
1487 * synchronize_rcu() update something.
1488 * rcu_read_unlock()
1489 * start move here.
1490 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001491
1492/* for quick checking without looking up memcg */
1493atomic_t memcg_moving __read_mostly;
1494
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001495static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001496{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001497 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001498 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001499 synchronize_rcu();
1500}
1501
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001502static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001503{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001504 /*
1505 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1506 * We check NULL in callee rather than caller.
1507 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001508 if (memcg) {
1509 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001510 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001511 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001512}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001513
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001514/*
1515 * 2 routines for checking "mem" is under move_account() or not.
1516 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001517 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1518 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001519 * pc->mem_cgroup may be overwritten.
1520 *
1521 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1522 * under hierarchy of moving cgroups. This is for
1523 * waiting at hith-memory prressure caused by "move".
1524 */
1525
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001526static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001527{
1528 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001529 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001530}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001531
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001532static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001533{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001534 struct mem_cgroup *from;
1535 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001536 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001537 /*
1538 * Unlike task_move routines, we access mc.to, mc.from not under
1539 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1540 */
1541 spin_lock(&mc.lock);
1542 from = mc.from;
1543 to = mc.to;
1544 if (!from)
1545 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001546
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001547 ret = mem_cgroup_same_or_subtree(memcg, from)
1548 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001549unlock:
1550 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001551 return ret;
1552}
1553
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001554static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001555{
1556 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001557 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001558 DEFINE_WAIT(wait);
1559 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1560 /* moving charge context might have finished. */
1561 if (mc.moving_task)
1562 schedule();
1563 finish_wait(&mc.waitq, &wait);
1564 return true;
1565 }
1566 }
1567 return false;
1568}
1569
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001570/*
1571 * Take this lock when
1572 * - a code tries to modify page's memcg while it's USED.
1573 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001574 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001575 */
1576static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1577 unsigned long *flags)
1578{
1579 spin_lock_irqsave(&memcg->move_lock, *flags);
1580}
1581
1582static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1583 unsigned long *flags)
1584{
1585 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1586}
1587
Sha Zhengju58cf1882013-02-22 16:32:05 -08001588#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001589/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001590 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001591 * @memcg: The memory cgroup that went over limit
1592 * @p: Task that is going to be killed
1593 *
1594 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1595 * enabled
1596 */
1597void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1598{
1599 struct cgroup *task_cgrp;
1600 struct cgroup *mem_cgrp;
1601 /*
1602 * Need a buffer in BSS, can't rely on allocations. The code relies
1603 * on the assumption that OOM is serialized for memory controller.
1604 * If this assumption is broken, revisit this code.
1605 */
1606 static char memcg_name[PATH_MAX];
1607 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001608 struct mem_cgroup *iter;
1609 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001610
Sha Zhengju58cf1882013-02-22 16:32:05 -08001611 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001612 return;
1613
Balbir Singhe2224322009-04-02 16:57:39 -07001614 rcu_read_lock();
1615
1616 mem_cgrp = memcg->css.cgroup;
1617 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1618
1619 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1620 if (ret < 0) {
1621 /*
1622 * Unfortunately, we are unable to convert to a useful name
1623 * But we'll still print out the usage information
1624 */
1625 rcu_read_unlock();
1626 goto done;
1627 }
1628 rcu_read_unlock();
1629
Andrew Mortond0451972013-02-22 16:32:06 -08001630 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001631
1632 rcu_read_lock();
1633 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1634 if (ret < 0) {
1635 rcu_read_unlock();
1636 goto done;
1637 }
1638 rcu_read_unlock();
1639
1640 /*
1641 * Continues from above, so we don't need an KERN_ level
1642 */
Andrew Mortond0451972013-02-22 16:32:06 -08001643 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001644done:
1645
Andrew Mortond0451972013-02-22 16:32:06 -08001646 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001647 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1648 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1649 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001650 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001651 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1652 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1653 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001654 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001655 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1656 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1657 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001658
1659 for_each_mem_cgroup_tree(iter, memcg) {
1660 pr_info("Memory cgroup stats");
1661
1662 rcu_read_lock();
1663 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1664 if (!ret)
1665 pr_cont(" for %s", memcg_name);
1666 rcu_read_unlock();
1667 pr_cont(":");
1668
1669 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1670 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1671 continue;
1672 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1673 K(mem_cgroup_read_stat(iter, i)));
1674 }
1675
1676 for (i = 0; i < NR_LRU_LISTS; i++)
1677 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1678 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1679
1680 pr_cont("\n");
1681 }
Balbir Singhe2224322009-04-02 16:57:39 -07001682}
1683
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001684/*
1685 * This function returns the number of memcg under hierarchy tree. Returns
1686 * 1(self count) if no children.
1687 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001688static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001689{
1690 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001691 struct mem_cgroup *iter;
1692
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001693 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001694 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001695 return num;
1696}
1697
Balbir Singh6d61ef42009-01-07 18:08:06 -08001698/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001699 * Return the memory (and swap, if configured) limit for a memcg.
1700 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001701static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001702{
1703 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001704
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001705 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001706
David Rientjesa63d83f2010-08-09 17:19:46 -07001707 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001708 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001709 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001710 if (mem_cgroup_swappiness(memcg)) {
1711 u64 memsw;
1712
1713 limit += total_swap_pages << PAGE_SHIFT;
1714 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1715
1716 /*
1717 * If memsw is finite and limits the amount of swap space
1718 * available to this memcg, return that limit.
1719 */
1720 limit = min(limit, memsw);
1721 }
1722
1723 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001724}
1725
David Rientjes19965462012-12-11 16:00:26 -08001726static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1727 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001728{
1729 struct mem_cgroup *iter;
1730 unsigned long chosen_points = 0;
1731 unsigned long totalpages;
1732 unsigned int points = 0;
1733 struct task_struct *chosen = NULL;
1734
David Rientjes876aafb2012-07-31 16:43:48 -07001735 /*
1736 * If current has a pending SIGKILL, then automatically select it. The
1737 * goal is to allow it to allocate so that it may quickly exit and free
1738 * its memory.
1739 */
1740 if (fatal_signal_pending(current)) {
1741 set_thread_flag(TIF_MEMDIE);
1742 return;
1743 }
1744
1745 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001746 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1747 for_each_mem_cgroup_tree(iter, memcg) {
1748 struct cgroup *cgroup = iter->css.cgroup;
1749 struct cgroup_iter it;
1750 struct task_struct *task;
1751
1752 cgroup_iter_start(cgroup, &it);
1753 while ((task = cgroup_iter_next(cgroup, &it))) {
1754 switch (oom_scan_process_thread(task, totalpages, NULL,
1755 false)) {
1756 case OOM_SCAN_SELECT:
1757 if (chosen)
1758 put_task_struct(chosen);
1759 chosen = task;
1760 chosen_points = ULONG_MAX;
1761 get_task_struct(chosen);
1762 /* fall through */
1763 case OOM_SCAN_CONTINUE:
1764 continue;
1765 case OOM_SCAN_ABORT:
1766 cgroup_iter_end(cgroup, &it);
1767 mem_cgroup_iter_break(memcg, iter);
1768 if (chosen)
1769 put_task_struct(chosen);
1770 return;
1771 case OOM_SCAN_OK:
1772 break;
1773 };
1774 points = oom_badness(task, memcg, NULL, totalpages);
1775 if (points > chosen_points) {
1776 if (chosen)
1777 put_task_struct(chosen);
1778 chosen = task;
1779 chosen_points = points;
1780 get_task_struct(chosen);
1781 }
1782 }
1783 cgroup_iter_end(cgroup, &it);
1784 }
1785
1786 if (!chosen)
1787 return;
1788 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001789 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1790 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001791}
1792
Johannes Weiner56600482012-01-12 17:17:59 -08001793static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1794 gfp_t gfp_mask,
1795 unsigned long flags)
1796{
1797 unsigned long total = 0;
1798 bool noswap = false;
1799 int loop;
1800
1801 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1802 noswap = true;
1803 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1804 noswap = true;
1805
1806 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1807 if (loop)
1808 drain_all_stock_async(memcg);
1809 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1810 /*
1811 * Allow limit shrinkers, which are triggered directly
1812 * by userspace, to catch signals and stop reclaim
1813 * after minimal progress, regardless of the margin.
1814 */
1815 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1816 break;
1817 if (mem_cgroup_margin(memcg))
1818 break;
1819 /*
1820 * If nothing was reclaimed after two attempts, there
1821 * may be no reclaimable pages in this hierarchy.
1822 */
1823 if (loop && !total)
1824 break;
1825 }
1826 return total;
1827}
1828
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001829/**
1830 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001831 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001832 * @nid: the node ID to be checked.
1833 * @noswap : specify true here if the user wants flle only information.
1834 *
1835 * This function returns whether the specified memcg contains any
1836 * reclaimable pages on a node. Returns true if there are any reclaimable
1837 * pages in the node.
1838 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001839static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001840 int nid, bool noswap)
1841{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001842 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001843 return true;
1844 if (noswap || !total_swap_pages)
1845 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001846 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001847 return true;
1848 return false;
1849
1850}
Ying Han889976d2011-05-26 16:25:33 -07001851#if MAX_NUMNODES > 1
1852
1853/*
1854 * Always updating the nodemask is not very good - even if we have an empty
1855 * list or the wrong list here, we can start from some node and traverse all
1856 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1857 *
1858 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001860{
1861 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001862 /*
1863 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1864 * pagein/pageout changes since the last update.
1865 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001866 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001867 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001868 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001869 return;
1870
Ying Han889976d2011-05-26 16:25:33 -07001871 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001872 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001873
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001874 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001875
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001876 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1877 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001878 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001879
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001880 atomic_set(&memcg->numainfo_events, 0);
1881 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001882}
1883
1884/*
1885 * Selecting a node where we start reclaim from. Because what we need is just
1886 * reducing usage counter, start from anywhere is O,K. Considering
1887 * memory reclaim from current node, there are pros. and cons.
1888 *
1889 * Freeing memory from current node means freeing memory from a node which
1890 * we'll use or we've used. So, it may make LRU bad. And if several threads
1891 * hit limits, it will see a contention on a node. But freeing from remote
1892 * node means more costs for memory reclaim because of memory latency.
1893 *
1894 * Now, we use round-robin. Better algorithm is welcomed.
1895 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001897{
1898 int node;
1899
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001900 mem_cgroup_may_update_nodemask(memcg);
1901 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001902
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001903 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001904 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001905 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001906 /*
1907 * We call this when we hit limit, not when pages are added to LRU.
1908 * No LRU may hold pages because all pages are UNEVICTABLE or
1909 * memcg is too small and all pages are not on LRU. In that case,
1910 * we use curret node.
1911 */
1912 if (unlikely(node == MAX_NUMNODES))
1913 node = numa_node_id();
1914
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001915 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001916 return node;
1917}
1918
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001919/*
1920 * Check all nodes whether it contains reclaimable pages or not.
1921 * For quick scan, we make use of scan_nodes. This will allow us to skip
1922 * unused nodes. But scan_nodes is lazily updated and may not cotain
1923 * enough new information. We need to do double check.
1924 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001925static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001926{
1927 int nid;
1928
1929 /*
1930 * quick check...making use of scan_node.
1931 * We can skip unused nodes.
1932 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001933 if (!nodes_empty(memcg->scan_nodes)) {
1934 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001935 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001936 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001937
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001938 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001939 return true;
1940 }
1941 }
1942 /*
1943 * Check rest of nodes.
1944 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001945 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001946 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001947 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001948 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001949 return true;
1950 }
1951 return false;
1952}
1953
Ying Han889976d2011-05-26 16:25:33 -07001954#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001955int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001956{
1957 return 0;
1958}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001959
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001960static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001961{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001962 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001963}
Ying Han889976d2011-05-26 16:25:33 -07001964#endif
1965
Johannes Weiner56600482012-01-12 17:17:59 -08001966static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1967 struct zone *zone,
1968 gfp_t gfp_mask,
1969 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001970{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001971 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001972 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001973 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001974 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001975 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001976 struct mem_cgroup_reclaim_cookie reclaim = {
1977 .zone = zone,
1978 .priority = 0,
1979 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001980
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001982
Balbir Singh4e416952009-09-23 15:56:39 -07001983 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001984 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001985 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001986 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001987 if (loop >= 2) {
1988 /*
1989 * If we have not been able to reclaim
1990 * anything, it might because there are
1991 * no reclaimable pages under this hierarchy
1992 */
Johannes Weiner56600482012-01-12 17:17:59 -08001993 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001994 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001995 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001996 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001997 * excess >> 2 is not to excessive so as to
1998 * reclaim too much, nor too less that we keep
1999 * coming back to reclaim from this cgroup
2000 */
2001 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002002 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07002003 break;
Balbir Singh4e416952009-09-23 15:56:39 -07002004 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002005 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07002006 }
Johannes Weiner56600482012-01-12 17:17:59 -08002007 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08002008 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08002009 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2010 zone, &nr_scanned);
2011 *total_scanned += nr_scanned;
2012 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002013 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002014 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002015 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002016 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002017}
2018
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002019/*
2020 * Check OOM-Killer is already running under our hierarchy.
2021 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07002022 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002023 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002024static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002025{
Michal Hocko79dfdac2011-07-26 16:08:23 -07002026 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002027
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002028 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07002029 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002030 /*
2031 * this subtree of our hierarchy is already locked
2032 * so we cannot give a lock.
2033 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07002034 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002035 mem_cgroup_iter_break(memcg, iter);
2036 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07002037 } else
2038 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002039 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002040
Michal Hocko79dfdac2011-07-26 16:08:23 -07002041 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07002042 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002043
2044 /*
2045 * OK, we failed to lock the whole subtree so we have to clean up
2046 * what we set up to the failing subtree
2047 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002048 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002049 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002050 mem_cgroup_iter_break(memcg, iter);
2051 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002052 }
2053 iter->oom_lock = false;
2054 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002055 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002056}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002057
Michal Hocko79dfdac2011-07-26 16:08:23 -07002058/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002059 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002060 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002061static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002062{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002063 struct mem_cgroup *iter;
2064
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002065 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002066 iter->oom_lock = false;
2067 return 0;
2068}
2069
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002070static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002071{
2072 struct mem_cgroup *iter;
2073
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002074 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002075 atomic_inc(&iter->under_oom);
2076}
2077
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002079{
2080 struct mem_cgroup *iter;
2081
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002082 /*
2083 * When a new child is created while the hierarchy is under oom,
2084 * mem_cgroup_oom_lock() may not be called. We have to use
2085 * atomic_add_unless() here.
2086 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002087 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002088 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002089}
2090
Michal Hocko1af8efe2011-07-26 16:08:24 -07002091static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002092static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2093
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002094struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002095 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002096 wait_queue_t wait;
2097};
2098
2099static int memcg_oom_wake_function(wait_queue_t *wait,
2100 unsigned mode, int sync, void *arg)
2101{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002102 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2103 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002104 struct oom_wait_info *oom_wait_info;
2105
2106 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002107 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002108
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002109 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002110 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002111 * Then we can use css_is_ancestor without taking care of RCU.
2112 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002113 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2114 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002115 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002116 return autoremove_wake_function(wait, mode, sync, arg);
2117}
2118
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002119static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002120{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002121 /* for filtering, pass "memcg" as argument. */
2122 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002123}
2124
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002125static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002126{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002127 if (memcg && atomic_read(&memcg->under_oom))
2128 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002129}
2130
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002131/*
2132 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2133 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002134static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2135 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002136{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002137 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002138 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002139
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002140 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002141 owait.wait.flags = 0;
2142 owait.wait.func = memcg_oom_wake_function;
2143 owait.wait.private = current;
2144 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002145 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002147
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002148 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002149 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002151 /*
2152 * Even if signal_pending(), we can't quit charge() loop without
2153 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2154 * under OOM is always welcomed, use TASK_KILLABLE here.
2155 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002156 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002157 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002158 need_to_kill = false;
2159 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002160 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002161 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002162
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002163 if (need_to_kill) {
2164 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002165 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002166 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002167 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002168 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002169 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002170 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002171 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002172 mem_cgroup_oom_unlock(memcg);
2173 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002174 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002175
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002176 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002177
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002178 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2179 return false;
2180 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002181 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002182 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002183}
2184
Balbir Singhd69b0422009-06-17 16:26:34 -07002185/*
2186 * Currently used to update mapped file statistics, but the routine can be
2187 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002188 *
2189 * Notes: Race condition
2190 *
2191 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2192 * it tends to be costly. But considering some conditions, we doesn't need
2193 * to do so _always_.
2194 *
2195 * Considering "charge", lock_page_cgroup() is not required because all
2196 * file-stat operations happen after a page is attached to radix-tree. There
2197 * are no race with "charge".
2198 *
2199 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2200 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2201 * if there are race with "uncharge". Statistics itself is properly handled
2202 * by flags.
2203 *
2204 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002205 * small, we check mm->moving_account and detect there are possibility of race
2206 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002207 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002208
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002209void __mem_cgroup_begin_update_page_stat(struct page *page,
2210 bool *locked, unsigned long *flags)
2211{
2212 struct mem_cgroup *memcg;
2213 struct page_cgroup *pc;
2214
2215 pc = lookup_page_cgroup(page);
2216again:
2217 memcg = pc->mem_cgroup;
2218 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2219 return;
2220 /*
2221 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002222 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002223 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002224 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002225 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002226 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002227 return;
2228
2229 move_lock_mem_cgroup(memcg, flags);
2230 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2231 move_unlock_mem_cgroup(memcg, flags);
2232 goto again;
2233 }
2234 *locked = true;
2235}
2236
2237void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2238{
2239 struct page_cgroup *pc = lookup_page_cgroup(page);
2240
2241 /*
2242 * It's guaranteed that pc->mem_cgroup never changes while
2243 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002244 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002245 */
2246 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2247}
2248
Greg Thelen2a7106f2011-01-13 15:47:37 -08002249void mem_cgroup_update_page_stat(struct page *page,
2250 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002251{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002252 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002253 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002254 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002255
Johannes Weinercfa44942012-01-12 17:18:38 -08002256 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002257 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002258
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002259 memcg = pc->mem_cgroup;
2260 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002261 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002262
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002263 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002264 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002265 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002266 break;
2267 default:
2268 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002269 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002270
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002271 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002272}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002273
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002274/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002275 * size of first charge trial. "32" comes from vmscan.c's magic value.
2276 * TODO: maybe necessary to use big numbers in big irons.
2277 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002278#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279struct memcg_stock_pcp {
2280 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002281 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002282 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002283 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002284#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002285};
2286static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002287static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002288
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002289/**
2290 * consume_stock: Try to consume stocked charge on this cpu.
2291 * @memcg: memcg to consume from.
2292 * @nr_pages: how many pages to charge.
2293 *
2294 * The charges will only happen if @memcg matches the current cpu's memcg
2295 * stock, and at least @nr_pages are available in that stock. Failure to
2296 * service an allocation will refill the stock.
2297 *
2298 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002299 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002300static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002301{
2302 struct memcg_stock_pcp *stock;
2303 bool ret = true;
2304
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002305 if (nr_pages > CHARGE_BATCH)
2306 return false;
2307
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002308 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002309 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2310 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002311 else /* need to call res_counter_charge */
2312 ret = false;
2313 put_cpu_var(memcg_stock);
2314 return ret;
2315}
2316
2317/*
2318 * Returns stocks cached in percpu to res_counter and reset cached information.
2319 */
2320static void drain_stock(struct memcg_stock_pcp *stock)
2321{
2322 struct mem_cgroup *old = stock->cached;
2323
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002324 if (stock->nr_pages) {
2325 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2326
2327 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002328 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002329 res_counter_uncharge(&old->memsw, bytes);
2330 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002331 }
2332 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002333}
2334
2335/*
2336 * This must be called under preempt disabled or must be called by
2337 * a thread which is pinned to local cpu.
2338 */
2339static void drain_local_stock(struct work_struct *dummy)
2340{
2341 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2342 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002343 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002344}
2345
Michal Hockoe4777492013-02-22 16:35:40 -08002346static void __init memcg_stock_init(void)
2347{
2348 int cpu;
2349
2350 for_each_possible_cpu(cpu) {
2351 struct memcg_stock_pcp *stock =
2352 &per_cpu(memcg_stock, cpu);
2353 INIT_WORK(&stock->work, drain_local_stock);
2354 }
2355}
2356
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002357/*
2358 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002359 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002360 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002361static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002362{
2363 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002365 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002366 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002367 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002368 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002369 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002370 put_cpu_var(memcg_stock);
2371}
2372
2373/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002375 * of the hierarchy under it. sync flag says whether we should block
2376 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002377 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002378static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002379{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002380 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002381
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002382 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002383 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002384 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002385 for_each_online_cpu(cpu) {
2386 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002388
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002389 memcg = stock->cached;
2390 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002391 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002392 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002393 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002394 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2395 if (cpu == curcpu)
2396 drain_local_stock(&stock->work);
2397 else
2398 schedule_work_on(cpu, &stock->work);
2399 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002400 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002401 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002402
2403 if (!sync)
2404 goto out;
2405
2406 for_each_online_cpu(cpu) {
2407 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002408 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002409 flush_work(&stock->work);
2410 }
2411out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002412 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002413}
2414
2415/*
2416 * Tries to drain stocked charges in other cpus. This function is asynchronous
2417 * and just put a work per cpu for draining localy on each cpu. Caller can
2418 * expects some charges will be back to res_counter later but cannot wait for
2419 * it.
2420 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002421static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002422{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002423 /*
2424 * If someone calls draining, avoid adding more kworker runs.
2425 */
2426 if (!mutex_trylock(&percpu_charge_mutex))
2427 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002428 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002429 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002430}
2431
2432/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002433static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002434{
2435 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002436 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002437 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002438 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002439}
2440
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002441/*
2442 * This function drains percpu counter value from DEAD cpu and
2443 * move it to local cpu. Note that this function can be preempted.
2444 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002445static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002446{
2447 int i;
2448
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002449 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002450 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002451 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002453 per_cpu(memcg->stat->count[i], cpu) = 0;
2454 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002455 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002456 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002457 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002458
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002459 per_cpu(memcg->stat->events[i], cpu) = 0;
2460 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002461 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002462 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002463}
2464
2465static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002466 unsigned long action,
2467 void *hcpu)
2468{
2469 int cpu = (unsigned long)hcpu;
2470 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002471 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002472
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002473 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002474 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002475
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002476 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002477 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002478
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002479 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002480 mem_cgroup_drain_pcp_counter(iter, cpu);
2481
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002482 stock = &per_cpu(memcg_stock, cpu);
2483 drain_stock(stock);
2484 return NOTIFY_OK;
2485}
2486
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002487
2488/* See __mem_cgroup_try_charge() for details */
2489enum {
2490 CHARGE_OK, /* success */
2491 CHARGE_RETRY, /* need to retry but retry is not bad */
2492 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2493 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2494 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2495};
2496
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002497static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002498 unsigned int nr_pages, unsigned int min_pages,
2499 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002500{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002501 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002502 struct mem_cgroup *mem_over_limit;
2503 struct res_counter *fail_res;
2504 unsigned long flags = 0;
2505 int ret;
2506
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002507 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002508
2509 if (likely(!ret)) {
2510 if (!do_swap_account)
2511 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002512 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002513 if (likely(!ret))
2514 return CHARGE_OK;
2515
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002516 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002517 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2518 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2519 } else
2520 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002521 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002522 * Never reclaim on behalf of optional batching, retry with a
2523 * single page instead.
2524 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002525 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002526 return CHARGE_RETRY;
2527
2528 if (!(gfp_mask & __GFP_WAIT))
2529 return CHARGE_WOULDBLOCK;
2530
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002531 if (gfp_mask & __GFP_NORETRY)
2532 return CHARGE_NOMEM;
2533
Johannes Weiner56600482012-01-12 17:17:59 -08002534 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002535 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002536 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002537 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002538 * Even though the limit is exceeded at this point, reclaim
2539 * may have been able to free some pages. Retry the charge
2540 * before killing the task.
2541 *
2542 * Only for regular pages, though: huge pages are rather
2543 * unlikely to succeed so close to the limit, and we fall back
2544 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002545 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002546 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002547 return CHARGE_RETRY;
2548
2549 /*
2550 * At task move, charge accounts can be doubly counted. So, it's
2551 * better to wait until the end of task_move if something is going on.
2552 */
2553 if (mem_cgroup_wait_acct_move(mem_over_limit))
2554 return CHARGE_RETRY;
2555
2556 /* If we don't need to call oom-killer at el, return immediately */
2557 if (!oom_check)
2558 return CHARGE_NOMEM;
2559 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002560 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002561 return CHARGE_OOM_DIE;
2562
2563 return CHARGE_RETRY;
2564}
2565
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002566/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002567 * __mem_cgroup_try_charge() does
2568 * 1. detect memcg to be charged against from passed *mm and *ptr,
2569 * 2. update res_counter
2570 * 3. call memory reclaim if necessary.
2571 *
2572 * In some special case, if the task is fatal, fatal_signal_pending() or
2573 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2574 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2575 * as possible without any hazards. 2: all pages should have a valid
2576 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2577 * pointer, that is treated as a charge to root_mem_cgroup.
2578 *
2579 * So __mem_cgroup_try_charge() will return
2580 * 0 ... on success, filling *ptr with a valid memcg pointer.
2581 * -ENOMEM ... charge failure because of resource limits.
2582 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2583 *
2584 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2585 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002586 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002587static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002588 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002589 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002590 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002591 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002592{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002593 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002594 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002595 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002596 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002597
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002598 /*
2599 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2600 * in system level. So, allow to go ahead dying process in addition to
2601 * MEMDIE process.
2602 */
2603 if (unlikely(test_thread_flag(TIF_MEMDIE)
2604 || fatal_signal_pending(current)))
2605 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002606
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002607 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002608 * We always charge the cgroup the mm_struct belongs to.
2609 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002610 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002611 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002612 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002613 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002614 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002615again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002616 if (*ptr) { /* css should be a valid one */
2617 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002618 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002619 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002620 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002621 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002622 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002623 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002624 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002625
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002626 rcu_read_lock();
2627 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002628 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002629 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002630 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002631 * race with swapoff. Then, we have small risk of mis-accouning.
2632 * But such kind of mis-account by race always happens because
2633 * we don't have cgroup_mutex(). It's overkill and we allo that
2634 * small race, here.
2635 * (*) swapoff at el will charge against mm-struct not against
2636 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002637 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002638 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002639 if (!memcg)
2640 memcg = root_mem_cgroup;
2641 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002642 rcu_read_unlock();
2643 goto done;
2644 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002645 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002646 /*
2647 * It seems dagerous to access memcg without css_get().
2648 * But considering how consume_stok works, it's not
2649 * necessary. If consume_stock success, some charges
2650 * from this memcg are cached on this cpu. So, we
2651 * don't need to call css_get()/css_tryget() before
2652 * calling consume_stock().
2653 */
2654 rcu_read_unlock();
2655 goto done;
2656 }
2657 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002658 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002659 rcu_read_unlock();
2660 goto again;
2661 }
2662 rcu_read_unlock();
2663 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002664
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002665 do {
2666 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002667
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002668 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002669 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002670 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002671 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002672 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002673
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002674 oom_check = false;
2675 if (oom && !nr_oom_retries) {
2676 oom_check = true;
2677 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2678 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002679
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002680 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2681 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002682 switch (ret) {
2683 case CHARGE_OK:
2684 break;
2685 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002686 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002687 css_put(&memcg->css);
2688 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002689 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002690 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002691 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002692 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002693 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002694 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002695 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002696 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002697 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002698 /* If oom, we never return -ENOMEM */
2699 nr_oom_retries--;
2700 break;
2701 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002702 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002703 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002704 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002705 } while (ret != CHARGE_OK);
2706
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002707 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002708 refill_stock(memcg, batch - nr_pages);
2709 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002710done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002711 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002712 return 0;
2713nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002714 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002715 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002716bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002717 *ptr = root_mem_cgroup;
2718 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002719}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002720
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002721/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002722 * Somemtimes we have to undo a charge we got by try_charge().
2723 * This function is for that and do uncharge, put css's refcnt.
2724 * gotten by try_charge().
2725 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002726static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002727 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002728{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002729 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002730 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002731
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002732 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002733 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002734 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002735 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002736}
2737
2738/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002739 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2740 * This is useful when moving usage to parent cgroup.
2741 */
2742static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2743 unsigned int nr_pages)
2744{
2745 unsigned long bytes = nr_pages * PAGE_SIZE;
2746
2747 if (mem_cgroup_is_root(memcg))
2748 return;
2749
2750 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2751 if (do_swap_account)
2752 res_counter_uncharge_until(&memcg->memsw,
2753 memcg->memsw.parent, bytes);
2754}
2755
2756/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002757 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002758 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2759 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2760 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002761 */
2762static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2763{
2764 struct cgroup_subsys_state *css;
2765
2766 /* ID 0 is unused ID */
2767 if (!id)
2768 return NULL;
2769 css = css_lookup(&mem_cgroup_subsys, id);
2770 if (!css)
2771 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002772 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002773}
2774
Wu Fengguange42d9d52009-12-16 12:19:59 +01002775struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002776{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002777 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002778 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002779 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002780 swp_entry_t ent;
2781
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002782 VM_BUG_ON(!PageLocked(page));
2783
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002784 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002785 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002786 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002787 memcg = pc->mem_cgroup;
2788 if (memcg && !css_tryget(&memcg->css))
2789 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002790 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002791 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002792 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002793 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002794 memcg = mem_cgroup_lookup(id);
2795 if (memcg && !css_tryget(&memcg->css))
2796 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002797 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002798 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002799 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002800 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002801}
2802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002803static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002804 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002805 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002806 enum charge_type ctype,
2807 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002808{
Johannes Weinerce587e62012-04-24 20:22:33 +02002809 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002810 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002811 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002812 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002813 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002814
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002815 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002816 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002817 /*
2818 * we don't need page_cgroup_lock about tail pages, becase they are not
2819 * accessed by any other context at this point.
2820 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002821
2822 /*
2823 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2824 * may already be on some other mem_cgroup's LRU. Take care of it.
2825 */
2826 if (lrucare) {
2827 zone = page_zone(page);
2828 spin_lock_irq(&zone->lru_lock);
2829 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002830 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002831 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002832 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002833 was_on_lru = true;
2834 }
2835 }
2836
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002837 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002838 /*
2839 * We access a page_cgroup asynchronously without lock_page_cgroup().
2840 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2841 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2842 * before USED bit, we need memory barrier here.
2843 * See mem_cgroup_add_lru_list(), etc.
2844 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002845 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002846 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002847
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002848 if (lrucare) {
2849 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002850 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002851 VM_BUG_ON(PageLRU(page));
2852 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002853 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002854 }
2855 spin_unlock_irq(&zone->lru_lock);
2856 }
2857
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002858 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002859 anon = true;
2860 else
2861 anon = false;
2862
2863 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002864 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002865
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002866 /*
2867 * "charge_statistics" updated event counter. Then, check it.
2868 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2869 * if they exceeds softlimit.
2870 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002871 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002872}
2873
Glauber Costa7cf27982012-12-18 14:22:55 -08002874static DEFINE_MUTEX(set_limit_mutex);
2875
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002876#ifdef CONFIG_MEMCG_KMEM
2877static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2878{
2879 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2880 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2881}
2882
Glauber Costa1f458cb2012-12-18 14:22:50 -08002883/*
2884 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2885 * in the memcg_cache_params struct.
2886 */
2887static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2888{
2889 struct kmem_cache *cachep;
2890
2891 VM_BUG_ON(p->is_root_cache);
2892 cachep = p->root_cache;
2893 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2894}
2895
Glauber Costa749c5412012-12-18 14:23:01 -08002896#ifdef CONFIG_SLABINFO
2897static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2898 struct seq_file *m)
2899{
2900 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2901 struct memcg_cache_params *params;
2902
2903 if (!memcg_can_account_kmem(memcg))
2904 return -EIO;
2905
2906 print_slabinfo_header(m);
2907
2908 mutex_lock(&memcg->slab_caches_mutex);
2909 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2910 cache_show(memcg_params_to_cache(params), m);
2911 mutex_unlock(&memcg->slab_caches_mutex);
2912
2913 return 0;
2914}
2915#endif
2916
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002917static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2918{
2919 struct res_counter *fail_res;
2920 struct mem_cgroup *_memcg;
2921 int ret = 0;
2922 bool may_oom;
2923
2924 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2925 if (ret)
2926 return ret;
2927
2928 /*
2929 * Conditions under which we can wait for the oom_killer. Those are
2930 * the same conditions tested by the core page allocator
2931 */
2932 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2933
2934 _memcg = memcg;
2935 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2936 &_memcg, may_oom);
2937
2938 if (ret == -EINTR) {
2939 /*
2940 * __mem_cgroup_try_charge() chosed to bypass to root due to
2941 * OOM kill or fatal signal. Since our only options are to
2942 * either fail the allocation or charge it to this cgroup, do
2943 * it as a temporary condition. But we can't fail. From a
2944 * kmem/slab perspective, the cache has already been selected,
2945 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2946 * our minds.
2947 *
2948 * This condition will only trigger if the task entered
2949 * memcg_charge_kmem in a sane state, but was OOM-killed during
2950 * __mem_cgroup_try_charge() above. Tasks that were already
2951 * dying when the allocation triggers should have been already
2952 * directed to the root cgroup in memcontrol.h
2953 */
2954 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2955 if (do_swap_account)
2956 res_counter_charge_nofail(&memcg->memsw, size,
2957 &fail_res);
2958 ret = 0;
2959 } else if (ret)
2960 res_counter_uncharge(&memcg->kmem, size);
2961
2962 return ret;
2963}
2964
2965static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2966{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002967 res_counter_uncharge(&memcg->res, size);
2968 if (do_swap_account)
2969 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002970
2971 /* Not down to 0 */
2972 if (res_counter_uncharge(&memcg->kmem, size))
2973 return;
2974
2975 if (memcg_kmem_test_and_clear_dead(memcg))
2976 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002977}
2978
Glauber Costa2633d7a2012-12-18 14:22:34 -08002979void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2980{
2981 if (!memcg)
2982 return;
2983
2984 mutex_lock(&memcg->slab_caches_mutex);
2985 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2986 mutex_unlock(&memcg->slab_caches_mutex);
2987}
2988
2989/*
2990 * helper for acessing a memcg's index. It will be used as an index in the
2991 * child cache array in kmem_cache, and also to derive its name. This function
2992 * will return -1 when this is not a kmem-limited memcg.
2993 */
2994int memcg_cache_id(struct mem_cgroup *memcg)
2995{
2996 return memcg ? memcg->kmemcg_id : -1;
2997}
2998
Glauber Costa55007d82012-12-18 14:22:38 -08002999/*
3000 * This ends up being protected by the set_limit mutex, during normal
3001 * operation, because that is its main call site.
3002 *
3003 * But when we create a new cache, we can call this as well if its parent
3004 * is kmem-limited. That will have to hold set_limit_mutex as well.
3005 */
3006int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3007{
3008 int num, ret;
3009
3010 num = ida_simple_get(&kmem_limited_groups,
3011 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3012 if (num < 0)
3013 return num;
3014 /*
3015 * After this point, kmem_accounted (that we test atomically in
3016 * the beginning of this conditional), is no longer 0. This
3017 * guarantees only one process will set the following boolean
3018 * to true. We don't need test_and_set because we're protected
3019 * by the set_limit_mutex anyway.
3020 */
3021 memcg_kmem_set_activated(memcg);
3022
3023 ret = memcg_update_all_caches(num+1);
3024 if (ret) {
3025 ida_simple_remove(&kmem_limited_groups, num);
3026 memcg_kmem_clear_activated(memcg);
3027 return ret;
3028 }
3029
3030 memcg->kmemcg_id = num;
3031 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3032 mutex_init(&memcg->slab_caches_mutex);
3033 return 0;
3034}
3035
3036static size_t memcg_caches_array_size(int num_groups)
3037{
3038 ssize_t size;
3039 if (num_groups <= 0)
3040 return 0;
3041
3042 size = 2 * num_groups;
3043 if (size < MEMCG_CACHES_MIN_SIZE)
3044 size = MEMCG_CACHES_MIN_SIZE;
3045 else if (size > MEMCG_CACHES_MAX_SIZE)
3046 size = MEMCG_CACHES_MAX_SIZE;
3047
3048 return size;
3049}
3050
3051/*
3052 * We should update the current array size iff all caches updates succeed. This
3053 * can only be done from the slab side. The slab mutex needs to be held when
3054 * calling this.
3055 */
3056void memcg_update_array_size(int num)
3057{
3058 if (num > memcg_limited_groups_array_size)
3059 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3060}
3061
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003062static void kmem_cache_destroy_work_func(struct work_struct *w);
3063
Glauber Costa55007d82012-12-18 14:22:38 -08003064int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3065{
3066 struct memcg_cache_params *cur_params = s->memcg_params;
3067
3068 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3069
3070 if (num_groups > memcg_limited_groups_array_size) {
3071 int i;
3072 ssize_t size = memcg_caches_array_size(num_groups);
3073
3074 size *= sizeof(void *);
3075 size += sizeof(struct memcg_cache_params);
3076
3077 s->memcg_params = kzalloc(size, GFP_KERNEL);
3078 if (!s->memcg_params) {
3079 s->memcg_params = cur_params;
3080 return -ENOMEM;
3081 }
3082
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003083 INIT_WORK(&s->memcg_params->destroy,
3084 kmem_cache_destroy_work_func);
Glauber Costa55007d82012-12-18 14:22:38 -08003085 s->memcg_params->is_root_cache = true;
3086
3087 /*
3088 * There is the chance it will be bigger than
3089 * memcg_limited_groups_array_size, if we failed an allocation
3090 * in a cache, in which case all caches updated before it, will
3091 * have a bigger array.
3092 *
3093 * But if that is the case, the data after
3094 * memcg_limited_groups_array_size is certainly unused
3095 */
3096 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3097 if (!cur_params->memcg_caches[i])
3098 continue;
3099 s->memcg_params->memcg_caches[i] =
3100 cur_params->memcg_caches[i];
3101 }
3102
3103 /*
3104 * Ideally, we would wait until all caches succeed, and only
3105 * then free the old one. But this is not worth the extra
3106 * pointer per-cache we'd have to have for this.
3107 *
3108 * It is not a big deal if some caches are left with a size
3109 * bigger than the others. And all updates will reset this
3110 * anyway.
3111 */
3112 kfree(cur_params);
3113 }
3114 return 0;
3115}
3116
Glauber Costa943a4512012-12-18 14:23:03 -08003117int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3118 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003119{
3120 size_t size = sizeof(struct memcg_cache_params);
3121
3122 if (!memcg_kmem_enabled())
3123 return 0;
3124
Glauber Costa55007d82012-12-18 14:22:38 -08003125 if (!memcg)
3126 size += memcg_limited_groups_array_size * sizeof(void *);
3127
Glauber Costa2633d7a2012-12-18 14:22:34 -08003128 s->memcg_params = kzalloc(size, GFP_KERNEL);
3129 if (!s->memcg_params)
3130 return -ENOMEM;
3131
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003132 INIT_WORK(&s->memcg_params->destroy,
3133 kmem_cache_destroy_work_func);
Glauber Costa943a4512012-12-18 14:23:03 -08003134 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003135 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003136 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003137 } else
3138 s->memcg_params->is_root_cache = true;
3139
Glauber Costa2633d7a2012-12-18 14:22:34 -08003140 return 0;
3141}
3142
3143void memcg_release_cache(struct kmem_cache *s)
3144{
Glauber Costad7f25f82012-12-18 14:22:40 -08003145 struct kmem_cache *root;
3146 struct mem_cgroup *memcg;
3147 int id;
3148
3149 /*
3150 * This happens, for instance, when a root cache goes away before we
3151 * add any memcg.
3152 */
3153 if (!s->memcg_params)
3154 return;
3155
3156 if (s->memcg_params->is_root_cache)
3157 goto out;
3158
3159 memcg = s->memcg_params->memcg;
3160 id = memcg_cache_id(memcg);
3161
3162 root = s->memcg_params->root_cache;
3163 root->memcg_params->memcg_caches[id] = NULL;
3164 mem_cgroup_put(memcg);
3165
3166 mutex_lock(&memcg->slab_caches_mutex);
3167 list_del(&s->memcg_params->list);
3168 mutex_unlock(&memcg->slab_caches_mutex);
3169
3170out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003171 kfree(s->memcg_params);
3172}
3173
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003174/*
3175 * During the creation a new cache, we need to disable our accounting mechanism
3176 * altogether. This is true even if we are not creating, but rather just
3177 * enqueing new caches to be created.
3178 *
3179 * This is because that process will trigger allocations; some visible, like
3180 * explicit kmallocs to auxiliary data structures, name strings and internal
3181 * cache structures; some well concealed, like INIT_WORK() that can allocate
3182 * objects during debug.
3183 *
3184 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3185 * to it. This may not be a bounded recursion: since the first cache creation
3186 * failed to complete (waiting on the allocation), we'll just try to create the
3187 * cache again, failing at the same point.
3188 *
3189 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3190 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3191 * inside the following two functions.
3192 */
3193static inline void memcg_stop_kmem_account(void)
3194{
3195 VM_BUG_ON(!current->mm);
3196 current->memcg_kmem_skip_account++;
3197}
3198
3199static inline void memcg_resume_kmem_account(void)
3200{
3201 VM_BUG_ON(!current->mm);
3202 current->memcg_kmem_skip_account--;
3203}
3204
Glauber Costa1f458cb2012-12-18 14:22:50 -08003205static void kmem_cache_destroy_work_func(struct work_struct *w)
3206{
3207 struct kmem_cache *cachep;
3208 struct memcg_cache_params *p;
3209
3210 p = container_of(w, struct memcg_cache_params, destroy);
3211
3212 cachep = memcg_params_to_cache(p);
3213
Glauber Costa22933152012-12-18 14:22:59 -08003214 /*
3215 * If we get down to 0 after shrink, we could delete right away.
3216 * However, memcg_release_pages() already puts us back in the workqueue
3217 * in that case. If we proceed deleting, we'll get a dangling
3218 * reference, and removing the object from the workqueue in that case
3219 * is unnecessary complication. We are not a fast path.
3220 *
3221 * Note that this case is fundamentally different from racing with
3222 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3223 * kmem_cache_shrink, not only we would be reinserting a dead cache
3224 * into the queue, but doing so from inside the worker racing to
3225 * destroy it.
3226 *
3227 * So if we aren't down to zero, we'll just schedule a worker and try
3228 * again
3229 */
3230 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3231 kmem_cache_shrink(cachep);
3232 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3233 return;
3234 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003235 kmem_cache_destroy(cachep);
3236}
3237
3238void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3239{
3240 if (!cachep->memcg_params->dead)
3241 return;
3242
3243 /*
Glauber Costa22933152012-12-18 14:22:59 -08003244 * There are many ways in which we can get here.
3245 *
3246 * We can get to a memory-pressure situation while the delayed work is
3247 * still pending to run. The vmscan shrinkers can then release all
3248 * cache memory and get us to destruction. If this is the case, we'll
3249 * be executed twice, which is a bug (the second time will execute over
3250 * bogus data). In this case, cancelling the work should be fine.
3251 *
3252 * But we can also get here from the worker itself, if
3253 * kmem_cache_shrink is enough to shake all the remaining objects and
3254 * get the page count to 0. In this case, we'll deadlock if we try to
3255 * cancel the work (the worker runs with an internal lock held, which
3256 * is the same lock we would hold for cancel_work_sync().)
3257 *
3258 * Since we can't possibly know who got us here, just refrain from
3259 * running if there is already work pending
3260 */
3261 if (work_pending(&cachep->memcg_params->destroy))
3262 return;
3263 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003264 * We have to defer the actual destroying to a workqueue, because
3265 * we might currently be in a context that cannot sleep.
3266 */
3267 schedule_work(&cachep->memcg_params->destroy);
3268}
3269
Glauber Costad7f25f82012-12-18 14:22:40 -08003270static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3271{
3272 char *name;
3273 struct dentry *dentry;
3274
3275 rcu_read_lock();
3276 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3277 rcu_read_unlock();
3278
3279 BUG_ON(dentry == NULL);
3280
3281 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3282 memcg_cache_id(memcg), dentry->d_name.name);
3283
3284 return name;
3285}
3286
3287static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3288 struct kmem_cache *s)
3289{
3290 char *name;
3291 struct kmem_cache *new;
3292
3293 name = memcg_cache_name(memcg, s);
3294 if (!name)
3295 return NULL;
3296
3297 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003298 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003299
Glauber Costad79923f2012-12-18 14:22:48 -08003300 if (new)
3301 new->allocflags |= __GFP_KMEMCG;
3302
Glauber Costad7f25f82012-12-18 14:22:40 -08003303 kfree(name);
3304 return new;
3305}
3306
3307/*
3308 * This lock protects updaters, not readers. We want readers to be as fast as
3309 * they can, and they will either see NULL or a valid cache value. Our model
3310 * allow them to see NULL, in which case the root memcg will be selected.
3311 *
3312 * We need this lock because multiple allocations to the same cache from a non
3313 * will span more than one worker. Only one of them can create the cache.
3314 */
3315static DEFINE_MUTEX(memcg_cache_mutex);
3316static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3317 struct kmem_cache *cachep)
3318{
3319 struct kmem_cache *new_cachep;
3320 int idx;
3321
3322 BUG_ON(!memcg_can_account_kmem(memcg));
3323
3324 idx = memcg_cache_id(memcg);
3325
3326 mutex_lock(&memcg_cache_mutex);
3327 new_cachep = cachep->memcg_params->memcg_caches[idx];
3328 if (new_cachep)
3329 goto out;
3330
3331 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003332 if (new_cachep == NULL) {
3333 new_cachep = cachep;
3334 goto out;
3335 }
3336
3337 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003338 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003339
3340 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3341 /*
3342 * the readers won't lock, make sure everybody sees the updated value,
3343 * so they won't put stuff in the queue again for no reason
3344 */
3345 wmb();
3346out:
3347 mutex_unlock(&memcg_cache_mutex);
3348 return new_cachep;
3349}
3350
Glauber Costa7cf27982012-12-18 14:22:55 -08003351void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3352{
3353 struct kmem_cache *c;
3354 int i;
3355
3356 if (!s->memcg_params)
3357 return;
3358 if (!s->memcg_params->is_root_cache)
3359 return;
3360
3361 /*
3362 * If the cache is being destroyed, we trust that there is no one else
3363 * requesting objects from it. Even if there are, the sanity checks in
3364 * kmem_cache_destroy should caught this ill-case.
3365 *
3366 * Still, we don't want anyone else freeing memcg_caches under our
3367 * noses, which can happen if a new memcg comes to life. As usual,
3368 * we'll take the set_limit_mutex to protect ourselves against this.
3369 */
3370 mutex_lock(&set_limit_mutex);
3371 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3372 c = s->memcg_params->memcg_caches[i];
3373 if (!c)
3374 continue;
3375
3376 /*
3377 * We will now manually delete the caches, so to avoid races
3378 * we need to cancel all pending destruction workers and
3379 * proceed with destruction ourselves.
3380 *
3381 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3382 * and that could spawn the workers again: it is likely that
3383 * the cache still have active pages until this very moment.
3384 * This would lead us back to mem_cgroup_destroy_cache.
3385 *
3386 * But that will not execute at all if the "dead" flag is not
3387 * set, so flip it down to guarantee we are in control.
3388 */
3389 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003390 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003391 kmem_cache_destroy(c);
3392 }
3393 mutex_unlock(&set_limit_mutex);
3394}
3395
Glauber Costad7f25f82012-12-18 14:22:40 -08003396struct create_work {
3397 struct mem_cgroup *memcg;
3398 struct kmem_cache *cachep;
3399 struct work_struct work;
3400};
3401
Glauber Costa1f458cb2012-12-18 14:22:50 -08003402static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3403{
3404 struct kmem_cache *cachep;
3405 struct memcg_cache_params *params;
3406
3407 if (!memcg_kmem_is_active(memcg))
3408 return;
3409
3410 mutex_lock(&memcg->slab_caches_mutex);
3411 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3412 cachep = memcg_params_to_cache(params);
3413 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003414 schedule_work(&cachep->memcg_params->destroy);
3415 }
3416 mutex_unlock(&memcg->slab_caches_mutex);
3417}
3418
Glauber Costad7f25f82012-12-18 14:22:40 -08003419static void memcg_create_cache_work_func(struct work_struct *w)
3420{
3421 struct create_work *cw;
3422
3423 cw = container_of(w, struct create_work, work);
3424 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3425 /* Drop the reference gotten when we enqueued. */
3426 css_put(&cw->memcg->css);
3427 kfree(cw);
3428}
3429
3430/*
3431 * Enqueue the creation of a per-memcg kmem_cache.
3432 * Called with rcu_read_lock.
3433 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003434static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3435 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003436{
3437 struct create_work *cw;
3438
3439 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3440 if (cw == NULL)
3441 return;
3442
3443 /* The corresponding put will be done in the workqueue. */
3444 if (!css_tryget(&memcg->css)) {
3445 kfree(cw);
3446 return;
3447 }
3448
3449 cw->memcg = memcg;
3450 cw->cachep = cachep;
3451
3452 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3453 schedule_work(&cw->work);
3454}
3455
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003456static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3457 struct kmem_cache *cachep)
3458{
3459 /*
3460 * We need to stop accounting when we kmalloc, because if the
3461 * corresponding kmalloc cache is not yet created, the first allocation
3462 * in __memcg_create_cache_enqueue will recurse.
3463 *
3464 * However, it is better to enclose the whole function. Depending on
3465 * the debugging options enabled, INIT_WORK(), for instance, can
3466 * trigger an allocation. This too, will make us recurse. Because at
3467 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3468 * the safest choice is to do it like this, wrapping the whole function.
3469 */
3470 memcg_stop_kmem_account();
3471 __memcg_create_cache_enqueue(memcg, cachep);
3472 memcg_resume_kmem_account();
3473}
Glauber Costad7f25f82012-12-18 14:22:40 -08003474/*
3475 * Return the kmem_cache we're supposed to use for a slab allocation.
3476 * We try to use the current memcg's version of the cache.
3477 *
3478 * If the cache does not exist yet, if we are the first user of it,
3479 * we either create it immediately, if possible, or create it asynchronously
3480 * in a workqueue.
3481 * In the latter case, we will let the current allocation go through with
3482 * the original cache.
3483 *
3484 * Can't be called in interrupt context or from kernel threads.
3485 * This function needs to be called with rcu_read_lock() held.
3486 */
3487struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3488 gfp_t gfp)
3489{
3490 struct mem_cgroup *memcg;
3491 int idx;
3492
3493 VM_BUG_ON(!cachep->memcg_params);
3494 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3495
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003496 if (!current->mm || current->memcg_kmem_skip_account)
3497 return cachep;
3498
Glauber Costad7f25f82012-12-18 14:22:40 -08003499 rcu_read_lock();
3500 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3501 rcu_read_unlock();
3502
3503 if (!memcg_can_account_kmem(memcg))
3504 return cachep;
3505
3506 idx = memcg_cache_id(memcg);
3507
3508 /*
3509 * barrier to mare sure we're always seeing the up to date value. The
3510 * code updating memcg_caches will issue a write barrier to match this.
3511 */
3512 read_barrier_depends();
3513 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3514 /*
3515 * If we are in a safe context (can wait, and not in interrupt
3516 * context), we could be be predictable and return right away.
3517 * This would guarantee that the allocation being performed
3518 * already belongs in the new cache.
3519 *
3520 * However, there are some clashes that can arrive from locking.
3521 * For instance, because we acquire the slab_mutex while doing
3522 * kmem_cache_dup, this means no further allocation could happen
3523 * with the slab_mutex held.
3524 *
3525 * Also, because cache creation issue get_online_cpus(), this
3526 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3527 * that ends up reversed during cpu hotplug. (cpuset allocates
3528 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3529 * better to defer everything.
3530 */
3531 memcg_create_cache_enqueue(memcg, cachep);
3532 return cachep;
3533 }
3534
3535 return cachep->memcg_params->memcg_caches[idx];
3536}
3537EXPORT_SYMBOL(__memcg_kmem_get_cache);
3538
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003539/*
3540 * We need to verify if the allocation against current->mm->owner's memcg is
3541 * possible for the given order. But the page is not allocated yet, so we'll
3542 * need a further commit step to do the final arrangements.
3543 *
3544 * It is possible for the task to switch cgroups in this mean time, so at
3545 * commit time, we can't rely on task conversion any longer. We'll then use
3546 * the handle argument to return to the caller which cgroup we should commit
3547 * against. We could also return the memcg directly and avoid the pointer
3548 * passing, but a boolean return value gives better semantics considering
3549 * the compiled-out case as well.
3550 *
3551 * Returning true means the allocation is possible.
3552 */
3553bool
3554__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3555{
3556 struct mem_cgroup *memcg;
3557 int ret;
3558
3559 *_memcg = NULL;
3560 memcg = try_get_mem_cgroup_from_mm(current->mm);
3561
3562 /*
3563 * very rare case described in mem_cgroup_from_task. Unfortunately there
3564 * isn't much we can do without complicating this too much, and it would
3565 * be gfp-dependent anyway. Just let it go
3566 */
3567 if (unlikely(!memcg))
3568 return true;
3569
3570 if (!memcg_can_account_kmem(memcg)) {
3571 css_put(&memcg->css);
3572 return true;
3573 }
3574
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003575 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3576 if (!ret)
3577 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003578
3579 css_put(&memcg->css);
3580 return (ret == 0);
3581}
3582
3583void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3584 int order)
3585{
3586 struct page_cgroup *pc;
3587
3588 VM_BUG_ON(mem_cgroup_is_root(memcg));
3589
3590 /* The page allocation failed. Revert */
3591 if (!page) {
3592 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003593 return;
3594 }
3595
3596 pc = lookup_page_cgroup(page);
3597 lock_page_cgroup(pc);
3598 pc->mem_cgroup = memcg;
3599 SetPageCgroupUsed(pc);
3600 unlock_page_cgroup(pc);
3601}
3602
3603void __memcg_kmem_uncharge_pages(struct page *page, int order)
3604{
3605 struct mem_cgroup *memcg = NULL;
3606 struct page_cgroup *pc;
3607
3608
3609 pc = lookup_page_cgroup(page);
3610 /*
3611 * Fast unlocked return. Theoretically might have changed, have to
3612 * check again after locking.
3613 */
3614 if (!PageCgroupUsed(pc))
3615 return;
3616
3617 lock_page_cgroup(pc);
3618 if (PageCgroupUsed(pc)) {
3619 memcg = pc->mem_cgroup;
3620 ClearPageCgroupUsed(pc);
3621 }
3622 unlock_page_cgroup(pc);
3623
3624 /*
3625 * We trust that only if there is a memcg associated with the page, it
3626 * is a valid allocation
3627 */
3628 if (!memcg)
3629 return;
3630
3631 VM_BUG_ON(mem_cgroup_is_root(memcg));
3632 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003633}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003634#else
3635static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3636{
3637}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003638#endif /* CONFIG_MEMCG_KMEM */
3639
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003640#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3641
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003642#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003643/*
3644 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003645 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3646 * charge/uncharge will be never happen and move_account() is done under
3647 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003648 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003649void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003650{
3651 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003652 struct page_cgroup *pc;
3653 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003654
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003655 if (mem_cgroup_disabled())
3656 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003657 for (i = 1; i < HPAGE_PMD_NR; i++) {
3658 pc = head_pc + i;
3659 pc->mem_cgroup = head_pc->mem_cgroup;
3660 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003661 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3662 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003663}
Hugh Dickins12d27102012-01-12 17:19:52 -08003664#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003665
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003666/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003667 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003668 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003669 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003670 * @pc: page_cgroup of the page.
3671 * @from: mem_cgroup which the page is moved from.
3672 * @to: mem_cgroup which the page is moved to. @from != @to.
3673 *
3674 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003675 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003676 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003677 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003678 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3679 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003680 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003681static int mem_cgroup_move_account(struct page *page,
3682 unsigned int nr_pages,
3683 struct page_cgroup *pc,
3684 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003685 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003686{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003687 unsigned long flags;
3688 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003689 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003690
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003691 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003692 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003693 /*
3694 * The page is isolated from LRU. So, collapse function
3695 * will not handle this page. But page splitting can happen.
3696 * Do this check under compound_page_lock(). The caller should
3697 * hold it.
3698 */
3699 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003700 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003701 goto out;
3702
3703 lock_page_cgroup(pc);
3704
3705 ret = -EINVAL;
3706 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3707 goto unlock;
3708
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003709 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003711 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003712 /* Update mapped_file data for mem_cgroup */
3713 preempt_disable();
3714 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3715 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3716 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003717 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003718 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003719
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003720 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003721 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003722 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003723 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003724 ret = 0;
3725unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003726 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003727 /*
3728 * check events
3729 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003730 memcg_check_events(to, page);
3731 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003732out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003733 return ret;
3734}
3735
Michal Hocko2ef37d32012-10-26 13:37:30 +02003736/**
3737 * mem_cgroup_move_parent - moves page to the parent group
3738 * @page: the page to move
3739 * @pc: page_cgroup of the page
3740 * @child: page's cgroup
3741 *
3742 * move charges to its parent or the root cgroup if the group has no
3743 * parent (aka use_hierarchy==0).
3744 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3745 * mem_cgroup_move_account fails) the failure is always temporary and
3746 * it signals a race with a page removal/uncharge or migration. In the
3747 * first case the page is on the way out and it will vanish from the LRU
3748 * on the next attempt and the call should be retried later.
3749 * Isolation from the LRU fails only if page has been isolated from
3750 * the LRU since we looked at it and that usually means either global
3751 * reclaim or migration going on. The page will either get back to the
3752 * LRU or vanish.
3753 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3754 * (!PageCgroupUsed) or moved to a different group. The page will
3755 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003756 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003757static int mem_cgroup_move_parent(struct page *page,
3758 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003759 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003760{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003761 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003762 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003763 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003764 int ret;
3765
Michal Hockod8423012012-10-26 13:37:29 +02003766 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003767
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003768 ret = -EBUSY;
3769 if (!get_page_unless_zero(page))
3770 goto out;
3771 if (isolate_lru_page(page))
3772 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003773
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003774 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003775
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003776 parent = parent_mem_cgroup(child);
3777 /*
3778 * If no parent, move charges to root cgroup.
3779 */
3780 if (!parent)
3781 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003782
Michal Hocko2ef37d32012-10-26 13:37:30 +02003783 if (nr_pages > 1) {
3784 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003785 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003786 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003787
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003788 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003789 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003790 if (!ret)
3791 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003792
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003793 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003794 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003795 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003796put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003797 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003798out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003799 return ret;
3800}
3801
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003802/*
3803 * Charge the memory controller for page usage.
3804 * Return
3805 * 0 if the charge was successful
3806 * < 0 if the cgroup is over its limit
3807 */
3808static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003809 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003810{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003811 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003812 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003813 bool oom = true;
3814 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003815
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003816 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003817 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003818 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003819 /*
3820 * Never OOM-kill a process for a huge page. The
3821 * fault handler will fall back to regular pages.
3822 */
3823 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003824 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003825
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003826 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003827 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003828 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003829 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003830 return 0;
3831}
3832
3833int mem_cgroup_newpage_charge(struct page *page,
3834 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003835{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003836 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003837 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003838 VM_BUG_ON(page_mapped(page));
3839 VM_BUG_ON(page->mapping && !PageAnon(page));
3840 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003841 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003842 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003843}
3844
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003845/*
3846 * While swap-in, try_charge -> commit or cancel, the page is locked.
3847 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003848 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003849 * "commit()" or removed by "cancel()"
3850 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003851static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3852 struct page *page,
3853 gfp_t mask,
3854 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003855{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003856 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003857 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003858 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003859
Johannes Weiner90deb782012-07-31 16:45:47 -07003860 pc = lookup_page_cgroup(page);
3861 /*
3862 * Every swap fault against a single page tries to charge the
3863 * page, bail as early as possible. shmem_unuse() encounters
3864 * already charged pages, too. The USED bit is protected by
3865 * the page lock, which serializes swap cache removal, which
3866 * in turn serializes uncharging.
3867 */
3868 if (PageCgroupUsed(pc))
3869 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003870 if (!do_swap_account)
3871 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003872 memcg = try_get_mem_cgroup_from_page(page);
3873 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003874 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003875 *memcgp = memcg;
3876 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003877 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003878 if (ret == -EINTR)
3879 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003880 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003881charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003882 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3883 if (ret == -EINTR)
3884 ret = 0;
3885 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003886}
3887
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003888int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3889 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3890{
3891 *memcgp = NULL;
3892 if (mem_cgroup_disabled())
3893 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003894 /*
3895 * A racing thread's fault, or swapoff, may have already
3896 * updated the pte, and even removed page from swap cache: in
3897 * those cases unuse_pte()'s pte_same() test will fail; but
3898 * there's also a KSM case which does need to charge the page.
3899 */
3900 if (!PageSwapCache(page)) {
3901 int ret;
3902
3903 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3904 if (ret == -EINTR)
3905 ret = 0;
3906 return ret;
3907 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003908 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3909}
3910
Johannes Weiner827a03d2012-07-31 16:45:36 -07003911void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3912{
3913 if (mem_cgroup_disabled())
3914 return;
3915 if (!memcg)
3916 return;
3917 __mem_cgroup_cancel_charge(memcg, 1);
3918}
3919
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003920static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003921__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003922 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003923{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003924 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003925 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003926 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003927 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003928
Johannes Weinerce587e62012-04-24 20:22:33 +02003929 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003930 /*
3931 * Now swap is on-memory. This means this page may be
3932 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003933 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3934 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3935 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003936 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003937 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003938 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003939 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003940 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003941}
3942
Johannes Weiner72835c82012-01-12 17:18:32 -08003943void mem_cgroup_commit_charge_swapin(struct page *page,
3944 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003945{
Johannes Weiner72835c82012-01-12 17:18:32 -08003946 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003947 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003948}
3949
Johannes Weiner827a03d2012-07-31 16:45:36 -07003950int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3951 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003952{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003953 struct mem_cgroup *memcg = NULL;
3954 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3955 int ret;
3956
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003957 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003958 return 0;
3959 if (PageCompound(page))
3960 return 0;
3961
Johannes Weiner827a03d2012-07-31 16:45:36 -07003962 if (!PageSwapCache(page))
3963 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3964 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003965 ret = __mem_cgroup_try_charge_swapin(mm, page,
3966 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003967 if (!ret)
3968 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3969 }
3970 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003971}
3972
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003973static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003974 unsigned int nr_pages,
3975 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003976{
3977 struct memcg_batch_info *batch = NULL;
3978 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003979
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003980 /* If swapout, usage of swap doesn't decrease */
3981 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3982 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003983
3984 batch = &current->memcg_batch;
3985 /*
3986 * In usual, we do css_get() when we remember memcg pointer.
3987 * But in this case, we keep res->usage until end of a series of
3988 * uncharges. Then, it's ok to ignore memcg's refcnt.
3989 */
3990 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003991 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003992 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003993 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003994 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003995 * the same cgroup and we have chance to coalesce uncharges.
3996 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3997 * because we want to do uncharge as soon as possible.
3998 */
3999
4000 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4001 goto direct_uncharge;
4002
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004003 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004004 goto direct_uncharge;
4005
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004006 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004007 * In typical case, batch->memcg == mem. This means we can
4008 * merge a series of uncharges to an uncharge of res_counter.
4009 * If not, we uncharge res_counter ony by one.
4010 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004011 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004012 goto direct_uncharge;
4013 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004014 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004015 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004016 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004017 return;
4018direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004019 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004020 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004021 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4022 if (unlikely(batch->memcg != memcg))
4023 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004024}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004025
Balbir Singh8697d332008-02-07 00:13:59 -08004026/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004027 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004028 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004029static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004030__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4031 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004032{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004033 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004034 unsigned int nr_pages = 1;
4035 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004036 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004037
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004038 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004039 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004040
Johannes Weiner0c59b892012-07-31 16:45:31 -07004041 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004042
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004043 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004044 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004045 VM_BUG_ON(!PageTransHuge(page));
4046 }
Balbir Singh8697d332008-02-07 00:13:59 -08004047 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004048 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004049 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004050 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004051 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004052 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004053
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004054 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004055
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004056 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004057
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004058 if (!PageCgroupUsed(pc))
4059 goto unlock_out;
4060
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004061 anon = PageAnon(page);
4062
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004063 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004064 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004065 /*
4066 * Generally PageAnon tells if it's the anon statistics to be
4067 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4068 * used before page reached the stage of being marked PageAnon.
4069 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004070 anon = true;
4071 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004072 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004073 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004074 if (page_mapped(page))
4075 goto unlock_out;
4076 /*
4077 * Pages under migration may not be uncharged. But
4078 * end_migration() /must/ be the one uncharging the
4079 * unused post-migration page and so it has to call
4080 * here with the migration bit still set. See the
4081 * res_counter handling below.
4082 */
4083 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004084 goto unlock_out;
4085 break;
4086 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4087 if (!PageAnon(page)) { /* Shared memory */
4088 if (page->mapping && !page_is_file_cache(page))
4089 goto unlock_out;
4090 } else if (page_mapped(page)) /* Anon */
4091 goto unlock_out;
4092 break;
4093 default:
4094 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004095 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004096
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004097 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004098
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004099 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004100 /*
4101 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4102 * freed from LRU. This is safe because uncharged page is expected not
4103 * to be reused (freed soon). Exception is SwapCache, it's handled by
4104 * special functions.
4105 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004106
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004107 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004108 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004109 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004110 * will never be freed.
4111 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004112 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004113 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004114 mem_cgroup_swap_statistics(memcg, true);
4115 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004116 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004117 /*
4118 * Migration does not charge the res_counter for the
4119 * replacement page, so leave it alone when phasing out the
4120 * page that is unused after the migration.
4121 */
4122 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004123 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004124
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004125 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004126
4127unlock_out:
4128 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004129 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004130}
4131
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004132void mem_cgroup_uncharge_page(struct page *page)
4133{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004134 /* early check. */
4135 if (page_mapped(page))
4136 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004137 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004138 if (PageSwapCache(page))
4139 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004140 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004141}
4142
4143void mem_cgroup_uncharge_cache_page(struct page *page)
4144{
4145 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004146 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004147 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004148}
4149
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004150/*
4151 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4152 * In that cases, pages are freed continuously and we can expect pages
4153 * are in the same memcg. All these calls itself limits the number of
4154 * pages freed at once, then uncharge_start/end() is called properly.
4155 * This may be called prural(2) times in a context,
4156 */
4157
4158void mem_cgroup_uncharge_start(void)
4159{
4160 current->memcg_batch.do_batch++;
4161 /* We can do nest. */
4162 if (current->memcg_batch.do_batch == 1) {
4163 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004164 current->memcg_batch.nr_pages = 0;
4165 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004166 }
4167}
4168
4169void mem_cgroup_uncharge_end(void)
4170{
4171 struct memcg_batch_info *batch = &current->memcg_batch;
4172
4173 if (!batch->do_batch)
4174 return;
4175
4176 batch->do_batch--;
4177 if (batch->do_batch) /* If stacked, do nothing. */
4178 return;
4179
4180 if (!batch->memcg)
4181 return;
4182 /*
4183 * This "batch->memcg" is valid without any css_get/put etc...
4184 * bacause we hide charges behind us.
4185 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004186 if (batch->nr_pages)
4187 res_counter_uncharge(&batch->memcg->res,
4188 batch->nr_pages * PAGE_SIZE);
4189 if (batch->memsw_nr_pages)
4190 res_counter_uncharge(&batch->memcg->memsw,
4191 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004192 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004193 /* forget this pointer (for sanity check) */
4194 batch->memcg = NULL;
4195}
4196
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004197#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004198/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004199 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004200 * memcg information is recorded to swap_cgroup of "ent"
4201 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004202void
4203mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004204{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004205 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004206 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004207
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004208 if (!swapout) /* this was a swap cache but the swap is unused ! */
4209 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4210
Johannes Weiner0030f532012-07-31 16:45:25 -07004211 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004212
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004213 /*
4214 * record memcg information, if swapout && memcg != NULL,
4215 * mem_cgroup_get() was called in uncharge().
4216 */
4217 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004218 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004219}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004220#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004221
Andrew Mortonc255a452012-07-31 16:43:02 -07004222#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004223/*
4224 * called from swap_entry_free(). remove record in swap_cgroup and
4225 * uncharge "memsw" account.
4226 */
4227void mem_cgroup_uncharge_swap(swp_entry_t ent)
4228{
4229 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004230 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004231
4232 if (!do_swap_account)
4233 return;
4234
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004235 id = swap_cgroup_record(ent, 0);
4236 rcu_read_lock();
4237 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004238 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004239 /*
4240 * We uncharge this because swap is freed.
4241 * This memcg can be obsolete one. We avoid calling css_tryget
4242 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004243 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004244 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004245 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004246 mem_cgroup_put(memcg);
4247 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004248 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004249}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004250
4251/**
4252 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4253 * @entry: swap entry to be moved
4254 * @from: mem_cgroup which the entry is moved from
4255 * @to: mem_cgroup which the entry is moved to
4256 *
4257 * It succeeds only when the swap_cgroup's record for this entry is the same
4258 * as the mem_cgroup's id of @from.
4259 *
4260 * Returns 0 on success, -EINVAL on failure.
4261 *
4262 * The caller must have charged to @to, IOW, called res_counter_charge() about
4263 * both res and memsw, and called css_get().
4264 */
4265static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004266 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004267{
4268 unsigned short old_id, new_id;
4269
4270 old_id = css_id(&from->css);
4271 new_id = css_id(&to->css);
4272
4273 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004274 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004275 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004276 /*
4277 * This function is only called from task migration context now.
4278 * It postpones res_counter and refcount handling till the end
4279 * of task migration(mem_cgroup_clear_mc()) for performance
4280 * improvement. But we cannot postpone mem_cgroup_get(to)
4281 * because if the process that has been moved to @to does
4282 * swap-in, the refcount of @to might be decreased to 0.
4283 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004284 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004285 return 0;
4286 }
4287 return -EINVAL;
4288}
4289#else
4290static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004291 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004292{
4293 return -EINVAL;
4294}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004295#endif
4296
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004297/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004298 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4299 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004300 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004301void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4302 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004303{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004304 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004305 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004306 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004307 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004308
Johannes Weiner72835c82012-01-12 17:18:32 -08004309 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004310
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004311 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004312 return;
Balbir Singh40779602008-04-04 14:29:59 -07004313
Mel Gormanb32967f2012-11-19 12:35:47 +00004314 if (PageTransHuge(page))
4315 nr_pages <<= compound_order(page);
4316
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004317 pc = lookup_page_cgroup(page);
4318 lock_page_cgroup(pc);
4319 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004320 memcg = pc->mem_cgroup;
4321 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004322 /*
4323 * At migrating an anonymous page, its mapcount goes down
4324 * to 0 and uncharge() will be called. But, even if it's fully
4325 * unmapped, migration may fail and this page has to be
4326 * charged again. We set MIGRATION flag here and delay uncharge
4327 * until end_migration() is called
4328 *
4329 * Corner Case Thinking
4330 * A)
4331 * When the old page was mapped as Anon and it's unmap-and-freed
4332 * while migration was ongoing.
4333 * If unmap finds the old page, uncharge() of it will be delayed
4334 * until end_migration(). If unmap finds a new page, it's
4335 * uncharged when it make mapcount to be 1->0. If unmap code
4336 * finds swap_migration_entry, the new page will not be mapped
4337 * and end_migration() will find it(mapcount==0).
4338 *
4339 * B)
4340 * When the old page was mapped but migraion fails, the kernel
4341 * remaps it. A charge for it is kept by MIGRATION flag even
4342 * if mapcount goes down to 0. We can do remap successfully
4343 * without charging it again.
4344 *
4345 * C)
4346 * The "old" page is under lock_page() until the end of
4347 * migration, so, the old page itself will not be swapped-out.
4348 * If the new page is swapped out before end_migraton, our
4349 * hook to usual swap-out path will catch the event.
4350 */
4351 if (PageAnon(page))
4352 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004353 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004354 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004355 /*
4356 * If the page is not charged at this point,
4357 * we return here.
4358 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004359 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004360 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004361
Johannes Weiner72835c82012-01-12 17:18:32 -08004362 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004363 /*
4364 * We charge new page before it's used/mapped. So, even if unlock_page()
4365 * is called before end_migration, we can catch all events on this new
4366 * page. In the case new page is migrated but not remapped, new page's
4367 * mapcount will be finally 0 and we call uncharge in end_migration().
4368 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004369 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004370 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004371 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004372 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004373 /*
4374 * The page is committed to the memcg, but it's not actually
4375 * charged to the res_counter since we plan on replacing the
4376 * old one and only one page is going to be left afterwards.
4377 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004378 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004379}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004380
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004381/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004382void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004383 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004384{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004385 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004386 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004387 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004388
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004389 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004390 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004391
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004392 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004393 used = oldpage;
4394 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004395 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004396 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004397 unused = oldpage;
4398 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004399 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004400 __mem_cgroup_uncharge_common(unused,
4401 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4402 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4403 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004404 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004405 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004406 * We disallowed uncharge of pages under migration because mapcount
4407 * of the page goes down to zero, temporarly.
4408 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004409 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004410 pc = lookup_page_cgroup(oldpage);
4411 lock_page_cgroup(pc);
4412 ClearPageCgroupMigration(pc);
4413 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004414
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004415 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004416 * If a page is a file cache, radix-tree replacement is very atomic
4417 * and we can skip this check. When it was an Anon page, its mapcount
4418 * goes down to 0. But because we added MIGRATION flage, it's not
4419 * uncharged yet. There are several case but page->mapcount check
4420 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4421 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004422 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004423 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004424 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004425}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004426
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004427/*
4428 * At replace page cache, newpage is not under any memcg but it's on
4429 * LRU. So, this function doesn't touch res_counter but handles LRU
4430 * in correct way. Both pages are locked so we cannot race with uncharge.
4431 */
4432void mem_cgroup_replace_page_cache(struct page *oldpage,
4433 struct page *newpage)
4434{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004435 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004436 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004437 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004438
4439 if (mem_cgroup_disabled())
4440 return;
4441
4442 pc = lookup_page_cgroup(oldpage);
4443 /* fix accounting on old pages */
4444 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004445 if (PageCgroupUsed(pc)) {
4446 memcg = pc->mem_cgroup;
4447 mem_cgroup_charge_statistics(memcg, false, -1);
4448 ClearPageCgroupUsed(pc);
4449 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004450 unlock_page_cgroup(pc);
4451
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004452 /*
4453 * When called from shmem_replace_page(), in some cases the
4454 * oldpage has already been charged, and in some cases not.
4455 */
4456 if (!memcg)
4457 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004458 /*
4459 * Even if newpage->mapping was NULL before starting replacement,
4460 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4461 * LRU while we overwrite pc->mem_cgroup.
4462 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004463 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004464}
4465
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004466#ifdef CONFIG_DEBUG_VM
4467static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4468{
4469 struct page_cgroup *pc;
4470
4471 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004472 /*
4473 * Can be NULL while feeding pages into the page allocator for
4474 * the first time, i.e. during boot or memory hotplug;
4475 * or when mem_cgroup_disabled().
4476 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004477 if (likely(pc) && PageCgroupUsed(pc))
4478 return pc;
4479 return NULL;
4480}
4481
4482bool mem_cgroup_bad_page_check(struct page *page)
4483{
4484 if (mem_cgroup_disabled())
4485 return false;
4486
4487 return lookup_page_cgroup_used(page) != NULL;
4488}
4489
4490void mem_cgroup_print_bad_page(struct page *page)
4491{
4492 struct page_cgroup *pc;
4493
4494 pc = lookup_page_cgroup_used(page);
4495 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004496 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4497 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004498 }
4499}
4500#endif
4501
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004502static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004503 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004504{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004505 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004506 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004507 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004508 int children = mem_cgroup_count_children(memcg);
4509 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004510 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004511
4512 /*
4513 * For keeping hierarchical_reclaim simple, how long we should retry
4514 * is depends on callers. We set our retry-count to be function
4515 * of # of children which we should visit in this loop.
4516 */
4517 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4518
4519 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004520
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004521 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004522 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004523 if (signal_pending(current)) {
4524 ret = -EINTR;
4525 break;
4526 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004527 /*
4528 * Rather than hide all in some function, I do this in
4529 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004530 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004531 */
4532 mutex_lock(&set_limit_mutex);
4533 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4534 if (memswlimit < val) {
4535 ret = -EINVAL;
4536 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004537 break;
4538 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004539
4540 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4541 if (memlimit < val)
4542 enlarge = 1;
4543
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004544 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004545 if (!ret) {
4546 if (memswlimit == val)
4547 memcg->memsw_is_minimum = true;
4548 else
4549 memcg->memsw_is_minimum = false;
4550 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004551 mutex_unlock(&set_limit_mutex);
4552
4553 if (!ret)
4554 break;
4555
Johannes Weiner56600482012-01-12 17:17:59 -08004556 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4557 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004558 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4559 /* Usage is reduced ? */
4560 if (curusage >= oldusage)
4561 retry_count--;
4562 else
4563 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004564 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004565 if (!ret && enlarge)
4566 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004567
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004568 return ret;
4569}
4570
Li Zefan338c8432009-06-17 16:27:15 -07004571static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4572 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004573{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004574 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004575 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004576 int children = mem_cgroup_count_children(memcg);
4577 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004578 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004579
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004580 /* see mem_cgroup_resize_res_limit */
4581 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4582 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004583 while (retry_count) {
4584 if (signal_pending(current)) {
4585 ret = -EINTR;
4586 break;
4587 }
4588 /*
4589 * Rather than hide all in some function, I do this in
4590 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004591 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004592 */
4593 mutex_lock(&set_limit_mutex);
4594 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4595 if (memlimit > val) {
4596 ret = -EINVAL;
4597 mutex_unlock(&set_limit_mutex);
4598 break;
4599 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004600 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4601 if (memswlimit < val)
4602 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004603 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004604 if (!ret) {
4605 if (memlimit == val)
4606 memcg->memsw_is_minimum = true;
4607 else
4608 memcg->memsw_is_minimum = false;
4609 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004610 mutex_unlock(&set_limit_mutex);
4611
4612 if (!ret)
4613 break;
4614
Johannes Weiner56600482012-01-12 17:17:59 -08004615 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4616 MEM_CGROUP_RECLAIM_NOSWAP |
4617 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004618 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004619 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004620 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004621 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004622 else
4623 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004624 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004625 if (!ret && enlarge)
4626 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004627 return ret;
4628}
4629
Balbir Singh4e416952009-09-23 15:56:39 -07004630unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004631 gfp_t gfp_mask,
4632 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004633{
4634 unsigned long nr_reclaimed = 0;
4635 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4636 unsigned long reclaimed;
4637 int loop = 0;
4638 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004639 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004640 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004641
4642 if (order > 0)
4643 return 0;
4644
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004645 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004646 /*
4647 * This loop can run a while, specially if mem_cgroup's continuously
4648 * keep exceeding their soft limit and putting the system under
4649 * pressure
4650 */
4651 do {
4652 if (next_mz)
4653 mz = next_mz;
4654 else
4655 mz = mem_cgroup_largest_soft_limit_node(mctz);
4656 if (!mz)
4657 break;
4658
Ying Han0ae5e892011-05-26 16:25:25 -07004659 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004660 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004661 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004662 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004663 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004664 spin_lock(&mctz->lock);
4665
4666 /*
4667 * If we failed to reclaim anything from this memory cgroup
4668 * it is time to move on to the next cgroup
4669 */
4670 next_mz = NULL;
4671 if (!reclaimed) {
4672 do {
4673 /*
4674 * Loop until we find yet another one.
4675 *
4676 * By the time we get the soft_limit lock
4677 * again, someone might have aded the
4678 * group back on the RB tree. Iterate to
4679 * make sure we get a different mem.
4680 * mem_cgroup_largest_soft_limit_node returns
4681 * NULL if no other cgroup is present on
4682 * the tree
4683 */
4684 next_mz =
4685 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004686 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004687 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004688 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004689 break;
4690 } while (1);
4691 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004692 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4693 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004694 /*
4695 * One school of thought says that we should not add
4696 * back the node to the tree if reclaim returns 0.
4697 * But our reclaim could return 0, simply because due
4698 * to priority we are exposing a smaller subset of
4699 * memory to reclaim from. Consider this as a longer
4700 * term TODO.
4701 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004702 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004703 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004704 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004705 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004706 loop++;
4707 /*
4708 * Could not reclaim anything and there are no more
4709 * mem cgroups to try or we seem to be looping without
4710 * reclaiming anything.
4711 */
4712 if (!nr_reclaimed &&
4713 (next_mz == NULL ||
4714 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4715 break;
4716 } while (!nr_reclaimed);
4717 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004718 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004719 return nr_reclaimed;
4720}
4721
Michal Hocko2ef37d32012-10-26 13:37:30 +02004722/**
4723 * mem_cgroup_force_empty_list - clears LRU of a group
4724 * @memcg: group to clear
4725 * @node: NUMA node
4726 * @zid: zone id
4727 * @lru: lru to to clear
4728 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004729 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004730 * reclaim the pages page themselves - pages are moved to the parent (or root)
4731 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004732 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004733static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004734 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004735{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004736 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004737 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004738 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004739 struct page *busy;
4740 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004741
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004742 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004743 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4744 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004745
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004746 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004747 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004748 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004749 struct page *page;
4750
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004751 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004752 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004753 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004754 break;
4755 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004756 page = list_entry(list->prev, struct page, lru);
4757 if (busy == page) {
4758 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004759 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004760 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004761 continue;
4762 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004763 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004764
Johannes Weiner925b7672012-01-12 17:18:15 -08004765 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004766
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004767 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004768 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004769 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004770 cond_resched();
4771 } else
4772 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004773 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004774}
4775
4776/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004777 * make mem_cgroup's charge to be 0 if there is no task by moving
4778 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004779 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004780 *
4781 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004782 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004783static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004784{
Michal Hockoc26251f2012-10-26 13:37:28 +02004785 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004786 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004787
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004788 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004789 /* This is for making all *used* pages to be on LRU. */
4790 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004791 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004792 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004793 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004794 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004795 enum lru_list lru;
4796 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004797 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004798 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004799 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004800 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004801 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004802 mem_cgroup_end_move(memcg);
4803 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004804 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004805
Michal Hocko2ef37d32012-10-26 13:37:30 +02004806 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004807 * Kernel memory may not necessarily be trackable to a specific
4808 * process. So they are not migrated, and therefore we can't
4809 * expect their value to drop to 0 here.
4810 * Having res filled up with kmem only is enough.
4811 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004812 * This is a safety check because mem_cgroup_force_empty_list
4813 * could have raced with mem_cgroup_replace_page_cache callers
4814 * so the lru seemed empty but the page could have been added
4815 * right after the check. RES_USAGE should be safe as we always
4816 * charge before adding to the LRU.
4817 */
Glauber Costabea207c2012-12-18 14:22:11 -08004818 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4819 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4820 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004821}
4822
4823/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004824 * This mainly exists for tests during the setting of set of use_hierarchy.
4825 * Since this is the very setting we are changing, the current hierarchy value
4826 * is meaningless
4827 */
4828static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4829{
4830 struct cgroup *pos;
4831
4832 /* bounce at first found */
4833 cgroup_for_each_child(pos, memcg->css.cgroup)
4834 return true;
4835 return false;
4836}
4837
4838/*
Glauber Costa09998212013-02-22 16:34:55 -08004839 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4840 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004841 * from mem_cgroup_count_children(), in the sense that we don't really care how
4842 * many children we have; we only need to know if we have any. It also counts
4843 * any memcg without hierarchy as infertile.
4844 */
4845static inline bool memcg_has_children(struct mem_cgroup *memcg)
4846{
4847 return memcg->use_hierarchy && __memcg_has_children(memcg);
4848}
4849
4850/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004851 * Reclaims as many pages from the given memcg as possible and moves
4852 * the rest to the parent.
4853 *
4854 * Caller is responsible for holding css reference for memcg.
4855 */
4856static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4857{
4858 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4859 struct cgroup *cgrp = memcg->css.cgroup;
4860
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004861 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004862 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4863 return -EBUSY;
4864
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004865 /* we call try-to-free pages for make this cgroup empty */
4866 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004867 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004868 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004869 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004870
Michal Hockoc26251f2012-10-26 13:37:28 +02004871 if (signal_pending(current))
4872 return -EINTR;
4873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004874 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004875 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004876 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004877 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004878 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004879 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004880 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004881
4882 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004883 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004884 mem_cgroup_reparent_charges(memcg);
4885
4886 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004887}
4888
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004889static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004890{
Michal Hockoc26251f2012-10-26 13:37:28 +02004891 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4892 int ret;
4893
Michal Hockod8423012012-10-26 13:37:29 +02004894 if (mem_cgroup_is_root(memcg))
4895 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004896 css_get(&memcg->css);
4897 ret = mem_cgroup_force_empty(memcg);
4898 css_put(&memcg->css);
4899
4900 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004901}
4902
4903
Balbir Singh18f59ea2009-01-07 18:08:07 -08004904static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4905{
4906 return mem_cgroup_from_cont(cont)->use_hierarchy;
4907}
4908
4909static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4910 u64 val)
4911{
4912 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004913 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004914 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004915 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004916
4917 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004918 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004919
Glauber Costa09998212013-02-22 16:34:55 -08004920 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004921
4922 if (memcg->use_hierarchy == val)
4923 goto out;
4924
Balbir Singh18f59ea2009-01-07 18:08:07 -08004925 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004926 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004927 * in the child subtrees. If it is unset, then the change can
4928 * occur, provided the current cgroup has no children.
4929 *
4930 * For the root cgroup, parent_mem is NULL, we allow value to be
4931 * set if there are no children.
4932 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004933 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004934 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004935 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004936 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004937 else
4938 retval = -EBUSY;
4939 } else
4940 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004941
4942out:
Glauber Costa09998212013-02-22 16:34:55 -08004943 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004944
4945 return retval;
4946}
4947
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004948
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004949static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004950 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004951{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004952 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004953 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004954
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004955 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004956 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004957 val += mem_cgroup_read_stat(iter, idx);
4958
4959 if (val < 0) /* race ? */
4960 val = 0;
4961 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004962}
4963
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004964static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004965{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004966 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004967
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004968 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004969 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004970 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004971 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004972 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004973 }
4974
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004975 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4976 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004977
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004978 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004979 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004980
4981 return val << PAGE_SHIFT;
4982}
4983
Tejun Heoaf36f902012-04-01 12:09:55 -07004984static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4985 struct file *file, char __user *buf,
4986 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004987{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004988 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004989 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004990 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004991 int name, len;
4992 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004993
4994 type = MEMFILE_TYPE(cft->private);
4995 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004996
4997 if (!do_swap_account && type == _MEMSWAP)
4998 return -EOPNOTSUPP;
4999
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005000 switch (type) {
5001 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005002 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005003 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005004 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005005 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005006 break;
5007 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005008 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005009 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005010 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005011 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005012 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005013 case _KMEM:
5014 val = res_counter_read_u64(&memcg->kmem, name);
5015 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005016 default:
5017 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005018 }
Tejun Heoaf36f902012-04-01 12:09:55 -07005019
5020 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5021 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005022}
Glauber Costa510fc4e2012-12-18 14:21:47 -08005023
5024static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
5025{
5026 int ret = -EINVAL;
5027#ifdef CONFIG_MEMCG_KMEM
5028 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5029 /*
5030 * For simplicity, we won't allow this to be disabled. It also can't
5031 * be changed if the cgroup has children already, or if tasks had
5032 * already joined.
5033 *
5034 * If tasks join before we set the limit, a person looking at
5035 * kmem.usage_in_bytes will have no way to determine when it took
5036 * place, which makes the value quite meaningless.
5037 *
5038 * After it first became limited, changes in the value of the limit are
5039 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08005040 */
Glauber Costa09998212013-02-22 16:34:55 -08005041 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005042 mutex_lock(&set_limit_mutex);
5043 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08005044 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08005045 ret = -EBUSY;
5046 goto out;
5047 }
5048 ret = res_counter_set_limit(&memcg->kmem, val);
5049 VM_BUG_ON(ret);
5050
Glauber Costa55007d82012-12-18 14:22:38 -08005051 ret = memcg_update_cache_sizes(memcg);
5052 if (ret) {
5053 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5054 goto out;
5055 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005056 static_key_slow_inc(&memcg_kmem_enabled_key);
5057 /*
5058 * setting the active bit after the inc will guarantee no one
5059 * starts accounting before all call sites are patched
5060 */
5061 memcg_kmem_set_active(memcg);
5062
Glauber Costa7de37682012-12-18 14:22:07 -08005063 /*
5064 * kmem charges can outlive the cgroup. In the case of slab
5065 * pages, for instance, a page contain objects from various
5066 * processes, so it is unfeasible to migrate them away. We
5067 * need to reference count the memcg because of that.
5068 */
5069 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005070 } else
5071 ret = res_counter_set_limit(&memcg->kmem, val);
5072out:
5073 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005074 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005075#endif
5076 return ret;
5077}
5078
Hugh Dickins6d0439902013-02-22 16:35:50 -08005079#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005080static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005081{
Glauber Costa55007d82012-12-18 14:22:38 -08005082 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005083 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5084 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005085 goto out;
5086
Glauber Costa510fc4e2012-12-18 14:21:47 -08005087 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005088 /*
5089 * When that happen, we need to disable the static branch only on those
5090 * memcgs that enabled it. To achieve this, we would be forced to
5091 * complicate the code by keeping track of which memcgs were the ones
5092 * that actually enabled limits, and which ones got it from its
5093 * parents.
5094 *
5095 * It is a lot simpler just to do static_key_slow_inc() on every child
5096 * that is accounted.
5097 */
Glauber Costa55007d82012-12-18 14:22:38 -08005098 if (!memcg_kmem_is_active(memcg))
5099 goto out;
5100
5101 /*
5102 * destroy(), called if we fail, will issue static_key_slow_inc() and
5103 * mem_cgroup_put() if kmem is enabled. We have to either call them
5104 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5105 * this more consistent, since it always leads to the same destroy path
5106 */
5107 mem_cgroup_get(memcg);
5108 static_key_slow_inc(&memcg_kmem_enabled_key);
5109
5110 mutex_lock(&set_limit_mutex);
5111 ret = memcg_update_cache_sizes(memcg);
5112 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005113out:
5114 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005115}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005116#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005117
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005118/*
5119 * The user of this function is...
5120 * RES_LIMIT.
5121 */
Paul Menage856c13a2008-07-25 01:47:04 -07005122static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5123 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005124{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005125 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005126 enum res_type type;
5127 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005128 unsigned long long val;
5129 int ret;
5130
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005131 type = MEMFILE_TYPE(cft->private);
5132 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005133
5134 if (!do_swap_account && type == _MEMSWAP)
5135 return -EOPNOTSUPP;
5136
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005137 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005138 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005139 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5140 ret = -EINVAL;
5141 break;
5142 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005143 /* This function does all necessary parse...reuse it */
5144 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005145 if (ret)
5146 break;
5147 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005148 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005149 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005150 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005151 else if (type == _KMEM)
5152 ret = memcg_update_kmem_limit(cont, val);
5153 else
5154 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005155 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005156 case RES_SOFT_LIMIT:
5157 ret = res_counter_memparse_write_strategy(buffer, &val);
5158 if (ret)
5159 break;
5160 /*
5161 * For memsw, soft limits are hard to implement in terms
5162 * of semantics, for now, we support soft limits for
5163 * control without swap
5164 */
5165 if (type == _MEM)
5166 ret = res_counter_set_soft_limit(&memcg->res, val);
5167 else
5168 ret = -EINVAL;
5169 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005170 default:
5171 ret = -EINVAL; /* should be BUG() ? */
5172 break;
5173 }
5174 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005175}
5176
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005177static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5178 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5179{
5180 struct cgroup *cgroup;
5181 unsigned long long min_limit, min_memsw_limit, tmp;
5182
5183 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5184 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5185 cgroup = memcg->css.cgroup;
5186 if (!memcg->use_hierarchy)
5187 goto out;
5188
5189 while (cgroup->parent) {
5190 cgroup = cgroup->parent;
5191 memcg = mem_cgroup_from_cont(cgroup);
5192 if (!memcg->use_hierarchy)
5193 break;
5194 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5195 min_limit = min(min_limit, tmp);
5196 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5197 min_memsw_limit = min(min_memsw_limit, tmp);
5198 }
5199out:
5200 *mem_limit = min_limit;
5201 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005202}
5203
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005204static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005205{
Tejun Heoaf36f902012-04-01 12:09:55 -07005206 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005207 int name;
5208 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005209
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005210 type = MEMFILE_TYPE(event);
5211 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005212
5213 if (!do_swap_account && type == _MEMSWAP)
5214 return -EOPNOTSUPP;
5215
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005216 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005217 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005218 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005219 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005220 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005221 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005222 else if (type == _KMEM)
5223 res_counter_reset_max(&memcg->kmem);
5224 else
5225 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005226 break;
5227 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005228 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005229 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005230 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005231 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005232 else if (type == _KMEM)
5233 res_counter_reset_failcnt(&memcg->kmem);
5234 else
5235 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005236 break;
5237 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005238
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005239 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005240}
5241
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005242static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5243 struct cftype *cft)
5244{
5245 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5246}
5247
Daisuke Nishimura02491442010-03-10 15:22:17 -08005248#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005249static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5250 struct cftype *cft, u64 val)
5251{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005252 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005253
5254 if (val >= (1 << NR_MOVE_TYPE))
5255 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005256
Glauber Costaee5e8472013-02-22 16:34:50 -08005257 /*
5258 * No kind of locking is needed in here, because ->can_attach() will
5259 * check this value once in the beginning of the process, and then carry
5260 * on with stale data. This means that changes to this value will only
5261 * affect task migrations starting after the change.
5262 */
5263 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005264 return 0;
5265}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005266#else
5267static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5268 struct cftype *cft, u64 val)
5269{
5270 return -ENOSYS;
5271}
5272#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005273
Ying Han406eb0c2011-05-26 16:25:37 -07005274#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005275static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005276 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005277{
5278 int nid;
5279 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5280 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005281 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005282
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005283 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005284 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005285 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005286 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005287 seq_printf(m, " N%d=%lu", nid, node_nr);
5288 }
5289 seq_putc(m, '\n');
5290
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005291 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005292 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005293 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005294 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005295 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005296 seq_printf(m, " N%d=%lu", nid, node_nr);
5297 }
5298 seq_putc(m, '\n');
5299
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005300 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005301 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005302 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005303 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005304 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005305 seq_printf(m, " N%d=%lu", nid, node_nr);
5306 }
5307 seq_putc(m, '\n');
5308
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005309 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005310 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005311 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005312 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005313 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005314 seq_printf(m, " N%d=%lu", nid, node_nr);
5315 }
5316 seq_putc(m, '\n');
5317 return 0;
5318}
5319#endif /* CONFIG_NUMA */
5320
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005321static inline void mem_cgroup_lru_names_not_uptodate(void)
5322{
5323 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5324}
5325
Wanpeng Liab215882012-07-31 16:43:09 -07005326static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005327 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005328{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005329 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005330 struct mem_cgroup *mi;
5331 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005332
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005333 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005334 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005335 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005336 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5337 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005338 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005339
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005340 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5341 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5342 mem_cgroup_read_events(memcg, i));
5343
5344 for (i = 0; i < NR_LRU_LISTS; i++)
5345 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5346 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5347
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005348 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005349 {
5350 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005351 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005352 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005353 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005354 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5355 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005356 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005357
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005358 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5359 long long val = 0;
5360
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005361 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005362 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005363 for_each_mem_cgroup_tree(mi, memcg)
5364 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5365 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5366 }
5367
5368 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5369 unsigned long long val = 0;
5370
5371 for_each_mem_cgroup_tree(mi, memcg)
5372 val += mem_cgroup_read_events(mi, i);
5373 seq_printf(m, "total_%s %llu\n",
5374 mem_cgroup_events_names[i], val);
5375 }
5376
5377 for (i = 0; i < NR_LRU_LISTS; i++) {
5378 unsigned long long val = 0;
5379
5380 for_each_mem_cgroup_tree(mi, memcg)
5381 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5382 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005383 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005384
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005385#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005386 {
5387 int nid, zid;
5388 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005389 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005390 unsigned long recent_rotated[2] = {0, 0};
5391 unsigned long recent_scanned[2] = {0, 0};
5392
5393 for_each_online_node(nid)
5394 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005395 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005396 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005397
Hugh Dickins89abfab2012-05-29 15:06:53 -07005398 recent_rotated[0] += rstat->recent_rotated[0];
5399 recent_rotated[1] += rstat->recent_rotated[1];
5400 recent_scanned[0] += rstat->recent_scanned[0];
5401 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005402 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005403 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5404 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5405 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5406 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005407 }
5408#endif
5409
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005410 return 0;
5411}
5412
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005413static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5414{
5415 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5416
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005417 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005418}
5419
5420static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5421 u64 val)
5422{
5423 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5424 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005425
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005426 if (val > 100)
5427 return -EINVAL;
5428
5429 if (cgrp->parent == NULL)
5430 return -EINVAL;
5431
5432 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005433
Glauber Costa09998212013-02-22 16:34:55 -08005434 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005435
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005436 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005437 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005438 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005439 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005440 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005441
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005442 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005443
Glauber Costa09998212013-02-22 16:34:55 -08005444 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005445
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005446 return 0;
5447}
5448
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005449static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5450{
5451 struct mem_cgroup_threshold_ary *t;
5452 u64 usage;
5453 int i;
5454
5455 rcu_read_lock();
5456 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005457 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005458 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005459 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005460
5461 if (!t)
5462 goto unlock;
5463
5464 usage = mem_cgroup_usage(memcg, swap);
5465
5466 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005467 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005468 * If it's not true, a threshold was crossed after last
5469 * call of __mem_cgroup_threshold().
5470 */
Phil Carmody5407a562010-05-26 14:42:42 -07005471 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005472
5473 /*
5474 * Iterate backward over array of thresholds starting from
5475 * current_threshold and check if a threshold is crossed.
5476 * If none of thresholds below usage is crossed, we read
5477 * only one element of the array here.
5478 */
5479 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5480 eventfd_signal(t->entries[i].eventfd, 1);
5481
5482 /* i = current_threshold + 1 */
5483 i++;
5484
5485 /*
5486 * Iterate forward over array of thresholds starting from
5487 * current_threshold+1 and check if a threshold is crossed.
5488 * If none of thresholds above usage is crossed, we read
5489 * only one element of the array here.
5490 */
5491 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5492 eventfd_signal(t->entries[i].eventfd, 1);
5493
5494 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005495 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005496unlock:
5497 rcu_read_unlock();
5498}
5499
5500static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5501{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005502 while (memcg) {
5503 __mem_cgroup_threshold(memcg, false);
5504 if (do_swap_account)
5505 __mem_cgroup_threshold(memcg, true);
5506
5507 memcg = parent_mem_cgroup(memcg);
5508 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005509}
5510
5511static int compare_thresholds(const void *a, const void *b)
5512{
5513 const struct mem_cgroup_threshold *_a = a;
5514 const struct mem_cgroup_threshold *_b = b;
5515
5516 return _a->threshold - _b->threshold;
5517}
5518
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005519static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005520{
5521 struct mem_cgroup_eventfd_list *ev;
5522
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005523 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005524 eventfd_signal(ev->eventfd, 1);
5525 return 0;
5526}
5527
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005528static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005529{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005530 struct mem_cgroup *iter;
5531
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005532 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005533 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005534}
5535
5536static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5537 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005538{
5539 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005540 struct mem_cgroup_thresholds *thresholds;
5541 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005542 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005543 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005544 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005545
5546 ret = res_counter_memparse_write_strategy(args, &threshold);
5547 if (ret)
5548 return ret;
5549
5550 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005551
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005552 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005553 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005554 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005555 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005556 else
5557 BUG();
5558
5559 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5560
5561 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005562 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5564
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005565 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005566
5567 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005568 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005569 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005570 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005571 ret = -ENOMEM;
5572 goto unlock;
5573 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005574 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005575
5576 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005577 if (thresholds->primary) {
5578 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005579 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005580 }
5581
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005582 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005583 new->entries[size - 1].eventfd = eventfd;
5584 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005585
5586 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005587 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005588 compare_thresholds, NULL);
5589
5590 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005591 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005592 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005593 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005594 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005595 * new->current_threshold will not be used until
5596 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005597 * it here.
5598 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005599 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005600 } else
5601 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005602 }
5603
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005604 /* Free old spare buffer and save old primary buffer as spare */
5605 kfree(thresholds->spare);
5606 thresholds->spare = thresholds->primary;
5607
5608 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005609
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005610 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005611 synchronize_rcu();
5612
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005613unlock:
5614 mutex_unlock(&memcg->thresholds_lock);
5615
5616 return ret;
5617}
5618
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005619static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005620 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005621{
5622 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005623 struct mem_cgroup_thresholds *thresholds;
5624 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005625 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005626 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005627 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005628
5629 mutex_lock(&memcg->thresholds_lock);
5630 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005631 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005632 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005633 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005634 else
5635 BUG();
5636
Anton Vorontsov371528c2012-02-24 05:14:46 +04005637 if (!thresholds->primary)
5638 goto unlock;
5639
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005640 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5641
5642 /* Check if a threshold crossed before removing */
5643 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5644
5645 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005646 size = 0;
5647 for (i = 0; i < thresholds->primary->size; i++) {
5648 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005649 size++;
5650 }
5651
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005652 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005653
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005654 /* Set thresholds array to NULL if we don't have thresholds */
5655 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005656 kfree(new);
5657 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005658 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005659 }
5660
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005661 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005662
5663 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005664 new->current_threshold = -1;
5665 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5666 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005667 continue;
5668
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005669 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005670 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005671 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005672 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005673 * until rcu_assign_pointer(), so it's safe to increment
5674 * it here.
5675 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005676 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005677 }
5678 j++;
5679 }
5680
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005681swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005682 /* Swap primary and spare array */
5683 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005684 /* If all events are unregistered, free the spare array */
5685 if (!new) {
5686 kfree(thresholds->spare);
5687 thresholds->spare = NULL;
5688 }
5689
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005690 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005691
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005692 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005693 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005694unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005695 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005696}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005697
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005698static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5699 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5700{
5701 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5702 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005703 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005704
5705 BUG_ON(type != _OOM_TYPE);
5706 event = kmalloc(sizeof(*event), GFP_KERNEL);
5707 if (!event)
5708 return -ENOMEM;
5709
Michal Hocko1af8efe2011-07-26 16:08:24 -07005710 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005711
5712 event->eventfd = eventfd;
5713 list_add(&event->list, &memcg->oom_notify);
5714
5715 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005716 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005717 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005718 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005719
5720 return 0;
5721}
5722
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005723static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005724 struct cftype *cft, struct eventfd_ctx *eventfd)
5725{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005726 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005727 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005728 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005729
5730 BUG_ON(type != _OOM_TYPE);
5731
Michal Hocko1af8efe2011-07-26 16:08:24 -07005732 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005733
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005734 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005735 if (ev->eventfd == eventfd) {
5736 list_del(&ev->list);
5737 kfree(ev);
5738 }
5739 }
5740
Michal Hocko1af8efe2011-07-26 16:08:24 -07005741 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005742}
5743
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005744static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5745 struct cftype *cft, struct cgroup_map_cb *cb)
5746{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005747 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005748
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005749 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005750
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005751 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005752 cb->fill(cb, "under_oom", 1);
5753 else
5754 cb->fill(cb, "under_oom", 0);
5755 return 0;
5756}
5757
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005758static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5759 struct cftype *cft, u64 val)
5760{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005761 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005762 struct mem_cgroup *parent;
5763
5764 /* cannot set to root cgroup and only 0 and 1 are allowed */
5765 if (!cgrp->parent || !((val == 0) || (val == 1)))
5766 return -EINVAL;
5767
5768 parent = mem_cgroup_from_cont(cgrp->parent);
5769
Glauber Costa09998212013-02-22 16:34:55 -08005770 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005771 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005772 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005773 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005774 return -EINVAL;
5775 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005776 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005777 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005778 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005779 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005780 return 0;
5781}
5782
Andrew Mortonc255a452012-07-31 16:43:02 -07005783#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005784static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005785{
Glauber Costa55007d82012-12-18 14:22:38 -08005786 int ret;
5787
Glauber Costa2633d7a2012-12-18 14:22:34 -08005788 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005789 ret = memcg_propagate_kmem(memcg);
5790 if (ret)
5791 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005792
Glauber Costa1d62e432012-04-09 19:36:33 -03005793 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005794};
5795
Glauber Costa1d62e432012-04-09 19:36:33 -03005796static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005797{
Glauber Costa1d62e432012-04-09 19:36:33 -03005798 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005799
5800 memcg_kmem_mark_dead(memcg);
5801
5802 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5803 return;
5804
5805 /*
5806 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5807 * path here, being careful not to race with memcg_uncharge_kmem: it is
5808 * possible that the charges went down to 0 between mark_dead and the
5809 * res_counter read, so in that case, we don't need the put
5810 */
5811 if (memcg_kmem_test_and_clear_dead(memcg))
5812 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005813}
Glauber Costae5671df2011-12-11 21:47:01 +00005814#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005815static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005816{
5817 return 0;
5818}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005819
Glauber Costa1d62e432012-04-09 19:36:33 -03005820static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005821{
5822}
Glauber Costae5671df2011-12-11 21:47:01 +00005823#endif
5824
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005825static struct cftype mem_cgroup_files[] = {
5826 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005827 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005828 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005829 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005830 .register_event = mem_cgroup_usage_register_event,
5831 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005832 },
5833 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005834 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005835 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005836 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005837 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005838 },
5839 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005840 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005841 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005842 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005843 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005844 },
5845 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005846 .name = "soft_limit_in_bytes",
5847 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5848 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005849 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005850 },
5851 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005852 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005853 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005854 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005855 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005856 },
Balbir Singh8697d332008-02-07 00:13:59 -08005857 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005858 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005859 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005860 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005861 {
5862 .name = "force_empty",
5863 .trigger = mem_cgroup_force_empty_write,
5864 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005865 {
5866 .name = "use_hierarchy",
5867 .write_u64 = mem_cgroup_hierarchy_write,
5868 .read_u64 = mem_cgroup_hierarchy_read,
5869 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005870 {
5871 .name = "swappiness",
5872 .read_u64 = mem_cgroup_swappiness_read,
5873 .write_u64 = mem_cgroup_swappiness_write,
5874 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005875 {
5876 .name = "move_charge_at_immigrate",
5877 .read_u64 = mem_cgroup_move_charge_read,
5878 .write_u64 = mem_cgroup_move_charge_write,
5879 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005880 {
5881 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005882 .read_map = mem_cgroup_oom_control_read,
5883 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005884 .register_event = mem_cgroup_oom_register_event,
5885 .unregister_event = mem_cgroup_oom_unregister_event,
5886 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5887 },
Ying Han406eb0c2011-05-26 16:25:37 -07005888#ifdef CONFIG_NUMA
5889 {
5890 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005891 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005892 },
5893#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005894#ifdef CONFIG_MEMCG_KMEM
5895 {
5896 .name = "kmem.limit_in_bytes",
5897 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5898 .write_string = mem_cgroup_write,
5899 .read = mem_cgroup_read,
5900 },
5901 {
5902 .name = "kmem.usage_in_bytes",
5903 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5904 .read = mem_cgroup_read,
5905 },
5906 {
5907 .name = "kmem.failcnt",
5908 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5909 .trigger = mem_cgroup_reset,
5910 .read = mem_cgroup_read,
5911 },
5912 {
5913 .name = "kmem.max_usage_in_bytes",
5914 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5915 .trigger = mem_cgroup_reset,
5916 .read = mem_cgroup_read,
5917 },
Glauber Costa749c5412012-12-18 14:23:01 -08005918#ifdef CONFIG_SLABINFO
5919 {
5920 .name = "kmem.slabinfo",
5921 .read_seq_string = mem_cgroup_slabinfo_read,
5922 },
5923#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005924#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005925 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005926};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005927
Michal Hocko2d110852013-02-22 16:34:43 -08005928#ifdef CONFIG_MEMCG_SWAP
5929static struct cftype memsw_cgroup_files[] = {
5930 {
5931 .name = "memsw.usage_in_bytes",
5932 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5933 .read = mem_cgroup_read,
5934 .register_event = mem_cgroup_usage_register_event,
5935 .unregister_event = mem_cgroup_usage_unregister_event,
5936 },
5937 {
5938 .name = "memsw.max_usage_in_bytes",
5939 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5940 .trigger = mem_cgroup_reset,
5941 .read = mem_cgroup_read,
5942 },
5943 {
5944 .name = "memsw.limit_in_bytes",
5945 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5946 .write_string = mem_cgroup_write,
5947 .read = mem_cgroup_read,
5948 },
5949 {
5950 .name = "memsw.failcnt",
5951 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5952 .trigger = mem_cgroup_reset,
5953 .read = mem_cgroup_read,
5954 },
5955 { }, /* terminate */
5956};
5957#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005958static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005959{
5960 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005961 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005962 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005963 /*
5964 * This routine is called against possible nodes.
5965 * But it's BUG to call kmalloc() against offline node.
5966 *
5967 * TODO: this routine can waste much memory for nodes which will
5968 * never be onlined. It's better to use memory hotplug callback
5969 * function.
5970 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005971 if (!node_state(node, N_NORMAL_MEMORY))
5972 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005973 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005974 if (!pn)
5975 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005976
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005977 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Michal Hocko542f85f2013-04-29 15:07:15 -07005978 int prio;
5979
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005980 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005981 lruvec_init(&mz->lruvec);
Michal Hocko542f85f2013-04-29 15:07:15 -07005982 for (prio = 0; prio < DEF_PRIORITY + 1; prio++)
5983 spin_lock_init(&mz->reclaim_iter[prio].iter_lock);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005984 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005985 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005986 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005987 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005988 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005989 return 0;
5990}
5991
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005992static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005993{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005994 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005995}
5996
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005997static struct mem_cgroup *mem_cgroup_alloc(void)
5998{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005999 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006000 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006001
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006002 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08006003 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006004 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006005 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006006 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006007
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006008 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07006009 return NULL;
6010
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006011 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6012 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006013 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006014 spin_lock_init(&memcg->pcp_counter_lock);
6015 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006016
6017out_free:
6018 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006019 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006020 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006021 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006022 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006023}
6024
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006025/*
Glauber Costac8b2a362012-12-18 14:22:13 -08006026 * At destroying mem_cgroup, references from swap_cgroup can remain.
6027 * (scanning all at force_empty is too costly...)
6028 *
6029 * Instead of clearing all references at force_empty, we remember
6030 * the number of reference from swap_cgroup and free mem_cgroup when
6031 * it goes down to 0.
6032 *
6033 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07006034 */
Glauber Costac8b2a362012-12-18 14:22:13 -08006035
6036static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006037{
Glauber Costac8b2a362012-12-18 14:22:13 -08006038 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006039 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006040
Glauber Costac8b2a362012-12-18 14:22:13 -08006041 mem_cgroup_remove_from_trees(memcg);
6042 free_css_id(&mem_cgroup_subsys, &memcg->css);
6043
6044 for_each_node(node)
6045 free_mem_cgroup_per_zone_info(memcg, node);
6046
6047 free_percpu(memcg->stat);
6048
Glauber Costa3f134612012-05-29 15:07:11 -07006049 /*
6050 * We need to make sure that (at least for now), the jump label
6051 * destruction code runs outside of the cgroup lock. This is because
6052 * get_online_cpus(), which is called from the static_branch update,
6053 * can't be called inside the cgroup_lock. cpusets are the ones
6054 * enforcing this dependency, so if they ever change, we might as well.
6055 *
6056 * schedule_work() will guarantee this happens. Be careful if you need
6057 * to move this code around, and make sure it is outside
6058 * the cgroup_lock.
6059 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006060 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006061 if (size < PAGE_SIZE)
6062 kfree(memcg);
6063 else
6064 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006065}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006066
Glauber Costac8b2a362012-12-18 14:22:13 -08006067
6068/*
6069 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6070 * but in process context. The work_freeing structure is overlaid
6071 * on the rcu_freeing structure, which itself is overlaid on memsw.
6072 */
6073static void free_work(struct work_struct *work)
6074{
6075 struct mem_cgroup *memcg;
6076
6077 memcg = container_of(work, struct mem_cgroup, work_freeing);
6078 __mem_cgroup_free(memcg);
6079}
6080
Glauber Costa3afe36b2012-05-29 15:07:10 -07006081static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006082{
6083 struct mem_cgroup *memcg;
6084
6085 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006086 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006087 schedule_work(&memcg->work_freeing);
6088}
6089
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006090static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006091{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006092 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006093}
6094
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006095static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006096{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006097 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6098 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006099 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006100 if (parent)
6101 mem_cgroup_put(parent);
6102 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006103}
6104
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006105static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006106{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006107 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006108}
6109
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006110/*
6111 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6112 */
Glauber Costae1aab162011-12-11 21:47:03 +00006113struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006114{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006115 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006116 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006117 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006118}
Glauber Costae1aab162011-12-11 21:47:03 +00006119EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006120
Michal Hocko8787a1d2013-02-22 16:35:39 -08006121static void __init mem_cgroup_soft_limit_tree_init(void)
Balbir Singhf64c3f52009-09-23 15:56:37 -07006122{
6123 struct mem_cgroup_tree_per_node *rtpn;
6124 struct mem_cgroup_tree_per_zone *rtpz;
6125 int tmp, node, zone;
6126
Bob Liu3ed28fa2012-01-12 17:19:04 -08006127 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006128 tmp = node;
6129 if (!node_state(node, N_NORMAL_MEMORY))
6130 tmp = -1;
6131 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
Michal Hocko8787a1d2013-02-22 16:35:39 -08006132 BUG_ON(!rtpn);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006133
6134 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6135
6136 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6137 rtpz = &rtpn->rb_tree_per_zone[zone];
6138 rtpz->rb_root = RB_ROOT;
6139 spin_lock_init(&rtpz->lock);
6140 }
6141 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07006142}
6143
Li Zefan0eb253e2009-01-15 13:51:25 -08006144static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006145mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006146{
Glauber Costad142e3e2013-02-22 16:34:52 -08006147 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006148 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006149 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006150
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006151 memcg = mem_cgroup_alloc();
6152 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006153 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006154
Bob Liu3ed28fa2012-01-12 17:19:04 -08006155 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006156 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006157 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006158
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006159 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006160 if (cont->parent == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006161 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006162 res_counter_init(&memcg->res, NULL);
6163 res_counter_init(&memcg->memsw, NULL);
6164 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006165 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006166
Glauber Costad142e3e2013-02-22 16:34:52 -08006167 memcg->last_scanned_node = MAX_NUMNODES;
6168 INIT_LIST_HEAD(&memcg->oom_notify);
6169 atomic_set(&memcg->refcnt, 1);
6170 memcg->move_charge_at_immigrate = 0;
6171 mutex_init(&memcg->thresholds_lock);
6172 spin_lock_init(&memcg->move_lock);
6173
6174 return &memcg->css;
6175
6176free_out:
6177 __mem_cgroup_free(memcg);
6178 return ERR_PTR(error);
6179}
6180
6181static int
6182mem_cgroup_css_online(struct cgroup *cont)
6183{
6184 struct mem_cgroup *memcg, *parent;
6185 int error = 0;
6186
6187 if (!cont->parent)
6188 return 0;
6189
Glauber Costa09998212013-02-22 16:34:55 -08006190 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006191 memcg = mem_cgroup_from_cont(cont);
6192 parent = mem_cgroup_from_cont(cont->parent);
6193
6194 memcg->use_hierarchy = parent->use_hierarchy;
6195 memcg->oom_kill_disable = parent->oom_kill_disable;
6196 memcg->swappiness = mem_cgroup_swappiness(parent);
6197
6198 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006199 res_counter_init(&memcg->res, &parent->res);
6200 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006201 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006202
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006203 /*
6204 * We increment refcnt of the parent to ensure that we can
6205 * safely access it on res_counter_charge/uncharge.
6206 * This refcnt will be decremented when freeing this
6207 * mem_cgroup(see mem_cgroup_put).
6208 */
6209 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006210 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006211 res_counter_init(&memcg->res, NULL);
6212 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006213 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006214 /*
6215 * Deeper hierachy with use_hierarchy == false doesn't make
6216 * much sense so let cgroup subsystem know about this
6217 * unfortunate state in our controller.
6218 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006219 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006220 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006221 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006222
6223 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006224 mutex_unlock(&memcg_create_mutex);
Glauber Costacbe128e32012-04-09 19:36:34 -03006225 if (error) {
6226 /*
6227 * We call put now because our (and parent's) refcnts
6228 * are already in place. mem_cgroup_put() will internally
6229 * call __mem_cgroup_free, so return directly
6230 */
6231 mem_cgroup_put(memcg);
Glauber Costae4715f02013-02-22 16:34:57 -08006232 if (parent->use_hierarchy)
6233 mem_cgroup_put(parent);
Glauber Costacbe128e32012-04-09 19:36:34 -03006234 }
Glauber Costad142e3e2013-02-22 16:34:52 -08006235 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006236}
6237
Tejun Heo92fb9742012-11-19 08:13:38 -08006238static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006239{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006240 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006241
Michal Hockoab5196c2012-10-26 13:37:32 +02006242 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006243 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006244}
6245
Tejun Heo92fb9742012-11-19 08:13:38 -08006246static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006247{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006248 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006249
Glauber Costa1d62e432012-04-09 19:36:33 -03006250 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006251
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006252 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006253}
6254
Daisuke Nishimura02491442010-03-10 15:22:17 -08006255#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006256/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006257#define PRECHARGE_COUNT_AT_ONCE 256
6258static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006259{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006260 int ret = 0;
6261 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006262 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006263
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006264 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006265 mc.precharge += count;
6266 /* we don't need css_get for root */
6267 return ret;
6268 }
6269 /* try to charge at once */
6270 if (count > 1) {
6271 struct res_counter *dummy;
6272 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006273 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006274 * by cgroup_lock_live_cgroup() that it is not removed and we
6275 * are still under the same cgroup_mutex. So we can postpone
6276 * css_get().
6277 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006278 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006279 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006280 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006281 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006282 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006283 goto one_by_one;
6284 }
6285 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006286 return ret;
6287 }
6288one_by_one:
6289 /* fall back to one by one charge */
6290 while (count--) {
6291 if (signal_pending(current)) {
6292 ret = -EINTR;
6293 break;
6294 }
6295 if (!batch_count--) {
6296 batch_count = PRECHARGE_COUNT_AT_ONCE;
6297 cond_resched();
6298 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006299 ret = __mem_cgroup_try_charge(NULL,
6300 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006301 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006302 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006303 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006304 mc.precharge++;
6305 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006306 return ret;
6307}
6308
6309/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006310 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006311 * @vma: the vma the pte to be checked belongs
6312 * @addr: the address corresponding to the pte to be checked
6313 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006314 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006315 *
6316 * Returns
6317 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6318 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6319 * move charge. if @target is not NULL, the page is stored in target->page
6320 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006321 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6322 * target for charge migration. if @target is not NULL, the entry is stored
6323 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006324 *
6325 * Called with pte lock held.
6326 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006327union mc_target {
6328 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006329 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006330};
6331
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006332enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006333 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006334 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006335 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006336};
6337
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006338static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6339 unsigned long addr, pte_t ptent)
6340{
6341 struct page *page = vm_normal_page(vma, addr, ptent);
6342
6343 if (!page || !page_mapped(page))
6344 return NULL;
6345 if (PageAnon(page)) {
6346 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006347 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006348 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006349 } else if (!move_file())
6350 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006351 return NULL;
6352 if (!get_page_unless_zero(page))
6353 return NULL;
6354
6355 return page;
6356}
6357
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006358#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006359static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6360 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6361{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006362 struct page *page = NULL;
6363 swp_entry_t ent = pte_to_swp_entry(ptent);
6364
6365 if (!move_anon() || non_swap_entry(ent))
6366 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006367 /*
6368 * Because lookup_swap_cache() updates some statistics counter,
6369 * we call find_get_page() with swapper_space directly.
6370 */
Shaohua Li33806f02013-02-22 16:34:37 -08006371 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006372 if (do_swap_account)
6373 entry->val = ent.val;
6374
6375 return page;
6376}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006377#else
6378static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6379 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6380{
6381 return NULL;
6382}
6383#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006384
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006385static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6386 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6387{
6388 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006389 struct address_space *mapping;
6390 pgoff_t pgoff;
6391
6392 if (!vma->vm_file) /* anonymous vma */
6393 return NULL;
6394 if (!move_file())
6395 return NULL;
6396
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006397 mapping = vma->vm_file->f_mapping;
6398 if (pte_none(ptent))
6399 pgoff = linear_page_index(vma, addr);
6400 else /* pte_file(ptent) is true */
6401 pgoff = pte_to_pgoff(ptent);
6402
6403 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006404 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006405
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006406#ifdef CONFIG_SWAP
6407 /* shmem/tmpfs may report page out on swap: account for that too. */
6408 if (radix_tree_exceptional_entry(page)) {
6409 swp_entry_t swap = radix_to_swp_entry(page);
6410 if (do_swap_account)
6411 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006412 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006413 }
6414#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006415 return page;
6416}
6417
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006418static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006419 unsigned long addr, pte_t ptent, union mc_target *target)
6420{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006421 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006422 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006423 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006424 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006425
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006426 if (pte_present(ptent))
6427 page = mc_handle_present_pte(vma, addr, ptent);
6428 else if (is_swap_pte(ptent))
6429 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006430 else if (pte_none(ptent) || pte_file(ptent))
6431 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006432
6433 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006434 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006435 if (page) {
6436 pc = lookup_page_cgroup(page);
6437 /*
6438 * Do only loose check w/o page_cgroup lock.
6439 * mem_cgroup_move_account() checks the pc is valid or not under
6440 * the lock.
6441 */
6442 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6443 ret = MC_TARGET_PAGE;
6444 if (target)
6445 target->page = page;
6446 }
6447 if (!ret || !target)
6448 put_page(page);
6449 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006450 /* There is a swap entry and a page doesn't exist or isn't charged */
6451 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006452 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006453 ret = MC_TARGET_SWAP;
6454 if (target)
6455 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006456 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006457 return ret;
6458}
6459
Naoya Horiguchi12724852012-03-21 16:34:28 -07006460#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6461/*
6462 * We don't consider swapping or file mapped pages because THP does not
6463 * support them for now.
6464 * Caller should make sure that pmd_trans_huge(pmd) is true.
6465 */
6466static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6467 unsigned long addr, pmd_t pmd, union mc_target *target)
6468{
6469 struct page *page = NULL;
6470 struct page_cgroup *pc;
6471 enum mc_target_type ret = MC_TARGET_NONE;
6472
6473 page = pmd_page(pmd);
6474 VM_BUG_ON(!page || !PageHead(page));
6475 if (!move_anon())
6476 return ret;
6477 pc = lookup_page_cgroup(page);
6478 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6479 ret = MC_TARGET_PAGE;
6480 if (target) {
6481 get_page(page);
6482 target->page = page;
6483 }
6484 }
6485 return ret;
6486}
6487#else
6488static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6489 unsigned long addr, pmd_t pmd, union mc_target *target)
6490{
6491 return MC_TARGET_NONE;
6492}
6493#endif
6494
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006495static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6496 unsigned long addr, unsigned long end,
6497 struct mm_walk *walk)
6498{
6499 struct vm_area_struct *vma = walk->private;
6500 pte_t *pte;
6501 spinlock_t *ptl;
6502
Naoya Horiguchi12724852012-03-21 16:34:28 -07006503 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6504 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6505 mc.precharge += HPAGE_PMD_NR;
6506 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006507 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006508 }
Dave Hansen03319322011-03-22 16:32:56 -07006509
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006510 if (pmd_trans_unstable(pmd))
6511 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006512 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6513 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006514 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006515 mc.precharge++; /* increment precharge temporarily */
6516 pte_unmap_unlock(pte - 1, ptl);
6517 cond_resched();
6518
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006519 return 0;
6520}
6521
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006522static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6523{
6524 unsigned long precharge;
6525 struct vm_area_struct *vma;
6526
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006527 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006528 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6529 struct mm_walk mem_cgroup_count_precharge_walk = {
6530 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6531 .mm = mm,
6532 .private = vma,
6533 };
6534 if (is_vm_hugetlb_page(vma))
6535 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006536 walk_page_range(vma->vm_start, vma->vm_end,
6537 &mem_cgroup_count_precharge_walk);
6538 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006539 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006540
6541 precharge = mc.precharge;
6542 mc.precharge = 0;
6543
6544 return precharge;
6545}
6546
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006547static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6548{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006549 unsigned long precharge = mem_cgroup_count_precharge(mm);
6550
6551 VM_BUG_ON(mc.moving_task);
6552 mc.moving_task = current;
6553 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006554}
6555
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006556/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6557static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006558{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006559 struct mem_cgroup *from = mc.from;
6560 struct mem_cgroup *to = mc.to;
6561
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006562 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006563 if (mc.precharge) {
6564 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6565 mc.precharge = 0;
6566 }
6567 /*
6568 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6569 * we must uncharge here.
6570 */
6571 if (mc.moved_charge) {
6572 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6573 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006574 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006575 /* we must fixup refcnts and charges */
6576 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006577 /* uncharge swap account from the old cgroup */
6578 if (!mem_cgroup_is_root(mc.from))
6579 res_counter_uncharge(&mc.from->memsw,
6580 PAGE_SIZE * mc.moved_swap);
6581 __mem_cgroup_put(mc.from, mc.moved_swap);
6582
6583 if (!mem_cgroup_is_root(mc.to)) {
6584 /*
6585 * we charged both to->res and to->memsw, so we should
6586 * uncharge to->res.
6587 */
6588 res_counter_uncharge(&mc.to->res,
6589 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006590 }
6591 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006592 mc.moved_swap = 0;
6593 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006594 memcg_oom_recover(from);
6595 memcg_oom_recover(to);
6596 wake_up_all(&mc.waitq);
6597}
6598
6599static void mem_cgroup_clear_mc(void)
6600{
6601 struct mem_cgroup *from = mc.from;
6602
6603 /*
6604 * we must clear moving_task before waking up waiters at the end of
6605 * task migration.
6606 */
6607 mc.moving_task = NULL;
6608 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006609 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006610 mc.from = NULL;
6611 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006612 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006613 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006614}
6615
Li Zefan761b3ef2012-01-31 13:47:36 +08006616static int mem_cgroup_can_attach(struct cgroup *cgroup,
6617 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006618{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006619 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006620 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006621 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006622 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006623
Glauber Costaee5e8472013-02-22 16:34:50 -08006624 /*
6625 * We are now commited to this value whatever it is. Changes in this
6626 * tunable will only affect upcoming migrations, not the current one.
6627 * So we need to save it, and keep it going.
6628 */
6629 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6630 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006631 struct mm_struct *mm;
6632 struct mem_cgroup *from = mem_cgroup_from_task(p);
6633
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006634 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006635
6636 mm = get_task_mm(p);
6637 if (!mm)
6638 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006639 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006640 if (mm->owner == p) {
6641 VM_BUG_ON(mc.from);
6642 VM_BUG_ON(mc.to);
6643 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006644 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006645 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006646 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006647 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006648 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006649 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006650 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006651 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006652 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006653
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006654 ret = mem_cgroup_precharge_mc(mm);
6655 if (ret)
6656 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006657 }
6658 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006659 }
6660 return ret;
6661}
6662
Li Zefan761b3ef2012-01-31 13:47:36 +08006663static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6664 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006665{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006666 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006667}
6668
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006669static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6670 unsigned long addr, unsigned long end,
6671 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006672{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006673 int ret = 0;
6674 struct vm_area_struct *vma = walk->private;
6675 pte_t *pte;
6676 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006677 enum mc_target_type target_type;
6678 union mc_target target;
6679 struct page *page;
6680 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006681
Naoya Horiguchi12724852012-03-21 16:34:28 -07006682 /*
6683 * We don't take compound_lock() here but no race with splitting thp
6684 * happens because:
6685 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6686 * under splitting, which means there's no concurrent thp split,
6687 * - if another thread runs into split_huge_page() just after we
6688 * entered this if-block, the thread must wait for page table lock
6689 * to be unlocked in __split_huge_page_splitting(), where the main
6690 * part of thp split is not executed yet.
6691 */
6692 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006693 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006694 spin_unlock(&vma->vm_mm->page_table_lock);
6695 return 0;
6696 }
6697 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6698 if (target_type == MC_TARGET_PAGE) {
6699 page = target.page;
6700 if (!isolate_lru_page(page)) {
6701 pc = lookup_page_cgroup(page);
6702 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006703 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006704 mc.precharge -= HPAGE_PMD_NR;
6705 mc.moved_charge += HPAGE_PMD_NR;
6706 }
6707 putback_lru_page(page);
6708 }
6709 put_page(page);
6710 }
6711 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006712 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006713 }
6714
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006715 if (pmd_trans_unstable(pmd))
6716 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006717retry:
6718 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6719 for (; addr != end; addr += PAGE_SIZE) {
6720 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006721 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006722
6723 if (!mc.precharge)
6724 break;
6725
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006726 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006727 case MC_TARGET_PAGE:
6728 page = target.page;
6729 if (isolate_lru_page(page))
6730 goto put;
6731 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006732 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006733 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006734 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006735 /* we uncharge from mc.from later. */
6736 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006737 }
6738 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006739put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006740 put_page(page);
6741 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006742 case MC_TARGET_SWAP:
6743 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006744 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006745 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006746 /* we fixup refcnts and charges later. */
6747 mc.moved_swap++;
6748 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006749 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006750 default:
6751 break;
6752 }
6753 }
6754 pte_unmap_unlock(pte - 1, ptl);
6755 cond_resched();
6756
6757 if (addr != end) {
6758 /*
6759 * We have consumed all precharges we got in can_attach().
6760 * We try charge one by one, but don't do any additional
6761 * charges to mc.to if we have failed in charge once in attach()
6762 * phase.
6763 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006764 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006765 if (!ret)
6766 goto retry;
6767 }
6768
6769 return ret;
6770}
6771
6772static void mem_cgroup_move_charge(struct mm_struct *mm)
6773{
6774 struct vm_area_struct *vma;
6775
6776 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006777retry:
6778 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6779 /*
6780 * Someone who are holding the mmap_sem might be waiting in
6781 * waitq. So we cancel all extra charges, wake up all waiters,
6782 * and retry. Because we cancel precharges, we might not be able
6783 * to move enough charges, but moving charge is a best-effort
6784 * feature anyway, so it wouldn't be a big problem.
6785 */
6786 __mem_cgroup_clear_mc();
6787 cond_resched();
6788 goto retry;
6789 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006790 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6791 int ret;
6792 struct mm_walk mem_cgroup_move_charge_walk = {
6793 .pmd_entry = mem_cgroup_move_charge_pte_range,
6794 .mm = mm,
6795 .private = vma,
6796 };
6797 if (is_vm_hugetlb_page(vma))
6798 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006799 ret = walk_page_range(vma->vm_start, vma->vm_end,
6800 &mem_cgroup_move_charge_walk);
6801 if (ret)
6802 /*
6803 * means we have consumed all precharges and failed in
6804 * doing additional charge. Just abandon here.
6805 */
6806 break;
6807 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006808 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006809}
6810
Li Zefan761b3ef2012-01-31 13:47:36 +08006811static void mem_cgroup_move_task(struct cgroup *cont,
6812 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006813{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006814 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006815 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006816
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006817 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006818 if (mc.to)
6819 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006820 mmput(mm);
6821 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006822 if (mc.to)
6823 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006824}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006825#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006826static int mem_cgroup_can_attach(struct cgroup *cgroup,
6827 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006828{
6829 return 0;
6830}
Li Zefan761b3ef2012-01-31 13:47:36 +08006831static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6832 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006833{
6834}
Li Zefan761b3ef2012-01-31 13:47:36 +08006835static void mem_cgroup_move_task(struct cgroup *cont,
6836 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006837{
6838}
6839#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006840
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006841struct cgroup_subsys mem_cgroup_subsys = {
6842 .name = "memory",
6843 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006844 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006845 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006846 .css_offline = mem_cgroup_css_offline,
6847 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006848 .can_attach = mem_cgroup_can_attach,
6849 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006850 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006851 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006852 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006853 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006854};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006855
Andrew Mortonc255a452012-07-31 16:43:02 -07006856#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006857static int __init enable_swap_account(char *s)
6858{
6859 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006860 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006861 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006862 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006863 really_do_swap_account = 0;
6864 return 1;
6865}
Michal Hockoa2c89902011-05-24 17:12:50 -07006866__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006867
Michal Hocko2d110852013-02-22 16:34:43 -08006868static void __init memsw_file_init(void)
6869{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006870 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006871}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006872
6873static void __init enable_swap_cgroup(void)
6874{
6875 if (!mem_cgroup_disabled() && really_do_swap_account) {
6876 do_swap_account = 1;
6877 memsw_file_init();
6878 }
6879}
6880
Michal Hocko2d110852013-02-22 16:34:43 -08006881#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006882static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006883{
6884}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006885#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006886
6887/*
Michal Hocko10813122013-02-22 16:35:41 -08006888 * subsys_initcall() for memory controller.
6889 *
6890 * Some parts like hotcpu_notifier() have to be initialized from this context
6891 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6892 * everything that doesn't depend on a specific mem_cgroup structure should
6893 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006894 */
6895static int __init mem_cgroup_init(void)
6896{
6897 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006898 enable_swap_cgroup();
Michal Hocko8787a1d2013-02-22 16:35:39 -08006899 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08006900 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006901 return 0;
6902}
6903subsys_initcall(mem_cgroup_init);