blob: df87bdd4d692694d8c643d0511a9592f722013b5 [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 Hiroyuki453a9bf32011-07-08 15:39:43 -0700140 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700141 MEM_CGROUP_NTARGETS,
142};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700143#define THRESHOLDS_EVENTS_TARGET 128
144#define SOFTLIMIT_EVENTS_TARGET 1024
145#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700146
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800147struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700148 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700149 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700150 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700151 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800152};
153
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800154struct mem_cgroup_reclaim_iter {
155 /* css_id of the last scanned hierarchy member */
156 int position;
157 /* scan generation, increased every round-trip */
158 unsigned int generation;
159};
160
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800161/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800162 * per-zone information in memory controller.
163 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800165 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700166 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800167
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800168 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
169
Balbir Singhf64c3f52009-09-23 15:56:37 -0700170 struct rb_node tree_node; /* RB tree node */
171 unsigned long long usage_in_excess;/* Set to the value by which */
172 /* the soft limit is exceeded*/
173 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700174 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700175 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800176};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800177
178struct mem_cgroup_per_node {
179 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
180};
181
182struct mem_cgroup_lru_info {
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800183 struct mem_cgroup_per_node *nodeinfo[0];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800184};
185
186/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700187 * Cgroups above their limits are maintained in a RB-Tree, independent of
188 * their hierarchy representation
189 */
190
191struct mem_cgroup_tree_per_zone {
192 struct rb_root rb_root;
193 spinlock_t lock;
194};
195
196struct mem_cgroup_tree_per_node {
197 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
198};
199
200struct mem_cgroup_tree {
201 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
202};
203
204static struct mem_cgroup_tree soft_limit_tree __read_mostly;
205
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206struct mem_cgroup_threshold {
207 struct eventfd_ctx *eventfd;
208 u64 threshold;
209};
210
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700211/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800212struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700213 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700214 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215 /* Size of entries[] */
216 unsigned int size;
217 /* Array of thresholds */
218 struct mem_cgroup_threshold entries[0];
219};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700220
221struct mem_cgroup_thresholds {
222 /* Primary thresholds array */
223 struct mem_cgroup_threshold_ary *primary;
224 /*
225 * Spare threshold array.
226 * This is needed to make mem_cgroup_unregister_event() "never fail".
227 * It must be able to store at least primary->size - 1 entries.
228 */
229 struct mem_cgroup_threshold_ary *spare;
230};
231
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700232/* for OOM */
233struct mem_cgroup_eventfd_list {
234 struct list_head list;
235 struct eventfd_ctx *eventfd;
236};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800237
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700238static void mem_cgroup_threshold(struct mem_cgroup *memcg);
239static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800240
Balbir Singhf64c3f52009-09-23 15:56:37 -0700241/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800242 * The memory controller data structure. The memory controller controls both
243 * page cache and RSS per cgroup. We would eventually like to provide
244 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
245 * to help the administrator determine what knobs to tune.
246 *
247 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800248 * we hit the water mark. May be even add a low water mark, such that
249 * no reclaim occurs from a cgroup at it's low water mark, this is
250 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800251 */
252struct mem_cgroup {
253 struct cgroup_subsys_state css;
254 /*
255 * the counter to account for memory usage
256 */
257 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700258
259 union {
260 /*
261 * the counter to account for mem+swap usage.
262 */
263 struct res_counter memsw;
264
265 /*
266 * rcu_freeing is used only when freeing struct mem_cgroup,
267 * so put it into a union to avoid wasting more memory.
268 * It must be disjoint from the css field. It could be
269 * in a union with the res field, but res plays a much
270 * larger part in mem_cgroup life than memsw, and might
271 * be of interest, even at time of free, when debugging.
272 * So share rcu_head with the less interesting memsw.
273 */
274 struct rcu_head rcu_freeing;
275 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700276 * We also need some space for a worker in deferred freeing.
277 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700278 */
279 struct work_struct work_freeing;
280 };
281
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800282 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800283 * the counter to account for kernel memory usage.
284 */
285 struct res_counter kmem;
286 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800287 * Should the accounting and control be hierarchical, per subtree?
288 */
289 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800290 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700291
292 bool oom_lock;
293 atomic_t under_oom;
294
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800295 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800296
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700297 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700298 /* OOM-Killer disable */
299 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800300
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700301 /* set when res.limit == memsw.limit */
302 bool memsw_is_minimum;
303
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800304 /* protect arrays of thresholds */
305 struct mutex thresholds_lock;
306
307 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700308 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700309
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800310 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700311 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700312
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700313 /* For oom notifier event fd */
314 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700315
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800316 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800317 * Should we move charges of a task when a task is moved into this
318 * mem_cgroup ? And what type of charges should we move ?
319 */
320 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800321 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700322 * set > 0 if pages under this cgroup are moving to other cgroup.
323 */
324 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700325 /* taken only while moving_account > 0 */
326 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700327 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800328 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800329 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700330 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700331 /*
332 * used when a cpu is offlined or other synchronizations
333 * See mem_cgroup_read_stat().
334 */
335 struct mem_cgroup_stat_cpu nocpu_base;
336 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000337
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700338#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000339 struct tcp_memcontrol tcp_mem;
340#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800341#if defined(CONFIG_MEMCG_KMEM)
342 /* analogous to slab_common's slab_caches list. per-memcg */
343 struct list_head memcg_slab_caches;
344 /* Not a spinlock, we can take a lot of time walking the list */
345 struct mutex slab_caches_mutex;
346 /* Index in the kmem_cache->memcg_params->memcg_caches array */
347 int kmemcg_id;
348#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800349
350 int last_scanned_node;
351#if MAX_NUMNODES > 1
352 nodemask_t scan_nodes;
353 atomic_t numainfo_events;
354 atomic_t numainfo_updating;
355#endif
356 /*
357 * Per cgroup active and inactive list, similar to the
358 * per zone LRU lists.
359 *
360 * WARNING: This has to be the last element of the struct. Don't
361 * add new fields after this point.
362 */
363 struct mem_cgroup_lru_info info;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800364};
365
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800366static size_t memcg_size(void)
367{
368 return sizeof(struct mem_cgroup) +
369 nr_node_ids * sizeof(struct mem_cgroup_per_node);
370}
371
Glauber Costa510fc4e2012-12-18 14:21:47 -0800372/* internal only representation about the status of kmem accounting. */
373enum {
374 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800375 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800376 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800377};
378
Glauber Costaa8964b92012-12-18 14:22:09 -0800379/* We account when limit is on, but only after call sites are patched */
380#define KMEM_ACCOUNTED_MASK \
381 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800382
383#ifdef CONFIG_MEMCG_KMEM
384static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
385{
386 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
387}
Glauber Costa7de37682012-12-18 14:22:07 -0800388
389static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
390{
391 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
392}
393
Glauber Costaa8964b92012-12-18 14:22:09 -0800394static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
395{
396 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
397}
398
Glauber Costa55007d82012-12-18 14:22:38 -0800399static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
400{
401 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
402}
403
Glauber Costa7de37682012-12-18 14:22:07 -0800404static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
405{
406 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
407 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
408}
409
410static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
411{
412 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
413 &memcg->kmem_account_flags);
414}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800415#endif
416
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800417/* Stuffs for move charges at task migration. */
418/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800419 * Types of charges to be moved. "move_charge_at_immitgrate" and
420 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800421 */
422enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800423 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700424 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800425 NR_MOVE_TYPE,
426};
427
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800428/* "mc" and its members are protected by cgroup_mutex */
429static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800430 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800431 struct mem_cgroup *from;
432 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800433 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800434 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800435 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800436 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800437 struct task_struct *moving_task; /* a task moving charges */
438 wait_queue_head_t waitq; /* a waitq for other context */
439} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700440 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800441 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
442};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800443
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700444static bool move_anon(void)
445{
Glauber Costaee5e8472013-02-22 16:34:50 -0800446 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700447}
448
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700449static bool move_file(void)
450{
Glauber Costaee5e8472013-02-22 16:34:50 -0800451 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700452}
453
Balbir Singh4e416952009-09-23 15:56:39 -0700454/*
455 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
456 * limit reclaim to prevent infinite loops, if they ever occur.
457 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700458#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
459#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700460
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800461enum charge_type {
462 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700463 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800464 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -0700465 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700466 NR_CHARGE_TYPE,
467};
468
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800469/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800470enum res_type {
471 _MEM,
472 _MEMSWAP,
473 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800474 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800475};
476
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700477#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
478#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800479#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700480/* Used for OOM nofiier */
481#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800482
Balbir Singh75822b42009-09-23 15:56:38 -0700483/*
484 * Reclaim flags for mem_cgroup_hierarchical_reclaim
485 */
486#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
487#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
488#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
489#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
490
Glauber Costa09998212013-02-22 16:34:55 -0800491/*
492 * The memcg_create_mutex will be held whenever a new cgroup is created.
493 * As a consequence, any change that needs to protect against new child cgroups
494 * appearing has to hold it as well.
495 */
496static DEFINE_MUTEX(memcg_create_mutex);
497
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700498static void mem_cgroup_get(struct mem_cgroup *memcg);
499static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000500
Wanpeng Lib2145142012-07-31 16:46:01 -0700501static inline
502struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
503{
504 return container_of(s, struct mem_cgroup, css);
505}
506
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700507static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
508{
509 return (memcg == root_mem_cgroup);
510}
511
Glauber Costae1aab162011-12-11 21:47:03 +0000512/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700513#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000514
Glauber Costae1aab162011-12-11 21:47:03 +0000515void sock_update_memcg(struct sock *sk)
516{
Glauber Costa376be5f2012-01-20 04:57:14 +0000517 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000518 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700519 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000520
521 BUG_ON(!sk->sk_prot->proto_cgroup);
522
Glauber Costaf3f511e2012-01-05 20:16:39 +0000523 /* Socket cloning can throw us here with sk_cgrp already
524 * filled. It won't however, necessarily happen from
525 * process context. So the test for root memcg given
526 * the current task's memcg won't help us in this case.
527 *
528 * Respecting the original socket's memcg is a better
529 * decision in this case.
530 */
531 if (sk->sk_cgrp) {
532 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
533 mem_cgroup_get(sk->sk_cgrp->memcg);
534 return;
535 }
536
Glauber Costae1aab162011-12-11 21:47:03 +0000537 rcu_read_lock();
538 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700539 cg_proto = sk->sk_prot->proto_cgroup(memcg);
540 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000541 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700542 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000543 }
544 rcu_read_unlock();
545 }
546}
547EXPORT_SYMBOL(sock_update_memcg);
548
549void sock_release_memcg(struct sock *sk)
550{
Glauber Costa376be5f2012-01-20 04:57:14 +0000551 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000552 struct mem_cgroup *memcg;
553 WARN_ON(!sk->sk_cgrp->memcg);
554 memcg = sk->sk_cgrp->memcg;
555 mem_cgroup_put(memcg);
556 }
557}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000558
559struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
560{
561 if (!memcg || mem_cgroup_is_root(memcg))
562 return NULL;
563
564 return &memcg->tcp_mem.cg_proto;
565}
566EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000567
Glauber Costa3f134612012-05-29 15:07:11 -0700568static void disarm_sock_keys(struct mem_cgroup *memcg)
569{
570 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
571 return;
572 static_key_slow_dec(&memcg_socket_limit_enabled);
573}
574#else
575static void disarm_sock_keys(struct mem_cgroup *memcg)
576{
577}
578#endif
579
Glauber Costaa8964b92012-12-18 14:22:09 -0800580#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800581/*
582 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
583 * There are two main reasons for not using the css_id for this:
584 * 1) this works better in sparse environments, where we have a lot of memcgs,
585 * but only a few kmem-limited. Or also, if we have, for instance, 200
586 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
587 * 200 entry array for that.
588 *
589 * 2) In order not to violate the cgroup API, we would like to do all memory
590 * allocation in ->create(). At that point, we haven't yet allocated the
591 * css_id. Having a separate index prevents us from messing with the cgroup
592 * core for this
593 *
594 * The current size of the caches array is stored in
595 * memcg_limited_groups_array_size. It will double each time we have to
596 * increase it.
597 */
598static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800599int memcg_limited_groups_array_size;
600
Glauber Costa55007d82012-12-18 14:22:38 -0800601/*
602 * MIN_SIZE is different than 1, because we would like to avoid going through
603 * the alloc/free process all the time. In a small machine, 4 kmem-limited
604 * cgroups is a reasonable guess. In the future, it could be a parameter or
605 * tunable, but that is strictly not necessary.
606 *
607 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
608 * this constant directly from cgroup, but it is understandable that this is
609 * better kept as an internal representation in cgroup.c. In any case, the
610 * css_id space is not getting any smaller, and we don't have to necessarily
611 * increase ours as well if it increases.
612 */
613#define MEMCG_CACHES_MIN_SIZE 4
614#define MEMCG_CACHES_MAX_SIZE 65535
615
Glauber Costad7f25f82012-12-18 14:22:40 -0800616/*
617 * A lot of the calls to the cache allocation functions are expected to be
618 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
619 * conditional to this static branch, we'll have to allow modules that does
620 * kmem_cache_alloc and the such to see this symbol as well
621 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800622struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800623EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800624
625static void disarm_kmem_keys(struct mem_cgroup *memcg)
626{
Glauber Costa55007d82012-12-18 14:22:38 -0800627 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800628 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800629 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
630 }
Glauber Costabea207c2012-12-18 14:22:11 -0800631 /*
632 * This check can't live in kmem destruction function,
633 * since the charges will outlive the cgroup
634 */
635 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800636}
637#else
638static void disarm_kmem_keys(struct mem_cgroup *memcg)
639{
640}
641#endif /* CONFIG_MEMCG_KMEM */
642
643static void disarm_static_keys(struct mem_cgroup *memcg)
644{
645 disarm_sock_keys(memcg);
646 disarm_kmem_keys(memcg);
647}
648
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700649static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800650
Balbir Singhf64c3f52009-09-23 15:56:37 -0700651static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800654 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700655 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700656}
657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700658struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100659{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700660 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100661}
662
Balbir Singhf64c3f52009-09-23 15:56:37 -0700663static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700664page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700665{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700666 int nid = page_to_nid(page);
667 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700668
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700669 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700670}
671
672static struct mem_cgroup_tree_per_zone *
673soft_limit_tree_node_zone(int nid, int zid)
674{
675 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
676}
677
678static struct mem_cgroup_tree_per_zone *
679soft_limit_tree_from_page(struct page *page)
680{
681 int nid = page_to_nid(page);
682 int zid = page_zonenum(page);
683
684 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
685}
686
687static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700688__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700689 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700690 struct mem_cgroup_tree_per_zone *mctz,
691 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700692{
693 struct rb_node **p = &mctz->rb_root.rb_node;
694 struct rb_node *parent = NULL;
695 struct mem_cgroup_per_zone *mz_node;
696
697 if (mz->on_tree)
698 return;
699
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700700 mz->usage_in_excess = new_usage_in_excess;
701 if (!mz->usage_in_excess)
702 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700703 while (*p) {
704 parent = *p;
705 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
706 tree_node);
707 if (mz->usage_in_excess < mz_node->usage_in_excess)
708 p = &(*p)->rb_left;
709 /*
710 * We can't avoid mem cgroups that are over their soft
711 * limit by the same amount
712 */
713 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
714 p = &(*p)->rb_right;
715 }
716 rb_link_node(&mz->tree_node, parent, p);
717 rb_insert_color(&mz->tree_node, &mctz->rb_root);
718 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700719}
720
721static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700723 struct mem_cgroup_per_zone *mz,
724 struct mem_cgroup_tree_per_zone *mctz)
725{
726 if (!mz->on_tree)
727 return;
728 rb_erase(&mz->tree_node, &mctz->rb_root);
729 mz->on_tree = false;
730}
731
732static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700733mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700734 struct mem_cgroup_per_zone *mz,
735 struct mem_cgroup_tree_per_zone *mctz)
736{
737 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700738 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700739 spin_unlock(&mctz->lock);
740}
741
Balbir Singhf64c3f52009-09-23 15:56:37 -0700742
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700743static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700744{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700745 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700746 struct mem_cgroup_per_zone *mz;
747 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700748 int nid = page_to_nid(page);
749 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700750 mctz = soft_limit_tree_from_page(page);
751
752 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700753 * Necessary to update all ancestors when hierarchy is used.
754 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700755 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700756 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
757 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
758 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700759 /*
760 * We have to update the tree if mz is on RB-tree or
761 * mem is over its softlimit.
762 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700763 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700764 spin_lock(&mctz->lock);
765 /* if on-tree, remove it */
766 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700767 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700768 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700769 * Insert again. mz->usage_in_excess will be updated.
770 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700771 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700772 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700773 spin_unlock(&mctz->lock);
774 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700775 }
776}
777
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700778static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700779{
780 int node, zone;
781 struct mem_cgroup_per_zone *mz;
782 struct mem_cgroup_tree_per_zone *mctz;
783
Bob Liu3ed28fa2012-01-12 17:19:04 -0800784 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700785 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700786 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700787 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700789 }
790 }
791}
792
Balbir Singh4e416952009-09-23 15:56:39 -0700793static struct mem_cgroup_per_zone *
794__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
795{
796 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700797 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700798
799retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700800 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700801 rightmost = rb_last(&mctz->rb_root);
802 if (!rightmost)
803 goto done; /* Nothing to reclaim from */
804
805 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
806 /*
807 * Remove the node now but someone else can add it back,
808 * we will to add it back at the end of reclaim to its correct
809 * position in the tree.
810 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700811 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
812 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
813 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700814 goto retry;
815done:
816 return mz;
817}
818
819static struct mem_cgroup_per_zone *
820mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
821{
822 struct mem_cgroup_per_zone *mz;
823
824 spin_lock(&mctz->lock);
825 mz = __mem_cgroup_largest_soft_limit_node(mctz);
826 spin_unlock(&mctz->lock);
827 return mz;
828}
829
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700830/*
831 * Implementation Note: reading percpu statistics for memcg.
832 *
833 * Both of vmstat[] and percpu_counter has threshold and do periodic
834 * synchronization to implement "quick" read. There are trade-off between
835 * reading cost and precision of value. Then, we may have a chance to implement
836 * a periodic synchronizion of counter in memcg's counter.
837 *
838 * But this _read() function is used for user interface now. The user accounts
839 * memory usage by memory cgroup and he _always_ requires exact value because
840 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
841 * have to visit all online cpus and make sum. So, for now, unnecessary
842 * synchronization is not implemented. (just implemented for cpu hotplug)
843 *
844 * If there are kernel internal actions which can make use of some not-exact
845 * value, and reading all cpu value can be performance bottleneck in some
846 * common workload, threashold and synchonization as vmstat[] should be
847 * implemented.
848 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700849static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700850 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800851{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700852 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800853 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800854
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700855 get_online_cpus();
856 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700857 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700858#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700859 spin_lock(&memcg->pcp_counter_lock);
860 val += memcg->nocpu_base.count[idx];
861 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700862#endif
863 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800864 return val;
865}
866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700868 bool charge)
869{
870 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700871 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700872}
873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700874static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700875 enum mem_cgroup_events_index idx)
876{
877 unsigned long val = 0;
878 int cpu;
879
880 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700881 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700882#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883 spin_lock(&memcg->pcp_counter_lock);
884 val += memcg->nocpu_base.events[idx];
885 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700886#endif
887 return val;
888}
889
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700890static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700891 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800892{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800893 preempt_disable();
894
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700895 /*
896 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
897 * counted as CACHE even if it's on ANON LRU.
898 */
899 if (anon)
900 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700901 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800902 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700903 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700904 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700905
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800906 /* pagein of a big page is an event. So, ignore page size */
907 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700908 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800909 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700910 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800911 nr_pages = -nr_pages; /* for event */
912 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800913
Johannes Weiner13114712012-05-29 15:07:07 -0700914 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800915
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800916 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800917}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800918
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700919unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700920mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700921{
922 struct mem_cgroup_per_zone *mz;
923
924 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
925 return mz->lru_size[lru];
926}
927
928static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700930 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700931{
932 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700933 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700934 unsigned long ret = 0;
935
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700936 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700937
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700938 for_each_lru(lru) {
939 if (BIT(lru) & lru_mask)
940 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700941 }
942 return ret;
943}
944
945static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700946mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700947 int nid, unsigned int lru_mask)
948{
Ying Han889976d2011-05-26 16:25:33 -0700949 u64 total = 0;
950 int zid;
951
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700952 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700953 total += mem_cgroup_zone_nr_lru_pages(memcg,
954 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700955
Ying Han889976d2011-05-26 16:25:33 -0700956 return total;
957}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700958
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700959static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700960 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800961{
Ying Han889976d2011-05-26 16:25:33 -0700962 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800963 u64 total = 0;
964
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800965 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700966 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800967 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800968}
969
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800970static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
971 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800972{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700973 unsigned long val, next;
974
Johannes Weiner13114712012-05-29 15:07:07 -0700975 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700976 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700977 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800978 if ((long)next - (long)val < 0) {
979 switch (target) {
980 case MEM_CGROUP_TARGET_THRESH:
981 next = val + THRESHOLDS_EVENTS_TARGET;
982 break;
983 case MEM_CGROUP_TARGET_SOFTLIMIT:
984 next = val + SOFTLIMIT_EVENTS_TARGET;
985 break;
986 case MEM_CGROUP_TARGET_NUMAINFO:
987 next = val + NUMAINFO_EVENTS_TARGET;
988 break;
989 default:
990 break;
991 }
992 __this_cpu_write(memcg->stat->targets[target], next);
993 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700994 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800995 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800996}
997
998/*
999 * Check events in order.
1000 *
1001 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001002static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001003{
Steven Rostedt47994012011-11-02 13:38:33 -07001004 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001005 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001006 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1007 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001008 bool do_softlimit;
1009 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001010
1011 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1012 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001013#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001014 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1015 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001016#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001017 preempt_enable();
1018
1019 mem_cgroup_threshold(memcg);
1020 if (unlikely(do_softlimit))
1021 mem_cgroup_update_tree(memcg, page);
1022#if MAX_NUMNODES > 1
1023 if (unlikely(do_numainfo))
1024 atomic_inc(&memcg->numainfo_events);
1025#endif
1026 } else
1027 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001028}
1029
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001030struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001031{
Wanpeng Lib2145142012-07-31 16:46:01 -07001032 return mem_cgroup_from_css(
1033 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001034}
1035
Balbir Singhcf475ad2008-04-29 01:00:16 -07001036struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001037{
Balbir Singh31a78f22008-09-28 23:09:31 +01001038 /*
1039 * mm_update_next_owner() may clear mm->owner to NULL
1040 * if it races with swapoff, page migration, etc.
1041 * So this can be called with p == NULL.
1042 */
1043 if (unlikely(!p))
1044 return NULL;
1045
Wanpeng Lib2145142012-07-31 16:46:01 -07001046 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001047}
1048
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001049struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001050{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001051 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001052
1053 if (!mm)
1054 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001055 /*
1056 * Because we have no locks, mm->owner's may be being moved to other
1057 * cgroup. We use css_tryget() here even if this looks
1058 * pessimistic (rather than adding locks here).
1059 */
1060 rcu_read_lock();
1061 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001062 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1063 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001064 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001065 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001066 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001067 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001068}
1069
Johannes Weiner56600482012-01-12 17:17:59 -08001070/**
1071 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1072 * @root: hierarchy root
1073 * @prev: previously returned memcg, NULL on first invocation
1074 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1075 *
1076 * Returns references to children of the hierarchy below @root, or
1077 * @root itself, or %NULL after a full round-trip.
1078 *
1079 * Caller must pass the return value in @prev on subsequent
1080 * invocations for reference counting, or use mem_cgroup_iter_break()
1081 * to cancel a hierarchy walk before the round-trip is complete.
1082 *
1083 * Reclaimers can specify a zone and a priority level in @reclaim to
1084 * divide up the memcgs in the hierarchy among all concurrent
1085 * reclaimers operating on the same zone and priority.
1086 */
1087struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1088 struct mem_cgroup *prev,
1089 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001090{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001091 struct mem_cgroup *memcg = NULL;
1092 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001093
Johannes Weiner56600482012-01-12 17:17:59 -08001094 if (mem_cgroup_disabled())
1095 return NULL;
1096
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001097 if (!root)
1098 root = root_mem_cgroup;
1099
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001100 if (prev && !reclaim)
1101 id = css_id(&prev->css);
1102
1103 if (prev && prev != root)
1104 css_put(&prev->css);
1105
1106 if (!root->use_hierarchy && root != root_mem_cgroup) {
1107 if (prev)
1108 return NULL;
1109 return root;
1110 }
1111
1112 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001113 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001114 struct cgroup_subsys_state *css;
1115
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];
1123 if (prev && reclaim->generation != iter->generation)
1124 return NULL;
1125 id = iter->position;
1126 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001127
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001128 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001129 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1130 if (css) {
1131 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001132 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001133 } else
1134 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001135 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001136
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001137 if (reclaim) {
1138 iter->position = id;
1139 if (!css)
1140 iter->generation++;
1141 else if (!prev && memcg)
1142 reclaim->generation = iter->generation;
1143 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001144
1145 if (prev && !css)
1146 return NULL;
1147 }
1148 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001149}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001150
Johannes Weiner56600482012-01-12 17:17:59 -08001151/**
1152 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1153 * @root: hierarchy root
1154 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1155 */
1156void mem_cgroup_iter_break(struct mem_cgroup *root,
1157 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001158{
1159 if (!root)
1160 root = root_mem_cgroup;
1161 if (prev && prev != root)
1162 css_put(&prev->css);
1163}
1164
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001165/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001166 * Iteration constructs for visiting all cgroups (under a tree). If
1167 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1168 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001169 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001170#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001171 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001172 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001173 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001174
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001175#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001176 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001177 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001178 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001179
David Rientjes68ae5642012-12-12 13:51:57 -08001180void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001181{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001182 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001183
Ying Han456f9982011-05-26 16:25:38 -07001184 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001185 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1186 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001187 goto out;
1188
1189 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001190 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001191 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1192 break;
1193 case PGMAJFAULT:
1194 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001195 break;
1196 default:
1197 BUG();
1198 }
1199out:
1200 rcu_read_unlock();
1201}
David Rientjes68ae5642012-12-12 13:51:57 -08001202EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001203
Johannes Weiner925b7672012-01-12 17:18:15 -08001204/**
1205 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1206 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001207 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001208 *
1209 * Returns the lru list vector holding pages for the given @zone and
1210 * @mem. This can be the global zone lruvec, if the memory controller
1211 * is disabled.
1212 */
1213struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1214 struct mem_cgroup *memcg)
1215{
1216 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001217 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001218
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001219 if (mem_cgroup_disabled()) {
1220 lruvec = &zone->lruvec;
1221 goto out;
1222 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001223
1224 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001225 lruvec = &mz->lruvec;
1226out:
1227 /*
1228 * Since a node can be onlined after the mem_cgroup was created,
1229 * we have to be prepared to initialize lruvec->zone here;
1230 * and if offlined then reonlined, we need to reinitialize it.
1231 */
1232 if (unlikely(lruvec->zone != zone))
1233 lruvec->zone = zone;
1234 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001235}
1236
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001237/*
1238 * Following LRU functions are allowed to be used without PCG_LOCK.
1239 * Operations are called by routine of global LRU independently from memcg.
1240 * What we have to take care of here is validness of pc->mem_cgroup.
1241 *
1242 * Changes to pc->mem_cgroup happens when
1243 * 1. charge
1244 * 2. moving account
1245 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1246 * It is added to LRU before charge.
1247 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1248 * When moving account, the page is not on LRU. It's isolated.
1249 */
1250
Johannes Weiner925b7672012-01-12 17:18:15 -08001251/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001252 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001253 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001254 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001255 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001256struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001257{
1258 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001259 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001260 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001261 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001262
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001263 if (mem_cgroup_disabled()) {
1264 lruvec = &zone->lruvec;
1265 goto out;
1266 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001267
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001268 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001269 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001270
1271 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001272 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001273 * an uncharged page off lru does nothing to secure
1274 * its former mem_cgroup from sudden removal.
1275 *
1276 * Our caller holds lru_lock, and PageCgroupUsed is updated
1277 * under page_cgroup lock: between them, they make all uses
1278 * of pc->mem_cgroup safe.
1279 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001280 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001281 pc->mem_cgroup = memcg = root_mem_cgroup;
1282
Johannes Weiner925b7672012-01-12 17:18:15 -08001283 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001284 lruvec = &mz->lruvec;
1285out:
1286 /*
1287 * Since a node can be onlined after the mem_cgroup was created,
1288 * we have to be prepared to initialize lruvec->zone here;
1289 * and if offlined then reonlined, we need to reinitialize it.
1290 */
1291 if (unlikely(lruvec->zone != zone))
1292 lruvec->zone = zone;
1293 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001294}
1295
1296/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001297 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1298 * @lruvec: mem_cgroup per zone lru vector
1299 * @lru: index of lru list the page is sitting on
1300 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001301 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001302 * This function must be called when a page is added to or removed from an
1303 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001304 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001305void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1306 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001307{
1308 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001309 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001310
1311 if (mem_cgroup_disabled())
1312 return;
1313
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001314 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1315 lru_size = mz->lru_size + lru;
1316 *lru_size += nr_pages;
1317 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001318}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001319
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001320/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001321 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001322 * hierarchy subtree
1323 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001324bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1325 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001326{
Johannes Weiner91c637342012-05-29 15:06:24 -07001327 if (root_memcg == memcg)
1328 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001329 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001330 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001331 return css_is_ancestor(&memcg->css, &root_memcg->css);
1332}
1333
1334static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1335 struct mem_cgroup *memcg)
1336{
1337 bool ret;
1338
Johannes Weiner91c637342012-05-29 15:06:24 -07001339 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001340 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001341 rcu_read_unlock();
1342 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001343}
1344
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001345int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001346{
1347 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001348 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001349 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001350
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001351 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001352 if (p) {
1353 curr = try_get_mem_cgroup_from_mm(p->mm);
1354 task_unlock(p);
1355 } else {
1356 /*
1357 * All threads may have already detached their mm's, but the oom
1358 * killer still needs to detect if they have already been oom
1359 * killed to prevent needlessly killing additional tasks.
1360 */
1361 task_lock(task);
1362 curr = mem_cgroup_from_task(task);
1363 if (curr)
1364 css_get(&curr->css);
1365 task_unlock(task);
1366 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001367 if (!curr)
1368 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001369 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001370 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001371 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001372 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1373 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001374 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001375 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001376 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001377 return ret;
1378}
1379
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001380int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001381{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001382 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001383 unsigned long inactive;
1384 unsigned long active;
1385 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001386
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001387 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1388 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001389
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001390 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1391 if (gb)
1392 inactive_ratio = int_sqrt(10 * gb);
1393 else
1394 inactive_ratio = 1;
1395
Johannes Weiner9b272972011-11-02 13:38:23 -07001396 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001397}
1398
Balbir Singh6d61ef42009-01-07 18:08:06 -08001399#define mem_cgroup_from_res_counter(counter, member) \
1400 container_of(counter, struct mem_cgroup, member)
1401
Johannes Weiner19942822011-02-01 15:52:43 -08001402/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001403 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001404 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001405 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001406 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001407 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001408 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001409static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001410{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001411 unsigned long long margin;
1412
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001413 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001414 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001415 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001416 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001417}
1418
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001419int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001420{
1421 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001422
1423 /* root ? */
1424 if (cgrp->parent == NULL)
1425 return vm_swappiness;
1426
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001427 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001428}
1429
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001430/*
1431 * memcg->moving_account is used for checking possibility that some thread is
1432 * calling move_account(). When a thread on CPU-A starts moving pages under
1433 * a memcg, other threads should check memcg->moving_account under
1434 * rcu_read_lock(), like this:
1435 *
1436 * CPU-A CPU-B
1437 * rcu_read_lock()
1438 * memcg->moving_account+1 if (memcg->mocing_account)
1439 * take heavy locks.
1440 * synchronize_rcu() update something.
1441 * rcu_read_unlock()
1442 * start move here.
1443 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001444
1445/* for quick checking without looking up memcg */
1446atomic_t memcg_moving __read_mostly;
1447
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001448static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001449{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001450 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001451 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001452 synchronize_rcu();
1453}
1454
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001455static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001456{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001457 /*
1458 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1459 * We check NULL in callee rather than caller.
1460 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001461 if (memcg) {
1462 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001463 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001464 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001465}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001466
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001467/*
1468 * 2 routines for checking "mem" is under move_account() or not.
1469 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001470 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1471 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001472 * pc->mem_cgroup may be overwritten.
1473 *
1474 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1475 * under hierarchy of moving cgroups. This is for
1476 * waiting at hith-memory prressure caused by "move".
1477 */
1478
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001479static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001480{
1481 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001482 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001483}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001484
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001485static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001486{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001487 struct mem_cgroup *from;
1488 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001489 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001490 /*
1491 * Unlike task_move routines, we access mc.to, mc.from not under
1492 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1493 */
1494 spin_lock(&mc.lock);
1495 from = mc.from;
1496 to = mc.to;
1497 if (!from)
1498 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001499
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001500 ret = mem_cgroup_same_or_subtree(memcg, from)
1501 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001502unlock:
1503 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001504 return ret;
1505}
1506
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001507static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001508{
1509 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001510 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001511 DEFINE_WAIT(wait);
1512 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1513 /* moving charge context might have finished. */
1514 if (mc.moving_task)
1515 schedule();
1516 finish_wait(&mc.waitq, &wait);
1517 return true;
1518 }
1519 }
1520 return false;
1521}
1522
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001523/*
1524 * Take this lock when
1525 * - a code tries to modify page's memcg while it's USED.
1526 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001527 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001528 */
1529static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1530 unsigned long *flags)
1531{
1532 spin_lock_irqsave(&memcg->move_lock, *flags);
1533}
1534
1535static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1536 unsigned long *flags)
1537{
1538 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1539}
1540
Sha Zhengju58cf1882013-02-22 16:32:05 -08001541#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001542/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001543 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001544 * @memcg: The memory cgroup that went over limit
1545 * @p: Task that is going to be killed
1546 *
1547 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1548 * enabled
1549 */
1550void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1551{
1552 struct cgroup *task_cgrp;
1553 struct cgroup *mem_cgrp;
1554 /*
1555 * Need a buffer in BSS, can't rely on allocations. The code relies
1556 * on the assumption that OOM is serialized for memory controller.
1557 * If this assumption is broken, revisit this code.
1558 */
1559 static char memcg_name[PATH_MAX];
1560 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001561 struct mem_cgroup *iter;
1562 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001563
Sha Zhengju58cf1882013-02-22 16:32:05 -08001564 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001565 return;
1566
Balbir Singhe2224322009-04-02 16:57:39 -07001567 rcu_read_lock();
1568
1569 mem_cgrp = memcg->css.cgroup;
1570 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1571
1572 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1573 if (ret < 0) {
1574 /*
1575 * Unfortunately, we are unable to convert to a useful name
1576 * But we'll still print out the usage information
1577 */
1578 rcu_read_unlock();
1579 goto done;
1580 }
1581 rcu_read_unlock();
1582
Andrew Mortond0451972013-02-22 16:32:06 -08001583 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001584
1585 rcu_read_lock();
1586 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1587 if (ret < 0) {
1588 rcu_read_unlock();
1589 goto done;
1590 }
1591 rcu_read_unlock();
1592
1593 /*
1594 * Continues from above, so we don't need an KERN_ level
1595 */
Andrew Mortond0451972013-02-22 16:32:06 -08001596 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001597done:
1598
Andrew Mortond0451972013-02-22 16:32:06 -08001599 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001600 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1601 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1602 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001603 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001604 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1605 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1606 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001607 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001608 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1609 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1610 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001611
1612 for_each_mem_cgroup_tree(iter, memcg) {
1613 pr_info("Memory cgroup stats");
1614
1615 rcu_read_lock();
1616 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1617 if (!ret)
1618 pr_cont(" for %s", memcg_name);
1619 rcu_read_unlock();
1620 pr_cont(":");
1621
1622 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1623 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1624 continue;
1625 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1626 K(mem_cgroup_read_stat(iter, i)));
1627 }
1628
1629 for (i = 0; i < NR_LRU_LISTS; i++)
1630 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1631 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1632
1633 pr_cont("\n");
1634 }
Balbir Singhe2224322009-04-02 16:57:39 -07001635}
1636
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001637/*
1638 * This function returns the number of memcg under hierarchy tree. Returns
1639 * 1(self count) if no children.
1640 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001641static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001642{
1643 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001644 struct mem_cgroup *iter;
1645
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001646 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001647 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001648 return num;
1649}
1650
Balbir Singh6d61ef42009-01-07 18:08:06 -08001651/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001652 * Return the memory (and swap, if configured) limit for a memcg.
1653 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001654static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001655{
1656 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001657
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001658 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001659
David Rientjesa63d83f2010-08-09 17:19:46 -07001660 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001661 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001662 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001663 if (mem_cgroup_swappiness(memcg)) {
1664 u64 memsw;
1665
1666 limit += total_swap_pages << PAGE_SHIFT;
1667 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1668
1669 /*
1670 * If memsw is finite and limits the amount of swap space
1671 * available to this memcg, return that limit.
1672 */
1673 limit = min(limit, memsw);
1674 }
1675
1676 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001677}
1678
David Rientjes19965462012-12-11 16:00:26 -08001679static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1680 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001681{
1682 struct mem_cgroup *iter;
1683 unsigned long chosen_points = 0;
1684 unsigned long totalpages;
1685 unsigned int points = 0;
1686 struct task_struct *chosen = NULL;
1687
David Rientjes876aafb2012-07-31 16:43:48 -07001688 /*
1689 * If current has a pending SIGKILL, then automatically select it. The
1690 * goal is to allow it to allocate so that it may quickly exit and free
1691 * its memory.
1692 */
1693 if (fatal_signal_pending(current)) {
1694 set_thread_flag(TIF_MEMDIE);
1695 return;
1696 }
1697
1698 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001699 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1700 for_each_mem_cgroup_tree(iter, memcg) {
1701 struct cgroup *cgroup = iter->css.cgroup;
1702 struct cgroup_iter it;
1703 struct task_struct *task;
1704
1705 cgroup_iter_start(cgroup, &it);
1706 while ((task = cgroup_iter_next(cgroup, &it))) {
1707 switch (oom_scan_process_thread(task, totalpages, NULL,
1708 false)) {
1709 case OOM_SCAN_SELECT:
1710 if (chosen)
1711 put_task_struct(chosen);
1712 chosen = task;
1713 chosen_points = ULONG_MAX;
1714 get_task_struct(chosen);
1715 /* fall through */
1716 case OOM_SCAN_CONTINUE:
1717 continue;
1718 case OOM_SCAN_ABORT:
1719 cgroup_iter_end(cgroup, &it);
1720 mem_cgroup_iter_break(memcg, iter);
1721 if (chosen)
1722 put_task_struct(chosen);
1723 return;
1724 case OOM_SCAN_OK:
1725 break;
1726 };
1727 points = oom_badness(task, memcg, NULL, totalpages);
1728 if (points > chosen_points) {
1729 if (chosen)
1730 put_task_struct(chosen);
1731 chosen = task;
1732 chosen_points = points;
1733 get_task_struct(chosen);
1734 }
1735 }
1736 cgroup_iter_end(cgroup, &it);
1737 }
1738
1739 if (!chosen)
1740 return;
1741 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001742 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1743 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001744}
1745
Johannes Weiner56600482012-01-12 17:17:59 -08001746static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1747 gfp_t gfp_mask,
1748 unsigned long flags)
1749{
1750 unsigned long total = 0;
1751 bool noswap = false;
1752 int loop;
1753
1754 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1755 noswap = true;
1756 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1757 noswap = true;
1758
1759 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1760 if (loop)
1761 drain_all_stock_async(memcg);
1762 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1763 /*
1764 * Allow limit shrinkers, which are triggered directly
1765 * by userspace, to catch signals and stop reclaim
1766 * after minimal progress, regardless of the margin.
1767 */
1768 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1769 break;
1770 if (mem_cgroup_margin(memcg))
1771 break;
1772 /*
1773 * If nothing was reclaimed after two attempts, there
1774 * may be no reclaimable pages in this hierarchy.
1775 */
1776 if (loop && !total)
1777 break;
1778 }
1779 return total;
1780}
1781
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001782/**
1783 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001784 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001785 * @nid: the node ID to be checked.
1786 * @noswap : specify true here if the user wants flle only information.
1787 *
1788 * This function returns whether the specified memcg contains any
1789 * reclaimable pages on a node. Returns true if there are any reclaimable
1790 * pages in the node.
1791 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001792static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001793 int nid, bool noswap)
1794{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001795 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001796 return true;
1797 if (noswap || !total_swap_pages)
1798 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001800 return true;
1801 return false;
1802
1803}
Ying Han889976d2011-05-26 16:25:33 -07001804#if MAX_NUMNODES > 1
1805
1806/*
1807 * Always updating the nodemask is not very good - even if we have an empty
1808 * list or the wrong list here, we can start from some node and traverse all
1809 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1810 *
1811 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001812static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001813{
1814 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001815 /*
1816 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1817 * pagein/pageout changes since the last update.
1818 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001819 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001820 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001821 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001822 return;
1823
Ying Han889976d2011-05-26 16:25:33 -07001824 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001825 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001826
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001827 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001828
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001829 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1830 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001831 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001832
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001833 atomic_set(&memcg->numainfo_events, 0);
1834 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001835}
1836
1837/*
1838 * Selecting a node where we start reclaim from. Because what we need is just
1839 * reducing usage counter, start from anywhere is O,K. Considering
1840 * memory reclaim from current node, there are pros. and cons.
1841 *
1842 * Freeing memory from current node means freeing memory from a node which
1843 * we'll use or we've used. So, it may make LRU bad. And if several threads
1844 * hit limits, it will see a contention on a node. But freeing from remote
1845 * node means more costs for memory reclaim because of memory latency.
1846 *
1847 * Now, we use round-robin. Better algorithm is welcomed.
1848 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001849int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001850{
1851 int node;
1852
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853 mem_cgroup_may_update_nodemask(memcg);
1854 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001855
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001856 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001857 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001858 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001859 /*
1860 * We call this when we hit limit, not when pages are added to LRU.
1861 * No LRU may hold pages because all pages are UNEVICTABLE or
1862 * memcg is too small and all pages are not on LRU. In that case,
1863 * we use curret node.
1864 */
1865 if (unlikely(node == MAX_NUMNODES))
1866 node = numa_node_id();
1867
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001868 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001869 return node;
1870}
1871
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001872/*
1873 * Check all nodes whether it contains reclaimable pages or not.
1874 * For quick scan, we make use of scan_nodes. This will allow us to skip
1875 * unused nodes. But scan_nodes is lazily updated and may not cotain
1876 * enough new information. We need to do double check.
1877 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001878static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001879{
1880 int nid;
1881
1882 /*
1883 * quick check...making use of scan_node.
1884 * We can skip unused nodes.
1885 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001886 if (!nodes_empty(memcg->scan_nodes)) {
1887 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001888 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001889 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001891 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001892 return true;
1893 }
1894 }
1895 /*
1896 * Check rest of nodes.
1897 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001898 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001899 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001900 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001901 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001902 return true;
1903 }
1904 return false;
1905}
1906
Ying Han889976d2011-05-26 16:25:33 -07001907#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001908int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001909{
1910 return 0;
1911}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001912
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001913static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001914{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001915 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001916}
Ying Han889976d2011-05-26 16:25:33 -07001917#endif
1918
Johannes Weiner56600482012-01-12 17:17:59 -08001919static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1920 struct zone *zone,
1921 gfp_t gfp_mask,
1922 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001923{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001924 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001925 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001926 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001927 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001928 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001929 struct mem_cgroup_reclaim_cookie reclaim = {
1930 .zone = zone,
1931 .priority = 0,
1932 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001933
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001935
Balbir Singh4e416952009-09-23 15:56:39 -07001936 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001937 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001938 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001939 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001940 if (loop >= 2) {
1941 /*
1942 * If we have not been able to reclaim
1943 * anything, it might because there are
1944 * no reclaimable pages under this hierarchy
1945 */
Johannes Weiner56600482012-01-12 17:17:59 -08001946 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001947 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001948 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001949 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001950 * excess >> 2 is not to excessive so as to
1951 * reclaim too much, nor too less that we keep
1952 * coming back to reclaim from this cgroup
1953 */
1954 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001955 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001956 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001957 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001958 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001959 }
Johannes Weiner56600482012-01-12 17:17:59 -08001960 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001961 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001962 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1963 zone, &nr_scanned);
1964 *total_scanned += nr_scanned;
1965 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001966 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001967 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001968 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001969 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001970}
1971
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001972/*
1973 * Check OOM-Killer is already running under our hierarchy.
1974 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001975 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001976 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001978{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001979 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001980
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001981 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001982 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001983 /*
1984 * this subtree of our hierarchy is already locked
1985 * so we cannot give a lock.
1986 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001987 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001988 mem_cgroup_iter_break(memcg, iter);
1989 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001990 } else
1991 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001992 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001993
Michal Hocko79dfdac2011-07-26 16:08:23 -07001994 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001995 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001996
1997 /*
1998 * OK, we failed to lock the whole subtree so we have to clean up
1999 * what we set up to the failing subtree
2000 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002001 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002002 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002003 mem_cgroup_iter_break(memcg, iter);
2004 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002005 }
2006 iter->oom_lock = false;
2007 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002008 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002009}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002010
Michal Hocko79dfdac2011-07-26 16:08:23 -07002011/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002012 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002013 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002014static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002015{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002016 struct mem_cgroup *iter;
2017
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002019 iter->oom_lock = false;
2020 return 0;
2021}
2022
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002023static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002024{
2025 struct mem_cgroup *iter;
2026
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002027 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002028 atomic_inc(&iter->under_oom);
2029}
2030
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002031static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002032{
2033 struct mem_cgroup *iter;
2034
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002035 /*
2036 * When a new child is created while the hierarchy is under oom,
2037 * mem_cgroup_oom_lock() may not be called. We have to use
2038 * atomic_add_unless() here.
2039 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002040 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002041 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002042}
2043
Michal Hocko1af8efe2011-07-26 16:08:24 -07002044static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002045static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2046
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002047struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002048 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002049 wait_queue_t wait;
2050};
2051
2052static int memcg_oom_wake_function(wait_queue_t *wait,
2053 unsigned mode, int sync, void *arg)
2054{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002055 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2056 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002057 struct oom_wait_info *oom_wait_info;
2058
2059 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002060 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002061
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002062 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002063 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002064 * Then we can use css_is_ancestor without taking care of RCU.
2065 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002066 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2067 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002068 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002069 return autoremove_wake_function(wait, mode, sync, arg);
2070}
2071
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002072static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002073{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002074 /* for filtering, pass "memcg" as argument. */
2075 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002076}
2077
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002079{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002080 if (memcg && atomic_read(&memcg->under_oom))
2081 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002082}
2083
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002084/*
2085 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2086 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002087static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2088 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002089{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002090 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002091 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002092
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002093 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002094 owait.wait.flags = 0;
2095 owait.wait.func = memcg_oom_wake_function;
2096 owait.wait.private = current;
2097 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002098 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002099 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002100
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002101 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002102 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002103 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002104 /*
2105 * Even if signal_pending(), we can't quit charge() loop without
2106 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2107 * under OOM is always welcomed, use TASK_KILLABLE here.
2108 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002109 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002110 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002111 need_to_kill = false;
2112 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002113 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002114 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002115
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002116 if (need_to_kill) {
2117 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002118 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002119 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002120 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002121 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002122 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002123 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002124 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002125 mem_cgroup_oom_unlock(memcg);
2126 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002127 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002128
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002129 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002130
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002131 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2132 return false;
2133 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002134 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002135 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002136}
2137
Balbir Singhd69b0422009-06-17 16:26:34 -07002138/*
2139 * Currently used to update mapped file statistics, but the routine can be
2140 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002141 *
2142 * Notes: Race condition
2143 *
2144 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2145 * it tends to be costly. But considering some conditions, we doesn't need
2146 * to do so _always_.
2147 *
2148 * Considering "charge", lock_page_cgroup() is not required because all
2149 * file-stat operations happen after a page is attached to radix-tree. There
2150 * are no race with "charge".
2151 *
2152 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2153 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2154 * if there are race with "uncharge". Statistics itself is properly handled
2155 * by flags.
2156 *
2157 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002158 * small, we check mm->moving_account and detect there are possibility of race
2159 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002160 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002161
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002162void __mem_cgroup_begin_update_page_stat(struct page *page,
2163 bool *locked, unsigned long *flags)
2164{
2165 struct mem_cgroup *memcg;
2166 struct page_cgroup *pc;
2167
2168 pc = lookup_page_cgroup(page);
2169again:
2170 memcg = pc->mem_cgroup;
2171 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2172 return;
2173 /*
2174 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002175 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002176 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002177 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002178 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002179 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002180 return;
2181
2182 move_lock_mem_cgroup(memcg, flags);
2183 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2184 move_unlock_mem_cgroup(memcg, flags);
2185 goto again;
2186 }
2187 *locked = true;
2188}
2189
2190void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2191{
2192 struct page_cgroup *pc = lookup_page_cgroup(page);
2193
2194 /*
2195 * It's guaranteed that pc->mem_cgroup never changes while
2196 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002197 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002198 */
2199 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2200}
2201
Greg Thelen2a7106f2011-01-13 15:47:37 -08002202void mem_cgroup_update_page_stat(struct page *page,
2203 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002204{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002205 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002206 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002207 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002208
Johannes Weinercfa44942012-01-12 17:18:38 -08002209 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002210 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002211
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002212 memcg = pc->mem_cgroup;
2213 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002214 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002215
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002216 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002217 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002218 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002219 break;
2220 default:
2221 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002222 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002223
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002224 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002225}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002226
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002227/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002228 * size of first charge trial. "32" comes from vmscan.c's magic value.
2229 * TODO: maybe necessary to use big numbers in big irons.
2230 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002231#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002232struct memcg_stock_pcp {
2233 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002234 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002235 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002236 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002237#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002238};
2239static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002240static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002241
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002242/**
2243 * consume_stock: Try to consume stocked charge on this cpu.
2244 * @memcg: memcg to consume from.
2245 * @nr_pages: how many pages to charge.
2246 *
2247 * The charges will only happen if @memcg matches the current cpu's memcg
2248 * stock, and at least @nr_pages are available in that stock. Failure to
2249 * service an allocation will refill the stock.
2250 *
2251 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002252 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002253static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002254{
2255 struct memcg_stock_pcp *stock;
2256 bool ret = true;
2257
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002258 if (nr_pages > CHARGE_BATCH)
2259 return false;
2260
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002261 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002262 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2263 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002264 else /* need to call res_counter_charge */
2265 ret = false;
2266 put_cpu_var(memcg_stock);
2267 return ret;
2268}
2269
2270/*
2271 * Returns stocks cached in percpu to res_counter and reset cached information.
2272 */
2273static void drain_stock(struct memcg_stock_pcp *stock)
2274{
2275 struct mem_cgroup *old = stock->cached;
2276
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002277 if (stock->nr_pages) {
2278 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2279
2280 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002281 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002282 res_counter_uncharge(&old->memsw, bytes);
2283 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002284 }
2285 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002286}
2287
2288/*
2289 * This must be called under preempt disabled or must be called by
2290 * a thread which is pinned to local cpu.
2291 */
2292static void drain_local_stock(struct work_struct *dummy)
2293{
2294 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2295 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002296 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002297}
2298
Michal Hockoe4777492013-02-22 16:35:40 -08002299static void __init memcg_stock_init(void)
2300{
2301 int cpu;
2302
2303 for_each_possible_cpu(cpu) {
2304 struct memcg_stock_pcp *stock =
2305 &per_cpu(memcg_stock, cpu);
2306 INIT_WORK(&stock->work, drain_local_stock);
2307 }
2308}
2309
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002310/*
2311 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002312 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002314static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002315{
2316 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2317
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002318 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002319 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002320 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002321 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002322 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002323 put_cpu_var(memcg_stock);
2324}
2325
2326/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002327 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002328 * of the hierarchy under it. sync flag says whether we should block
2329 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002330 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002331static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002332{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002333 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002334
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002335 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002336 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002337 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002338 for_each_online_cpu(cpu) {
2339 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002340 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002341
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002342 memcg = stock->cached;
2343 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002344 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002345 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002346 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002347 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2348 if (cpu == curcpu)
2349 drain_local_stock(&stock->work);
2350 else
2351 schedule_work_on(cpu, &stock->work);
2352 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002353 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002354 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002355
2356 if (!sync)
2357 goto out;
2358
2359 for_each_online_cpu(cpu) {
2360 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002361 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002362 flush_work(&stock->work);
2363 }
2364out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002365 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002366}
2367
2368/*
2369 * Tries to drain stocked charges in other cpus. This function is asynchronous
2370 * and just put a work per cpu for draining localy on each cpu. Caller can
2371 * expects some charges will be back to res_counter later but cannot wait for
2372 * it.
2373 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002375{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002376 /*
2377 * If someone calls draining, avoid adding more kworker runs.
2378 */
2379 if (!mutex_trylock(&percpu_charge_mutex))
2380 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002381 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002382 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002383}
2384
2385/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002387{
2388 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002389 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002390 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002391 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002392}
2393
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002394/*
2395 * This function drains percpu counter value from DEAD cpu and
2396 * move it to local cpu. Note that this function can be preempted.
2397 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002399{
2400 int i;
2401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002403 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002404 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002405
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002406 per_cpu(memcg->stat->count[i], cpu) = 0;
2407 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002408 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002409 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002411
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002412 per_cpu(memcg->stat->events[i], cpu) = 0;
2413 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002414 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002415 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002416}
2417
2418static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002419 unsigned long action,
2420 void *hcpu)
2421{
2422 int cpu = (unsigned long)hcpu;
2423 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002424 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002425
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002426 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002427 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002428
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002429 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002430 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002431
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002432 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002433 mem_cgroup_drain_pcp_counter(iter, cpu);
2434
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002435 stock = &per_cpu(memcg_stock, cpu);
2436 drain_stock(stock);
2437 return NOTIFY_OK;
2438}
2439
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002440
2441/* See __mem_cgroup_try_charge() for details */
2442enum {
2443 CHARGE_OK, /* success */
2444 CHARGE_RETRY, /* need to retry but retry is not bad */
2445 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2446 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2447 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2448};
2449
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002450static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002451 unsigned int nr_pages, unsigned int min_pages,
2452 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002453{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002454 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002455 struct mem_cgroup *mem_over_limit;
2456 struct res_counter *fail_res;
2457 unsigned long flags = 0;
2458 int ret;
2459
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002460 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002461
2462 if (likely(!ret)) {
2463 if (!do_swap_account)
2464 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002465 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002466 if (likely(!ret))
2467 return CHARGE_OK;
2468
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002469 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002470 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2471 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2472 } else
2473 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002474 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002475 * Never reclaim on behalf of optional batching, retry with a
2476 * single page instead.
2477 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002478 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002479 return CHARGE_RETRY;
2480
2481 if (!(gfp_mask & __GFP_WAIT))
2482 return CHARGE_WOULDBLOCK;
2483
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002484 if (gfp_mask & __GFP_NORETRY)
2485 return CHARGE_NOMEM;
2486
Johannes Weiner56600482012-01-12 17:17:59 -08002487 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002488 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002489 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002490 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002491 * Even though the limit is exceeded at this point, reclaim
2492 * may have been able to free some pages. Retry the charge
2493 * before killing the task.
2494 *
2495 * Only for regular pages, though: huge pages are rather
2496 * unlikely to succeed so close to the limit, and we fall back
2497 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002498 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002499 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002500 return CHARGE_RETRY;
2501
2502 /*
2503 * At task move, charge accounts can be doubly counted. So, it's
2504 * better to wait until the end of task_move if something is going on.
2505 */
2506 if (mem_cgroup_wait_acct_move(mem_over_limit))
2507 return CHARGE_RETRY;
2508
2509 /* If we don't need to call oom-killer at el, return immediately */
2510 if (!oom_check)
2511 return CHARGE_NOMEM;
2512 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002513 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002514 return CHARGE_OOM_DIE;
2515
2516 return CHARGE_RETRY;
2517}
2518
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002519/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002520 * __mem_cgroup_try_charge() does
2521 * 1. detect memcg to be charged against from passed *mm and *ptr,
2522 * 2. update res_counter
2523 * 3. call memory reclaim if necessary.
2524 *
2525 * In some special case, if the task is fatal, fatal_signal_pending() or
2526 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2527 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2528 * as possible without any hazards. 2: all pages should have a valid
2529 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2530 * pointer, that is treated as a charge to root_mem_cgroup.
2531 *
2532 * So __mem_cgroup_try_charge() will return
2533 * 0 ... on success, filling *ptr with a valid memcg pointer.
2534 * -ENOMEM ... charge failure because of resource limits.
2535 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2536 *
2537 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2538 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002539 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002540static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002541 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002542 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002543 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002544 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002545{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002546 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002547 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002548 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002549 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002550
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002551 /*
2552 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2553 * in system level. So, allow to go ahead dying process in addition to
2554 * MEMDIE process.
2555 */
2556 if (unlikely(test_thread_flag(TIF_MEMDIE)
2557 || fatal_signal_pending(current)))
2558 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002559
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002560 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002561 * We always charge the cgroup the mm_struct belongs to.
2562 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002563 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002564 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002565 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002567 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002568again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002569 if (*ptr) { /* css should be a valid one */
2570 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002572 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002573 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002574 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002575 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002576 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002577 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002578
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002579 rcu_read_lock();
2580 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002581 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002582 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002583 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002584 * race with swapoff. Then, we have small risk of mis-accouning.
2585 * But such kind of mis-account by race always happens because
2586 * we don't have cgroup_mutex(). It's overkill and we allo that
2587 * small race, here.
2588 * (*) swapoff at el will charge against mm-struct not against
2589 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002590 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002591 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002592 if (!memcg)
2593 memcg = root_mem_cgroup;
2594 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002595 rcu_read_unlock();
2596 goto done;
2597 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002598 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002599 /*
2600 * It seems dagerous to access memcg without css_get().
2601 * But considering how consume_stok works, it's not
2602 * necessary. If consume_stock success, some charges
2603 * from this memcg are cached on this cpu. So, we
2604 * don't need to call css_get()/css_tryget() before
2605 * calling consume_stock().
2606 */
2607 rcu_read_unlock();
2608 goto done;
2609 }
2610 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002611 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002612 rcu_read_unlock();
2613 goto again;
2614 }
2615 rcu_read_unlock();
2616 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002617
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002618 do {
2619 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002620
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002621 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002622 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002623 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002624 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002625 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002626
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002627 oom_check = false;
2628 if (oom && !nr_oom_retries) {
2629 oom_check = true;
2630 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2631 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002632
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002633 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2634 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002635 switch (ret) {
2636 case CHARGE_OK:
2637 break;
2638 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002639 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002640 css_put(&memcg->css);
2641 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002642 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002643 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002644 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002645 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002646 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002647 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002648 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002649 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002650 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002651 /* If oom, we never return -ENOMEM */
2652 nr_oom_retries--;
2653 break;
2654 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002655 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002656 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002657 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002658 } while (ret != CHARGE_OK);
2659
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002660 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002661 refill_stock(memcg, batch - nr_pages);
2662 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002663done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002664 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002665 return 0;
2666nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002667 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002668 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002669bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002670 *ptr = root_mem_cgroup;
2671 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002672}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002673
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002674/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002675 * Somemtimes we have to undo a charge we got by try_charge().
2676 * This function is for that and do uncharge, put css's refcnt.
2677 * gotten by try_charge().
2678 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002679static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002680 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002681{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002682 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002683 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002684
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002685 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002686 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002687 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002688 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002689}
2690
2691/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002692 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2693 * This is useful when moving usage to parent cgroup.
2694 */
2695static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2696 unsigned int nr_pages)
2697{
2698 unsigned long bytes = nr_pages * PAGE_SIZE;
2699
2700 if (mem_cgroup_is_root(memcg))
2701 return;
2702
2703 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2704 if (do_swap_account)
2705 res_counter_uncharge_until(&memcg->memsw,
2706 memcg->memsw.parent, bytes);
2707}
2708
2709/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002710 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002711 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2712 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2713 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002714 */
2715static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2716{
2717 struct cgroup_subsys_state *css;
2718
2719 /* ID 0 is unused ID */
2720 if (!id)
2721 return NULL;
2722 css = css_lookup(&mem_cgroup_subsys, id);
2723 if (!css)
2724 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002725 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002726}
2727
Wu Fengguange42d9d52009-12-16 12:19:59 +01002728struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002729{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002730 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002731 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002732 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002733 swp_entry_t ent;
2734
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002735 VM_BUG_ON(!PageLocked(page));
2736
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002737 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002738 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002739 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002740 memcg = pc->mem_cgroup;
2741 if (memcg && !css_tryget(&memcg->css))
2742 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002743 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002744 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002745 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002746 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002747 memcg = mem_cgroup_lookup(id);
2748 if (memcg && !css_tryget(&memcg->css))
2749 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002750 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002751 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002752 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002753 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002754}
2755
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002756static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002757 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002758 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002759 enum charge_type ctype,
2760 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002761{
Johannes Weinerce587e62012-04-24 20:22:33 +02002762 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002763 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002764 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002765 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002766 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002767
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002768 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002769 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002770 /*
2771 * we don't need page_cgroup_lock about tail pages, becase they are not
2772 * accessed by any other context at this point.
2773 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002774
2775 /*
2776 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2777 * may already be on some other mem_cgroup's LRU. Take care of it.
2778 */
2779 if (lrucare) {
2780 zone = page_zone(page);
2781 spin_lock_irq(&zone->lru_lock);
2782 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002783 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002784 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002785 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002786 was_on_lru = true;
2787 }
2788 }
2789
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002790 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002791 /*
2792 * We access a page_cgroup asynchronously without lock_page_cgroup().
2793 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2794 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2795 * before USED bit, we need memory barrier here.
2796 * See mem_cgroup_add_lru_list(), etc.
2797 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002798 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002799 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002800
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002801 if (lrucare) {
2802 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002803 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002804 VM_BUG_ON(PageLRU(page));
2805 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002806 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002807 }
2808 spin_unlock_irq(&zone->lru_lock);
2809 }
2810
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002811 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002812 anon = true;
2813 else
2814 anon = false;
2815
2816 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002817 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002818
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002819 /*
2820 * "charge_statistics" updated event counter. Then, check it.
2821 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2822 * if they exceeds softlimit.
2823 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002824 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002825}
2826
Glauber Costa7cf27982012-12-18 14:22:55 -08002827static DEFINE_MUTEX(set_limit_mutex);
2828
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002829#ifdef CONFIG_MEMCG_KMEM
2830static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2831{
2832 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2833 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2834}
2835
Glauber Costa1f458cb2012-12-18 14:22:50 -08002836/*
2837 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2838 * in the memcg_cache_params struct.
2839 */
2840static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2841{
2842 struct kmem_cache *cachep;
2843
2844 VM_BUG_ON(p->is_root_cache);
2845 cachep = p->root_cache;
2846 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2847}
2848
Glauber Costa749c5412012-12-18 14:23:01 -08002849#ifdef CONFIG_SLABINFO
2850static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2851 struct seq_file *m)
2852{
2853 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2854 struct memcg_cache_params *params;
2855
2856 if (!memcg_can_account_kmem(memcg))
2857 return -EIO;
2858
2859 print_slabinfo_header(m);
2860
2861 mutex_lock(&memcg->slab_caches_mutex);
2862 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2863 cache_show(memcg_params_to_cache(params), m);
2864 mutex_unlock(&memcg->slab_caches_mutex);
2865
2866 return 0;
2867}
2868#endif
2869
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002870static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2871{
2872 struct res_counter *fail_res;
2873 struct mem_cgroup *_memcg;
2874 int ret = 0;
2875 bool may_oom;
2876
2877 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2878 if (ret)
2879 return ret;
2880
2881 /*
2882 * Conditions under which we can wait for the oom_killer. Those are
2883 * the same conditions tested by the core page allocator
2884 */
2885 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2886
2887 _memcg = memcg;
2888 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2889 &_memcg, may_oom);
2890
2891 if (ret == -EINTR) {
2892 /*
2893 * __mem_cgroup_try_charge() chosed to bypass to root due to
2894 * OOM kill or fatal signal. Since our only options are to
2895 * either fail the allocation or charge it to this cgroup, do
2896 * it as a temporary condition. But we can't fail. From a
2897 * kmem/slab perspective, the cache has already been selected,
2898 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2899 * our minds.
2900 *
2901 * This condition will only trigger if the task entered
2902 * memcg_charge_kmem in a sane state, but was OOM-killed during
2903 * __mem_cgroup_try_charge() above. Tasks that were already
2904 * dying when the allocation triggers should have been already
2905 * directed to the root cgroup in memcontrol.h
2906 */
2907 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2908 if (do_swap_account)
2909 res_counter_charge_nofail(&memcg->memsw, size,
2910 &fail_res);
2911 ret = 0;
2912 } else if (ret)
2913 res_counter_uncharge(&memcg->kmem, size);
2914
2915 return ret;
2916}
2917
2918static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2919{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002920 res_counter_uncharge(&memcg->res, size);
2921 if (do_swap_account)
2922 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002923
2924 /* Not down to 0 */
2925 if (res_counter_uncharge(&memcg->kmem, size))
2926 return;
2927
2928 if (memcg_kmem_test_and_clear_dead(memcg))
2929 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002930}
2931
Glauber Costa2633d7a2012-12-18 14:22:34 -08002932void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2933{
2934 if (!memcg)
2935 return;
2936
2937 mutex_lock(&memcg->slab_caches_mutex);
2938 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2939 mutex_unlock(&memcg->slab_caches_mutex);
2940}
2941
2942/*
2943 * helper for acessing a memcg's index. It will be used as an index in the
2944 * child cache array in kmem_cache, and also to derive its name. This function
2945 * will return -1 when this is not a kmem-limited memcg.
2946 */
2947int memcg_cache_id(struct mem_cgroup *memcg)
2948{
2949 return memcg ? memcg->kmemcg_id : -1;
2950}
2951
Glauber Costa55007d82012-12-18 14:22:38 -08002952/*
2953 * This ends up being protected by the set_limit mutex, during normal
2954 * operation, because that is its main call site.
2955 *
2956 * But when we create a new cache, we can call this as well if its parent
2957 * is kmem-limited. That will have to hold set_limit_mutex as well.
2958 */
2959int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2960{
2961 int num, ret;
2962
2963 num = ida_simple_get(&kmem_limited_groups,
2964 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2965 if (num < 0)
2966 return num;
2967 /*
2968 * After this point, kmem_accounted (that we test atomically in
2969 * the beginning of this conditional), is no longer 0. This
2970 * guarantees only one process will set the following boolean
2971 * to true. We don't need test_and_set because we're protected
2972 * by the set_limit_mutex anyway.
2973 */
2974 memcg_kmem_set_activated(memcg);
2975
2976 ret = memcg_update_all_caches(num+1);
2977 if (ret) {
2978 ida_simple_remove(&kmem_limited_groups, num);
2979 memcg_kmem_clear_activated(memcg);
2980 return ret;
2981 }
2982
2983 memcg->kmemcg_id = num;
2984 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2985 mutex_init(&memcg->slab_caches_mutex);
2986 return 0;
2987}
2988
2989static size_t memcg_caches_array_size(int num_groups)
2990{
2991 ssize_t size;
2992 if (num_groups <= 0)
2993 return 0;
2994
2995 size = 2 * num_groups;
2996 if (size < MEMCG_CACHES_MIN_SIZE)
2997 size = MEMCG_CACHES_MIN_SIZE;
2998 else if (size > MEMCG_CACHES_MAX_SIZE)
2999 size = MEMCG_CACHES_MAX_SIZE;
3000
3001 return size;
3002}
3003
3004/*
3005 * We should update the current array size iff all caches updates succeed. This
3006 * can only be done from the slab side. The slab mutex needs to be held when
3007 * calling this.
3008 */
3009void memcg_update_array_size(int num)
3010{
3011 if (num > memcg_limited_groups_array_size)
3012 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3013}
3014
3015int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3016{
3017 struct memcg_cache_params *cur_params = s->memcg_params;
3018
3019 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3020
3021 if (num_groups > memcg_limited_groups_array_size) {
3022 int i;
3023 ssize_t size = memcg_caches_array_size(num_groups);
3024
3025 size *= sizeof(void *);
3026 size += sizeof(struct memcg_cache_params);
3027
3028 s->memcg_params = kzalloc(size, GFP_KERNEL);
3029 if (!s->memcg_params) {
3030 s->memcg_params = cur_params;
3031 return -ENOMEM;
3032 }
3033
3034 s->memcg_params->is_root_cache = true;
3035
3036 /*
3037 * There is the chance it will be bigger than
3038 * memcg_limited_groups_array_size, if we failed an allocation
3039 * in a cache, in which case all caches updated before it, will
3040 * have a bigger array.
3041 *
3042 * But if that is the case, the data after
3043 * memcg_limited_groups_array_size is certainly unused
3044 */
3045 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3046 if (!cur_params->memcg_caches[i])
3047 continue;
3048 s->memcg_params->memcg_caches[i] =
3049 cur_params->memcg_caches[i];
3050 }
3051
3052 /*
3053 * Ideally, we would wait until all caches succeed, and only
3054 * then free the old one. But this is not worth the extra
3055 * pointer per-cache we'd have to have for this.
3056 *
3057 * It is not a big deal if some caches are left with a size
3058 * bigger than the others. And all updates will reset this
3059 * anyway.
3060 */
3061 kfree(cur_params);
3062 }
3063 return 0;
3064}
3065
Glauber Costa943a4512012-12-18 14:23:03 -08003066int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3067 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003068{
3069 size_t size = sizeof(struct memcg_cache_params);
3070
3071 if (!memcg_kmem_enabled())
3072 return 0;
3073
Glauber Costa55007d82012-12-18 14:22:38 -08003074 if (!memcg)
3075 size += memcg_limited_groups_array_size * sizeof(void *);
3076
Glauber Costa2633d7a2012-12-18 14:22:34 -08003077 s->memcg_params = kzalloc(size, GFP_KERNEL);
3078 if (!s->memcg_params)
3079 return -ENOMEM;
3080
Glauber Costa943a4512012-12-18 14:23:03 -08003081 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003082 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003083 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003084 } else
3085 s->memcg_params->is_root_cache = true;
3086
Glauber Costa2633d7a2012-12-18 14:22:34 -08003087 return 0;
3088}
3089
3090void memcg_release_cache(struct kmem_cache *s)
3091{
Glauber Costad7f25f82012-12-18 14:22:40 -08003092 struct kmem_cache *root;
3093 struct mem_cgroup *memcg;
3094 int id;
3095
3096 /*
3097 * This happens, for instance, when a root cache goes away before we
3098 * add any memcg.
3099 */
3100 if (!s->memcg_params)
3101 return;
3102
3103 if (s->memcg_params->is_root_cache)
3104 goto out;
3105
3106 memcg = s->memcg_params->memcg;
3107 id = memcg_cache_id(memcg);
3108
3109 root = s->memcg_params->root_cache;
3110 root->memcg_params->memcg_caches[id] = NULL;
3111 mem_cgroup_put(memcg);
3112
3113 mutex_lock(&memcg->slab_caches_mutex);
3114 list_del(&s->memcg_params->list);
3115 mutex_unlock(&memcg->slab_caches_mutex);
3116
3117out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003118 kfree(s->memcg_params);
3119}
3120
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003121/*
3122 * During the creation a new cache, we need to disable our accounting mechanism
3123 * altogether. This is true even if we are not creating, but rather just
3124 * enqueing new caches to be created.
3125 *
3126 * This is because that process will trigger allocations; some visible, like
3127 * explicit kmallocs to auxiliary data structures, name strings and internal
3128 * cache structures; some well concealed, like INIT_WORK() that can allocate
3129 * objects during debug.
3130 *
3131 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3132 * to it. This may not be a bounded recursion: since the first cache creation
3133 * failed to complete (waiting on the allocation), we'll just try to create the
3134 * cache again, failing at the same point.
3135 *
3136 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3137 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3138 * inside the following two functions.
3139 */
3140static inline void memcg_stop_kmem_account(void)
3141{
3142 VM_BUG_ON(!current->mm);
3143 current->memcg_kmem_skip_account++;
3144}
3145
3146static inline void memcg_resume_kmem_account(void)
3147{
3148 VM_BUG_ON(!current->mm);
3149 current->memcg_kmem_skip_account--;
3150}
3151
Glauber Costa1f458cb2012-12-18 14:22:50 -08003152static void kmem_cache_destroy_work_func(struct work_struct *w)
3153{
3154 struct kmem_cache *cachep;
3155 struct memcg_cache_params *p;
3156
3157 p = container_of(w, struct memcg_cache_params, destroy);
3158
3159 cachep = memcg_params_to_cache(p);
3160
Glauber Costa22933152012-12-18 14:22:59 -08003161 /*
3162 * If we get down to 0 after shrink, we could delete right away.
3163 * However, memcg_release_pages() already puts us back in the workqueue
3164 * in that case. If we proceed deleting, we'll get a dangling
3165 * reference, and removing the object from the workqueue in that case
3166 * is unnecessary complication. We are not a fast path.
3167 *
3168 * Note that this case is fundamentally different from racing with
3169 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3170 * kmem_cache_shrink, not only we would be reinserting a dead cache
3171 * into the queue, but doing so from inside the worker racing to
3172 * destroy it.
3173 *
3174 * So if we aren't down to zero, we'll just schedule a worker and try
3175 * again
3176 */
3177 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3178 kmem_cache_shrink(cachep);
3179 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3180 return;
3181 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003182 kmem_cache_destroy(cachep);
3183}
3184
3185void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3186{
3187 if (!cachep->memcg_params->dead)
3188 return;
3189
3190 /*
Glauber Costa22933152012-12-18 14:22:59 -08003191 * There are many ways in which we can get here.
3192 *
3193 * We can get to a memory-pressure situation while the delayed work is
3194 * still pending to run. The vmscan shrinkers can then release all
3195 * cache memory and get us to destruction. If this is the case, we'll
3196 * be executed twice, which is a bug (the second time will execute over
3197 * bogus data). In this case, cancelling the work should be fine.
3198 *
3199 * But we can also get here from the worker itself, if
3200 * kmem_cache_shrink is enough to shake all the remaining objects and
3201 * get the page count to 0. In this case, we'll deadlock if we try to
3202 * cancel the work (the worker runs with an internal lock held, which
3203 * is the same lock we would hold for cancel_work_sync().)
3204 *
3205 * Since we can't possibly know who got us here, just refrain from
3206 * running if there is already work pending
3207 */
3208 if (work_pending(&cachep->memcg_params->destroy))
3209 return;
3210 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003211 * We have to defer the actual destroying to a workqueue, because
3212 * we might currently be in a context that cannot sleep.
3213 */
3214 schedule_work(&cachep->memcg_params->destroy);
3215}
3216
Glauber Costad7f25f82012-12-18 14:22:40 -08003217/*
3218 * This lock protects updaters, not readers. We want readers to be as fast as
3219 * they can, and they will either see NULL or a valid cache value. Our model
3220 * allow them to see NULL, in which case the root memcg will be selected.
3221 *
3222 * We need this lock because multiple allocations to the same cache from a non
3223 * will span more than one worker. Only one of them can create the cache.
3224 */
3225static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003226
3227/*
3228 * Called with memcg_cache_mutex held
3229 */
3230static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3231 struct kmem_cache *s)
3232{
3233 struct kmem_cache *new;
3234 static char *tmp_name = NULL;
3235
3236 lockdep_assert_held(&memcg_cache_mutex);
3237
3238 /*
3239 * kmem_cache_create_memcg duplicates the given name and
3240 * cgroup_name for this name requires RCU context.
3241 * This static temporary buffer is used to prevent from
3242 * pointless shortliving allocation.
3243 */
3244 if (!tmp_name) {
3245 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3246 if (!tmp_name)
3247 return NULL;
3248 }
3249
3250 rcu_read_lock();
3251 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3252 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3253 rcu_read_unlock();
3254
3255 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3256 (s->flags & ~SLAB_PANIC), s->ctor, s);
3257
3258 if (new)
3259 new->allocflags |= __GFP_KMEMCG;
3260
3261 return new;
3262}
3263
Glauber Costad7f25f82012-12-18 14:22:40 -08003264static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3265 struct kmem_cache *cachep)
3266{
3267 struct kmem_cache *new_cachep;
3268 int idx;
3269
3270 BUG_ON(!memcg_can_account_kmem(memcg));
3271
3272 idx = memcg_cache_id(memcg);
3273
3274 mutex_lock(&memcg_cache_mutex);
3275 new_cachep = cachep->memcg_params->memcg_caches[idx];
3276 if (new_cachep)
3277 goto out;
3278
3279 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003280 if (new_cachep == NULL) {
3281 new_cachep = cachep;
3282 goto out;
3283 }
3284
3285 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003286 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003287
3288 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3289 /*
3290 * the readers won't lock, make sure everybody sees the updated value,
3291 * so they won't put stuff in the queue again for no reason
3292 */
3293 wmb();
3294out:
3295 mutex_unlock(&memcg_cache_mutex);
3296 return new_cachep;
3297}
3298
Glauber Costa7cf27982012-12-18 14:22:55 -08003299void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3300{
3301 struct kmem_cache *c;
3302 int i;
3303
3304 if (!s->memcg_params)
3305 return;
3306 if (!s->memcg_params->is_root_cache)
3307 return;
3308
3309 /*
3310 * If the cache is being destroyed, we trust that there is no one else
3311 * requesting objects from it. Even if there are, the sanity checks in
3312 * kmem_cache_destroy should caught this ill-case.
3313 *
3314 * Still, we don't want anyone else freeing memcg_caches under our
3315 * noses, which can happen if a new memcg comes to life. As usual,
3316 * we'll take the set_limit_mutex to protect ourselves against this.
3317 */
3318 mutex_lock(&set_limit_mutex);
3319 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3320 c = s->memcg_params->memcg_caches[i];
3321 if (!c)
3322 continue;
3323
3324 /*
3325 * We will now manually delete the caches, so to avoid races
3326 * we need to cancel all pending destruction workers and
3327 * proceed with destruction ourselves.
3328 *
3329 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3330 * and that could spawn the workers again: it is likely that
3331 * the cache still have active pages until this very moment.
3332 * This would lead us back to mem_cgroup_destroy_cache.
3333 *
3334 * But that will not execute at all if the "dead" flag is not
3335 * set, so flip it down to guarantee we are in control.
3336 */
3337 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003338 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003339 kmem_cache_destroy(c);
3340 }
3341 mutex_unlock(&set_limit_mutex);
3342}
3343
Glauber Costad7f25f82012-12-18 14:22:40 -08003344struct create_work {
3345 struct mem_cgroup *memcg;
3346 struct kmem_cache *cachep;
3347 struct work_struct work;
3348};
3349
Glauber Costa1f458cb2012-12-18 14:22:50 -08003350static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3351{
3352 struct kmem_cache *cachep;
3353 struct memcg_cache_params *params;
3354
3355 if (!memcg_kmem_is_active(memcg))
3356 return;
3357
3358 mutex_lock(&memcg->slab_caches_mutex);
3359 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3360 cachep = memcg_params_to_cache(params);
3361 cachep->memcg_params->dead = true;
3362 INIT_WORK(&cachep->memcg_params->destroy,
Glauber Costa22933152012-12-18 14:22:59 -08003363 kmem_cache_destroy_work_func);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003364 schedule_work(&cachep->memcg_params->destroy);
3365 }
3366 mutex_unlock(&memcg->slab_caches_mutex);
3367}
3368
Glauber Costad7f25f82012-12-18 14:22:40 -08003369static void memcg_create_cache_work_func(struct work_struct *w)
3370{
3371 struct create_work *cw;
3372
3373 cw = container_of(w, struct create_work, work);
3374 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3375 /* Drop the reference gotten when we enqueued. */
3376 css_put(&cw->memcg->css);
3377 kfree(cw);
3378}
3379
3380/*
3381 * Enqueue the creation of a per-memcg kmem_cache.
3382 * Called with rcu_read_lock.
3383 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003384static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3385 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003386{
3387 struct create_work *cw;
3388
3389 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3390 if (cw == NULL)
3391 return;
3392
3393 /* The corresponding put will be done in the workqueue. */
3394 if (!css_tryget(&memcg->css)) {
3395 kfree(cw);
3396 return;
3397 }
3398
3399 cw->memcg = memcg;
3400 cw->cachep = cachep;
3401
3402 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3403 schedule_work(&cw->work);
3404}
3405
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003406static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3407 struct kmem_cache *cachep)
3408{
3409 /*
3410 * We need to stop accounting when we kmalloc, because if the
3411 * corresponding kmalloc cache is not yet created, the first allocation
3412 * in __memcg_create_cache_enqueue will recurse.
3413 *
3414 * However, it is better to enclose the whole function. Depending on
3415 * the debugging options enabled, INIT_WORK(), for instance, can
3416 * trigger an allocation. This too, will make us recurse. Because at
3417 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3418 * the safest choice is to do it like this, wrapping the whole function.
3419 */
3420 memcg_stop_kmem_account();
3421 __memcg_create_cache_enqueue(memcg, cachep);
3422 memcg_resume_kmem_account();
3423}
Glauber Costad7f25f82012-12-18 14:22:40 -08003424/*
3425 * Return the kmem_cache we're supposed to use for a slab allocation.
3426 * We try to use the current memcg's version of the cache.
3427 *
3428 * If the cache does not exist yet, if we are the first user of it,
3429 * we either create it immediately, if possible, or create it asynchronously
3430 * in a workqueue.
3431 * In the latter case, we will let the current allocation go through with
3432 * the original cache.
3433 *
3434 * Can't be called in interrupt context or from kernel threads.
3435 * This function needs to be called with rcu_read_lock() held.
3436 */
3437struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3438 gfp_t gfp)
3439{
3440 struct mem_cgroup *memcg;
3441 int idx;
3442
3443 VM_BUG_ON(!cachep->memcg_params);
3444 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3445
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003446 if (!current->mm || current->memcg_kmem_skip_account)
3447 return cachep;
3448
Glauber Costad7f25f82012-12-18 14:22:40 -08003449 rcu_read_lock();
3450 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3451 rcu_read_unlock();
3452
3453 if (!memcg_can_account_kmem(memcg))
3454 return cachep;
3455
3456 idx = memcg_cache_id(memcg);
3457
3458 /*
3459 * barrier to mare sure we're always seeing the up to date value. The
3460 * code updating memcg_caches will issue a write barrier to match this.
3461 */
3462 read_barrier_depends();
3463 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3464 /*
3465 * If we are in a safe context (can wait, and not in interrupt
3466 * context), we could be be predictable and return right away.
3467 * This would guarantee that the allocation being performed
3468 * already belongs in the new cache.
3469 *
3470 * However, there are some clashes that can arrive from locking.
3471 * For instance, because we acquire the slab_mutex while doing
3472 * kmem_cache_dup, this means no further allocation could happen
3473 * with the slab_mutex held.
3474 *
3475 * Also, because cache creation issue get_online_cpus(), this
3476 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3477 * that ends up reversed during cpu hotplug. (cpuset allocates
3478 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3479 * better to defer everything.
3480 */
3481 memcg_create_cache_enqueue(memcg, cachep);
3482 return cachep;
3483 }
3484
3485 return cachep->memcg_params->memcg_caches[idx];
3486}
3487EXPORT_SYMBOL(__memcg_kmem_get_cache);
3488
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003489/*
3490 * We need to verify if the allocation against current->mm->owner's memcg is
3491 * possible for the given order. But the page is not allocated yet, so we'll
3492 * need a further commit step to do the final arrangements.
3493 *
3494 * It is possible for the task to switch cgroups in this mean time, so at
3495 * commit time, we can't rely on task conversion any longer. We'll then use
3496 * the handle argument to return to the caller which cgroup we should commit
3497 * against. We could also return the memcg directly and avoid the pointer
3498 * passing, but a boolean return value gives better semantics considering
3499 * the compiled-out case as well.
3500 *
3501 * Returning true means the allocation is possible.
3502 */
3503bool
3504__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3505{
3506 struct mem_cgroup *memcg;
3507 int ret;
3508
3509 *_memcg = NULL;
3510 memcg = try_get_mem_cgroup_from_mm(current->mm);
3511
3512 /*
3513 * very rare case described in mem_cgroup_from_task. Unfortunately there
3514 * isn't much we can do without complicating this too much, and it would
3515 * be gfp-dependent anyway. Just let it go
3516 */
3517 if (unlikely(!memcg))
3518 return true;
3519
3520 if (!memcg_can_account_kmem(memcg)) {
3521 css_put(&memcg->css);
3522 return true;
3523 }
3524
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003525 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3526 if (!ret)
3527 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003528
3529 css_put(&memcg->css);
3530 return (ret == 0);
3531}
3532
3533void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3534 int order)
3535{
3536 struct page_cgroup *pc;
3537
3538 VM_BUG_ON(mem_cgroup_is_root(memcg));
3539
3540 /* The page allocation failed. Revert */
3541 if (!page) {
3542 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003543 return;
3544 }
3545
3546 pc = lookup_page_cgroup(page);
3547 lock_page_cgroup(pc);
3548 pc->mem_cgroup = memcg;
3549 SetPageCgroupUsed(pc);
3550 unlock_page_cgroup(pc);
3551}
3552
3553void __memcg_kmem_uncharge_pages(struct page *page, int order)
3554{
3555 struct mem_cgroup *memcg = NULL;
3556 struct page_cgroup *pc;
3557
3558
3559 pc = lookup_page_cgroup(page);
3560 /*
3561 * Fast unlocked return. Theoretically might have changed, have to
3562 * check again after locking.
3563 */
3564 if (!PageCgroupUsed(pc))
3565 return;
3566
3567 lock_page_cgroup(pc);
3568 if (PageCgroupUsed(pc)) {
3569 memcg = pc->mem_cgroup;
3570 ClearPageCgroupUsed(pc);
3571 }
3572 unlock_page_cgroup(pc);
3573
3574 /*
3575 * We trust that only if there is a memcg associated with the page, it
3576 * is a valid allocation
3577 */
3578 if (!memcg)
3579 return;
3580
3581 VM_BUG_ON(mem_cgroup_is_root(memcg));
3582 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003583}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003584#else
3585static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3586{
3587}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003588#endif /* CONFIG_MEMCG_KMEM */
3589
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003590#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3591
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003592#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003593/*
3594 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003595 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3596 * charge/uncharge will be never happen and move_account() is done under
3597 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003598 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003599void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003600{
3601 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003602 struct page_cgroup *pc;
3603 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003604
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003605 if (mem_cgroup_disabled())
3606 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003607 for (i = 1; i < HPAGE_PMD_NR; i++) {
3608 pc = head_pc + i;
3609 pc->mem_cgroup = head_pc->mem_cgroup;
3610 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003611 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3612 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003613}
Hugh Dickins12d27102012-01-12 17:19:52 -08003614#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003615
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003616/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003617 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003618 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003619 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003620 * @pc: page_cgroup of the page.
3621 * @from: mem_cgroup which the page is moved from.
3622 * @to: mem_cgroup which the page is moved to. @from != @to.
3623 *
3624 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003625 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003626 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003627 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003628 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3629 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003630 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003631static int mem_cgroup_move_account(struct page *page,
3632 unsigned int nr_pages,
3633 struct page_cgroup *pc,
3634 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003635 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003636{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003637 unsigned long flags;
3638 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003639 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003640
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003641 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003642 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003643 /*
3644 * The page is isolated from LRU. So, collapse function
3645 * will not handle this page. But page splitting can happen.
3646 * Do this check under compound_page_lock(). The caller should
3647 * hold it.
3648 */
3649 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003650 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003651 goto out;
3652
3653 lock_page_cgroup(pc);
3654
3655 ret = -EINVAL;
3656 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3657 goto unlock;
3658
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003659 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003660
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003661 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003662 /* Update mapped_file data for mem_cgroup */
3663 preempt_disable();
3664 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3665 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3666 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003667 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003668 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003669
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003670 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003671 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003672 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003673 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003674 ret = 0;
3675unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003676 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003677 /*
3678 * check events
3679 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003680 memcg_check_events(to, page);
3681 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003682out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003683 return ret;
3684}
3685
Michal Hocko2ef37d32012-10-26 13:37:30 +02003686/**
3687 * mem_cgroup_move_parent - moves page to the parent group
3688 * @page: the page to move
3689 * @pc: page_cgroup of the page
3690 * @child: page's cgroup
3691 *
3692 * move charges to its parent or the root cgroup if the group has no
3693 * parent (aka use_hierarchy==0).
3694 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3695 * mem_cgroup_move_account fails) the failure is always temporary and
3696 * it signals a race with a page removal/uncharge or migration. In the
3697 * first case the page is on the way out and it will vanish from the LRU
3698 * on the next attempt and the call should be retried later.
3699 * Isolation from the LRU fails only if page has been isolated from
3700 * the LRU since we looked at it and that usually means either global
3701 * reclaim or migration going on. The page will either get back to the
3702 * LRU or vanish.
3703 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3704 * (!PageCgroupUsed) or moved to a different group. The page will
3705 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003706 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003707static int mem_cgroup_move_parent(struct page *page,
3708 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003709 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003711 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003712 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003713 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003714 int ret;
3715
Michal Hockod8423012012-10-26 13:37:29 +02003716 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003717
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003718 ret = -EBUSY;
3719 if (!get_page_unless_zero(page))
3720 goto out;
3721 if (isolate_lru_page(page))
3722 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003723
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003724 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003725
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003726 parent = parent_mem_cgroup(child);
3727 /*
3728 * If no parent, move charges to root cgroup.
3729 */
3730 if (!parent)
3731 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003732
Michal Hocko2ef37d32012-10-26 13:37:30 +02003733 if (nr_pages > 1) {
3734 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003735 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003736 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003737
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003738 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003739 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003740 if (!ret)
3741 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003742
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003743 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003744 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003745 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003746put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003747 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003748out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003749 return ret;
3750}
3751
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003752/*
3753 * Charge the memory controller for page usage.
3754 * Return
3755 * 0 if the charge was successful
3756 * < 0 if the cgroup is over its limit
3757 */
3758static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003759 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003760{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003761 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003762 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003763 bool oom = true;
3764 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003765
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003766 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003767 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003768 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003769 /*
3770 * Never OOM-kill a process for a huge page. The
3771 * fault handler will fall back to regular pages.
3772 */
3773 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003774 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003775
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003776 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003777 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003778 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003779 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003780 return 0;
3781}
3782
3783int mem_cgroup_newpage_charge(struct page *page,
3784 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003785{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003786 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003787 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003788 VM_BUG_ON(page_mapped(page));
3789 VM_BUG_ON(page->mapping && !PageAnon(page));
3790 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003791 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003792 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003793}
3794
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003795/*
3796 * While swap-in, try_charge -> commit or cancel, the page is locked.
3797 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003798 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003799 * "commit()" or removed by "cancel()"
3800 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003801static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3802 struct page *page,
3803 gfp_t mask,
3804 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003805{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003806 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003807 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003808 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003809
Johannes Weiner90deb782012-07-31 16:45:47 -07003810 pc = lookup_page_cgroup(page);
3811 /*
3812 * Every swap fault against a single page tries to charge the
3813 * page, bail as early as possible. shmem_unuse() encounters
3814 * already charged pages, too. The USED bit is protected by
3815 * the page lock, which serializes swap cache removal, which
3816 * in turn serializes uncharging.
3817 */
3818 if (PageCgroupUsed(pc))
3819 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003820 if (!do_swap_account)
3821 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003822 memcg = try_get_mem_cgroup_from_page(page);
3823 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003824 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003825 *memcgp = memcg;
3826 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003827 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003828 if (ret == -EINTR)
3829 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003830 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003831charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003832 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3833 if (ret == -EINTR)
3834 ret = 0;
3835 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003836}
3837
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003838int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3839 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3840{
3841 *memcgp = NULL;
3842 if (mem_cgroup_disabled())
3843 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003844 /*
3845 * A racing thread's fault, or swapoff, may have already
3846 * updated the pte, and even removed page from swap cache: in
3847 * those cases unuse_pte()'s pte_same() test will fail; but
3848 * there's also a KSM case which does need to charge the page.
3849 */
3850 if (!PageSwapCache(page)) {
3851 int ret;
3852
3853 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3854 if (ret == -EINTR)
3855 ret = 0;
3856 return ret;
3857 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003858 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3859}
3860
Johannes Weiner827a03d2012-07-31 16:45:36 -07003861void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3862{
3863 if (mem_cgroup_disabled())
3864 return;
3865 if (!memcg)
3866 return;
3867 __mem_cgroup_cancel_charge(memcg, 1);
3868}
3869
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003870static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003871__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003872 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003873{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003874 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003875 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003876 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003877 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003878
Johannes Weinerce587e62012-04-24 20:22:33 +02003879 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003880 /*
3881 * Now swap is on-memory. This means this page may be
3882 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003883 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3884 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3885 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003886 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003887 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003888 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003889 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003890 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003891}
3892
Johannes Weiner72835c82012-01-12 17:18:32 -08003893void mem_cgroup_commit_charge_swapin(struct page *page,
3894 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003895{
Johannes Weiner72835c82012-01-12 17:18:32 -08003896 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003897 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003898}
3899
Johannes Weiner827a03d2012-07-31 16:45:36 -07003900int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3901 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003902{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003903 struct mem_cgroup *memcg = NULL;
3904 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3905 int ret;
3906
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003907 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003908 return 0;
3909 if (PageCompound(page))
3910 return 0;
3911
Johannes Weiner827a03d2012-07-31 16:45:36 -07003912 if (!PageSwapCache(page))
3913 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3914 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003915 ret = __mem_cgroup_try_charge_swapin(mm, page,
3916 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003917 if (!ret)
3918 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3919 }
3920 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003921}
3922
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003923static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003924 unsigned int nr_pages,
3925 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003926{
3927 struct memcg_batch_info *batch = NULL;
3928 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003929
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003930 /* If swapout, usage of swap doesn't decrease */
3931 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3932 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003933
3934 batch = &current->memcg_batch;
3935 /*
3936 * In usual, we do css_get() when we remember memcg pointer.
3937 * But in this case, we keep res->usage until end of a series of
3938 * uncharges. Then, it's ok to ignore memcg's refcnt.
3939 */
3940 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003941 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003942 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003943 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003944 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003945 * the same cgroup and we have chance to coalesce uncharges.
3946 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3947 * because we want to do uncharge as soon as possible.
3948 */
3949
3950 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3951 goto direct_uncharge;
3952
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003953 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003954 goto direct_uncharge;
3955
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003956 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003957 * In typical case, batch->memcg == mem. This means we can
3958 * merge a series of uncharges to an uncharge of res_counter.
3959 * If not, we uncharge res_counter ony by one.
3960 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003961 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003962 goto direct_uncharge;
3963 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003964 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003965 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003966 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003967 return;
3968direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003969 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003970 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003971 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3972 if (unlikely(batch->memcg != memcg))
3973 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003974}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003975
Balbir Singh8697d332008-02-07 00:13:59 -08003976/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003977 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003978 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003979static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003980__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3981 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003982{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003983 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003984 unsigned int nr_pages = 1;
3985 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003986 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003987
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003988 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003989 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003990
Johannes Weiner0c59b892012-07-31 16:45:31 -07003991 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003992
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003993 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003994 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003995 VM_BUG_ON(!PageTransHuge(page));
3996 }
Balbir Singh8697d332008-02-07 00:13:59 -08003997 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003998 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003999 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004000 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004001 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004002 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004003
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004004 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004005
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004006 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004007
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004008 if (!PageCgroupUsed(pc))
4009 goto unlock_out;
4010
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004011 anon = PageAnon(page);
4012
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004013 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004014 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004015 /*
4016 * Generally PageAnon tells if it's the anon statistics to be
4017 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4018 * used before page reached the stage of being marked PageAnon.
4019 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004020 anon = true;
4021 /* fallthrough */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07004022 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004023 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004024 if (page_mapped(page))
4025 goto unlock_out;
4026 /*
4027 * Pages under migration may not be uncharged. But
4028 * end_migration() /must/ be the one uncharging the
4029 * unused post-migration page and so it has to call
4030 * here with the migration bit still set. See the
4031 * res_counter handling below.
4032 */
4033 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004034 goto unlock_out;
4035 break;
4036 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4037 if (!PageAnon(page)) { /* Shared memory */
4038 if (page->mapping && !page_is_file_cache(page))
4039 goto unlock_out;
4040 } else if (page_mapped(page)) /* Anon */
4041 goto unlock_out;
4042 break;
4043 default:
4044 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004045 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004046
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004047 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004048
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004049 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004050 /*
4051 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4052 * freed from LRU. This is safe because uncharged page is expected not
4053 * to be reused (freed soon). Exception is SwapCache, it's handled by
4054 * special functions.
4055 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004056
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004057 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004058 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004059 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004060 * will never be freed.
4061 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004062 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004063 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004064 mem_cgroup_swap_statistics(memcg, true);
4065 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004066 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004067 /*
4068 * Migration does not charge the res_counter for the
4069 * replacement page, so leave it alone when phasing out the
4070 * page that is unused after the migration.
4071 */
4072 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004074
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004075 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004076
4077unlock_out:
4078 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004079 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004080}
4081
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004082void mem_cgroup_uncharge_page(struct page *page)
4083{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004084 /* early check. */
4085 if (page_mapped(page))
4086 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004087 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004088 if (PageSwapCache(page))
4089 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004090 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004091}
4092
4093void mem_cgroup_uncharge_cache_page(struct page *page)
4094{
4095 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004096 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004097 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004098}
4099
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004100/*
4101 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4102 * In that cases, pages are freed continuously and we can expect pages
4103 * are in the same memcg. All these calls itself limits the number of
4104 * pages freed at once, then uncharge_start/end() is called properly.
4105 * This may be called prural(2) times in a context,
4106 */
4107
4108void mem_cgroup_uncharge_start(void)
4109{
4110 current->memcg_batch.do_batch++;
4111 /* We can do nest. */
4112 if (current->memcg_batch.do_batch == 1) {
4113 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004114 current->memcg_batch.nr_pages = 0;
4115 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004116 }
4117}
4118
4119void mem_cgroup_uncharge_end(void)
4120{
4121 struct memcg_batch_info *batch = &current->memcg_batch;
4122
4123 if (!batch->do_batch)
4124 return;
4125
4126 batch->do_batch--;
4127 if (batch->do_batch) /* If stacked, do nothing. */
4128 return;
4129
4130 if (!batch->memcg)
4131 return;
4132 /*
4133 * This "batch->memcg" is valid without any css_get/put etc...
4134 * bacause we hide charges behind us.
4135 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004136 if (batch->nr_pages)
4137 res_counter_uncharge(&batch->memcg->res,
4138 batch->nr_pages * PAGE_SIZE);
4139 if (batch->memsw_nr_pages)
4140 res_counter_uncharge(&batch->memcg->memsw,
4141 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004142 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004143 /* forget this pointer (for sanity check) */
4144 batch->memcg = NULL;
4145}
4146
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004147#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004148/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004149 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004150 * memcg information is recorded to swap_cgroup of "ent"
4151 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07004152void
4153mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004154{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004155 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07004156 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004157
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07004158 if (!swapout) /* this was a swap cache but the swap is unused ! */
4159 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4160
Johannes Weiner0030f532012-07-31 16:45:25 -07004161 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07004162
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004163 /*
4164 * record memcg information, if swapout && memcg != NULL,
4165 * mem_cgroup_get() was called in uncharge().
4166 */
4167 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004168 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004169}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004170#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004171
Andrew Mortonc255a452012-07-31 16:43:02 -07004172#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004173/*
4174 * called from swap_entry_free(). remove record in swap_cgroup and
4175 * uncharge "memsw" account.
4176 */
4177void mem_cgroup_uncharge_swap(swp_entry_t ent)
4178{
4179 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004180 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004181
4182 if (!do_swap_account)
4183 return;
4184
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004185 id = swap_cgroup_record(ent, 0);
4186 rcu_read_lock();
4187 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004188 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004189 /*
4190 * We uncharge this because swap is freed.
4191 * This memcg can be obsolete one. We avoid calling css_tryget
4192 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004193 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004194 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004195 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004196 mem_cgroup_put(memcg);
4197 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004198 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004199}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004200
4201/**
4202 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4203 * @entry: swap entry to be moved
4204 * @from: mem_cgroup which the entry is moved from
4205 * @to: mem_cgroup which the entry is moved to
4206 *
4207 * It succeeds only when the swap_cgroup's record for this entry is the same
4208 * as the mem_cgroup's id of @from.
4209 *
4210 * Returns 0 on success, -EINVAL on failure.
4211 *
4212 * The caller must have charged to @to, IOW, called res_counter_charge() about
4213 * both res and memsw, and called css_get().
4214 */
4215static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004216 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004217{
4218 unsigned short old_id, new_id;
4219
4220 old_id = css_id(&from->css);
4221 new_id = css_id(&to->css);
4222
4223 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004224 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004225 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004226 /*
4227 * This function is only called from task migration context now.
4228 * It postpones res_counter and refcount handling till the end
4229 * of task migration(mem_cgroup_clear_mc()) for performance
4230 * improvement. But we cannot postpone mem_cgroup_get(to)
4231 * because if the process that has been moved to @to does
4232 * swap-in, the refcount of @to might be decreased to 0.
4233 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004234 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004235 return 0;
4236 }
4237 return -EINVAL;
4238}
4239#else
4240static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004241 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004242{
4243 return -EINVAL;
4244}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004245#endif
4246
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004247/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004248 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4249 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004250 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004251void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4252 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004253{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004254 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004255 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004256 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004257 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004258
Johannes Weiner72835c82012-01-12 17:18:32 -08004259 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004260
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004261 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004262 return;
Balbir Singh40779602008-04-04 14:29:59 -07004263
Mel Gormanb32967f2012-11-19 12:35:47 +00004264 if (PageTransHuge(page))
4265 nr_pages <<= compound_order(page);
4266
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004267 pc = lookup_page_cgroup(page);
4268 lock_page_cgroup(pc);
4269 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004270 memcg = pc->mem_cgroup;
4271 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004272 /*
4273 * At migrating an anonymous page, its mapcount goes down
4274 * to 0 and uncharge() will be called. But, even if it's fully
4275 * unmapped, migration may fail and this page has to be
4276 * charged again. We set MIGRATION flag here and delay uncharge
4277 * until end_migration() is called
4278 *
4279 * Corner Case Thinking
4280 * A)
4281 * When the old page was mapped as Anon and it's unmap-and-freed
4282 * while migration was ongoing.
4283 * If unmap finds the old page, uncharge() of it will be delayed
4284 * until end_migration(). If unmap finds a new page, it's
4285 * uncharged when it make mapcount to be 1->0. If unmap code
4286 * finds swap_migration_entry, the new page will not be mapped
4287 * and end_migration() will find it(mapcount==0).
4288 *
4289 * B)
4290 * When the old page was mapped but migraion fails, the kernel
4291 * remaps it. A charge for it is kept by MIGRATION flag even
4292 * if mapcount goes down to 0. We can do remap successfully
4293 * without charging it again.
4294 *
4295 * C)
4296 * The "old" page is under lock_page() until the end of
4297 * migration, so, the old page itself will not be swapped-out.
4298 * If the new page is swapped out before end_migraton, our
4299 * hook to usual swap-out path will catch the event.
4300 */
4301 if (PageAnon(page))
4302 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004303 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004304 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004305 /*
4306 * If the page is not charged at this point,
4307 * we return here.
4308 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004309 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004310 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004311
Johannes Weiner72835c82012-01-12 17:18:32 -08004312 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004313 /*
4314 * We charge new page before it's used/mapped. So, even if unlock_page()
4315 * is called before end_migration, we can catch all events on this new
4316 * page. In the case new page is migrated but not remapped, new page's
4317 * mapcount will be finally 0 and we call uncharge in end_migration().
4318 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004319 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004320 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004321 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004322 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004323 /*
4324 * The page is committed to the memcg, but it's not actually
4325 * charged to the res_counter since we plan on replacing the
4326 * old one and only one page is going to be left afterwards.
4327 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004328 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004329}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004330
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004331/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004332void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004333 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004334{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004335 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004336 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004337 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004338
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004339 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004340 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004341
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004342 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004343 used = oldpage;
4344 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004345 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004346 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004347 unused = oldpage;
4348 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004349 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004350 __mem_cgroup_uncharge_common(unused,
4351 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4352 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4353 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004354 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004355 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004356 * We disallowed uncharge of pages under migration because mapcount
4357 * of the page goes down to zero, temporarly.
4358 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004359 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004360 pc = lookup_page_cgroup(oldpage);
4361 lock_page_cgroup(pc);
4362 ClearPageCgroupMigration(pc);
4363 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004364
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004365 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004366 * If a page is a file cache, radix-tree replacement is very atomic
4367 * and we can skip this check. When it was an Anon page, its mapcount
4368 * goes down to 0. But because we added MIGRATION flage, it's not
4369 * uncharged yet. There are several case but page->mapcount check
4370 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4371 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004372 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004373 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004374 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004375}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004376
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004377/*
4378 * At replace page cache, newpage is not under any memcg but it's on
4379 * LRU. So, this function doesn't touch res_counter but handles LRU
4380 * in correct way. Both pages are locked so we cannot race with uncharge.
4381 */
4382void mem_cgroup_replace_page_cache(struct page *oldpage,
4383 struct page *newpage)
4384{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004385 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004386 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004387 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004388
4389 if (mem_cgroup_disabled())
4390 return;
4391
4392 pc = lookup_page_cgroup(oldpage);
4393 /* fix accounting on old pages */
4394 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004395 if (PageCgroupUsed(pc)) {
4396 memcg = pc->mem_cgroup;
4397 mem_cgroup_charge_statistics(memcg, false, -1);
4398 ClearPageCgroupUsed(pc);
4399 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004400 unlock_page_cgroup(pc);
4401
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004402 /*
4403 * When called from shmem_replace_page(), in some cases the
4404 * oldpage has already been charged, and in some cases not.
4405 */
4406 if (!memcg)
4407 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004408 /*
4409 * Even if newpage->mapping was NULL before starting replacement,
4410 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4411 * LRU while we overwrite pc->mem_cgroup.
4412 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004413 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004414}
4415
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004416#ifdef CONFIG_DEBUG_VM
4417static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4418{
4419 struct page_cgroup *pc;
4420
4421 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004422 /*
4423 * Can be NULL while feeding pages into the page allocator for
4424 * the first time, i.e. during boot or memory hotplug;
4425 * or when mem_cgroup_disabled().
4426 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004427 if (likely(pc) && PageCgroupUsed(pc))
4428 return pc;
4429 return NULL;
4430}
4431
4432bool mem_cgroup_bad_page_check(struct page *page)
4433{
4434 if (mem_cgroup_disabled())
4435 return false;
4436
4437 return lookup_page_cgroup_used(page) != NULL;
4438}
4439
4440void mem_cgroup_print_bad_page(struct page *page)
4441{
4442 struct page_cgroup *pc;
4443
4444 pc = lookup_page_cgroup_used(page);
4445 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004446 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4447 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004448 }
4449}
4450#endif
4451
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004452static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004453 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004454{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004455 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004456 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004457 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004458 int children = mem_cgroup_count_children(memcg);
4459 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004460 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004461
4462 /*
4463 * For keeping hierarchical_reclaim simple, how long we should retry
4464 * is depends on callers. We set our retry-count to be function
4465 * of # of children which we should visit in this loop.
4466 */
4467 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4468
4469 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004470
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004471 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004472 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004473 if (signal_pending(current)) {
4474 ret = -EINTR;
4475 break;
4476 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004477 /*
4478 * Rather than hide all in some function, I do this in
4479 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004480 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004481 */
4482 mutex_lock(&set_limit_mutex);
4483 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4484 if (memswlimit < val) {
4485 ret = -EINVAL;
4486 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004487 break;
4488 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004489
4490 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4491 if (memlimit < val)
4492 enlarge = 1;
4493
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004494 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004495 if (!ret) {
4496 if (memswlimit == val)
4497 memcg->memsw_is_minimum = true;
4498 else
4499 memcg->memsw_is_minimum = false;
4500 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004501 mutex_unlock(&set_limit_mutex);
4502
4503 if (!ret)
4504 break;
4505
Johannes Weiner56600482012-01-12 17:17:59 -08004506 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4507 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004508 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4509 /* Usage is reduced ? */
4510 if (curusage >= oldusage)
4511 retry_count--;
4512 else
4513 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004514 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004515 if (!ret && enlarge)
4516 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004517
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004518 return ret;
4519}
4520
Li Zefan338c8432009-06-17 16:27:15 -07004521static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4522 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004523{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004524 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004525 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004526 int children = mem_cgroup_count_children(memcg);
4527 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004528 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004529
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004530 /* see mem_cgroup_resize_res_limit */
4531 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4532 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004533 while (retry_count) {
4534 if (signal_pending(current)) {
4535 ret = -EINTR;
4536 break;
4537 }
4538 /*
4539 * Rather than hide all in some function, I do this in
4540 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004541 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004542 */
4543 mutex_lock(&set_limit_mutex);
4544 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4545 if (memlimit > val) {
4546 ret = -EINVAL;
4547 mutex_unlock(&set_limit_mutex);
4548 break;
4549 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004550 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4551 if (memswlimit < val)
4552 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004553 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004554 if (!ret) {
4555 if (memlimit == val)
4556 memcg->memsw_is_minimum = true;
4557 else
4558 memcg->memsw_is_minimum = false;
4559 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004560 mutex_unlock(&set_limit_mutex);
4561
4562 if (!ret)
4563 break;
4564
Johannes Weiner56600482012-01-12 17:17:59 -08004565 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4566 MEM_CGROUP_RECLAIM_NOSWAP |
4567 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004568 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004569 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004570 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004571 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004572 else
4573 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004574 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004575 if (!ret && enlarge)
4576 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004577 return ret;
4578}
4579
Balbir Singh4e416952009-09-23 15:56:39 -07004580unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004581 gfp_t gfp_mask,
4582 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004583{
4584 unsigned long nr_reclaimed = 0;
4585 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4586 unsigned long reclaimed;
4587 int loop = 0;
4588 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004589 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004590 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004591
4592 if (order > 0)
4593 return 0;
4594
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004595 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004596 /*
4597 * This loop can run a while, specially if mem_cgroup's continuously
4598 * keep exceeding their soft limit and putting the system under
4599 * pressure
4600 */
4601 do {
4602 if (next_mz)
4603 mz = next_mz;
4604 else
4605 mz = mem_cgroup_largest_soft_limit_node(mctz);
4606 if (!mz)
4607 break;
4608
Ying Han0ae5e892011-05-26 16:25:25 -07004609 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004610 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004611 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004612 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004613 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004614 spin_lock(&mctz->lock);
4615
4616 /*
4617 * If we failed to reclaim anything from this memory cgroup
4618 * it is time to move on to the next cgroup
4619 */
4620 next_mz = NULL;
4621 if (!reclaimed) {
4622 do {
4623 /*
4624 * Loop until we find yet another one.
4625 *
4626 * By the time we get the soft_limit lock
4627 * again, someone might have aded the
4628 * group back on the RB tree. Iterate to
4629 * make sure we get a different mem.
4630 * mem_cgroup_largest_soft_limit_node returns
4631 * NULL if no other cgroup is present on
4632 * the tree
4633 */
4634 next_mz =
4635 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004636 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004637 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004638 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004639 break;
4640 } while (1);
4641 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004642 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4643 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004644 /*
4645 * One school of thought says that we should not add
4646 * back the node to the tree if reclaim returns 0.
4647 * But our reclaim could return 0, simply because due
4648 * to priority we are exposing a smaller subset of
4649 * memory to reclaim from. Consider this as a longer
4650 * term TODO.
4651 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004652 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004653 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004654 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004655 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004656 loop++;
4657 /*
4658 * Could not reclaim anything and there are no more
4659 * mem cgroups to try or we seem to be looping without
4660 * reclaiming anything.
4661 */
4662 if (!nr_reclaimed &&
4663 (next_mz == NULL ||
4664 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4665 break;
4666 } while (!nr_reclaimed);
4667 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004668 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004669 return nr_reclaimed;
4670}
4671
Michal Hocko2ef37d32012-10-26 13:37:30 +02004672/**
4673 * mem_cgroup_force_empty_list - clears LRU of a group
4674 * @memcg: group to clear
4675 * @node: NUMA node
4676 * @zid: zone id
4677 * @lru: lru to to clear
4678 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004679 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004680 * reclaim the pages page themselves - pages are moved to the parent (or root)
4681 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004682 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004683static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004684 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004685{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004686 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004687 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004688 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004689 struct page *busy;
4690 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004691
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004692 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004693 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4694 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004695
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004696 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004697 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004698 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004699 struct page *page;
4700
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004701 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004702 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004703 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004704 break;
4705 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004706 page = list_entry(list->prev, struct page, lru);
4707 if (busy == page) {
4708 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004709 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004710 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004711 continue;
4712 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004713 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004714
Johannes Weiner925b7672012-01-12 17:18:15 -08004715 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004716
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004717 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004718 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004719 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004720 cond_resched();
4721 } else
4722 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004723 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004724}
4725
4726/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004727 * make mem_cgroup's charge to be 0 if there is no task by moving
4728 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004729 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004730 *
4731 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004732 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004733static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004734{
Michal Hockoc26251f2012-10-26 13:37:28 +02004735 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004736 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004737
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004738 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004739 /* This is for making all *used* pages to be on LRU. */
4740 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004741 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004742 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004743 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004744 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004745 enum lru_list lru;
4746 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004747 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004748 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004749 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004750 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004751 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004752 mem_cgroup_end_move(memcg);
4753 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004754 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004755
Michal Hocko2ef37d32012-10-26 13:37:30 +02004756 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004757 * Kernel memory may not necessarily be trackable to a specific
4758 * process. So they are not migrated, and therefore we can't
4759 * expect their value to drop to 0 here.
4760 * Having res filled up with kmem only is enough.
4761 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004762 * This is a safety check because mem_cgroup_force_empty_list
4763 * could have raced with mem_cgroup_replace_page_cache callers
4764 * so the lru seemed empty but the page could have been added
4765 * right after the check. RES_USAGE should be safe as we always
4766 * charge before adding to the LRU.
4767 */
Glauber Costabea207c2012-12-18 14:22:11 -08004768 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4769 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4770 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004771}
4772
4773/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004774 * This mainly exists for tests during the setting of set of use_hierarchy.
4775 * Since this is the very setting we are changing, the current hierarchy value
4776 * is meaningless
4777 */
4778static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4779{
4780 struct cgroup *pos;
4781
4782 /* bounce at first found */
4783 cgroup_for_each_child(pos, memcg->css.cgroup)
4784 return true;
4785 return false;
4786}
4787
4788/*
Glauber Costa09998212013-02-22 16:34:55 -08004789 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4790 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004791 * from mem_cgroup_count_children(), in the sense that we don't really care how
4792 * many children we have; we only need to know if we have any. It also counts
4793 * any memcg without hierarchy as infertile.
4794 */
4795static inline bool memcg_has_children(struct mem_cgroup *memcg)
4796{
4797 return memcg->use_hierarchy && __memcg_has_children(memcg);
4798}
4799
4800/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004801 * Reclaims as many pages from the given memcg as possible and moves
4802 * the rest to the parent.
4803 *
4804 * Caller is responsible for holding css reference for memcg.
4805 */
4806static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4807{
4808 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4809 struct cgroup *cgrp = memcg->css.cgroup;
4810
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004811 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004812 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4813 return -EBUSY;
4814
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004815 /* we call try-to-free pages for make this cgroup empty */
4816 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004817 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004818 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004819 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004820
Michal Hockoc26251f2012-10-26 13:37:28 +02004821 if (signal_pending(current))
4822 return -EINTR;
4823
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004824 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004825 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004826 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004827 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004828 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004829 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004830 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004831
4832 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004833 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004834 mem_cgroup_reparent_charges(memcg);
4835
4836 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004837}
4838
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004839static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004840{
Michal Hockoc26251f2012-10-26 13:37:28 +02004841 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4842 int ret;
4843
Michal Hockod8423012012-10-26 13:37:29 +02004844 if (mem_cgroup_is_root(memcg))
4845 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004846 css_get(&memcg->css);
4847 ret = mem_cgroup_force_empty(memcg);
4848 css_put(&memcg->css);
4849
4850 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004851}
4852
4853
Balbir Singh18f59ea2009-01-07 18:08:07 -08004854static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4855{
4856 return mem_cgroup_from_cont(cont)->use_hierarchy;
4857}
4858
4859static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4860 u64 val)
4861{
4862 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004863 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004864 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004865 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004866
4867 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004868 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004869
Glauber Costa09998212013-02-22 16:34:55 -08004870 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004871
4872 if (memcg->use_hierarchy == val)
4873 goto out;
4874
Balbir Singh18f59ea2009-01-07 18:08:07 -08004875 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004876 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004877 * in the child subtrees. If it is unset, then the change can
4878 * occur, provided the current cgroup has no children.
4879 *
4880 * For the root cgroup, parent_mem is NULL, we allow value to be
4881 * set if there are no children.
4882 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004883 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004884 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004885 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004886 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004887 else
4888 retval = -EBUSY;
4889 } else
4890 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004891
4892out:
Glauber Costa09998212013-02-22 16:34:55 -08004893 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004894
4895 return retval;
4896}
4897
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004898
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004900 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004901{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004902 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004903 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004904
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004905 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004906 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004907 val += mem_cgroup_read_stat(iter, idx);
4908
4909 if (val < 0) /* race ? */
4910 val = 0;
4911 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004912}
4913
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004914static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004915{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004916 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004917
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004918 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004919 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004920 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004921 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004922 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004923 }
4924
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004925 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4926 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004927
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004928 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004929 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004930
4931 return val << PAGE_SHIFT;
4932}
4933
Tejun Heoaf36f902012-04-01 12:09:55 -07004934static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4935 struct file *file, char __user *buf,
4936 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004937{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004938 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004939 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004940 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004941 int name, len;
4942 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004943
4944 type = MEMFILE_TYPE(cft->private);
4945 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004946
4947 if (!do_swap_account && type == _MEMSWAP)
4948 return -EOPNOTSUPP;
4949
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004950 switch (type) {
4951 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004952 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004953 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004954 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004955 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004956 break;
4957 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004958 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004959 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004960 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004961 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004962 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004963 case _KMEM:
4964 val = res_counter_read_u64(&memcg->kmem, name);
4965 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004966 default:
4967 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004968 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004969
4970 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4971 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004972}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004973
4974static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4975{
4976 int ret = -EINVAL;
4977#ifdef CONFIG_MEMCG_KMEM
4978 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4979 /*
4980 * For simplicity, we won't allow this to be disabled. It also can't
4981 * be changed if the cgroup has children already, or if tasks had
4982 * already joined.
4983 *
4984 * If tasks join before we set the limit, a person looking at
4985 * kmem.usage_in_bytes will have no way to determine when it took
4986 * place, which makes the value quite meaningless.
4987 *
4988 * After it first became limited, changes in the value of the limit are
4989 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004990 */
Glauber Costa09998212013-02-22 16:34:55 -08004991 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004992 mutex_lock(&set_limit_mutex);
4993 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004994 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004995 ret = -EBUSY;
4996 goto out;
4997 }
4998 ret = res_counter_set_limit(&memcg->kmem, val);
4999 VM_BUG_ON(ret);
5000
Glauber Costa55007d82012-12-18 14:22:38 -08005001 ret = memcg_update_cache_sizes(memcg);
5002 if (ret) {
5003 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5004 goto out;
5005 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005006 static_key_slow_inc(&memcg_kmem_enabled_key);
5007 /*
5008 * setting the active bit after the inc will guarantee no one
5009 * starts accounting before all call sites are patched
5010 */
5011 memcg_kmem_set_active(memcg);
5012
Glauber Costa7de37682012-12-18 14:22:07 -08005013 /*
5014 * kmem charges can outlive the cgroup. In the case of slab
5015 * pages, for instance, a page contain objects from various
5016 * processes, so it is unfeasible to migrate them away. We
5017 * need to reference count the memcg because of that.
5018 */
5019 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005020 } else
5021 ret = res_counter_set_limit(&memcg->kmem, val);
5022out:
5023 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005024 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005025#endif
5026 return ret;
5027}
5028
Hugh Dickins6d0439902013-02-22 16:35:50 -08005029#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005030static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005031{
Glauber Costa55007d82012-12-18 14:22:38 -08005032 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005033 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5034 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005035 goto out;
5036
Glauber Costa510fc4e2012-12-18 14:21:47 -08005037 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005038 /*
5039 * When that happen, we need to disable the static branch only on those
5040 * memcgs that enabled it. To achieve this, we would be forced to
5041 * complicate the code by keeping track of which memcgs were the ones
5042 * that actually enabled limits, and which ones got it from its
5043 * parents.
5044 *
5045 * It is a lot simpler just to do static_key_slow_inc() on every child
5046 * that is accounted.
5047 */
Glauber Costa55007d82012-12-18 14:22:38 -08005048 if (!memcg_kmem_is_active(memcg))
5049 goto out;
5050
5051 /*
5052 * destroy(), called if we fail, will issue static_key_slow_inc() and
5053 * mem_cgroup_put() if kmem is enabled. We have to either call them
5054 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5055 * this more consistent, since it always leads to the same destroy path
5056 */
5057 mem_cgroup_get(memcg);
5058 static_key_slow_inc(&memcg_kmem_enabled_key);
5059
5060 mutex_lock(&set_limit_mutex);
5061 ret = memcg_update_cache_sizes(memcg);
5062 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005063out:
5064 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005065}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005066#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005067
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005068/*
5069 * The user of this function is...
5070 * RES_LIMIT.
5071 */
Paul Menage856c13a2008-07-25 01:47:04 -07005072static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5073 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005074{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005075 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005076 enum res_type type;
5077 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005078 unsigned long long val;
5079 int ret;
5080
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005081 type = MEMFILE_TYPE(cft->private);
5082 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005083
5084 if (!do_swap_account && type == _MEMSWAP)
5085 return -EOPNOTSUPP;
5086
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005087 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005088 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005089 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5090 ret = -EINVAL;
5091 break;
5092 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005093 /* This function does all necessary parse...reuse it */
5094 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005095 if (ret)
5096 break;
5097 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005098 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005099 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005100 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005101 else if (type == _KMEM)
5102 ret = memcg_update_kmem_limit(cont, val);
5103 else
5104 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005105 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005106 case RES_SOFT_LIMIT:
5107 ret = res_counter_memparse_write_strategy(buffer, &val);
5108 if (ret)
5109 break;
5110 /*
5111 * For memsw, soft limits are hard to implement in terms
5112 * of semantics, for now, we support soft limits for
5113 * control without swap
5114 */
5115 if (type == _MEM)
5116 ret = res_counter_set_soft_limit(&memcg->res, val);
5117 else
5118 ret = -EINVAL;
5119 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005120 default:
5121 ret = -EINVAL; /* should be BUG() ? */
5122 break;
5123 }
5124 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005125}
5126
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005127static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5128 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5129{
5130 struct cgroup *cgroup;
5131 unsigned long long min_limit, min_memsw_limit, tmp;
5132
5133 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5134 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5135 cgroup = memcg->css.cgroup;
5136 if (!memcg->use_hierarchy)
5137 goto out;
5138
5139 while (cgroup->parent) {
5140 cgroup = cgroup->parent;
5141 memcg = mem_cgroup_from_cont(cgroup);
5142 if (!memcg->use_hierarchy)
5143 break;
5144 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5145 min_limit = min(min_limit, tmp);
5146 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5147 min_memsw_limit = min(min_memsw_limit, tmp);
5148 }
5149out:
5150 *mem_limit = min_limit;
5151 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005152}
5153
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005154static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005155{
Tejun Heoaf36f902012-04-01 12:09:55 -07005156 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005157 int name;
5158 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005159
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005160 type = MEMFILE_TYPE(event);
5161 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005162
5163 if (!do_swap_account && type == _MEMSWAP)
5164 return -EOPNOTSUPP;
5165
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005166 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005167 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005168 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005169 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005170 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005171 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005172 else if (type == _KMEM)
5173 res_counter_reset_max(&memcg->kmem);
5174 else
5175 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005176 break;
5177 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005178 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005179 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005180 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005181 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005182 else if (type == _KMEM)
5183 res_counter_reset_failcnt(&memcg->kmem);
5184 else
5185 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005186 break;
5187 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005188
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005189 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005190}
5191
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005192static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5193 struct cftype *cft)
5194{
5195 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5196}
5197
Daisuke Nishimura02491442010-03-10 15:22:17 -08005198#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005199static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5200 struct cftype *cft, u64 val)
5201{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005202 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005203
5204 if (val >= (1 << NR_MOVE_TYPE))
5205 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005206
Glauber Costaee5e8472013-02-22 16:34:50 -08005207 /*
5208 * No kind of locking is needed in here, because ->can_attach() will
5209 * check this value once in the beginning of the process, and then carry
5210 * on with stale data. This means that changes to this value will only
5211 * affect task migrations starting after the change.
5212 */
5213 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005214 return 0;
5215}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005216#else
5217static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5218 struct cftype *cft, u64 val)
5219{
5220 return -ENOSYS;
5221}
5222#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005223
Ying Han406eb0c2011-05-26 16:25:37 -07005224#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005225static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005226 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005227{
5228 int nid;
5229 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5230 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005231 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005232
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005233 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005234 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005235 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005236 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005237 seq_printf(m, " N%d=%lu", nid, node_nr);
5238 }
5239 seq_putc(m, '\n');
5240
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005241 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005242 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005243 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005244 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005245 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005246 seq_printf(m, " N%d=%lu", nid, node_nr);
5247 }
5248 seq_putc(m, '\n');
5249
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005250 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005251 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005252 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005253 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005254 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005255 seq_printf(m, " N%d=%lu", nid, node_nr);
5256 }
5257 seq_putc(m, '\n');
5258
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005259 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005260 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005261 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005262 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005263 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005264 seq_printf(m, " N%d=%lu", nid, node_nr);
5265 }
5266 seq_putc(m, '\n');
5267 return 0;
5268}
5269#endif /* CONFIG_NUMA */
5270
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005271static inline void mem_cgroup_lru_names_not_uptodate(void)
5272{
5273 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5274}
5275
Wanpeng Liab215882012-07-31 16:43:09 -07005276static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005277 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005278{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005279 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005280 struct mem_cgroup *mi;
5281 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005282
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005283 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005284 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005285 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005286 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5287 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005288 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005289
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005290 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5291 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5292 mem_cgroup_read_events(memcg, i));
5293
5294 for (i = 0; i < NR_LRU_LISTS; i++)
5295 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5296 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5297
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005298 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005299 {
5300 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005301 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005302 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005303 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005304 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5305 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005306 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005307
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005308 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5309 long long val = 0;
5310
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005311 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005312 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005313 for_each_mem_cgroup_tree(mi, memcg)
5314 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5315 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5316 }
5317
5318 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5319 unsigned long long val = 0;
5320
5321 for_each_mem_cgroup_tree(mi, memcg)
5322 val += mem_cgroup_read_events(mi, i);
5323 seq_printf(m, "total_%s %llu\n",
5324 mem_cgroup_events_names[i], val);
5325 }
5326
5327 for (i = 0; i < NR_LRU_LISTS; i++) {
5328 unsigned long long val = 0;
5329
5330 for_each_mem_cgroup_tree(mi, memcg)
5331 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5332 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005333 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005334
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005335#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005336 {
5337 int nid, zid;
5338 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005339 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005340 unsigned long recent_rotated[2] = {0, 0};
5341 unsigned long recent_scanned[2] = {0, 0};
5342
5343 for_each_online_node(nid)
5344 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005345 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005346 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005347
Hugh Dickins89abfab2012-05-29 15:06:53 -07005348 recent_rotated[0] += rstat->recent_rotated[0];
5349 recent_rotated[1] += rstat->recent_rotated[1];
5350 recent_scanned[0] += rstat->recent_scanned[0];
5351 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005352 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005353 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5354 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5355 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5356 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005357 }
5358#endif
5359
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005360 return 0;
5361}
5362
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005363static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5364{
5365 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5366
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005367 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005368}
5369
5370static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5371 u64 val)
5372{
5373 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5374 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005375
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005376 if (val > 100)
5377 return -EINVAL;
5378
5379 if (cgrp->parent == NULL)
5380 return -EINVAL;
5381
5382 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005383
Glauber Costa09998212013-02-22 16:34:55 -08005384 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005385
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005386 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005387 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005388 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005389 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005390 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005391
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005392 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005393
Glauber Costa09998212013-02-22 16:34:55 -08005394 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005395
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005396 return 0;
5397}
5398
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005399static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5400{
5401 struct mem_cgroup_threshold_ary *t;
5402 u64 usage;
5403 int i;
5404
5405 rcu_read_lock();
5406 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005407 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005408 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005409 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005410
5411 if (!t)
5412 goto unlock;
5413
5414 usage = mem_cgroup_usage(memcg, swap);
5415
5416 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005417 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005418 * If it's not true, a threshold was crossed after last
5419 * call of __mem_cgroup_threshold().
5420 */
Phil Carmody5407a562010-05-26 14:42:42 -07005421 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005422
5423 /*
5424 * Iterate backward over array of thresholds starting from
5425 * current_threshold and check if a threshold is crossed.
5426 * If none of thresholds below usage is crossed, we read
5427 * only one element of the array here.
5428 */
5429 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5430 eventfd_signal(t->entries[i].eventfd, 1);
5431
5432 /* i = current_threshold + 1 */
5433 i++;
5434
5435 /*
5436 * Iterate forward over array of thresholds starting from
5437 * current_threshold+1 and check if a threshold is crossed.
5438 * If none of thresholds above usage is crossed, we read
5439 * only one element of the array here.
5440 */
5441 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5442 eventfd_signal(t->entries[i].eventfd, 1);
5443
5444 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005445 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005446unlock:
5447 rcu_read_unlock();
5448}
5449
5450static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5451{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005452 while (memcg) {
5453 __mem_cgroup_threshold(memcg, false);
5454 if (do_swap_account)
5455 __mem_cgroup_threshold(memcg, true);
5456
5457 memcg = parent_mem_cgroup(memcg);
5458 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005459}
5460
5461static int compare_thresholds(const void *a, const void *b)
5462{
5463 const struct mem_cgroup_threshold *_a = a;
5464 const struct mem_cgroup_threshold *_b = b;
5465
5466 return _a->threshold - _b->threshold;
5467}
5468
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005469static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005470{
5471 struct mem_cgroup_eventfd_list *ev;
5472
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005473 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005474 eventfd_signal(ev->eventfd, 1);
5475 return 0;
5476}
5477
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005478static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005479{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005480 struct mem_cgroup *iter;
5481
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005482 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005483 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005484}
5485
5486static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5487 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005488{
5489 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005490 struct mem_cgroup_thresholds *thresholds;
5491 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005492 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005493 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005494 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005495
5496 ret = res_counter_memparse_write_strategy(args, &threshold);
5497 if (ret)
5498 return ret;
5499
5500 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005501
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005502 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005503 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005504 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506 else
5507 BUG();
5508
5509 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5510
5511 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005512 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005513 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5514
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005515 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005516
5517 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005518 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005519 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005520 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005521 ret = -ENOMEM;
5522 goto unlock;
5523 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005524 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005525
5526 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005527 if (thresholds->primary) {
5528 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005530 }
5531
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005532 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005533 new->entries[size - 1].eventfd = eventfd;
5534 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005535
5536 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005537 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005538 compare_thresholds, NULL);
5539
5540 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005541 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005542 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005543 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005544 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005545 * new->current_threshold will not be used until
5546 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005547 * it here.
5548 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005549 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005550 } else
5551 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005552 }
5553
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005554 /* Free old spare buffer and save old primary buffer as spare */
5555 kfree(thresholds->spare);
5556 thresholds->spare = thresholds->primary;
5557
5558 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005559
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005560 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005561 synchronize_rcu();
5562
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563unlock:
5564 mutex_unlock(&memcg->thresholds_lock);
5565
5566 return ret;
5567}
5568
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005569static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005570 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005571{
5572 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005573 struct mem_cgroup_thresholds *thresholds;
5574 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005575 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005576 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005577 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005578
5579 mutex_lock(&memcg->thresholds_lock);
5580 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005581 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005582 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005583 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005584 else
5585 BUG();
5586
Anton Vorontsov371528c2012-02-24 05:14:46 +04005587 if (!thresholds->primary)
5588 goto unlock;
5589
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005590 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5591
5592 /* Check if a threshold crossed before removing */
5593 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5594
5595 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005596 size = 0;
5597 for (i = 0; i < thresholds->primary->size; i++) {
5598 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005599 size++;
5600 }
5601
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005602 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005603
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005604 /* Set thresholds array to NULL if we don't have thresholds */
5605 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005606 kfree(new);
5607 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005608 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005609 }
5610
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005611 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005612
5613 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005614 new->current_threshold = -1;
5615 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5616 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005617 continue;
5618
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005619 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005620 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005621 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005622 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005623 * until rcu_assign_pointer(), so it's safe to increment
5624 * it here.
5625 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005626 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005627 }
5628 j++;
5629 }
5630
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005631swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005632 /* Swap primary and spare array */
5633 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005634 /* If all events are unregistered, free the spare array */
5635 if (!new) {
5636 kfree(thresholds->spare);
5637 thresholds->spare = NULL;
5638 }
5639
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005640 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005641
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005642 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005643 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005644unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005645 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005646}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005647
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005648static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5649 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5650{
5651 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5652 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005653 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005654
5655 BUG_ON(type != _OOM_TYPE);
5656 event = kmalloc(sizeof(*event), GFP_KERNEL);
5657 if (!event)
5658 return -ENOMEM;
5659
Michal Hocko1af8efe2011-07-26 16:08:24 -07005660 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005661
5662 event->eventfd = eventfd;
5663 list_add(&event->list, &memcg->oom_notify);
5664
5665 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005666 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005667 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005668 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005669
5670 return 0;
5671}
5672
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005673static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005674 struct cftype *cft, struct eventfd_ctx *eventfd)
5675{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005676 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005677 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005678 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005679
5680 BUG_ON(type != _OOM_TYPE);
5681
Michal Hocko1af8efe2011-07-26 16:08:24 -07005682 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005683
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005684 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005685 if (ev->eventfd == eventfd) {
5686 list_del(&ev->list);
5687 kfree(ev);
5688 }
5689 }
5690
Michal Hocko1af8efe2011-07-26 16:08:24 -07005691 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005692}
5693
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005694static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5695 struct cftype *cft, struct cgroup_map_cb *cb)
5696{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005697 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005698
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005699 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005700
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005701 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005702 cb->fill(cb, "under_oom", 1);
5703 else
5704 cb->fill(cb, "under_oom", 0);
5705 return 0;
5706}
5707
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005708static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5709 struct cftype *cft, u64 val)
5710{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005711 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005712 struct mem_cgroup *parent;
5713
5714 /* cannot set to root cgroup and only 0 and 1 are allowed */
5715 if (!cgrp->parent || !((val == 0) || (val == 1)))
5716 return -EINVAL;
5717
5718 parent = mem_cgroup_from_cont(cgrp->parent);
5719
Glauber Costa09998212013-02-22 16:34:55 -08005720 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005721 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005722 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005723 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005724 return -EINVAL;
5725 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005726 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005727 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005728 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005729 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005730 return 0;
5731}
5732
Andrew Mortonc255a452012-07-31 16:43:02 -07005733#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005734static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005735{
Glauber Costa55007d82012-12-18 14:22:38 -08005736 int ret;
5737
Glauber Costa2633d7a2012-12-18 14:22:34 -08005738 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005739 ret = memcg_propagate_kmem(memcg);
5740 if (ret)
5741 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005742
Glauber Costa1d62e432012-04-09 19:36:33 -03005743 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005744};
5745
Glauber Costa1d62e432012-04-09 19:36:33 -03005746static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005747{
Glauber Costa1d62e432012-04-09 19:36:33 -03005748 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005749
5750 memcg_kmem_mark_dead(memcg);
5751
5752 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5753 return;
5754
5755 /*
5756 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5757 * path here, being careful not to race with memcg_uncharge_kmem: it is
5758 * possible that the charges went down to 0 between mark_dead and the
5759 * res_counter read, so in that case, we don't need the put
5760 */
5761 if (memcg_kmem_test_and_clear_dead(memcg))
5762 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005763}
Glauber Costae5671df2011-12-11 21:47:01 +00005764#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005765static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005766{
5767 return 0;
5768}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005769
Glauber Costa1d62e432012-04-09 19:36:33 -03005770static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005771{
5772}
Glauber Costae5671df2011-12-11 21:47:01 +00005773#endif
5774
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005775static struct cftype mem_cgroup_files[] = {
5776 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005777 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005778 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005779 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005780 .register_event = mem_cgroup_usage_register_event,
5781 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005782 },
5783 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005784 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005785 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005786 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005787 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005788 },
5789 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005790 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005791 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005792 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005793 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005794 },
5795 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005796 .name = "soft_limit_in_bytes",
5797 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5798 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005799 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005800 },
5801 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005802 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005803 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005804 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005805 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005806 },
Balbir Singh8697d332008-02-07 00:13:59 -08005807 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005808 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005809 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005810 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005811 {
5812 .name = "force_empty",
5813 .trigger = mem_cgroup_force_empty_write,
5814 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005815 {
5816 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005817 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005818 .write_u64 = mem_cgroup_hierarchy_write,
5819 .read_u64 = mem_cgroup_hierarchy_read,
5820 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005821 {
5822 .name = "swappiness",
5823 .read_u64 = mem_cgroup_swappiness_read,
5824 .write_u64 = mem_cgroup_swappiness_write,
5825 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005826 {
5827 .name = "move_charge_at_immigrate",
5828 .read_u64 = mem_cgroup_move_charge_read,
5829 .write_u64 = mem_cgroup_move_charge_write,
5830 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005831 {
5832 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005833 .read_map = mem_cgroup_oom_control_read,
5834 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005835 .register_event = mem_cgroup_oom_register_event,
5836 .unregister_event = mem_cgroup_oom_unregister_event,
5837 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5838 },
Ying Han406eb0c2011-05-26 16:25:37 -07005839#ifdef CONFIG_NUMA
5840 {
5841 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005842 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005843 },
5844#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005845#ifdef CONFIG_MEMCG_KMEM
5846 {
5847 .name = "kmem.limit_in_bytes",
5848 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5849 .write_string = mem_cgroup_write,
5850 .read = mem_cgroup_read,
5851 },
5852 {
5853 .name = "kmem.usage_in_bytes",
5854 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5855 .read = mem_cgroup_read,
5856 },
5857 {
5858 .name = "kmem.failcnt",
5859 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5860 .trigger = mem_cgroup_reset,
5861 .read = mem_cgroup_read,
5862 },
5863 {
5864 .name = "kmem.max_usage_in_bytes",
5865 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5866 .trigger = mem_cgroup_reset,
5867 .read = mem_cgroup_read,
5868 },
Glauber Costa749c5412012-12-18 14:23:01 -08005869#ifdef CONFIG_SLABINFO
5870 {
5871 .name = "kmem.slabinfo",
5872 .read_seq_string = mem_cgroup_slabinfo_read,
5873 },
5874#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005875#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005876 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005877};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005878
Michal Hocko2d110852013-02-22 16:34:43 -08005879#ifdef CONFIG_MEMCG_SWAP
5880static struct cftype memsw_cgroup_files[] = {
5881 {
5882 .name = "memsw.usage_in_bytes",
5883 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5884 .read = mem_cgroup_read,
5885 .register_event = mem_cgroup_usage_register_event,
5886 .unregister_event = mem_cgroup_usage_unregister_event,
5887 },
5888 {
5889 .name = "memsw.max_usage_in_bytes",
5890 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5891 .trigger = mem_cgroup_reset,
5892 .read = mem_cgroup_read,
5893 },
5894 {
5895 .name = "memsw.limit_in_bytes",
5896 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5897 .write_string = mem_cgroup_write,
5898 .read = mem_cgroup_read,
5899 },
5900 {
5901 .name = "memsw.failcnt",
5902 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5903 .trigger = mem_cgroup_reset,
5904 .read = mem_cgroup_read,
5905 },
5906 { }, /* terminate */
5907};
5908#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005909static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005910{
5911 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005912 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005913 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005914 /*
5915 * This routine is called against possible nodes.
5916 * But it's BUG to call kmalloc() against offline node.
5917 *
5918 * TODO: this routine can waste much memory for nodes which will
5919 * never be onlined. It's better to use memory hotplug callback
5920 * function.
5921 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005922 if (!node_state(node, N_NORMAL_MEMORY))
5923 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005924 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005925 if (!pn)
5926 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005927
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005928 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5929 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005930 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005931 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005932 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005933 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005934 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005935 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005936 return 0;
5937}
5938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005939static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005940{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005941 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005942}
5943
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005944static struct mem_cgroup *mem_cgroup_alloc(void)
5945{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005946 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005947 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005948
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005949 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005950 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005951 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005952 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005953 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005954
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005955 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005956 return NULL;
5957
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005958 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5959 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005960 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005961 spin_lock_init(&memcg->pcp_counter_lock);
5962 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005963
5964out_free:
5965 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005966 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005967 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005968 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005969 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005970}
5971
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005972/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005973 * At destroying mem_cgroup, references from swap_cgroup can remain.
5974 * (scanning all at force_empty is too costly...)
5975 *
5976 * Instead of clearing all references at force_empty, we remember
5977 * the number of reference from swap_cgroup and free mem_cgroup when
5978 * it goes down to 0.
5979 *
5980 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005981 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005982
5983static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005984{
Glauber Costac8b2a362012-12-18 14:22:13 -08005985 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005986 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005987
Glauber Costac8b2a362012-12-18 14:22:13 -08005988 mem_cgroup_remove_from_trees(memcg);
5989 free_css_id(&mem_cgroup_subsys, &memcg->css);
5990
5991 for_each_node(node)
5992 free_mem_cgroup_per_zone_info(memcg, node);
5993
5994 free_percpu(memcg->stat);
5995
Glauber Costa3f134612012-05-29 15:07:11 -07005996 /*
5997 * We need to make sure that (at least for now), the jump label
5998 * destruction code runs outside of the cgroup lock. This is because
5999 * get_online_cpus(), which is called from the static_branch update,
6000 * can't be called inside the cgroup_lock. cpusets are the ones
6001 * enforcing this dependency, so if they ever change, we might as well.
6002 *
6003 * schedule_work() will guarantee this happens. Be careful if you need
6004 * to move this code around, and make sure it is outside
6005 * the cgroup_lock.
6006 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006007 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006008 if (size < PAGE_SIZE)
6009 kfree(memcg);
6010 else
6011 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006012}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006013
Glauber Costac8b2a362012-12-18 14:22:13 -08006014
6015/*
6016 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6017 * but in process context. The work_freeing structure is overlaid
6018 * on the rcu_freeing structure, which itself is overlaid on memsw.
6019 */
6020static void free_work(struct work_struct *work)
6021{
6022 struct mem_cgroup *memcg;
6023
6024 memcg = container_of(work, struct mem_cgroup, work_freeing);
6025 __mem_cgroup_free(memcg);
6026}
6027
Glauber Costa3afe36b2012-05-29 15:07:10 -07006028static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006029{
6030 struct mem_cgroup *memcg;
6031
6032 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006033 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006034 schedule_work(&memcg->work_freeing);
6035}
6036
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006037static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006038{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006039 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006040}
6041
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006042static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006043{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006044 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6045 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006046 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006047 if (parent)
6048 mem_cgroup_put(parent);
6049 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006050}
6051
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006052static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006053{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006054 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006055}
6056
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006057/*
6058 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6059 */
Glauber Costae1aab162011-12-11 21:47:03 +00006060struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006061{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006062 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006063 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006064 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006065}
Glauber Costae1aab162011-12-11 21:47:03 +00006066EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006067
Michal Hocko8787a1d2013-02-22 16:35:39 -08006068static void __init mem_cgroup_soft_limit_tree_init(void)
Balbir Singhf64c3f52009-09-23 15:56:37 -07006069{
6070 struct mem_cgroup_tree_per_node *rtpn;
6071 struct mem_cgroup_tree_per_zone *rtpz;
6072 int tmp, node, zone;
6073
Bob Liu3ed28fa2012-01-12 17:19:04 -08006074 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006075 tmp = node;
6076 if (!node_state(node, N_NORMAL_MEMORY))
6077 tmp = -1;
6078 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
Michal Hocko8787a1d2013-02-22 16:35:39 -08006079 BUG_ON(!rtpn);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006080
6081 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6082
6083 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6084 rtpz = &rtpn->rb_tree_per_zone[zone];
6085 rtpz->rb_root = RB_ROOT;
6086 spin_lock_init(&rtpz->lock);
6087 }
6088 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07006089}
6090
Li Zefan0eb253e2009-01-15 13:51:25 -08006091static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006092mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006093{
Glauber Costad142e3e2013-02-22 16:34:52 -08006094 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006095 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006096 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006097
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006098 memcg = mem_cgroup_alloc();
6099 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006100 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006101
Bob Liu3ed28fa2012-01-12 17:19:04 -08006102 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006103 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006104 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006105
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006106 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006107 if (cont->parent == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006108 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006109 res_counter_init(&memcg->res, NULL);
6110 res_counter_init(&memcg->memsw, NULL);
6111 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006112 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006113
Glauber Costad142e3e2013-02-22 16:34:52 -08006114 memcg->last_scanned_node = MAX_NUMNODES;
6115 INIT_LIST_HEAD(&memcg->oom_notify);
6116 atomic_set(&memcg->refcnt, 1);
6117 memcg->move_charge_at_immigrate = 0;
6118 mutex_init(&memcg->thresholds_lock);
6119 spin_lock_init(&memcg->move_lock);
6120
6121 return &memcg->css;
6122
6123free_out:
6124 __mem_cgroup_free(memcg);
6125 return ERR_PTR(error);
6126}
6127
6128static int
6129mem_cgroup_css_online(struct cgroup *cont)
6130{
6131 struct mem_cgroup *memcg, *parent;
6132 int error = 0;
6133
6134 if (!cont->parent)
6135 return 0;
6136
Glauber Costa09998212013-02-22 16:34:55 -08006137 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006138 memcg = mem_cgroup_from_cont(cont);
6139 parent = mem_cgroup_from_cont(cont->parent);
6140
6141 memcg->use_hierarchy = parent->use_hierarchy;
6142 memcg->oom_kill_disable = parent->oom_kill_disable;
6143 memcg->swappiness = mem_cgroup_swappiness(parent);
6144
6145 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006146 res_counter_init(&memcg->res, &parent->res);
6147 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006148 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006149
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006150 /*
6151 * We increment refcnt of the parent to ensure that we can
6152 * safely access it on res_counter_charge/uncharge.
6153 * This refcnt will be decremented when freeing this
6154 * mem_cgroup(see mem_cgroup_put).
6155 */
6156 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006157 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006158 res_counter_init(&memcg->res, NULL);
6159 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006160 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006161 /*
6162 * Deeper hierachy with use_hierarchy == false doesn't make
6163 * much sense so let cgroup subsystem know about this
6164 * unfortunate state in our controller.
6165 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006166 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006167 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006168 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006169
6170 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006171 mutex_unlock(&memcg_create_mutex);
Glauber Costacbe128e32012-04-09 19:36:34 -03006172 if (error) {
6173 /*
6174 * We call put now because our (and parent's) refcnts
6175 * are already in place. mem_cgroup_put() will internally
6176 * call __mem_cgroup_free, so return directly
6177 */
6178 mem_cgroup_put(memcg);
Glauber Costae4715f02013-02-22 16:34:57 -08006179 if (parent->use_hierarchy)
6180 mem_cgroup_put(parent);
Glauber Costacbe128e32012-04-09 19:36:34 -03006181 }
Glauber Costad142e3e2013-02-22 16:34:52 -08006182 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006183}
6184
Tejun Heo92fb9742012-11-19 08:13:38 -08006185static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006186{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006187 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006188
Michal Hockoab5196c2012-10-26 13:37:32 +02006189 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006190 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006191}
6192
Tejun Heo92fb9742012-11-19 08:13:38 -08006193static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006194{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006195 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006196
Glauber Costa1d62e432012-04-09 19:36:33 -03006197 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006198
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006199 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006200}
6201
Daisuke Nishimura02491442010-03-10 15:22:17 -08006202#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006203/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006204#define PRECHARGE_COUNT_AT_ONCE 256
6205static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006206{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006207 int ret = 0;
6208 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006209 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006210
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006211 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006212 mc.precharge += count;
6213 /* we don't need css_get for root */
6214 return ret;
6215 }
6216 /* try to charge at once */
6217 if (count > 1) {
6218 struct res_counter *dummy;
6219 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006220 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006221 * by cgroup_lock_live_cgroup() that it is not removed and we
6222 * are still under the same cgroup_mutex. So we can postpone
6223 * css_get().
6224 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006225 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006226 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006227 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006228 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006229 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006230 goto one_by_one;
6231 }
6232 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006233 return ret;
6234 }
6235one_by_one:
6236 /* fall back to one by one charge */
6237 while (count--) {
6238 if (signal_pending(current)) {
6239 ret = -EINTR;
6240 break;
6241 }
6242 if (!batch_count--) {
6243 batch_count = PRECHARGE_COUNT_AT_ONCE;
6244 cond_resched();
6245 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006246 ret = __mem_cgroup_try_charge(NULL,
6247 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006248 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006249 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006250 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006251 mc.precharge++;
6252 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006253 return ret;
6254}
6255
6256/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006257 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006258 * @vma: the vma the pte to be checked belongs
6259 * @addr: the address corresponding to the pte to be checked
6260 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006261 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006262 *
6263 * Returns
6264 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6265 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6266 * move charge. if @target is not NULL, the page is stored in target->page
6267 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006268 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6269 * target for charge migration. if @target is not NULL, the entry is stored
6270 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006271 *
6272 * Called with pte lock held.
6273 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006274union mc_target {
6275 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006276 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006277};
6278
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006279enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006280 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006281 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006282 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006283};
6284
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006285static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6286 unsigned long addr, pte_t ptent)
6287{
6288 struct page *page = vm_normal_page(vma, addr, ptent);
6289
6290 if (!page || !page_mapped(page))
6291 return NULL;
6292 if (PageAnon(page)) {
6293 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006294 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006295 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006296 } else if (!move_file())
6297 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006298 return NULL;
6299 if (!get_page_unless_zero(page))
6300 return NULL;
6301
6302 return page;
6303}
6304
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006305#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006306static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6307 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6308{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006309 struct page *page = NULL;
6310 swp_entry_t ent = pte_to_swp_entry(ptent);
6311
6312 if (!move_anon() || non_swap_entry(ent))
6313 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006314 /*
6315 * Because lookup_swap_cache() updates some statistics counter,
6316 * we call find_get_page() with swapper_space directly.
6317 */
Shaohua Li33806f02013-02-22 16:34:37 -08006318 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006319 if (do_swap_account)
6320 entry->val = ent.val;
6321
6322 return page;
6323}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006324#else
6325static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6326 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6327{
6328 return NULL;
6329}
6330#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006331
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006332static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6333 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6334{
6335 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006336 struct address_space *mapping;
6337 pgoff_t pgoff;
6338
6339 if (!vma->vm_file) /* anonymous vma */
6340 return NULL;
6341 if (!move_file())
6342 return NULL;
6343
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006344 mapping = vma->vm_file->f_mapping;
6345 if (pte_none(ptent))
6346 pgoff = linear_page_index(vma, addr);
6347 else /* pte_file(ptent) is true */
6348 pgoff = pte_to_pgoff(ptent);
6349
6350 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006351 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006352
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006353#ifdef CONFIG_SWAP
6354 /* shmem/tmpfs may report page out on swap: account for that too. */
6355 if (radix_tree_exceptional_entry(page)) {
6356 swp_entry_t swap = radix_to_swp_entry(page);
6357 if (do_swap_account)
6358 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006359 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006360 }
6361#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006362 return page;
6363}
6364
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006365static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006366 unsigned long addr, pte_t ptent, union mc_target *target)
6367{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006368 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006369 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006370 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006371 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006372
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006373 if (pte_present(ptent))
6374 page = mc_handle_present_pte(vma, addr, ptent);
6375 else if (is_swap_pte(ptent))
6376 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006377 else if (pte_none(ptent) || pte_file(ptent))
6378 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006379
6380 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006381 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006382 if (page) {
6383 pc = lookup_page_cgroup(page);
6384 /*
6385 * Do only loose check w/o page_cgroup lock.
6386 * mem_cgroup_move_account() checks the pc is valid or not under
6387 * the lock.
6388 */
6389 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6390 ret = MC_TARGET_PAGE;
6391 if (target)
6392 target->page = page;
6393 }
6394 if (!ret || !target)
6395 put_page(page);
6396 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006397 /* There is a swap entry and a page doesn't exist or isn't charged */
6398 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006399 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006400 ret = MC_TARGET_SWAP;
6401 if (target)
6402 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006403 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006404 return ret;
6405}
6406
Naoya Horiguchi12724852012-03-21 16:34:28 -07006407#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6408/*
6409 * We don't consider swapping or file mapped pages because THP does not
6410 * support them for now.
6411 * Caller should make sure that pmd_trans_huge(pmd) is true.
6412 */
6413static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6414 unsigned long addr, pmd_t pmd, union mc_target *target)
6415{
6416 struct page *page = NULL;
6417 struct page_cgroup *pc;
6418 enum mc_target_type ret = MC_TARGET_NONE;
6419
6420 page = pmd_page(pmd);
6421 VM_BUG_ON(!page || !PageHead(page));
6422 if (!move_anon())
6423 return ret;
6424 pc = lookup_page_cgroup(page);
6425 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6426 ret = MC_TARGET_PAGE;
6427 if (target) {
6428 get_page(page);
6429 target->page = page;
6430 }
6431 }
6432 return ret;
6433}
6434#else
6435static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6436 unsigned long addr, pmd_t pmd, union mc_target *target)
6437{
6438 return MC_TARGET_NONE;
6439}
6440#endif
6441
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006442static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6443 unsigned long addr, unsigned long end,
6444 struct mm_walk *walk)
6445{
6446 struct vm_area_struct *vma = walk->private;
6447 pte_t *pte;
6448 spinlock_t *ptl;
6449
Naoya Horiguchi12724852012-03-21 16:34:28 -07006450 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6451 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6452 mc.precharge += HPAGE_PMD_NR;
6453 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006454 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006455 }
Dave Hansen03319322011-03-22 16:32:56 -07006456
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006457 if (pmd_trans_unstable(pmd))
6458 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006459 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6460 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006461 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006462 mc.precharge++; /* increment precharge temporarily */
6463 pte_unmap_unlock(pte - 1, ptl);
6464 cond_resched();
6465
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006466 return 0;
6467}
6468
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006469static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6470{
6471 unsigned long precharge;
6472 struct vm_area_struct *vma;
6473
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006474 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006475 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6476 struct mm_walk mem_cgroup_count_precharge_walk = {
6477 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6478 .mm = mm,
6479 .private = vma,
6480 };
6481 if (is_vm_hugetlb_page(vma))
6482 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006483 walk_page_range(vma->vm_start, vma->vm_end,
6484 &mem_cgroup_count_precharge_walk);
6485 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006486 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006487
6488 precharge = mc.precharge;
6489 mc.precharge = 0;
6490
6491 return precharge;
6492}
6493
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006494static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6495{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006496 unsigned long precharge = mem_cgroup_count_precharge(mm);
6497
6498 VM_BUG_ON(mc.moving_task);
6499 mc.moving_task = current;
6500 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006501}
6502
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006503/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6504static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006505{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006506 struct mem_cgroup *from = mc.from;
6507 struct mem_cgroup *to = mc.to;
6508
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006509 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006510 if (mc.precharge) {
6511 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6512 mc.precharge = 0;
6513 }
6514 /*
6515 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6516 * we must uncharge here.
6517 */
6518 if (mc.moved_charge) {
6519 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6520 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006521 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006522 /* we must fixup refcnts and charges */
6523 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006524 /* uncharge swap account from the old cgroup */
6525 if (!mem_cgroup_is_root(mc.from))
6526 res_counter_uncharge(&mc.from->memsw,
6527 PAGE_SIZE * mc.moved_swap);
6528 __mem_cgroup_put(mc.from, mc.moved_swap);
6529
6530 if (!mem_cgroup_is_root(mc.to)) {
6531 /*
6532 * we charged both to->res and to->memsw, so we should
6533 * uncharge to->res.
6534 */
6535 res_counter_uncharge(&mc.to->res,
6536 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006537 }
6538 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006539 mc.moved_swap = 0;
6540 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006541 memcg_oom_recover(from);
6542 memcg_oom_recover(to);
6543 wake_up_all(&mc.waitq);
6544}
6545
6546static void mem_cgroup_clear_mc(void)
6547{
6548 struct mem_cgroup *from = mc.from;
6549
6550 /*
6551 * we must clear moving_task before waking up waiters at the end of
6552 * task migration.
6553 */
6554 mc.moving_task = NULL;
6555 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006556 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006557 mc.from = NULL;
6558 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006559 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006560 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006561}
6562
Li Zefan761b3ef52012-01-31 13:47:36 +08006563static int mem_cgroup_can_attach(struct cgroup *cgroup,
6564 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006565{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006566 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006567 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006568 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006569 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006570
Glauber Costaee5e8472013-02-22 16:34:50 -08006571 /*
6572 * We are now commited to this value whatever it is. Changes in this
6573 * tunable will only affect upcoming migrations, not the current one.
6574 * So we need to save it, and keep it going.
6575 */
6576 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6577 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006578 struct mm_struct *mm;
6579 struct mem_cgroup *from = mem_cgroup_from_task(p);
6580
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006581 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006582
6583 mm = get_task_mm(p);
6584 if (!mm)
6585 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006586 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006587 if (mm->owner == p) {
6588 VM_BUG_ON(mc.from);
6589 VM_BUG_ON(mc.to);
6590 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006591 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006592 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006593 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006594 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006595 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006596 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006597 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006598 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006599 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006600
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006601 ret = mem_cgroup_precharge_mc(mm);
6602 if (ret)
6603 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006604 }
6605 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006606 }
6607 return ret;
6608}
6609
Li Zefan761b3ef52012-01-31 13:47:36 +08006610static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6611 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006612{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006613 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006614}
6615
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006616static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6617 unsigned long addr, unsigned long end,
6618 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006619{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006620 int ret = 0;
6621 struct vm_area_struct *vma = walk->private;
6622 pte_t *pte;
6623 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006624 enum mc_target_type target_type;
6625 union mc_target target;
6626 struct page *page;
6627 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006628
Naoya Horiguchi12724852012-03-21 16:34:28 -07006629 /*
6630 * We don't take compound_lock() here but no race with splitting thp
6631 * happens because:
6632 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6633 * under splitting, which means there's no concurrent thp split,
6634 * - if another thread runs into split_huge_page() just after we
6635 * entered this if-block, the thread must wait for page table lock
6636 * to be unlocked in __split_huge_page_splitting(), where the main
6637 * part of thp split is not executed yet.
6638 */
6639 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006640 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006641 spin_unlock(&vma->vm_mm->page_table_lock);
6642 return 0;
6643 }
6644 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6645 if (target_type == MC_TARGET_PAGE) {
6646 page = target.page;
6647 if (!isolate_lru_page(page)) {
6648 pc = lookup_page_cgroup(page);
6649 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006650 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006651 mc.precharge -= HPAGE_PMD_NR;
6652 mc.moved_charge += HPAGE_PMD_NR;
6653 }
6654 putback_lru_page(page);
6655 }
6656 put_page(page);
6657 }
6658 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006659 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006660 }
6661
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006662 if (pmd_trans_unstable(pmd))
6663 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006664retry:
6665 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6666 for (; addr != end; addr += PAGE_SIZE) {
6667 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006668 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006669
6670 if (!mc.precharge)
6671 break;
6672
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006673 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006674 case MC_TARGET_PAGE:
6675 page = target.page;
6676 if (isolate_lru_page(page))
6677 goto put;
6678 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006679 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006680 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006681 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006682 /* we uncharge from mc.from later. */
6683 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006684 }
6685 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006686put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006687 put_page(page);
6688 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006689 case MC_TARGET_SWAP:
6690 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006691 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006692 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006693 /* we fixup refcnts and charges later. */
6694 mc.moved_swap++;
6695 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006696 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006697 default:
6698 break;
6699 }
6700 }
6701 pte_unmap_unlock(pte - 1, ptl);
6702 cond_resched();
6703
6704 if (addr != end) {
6705 /*
6706 * We have consumed all precharges we got in can_attach().
6707 * We try charge one by one, but don't do any additional
6708 * charges to mc.to if we have failed in charge once in attach()
6709 * phase.
6710 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006711 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006712 if (!ret)
6713 goto retry;
6714 }
6715
6716 return ret;
6717}
6718
6719static void mem_cgroup_move_charge(struct mm_struct *mm)
6720{
6721 struct vm_area_struct *vma;
6722
6723 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006724retry:
6725 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6726 /*
6727 * Someone who are holding the mmap_sem might be waiting in
6728 * waitq. So we cancel all extra charges, wake up all waiters,
6729 * and retry. Because we cancel precharges, we might not be able
6730 * to move enough charges, but moving charge is a best-effort
6731 * feature anyway, so it wouldn't be a big problem.
6732 */
6733 __mem_cgroup_clear_mc();
6734 cond_resched();
6735 goto retry;
6736 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006737 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6738 int ret;
6739 struct mm_walk mem_cgroup_move_charge_walk = {
6740 .pmd_entry = mem_cgroup_move_charge_pte_range,
6741 .mm = mm,
6742 .private = vma,
6743 };
6744 if (is_vm_hugetlb_page(vma))
6745 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006746 ret = walk_page_range(vma->vm_start, vma->vm_end,
6747 &mem_cgroup_move_charge_walk);
6748 if (ret)
6749 /*
6750 * means we have consumed all precharges and failed in
6751 * doing additional charge. Just abandon here.
6752 */
6753 break;
6754 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006755 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006756}
6757
Li Zefan761b3ef52012-01-31 13:47:36 +08006758static void mem_cgroup_move_task(struct cgroup *cont,
6759 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006760{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006761 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006762 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006763
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006764 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006765 if (mc.to)
6766 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006767 mmput(mm);
6768 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006769 if (mc.to)
6770 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006771}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006772#else /* !CONFIG_MMU */
Li Zefan761b3ef52012-01-31 13:47:36 +08006773static int mem_cgroup_can_attach(struct cgroup *cgroup,
6774 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006775{
6776 return 0;
6777}
Li Zefan761b3ef52012-01-31 13:47:36 +08006778static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6779 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006780{
6781}
Li Zefan761b3ef52012-01-31 13:47:36 +08006782static void mem_cgroup_move_task(struct cgroup *cont,
6783 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006784{
6785}
6786#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006787
Tejun Heof00baae2013-04-15 13:41:15 -07006788/*
6789 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6790 * to verify sane_behavior flag on each mount attempt.
6791 */
6792static void mem_cgroup_bind(struct cgroup *root)
6793{
6794 /*
6795 * use_hierarchy is forced with sane_behavior. cgroup core
6796 * guarantees that @root doesn't have any children, so turning it
6797 * on for the root memcg is enough.
6798 */
6799 if (cgroup_sane_behavior(root))
6800 mem_cgroup_from_cont(root)->use_hierarchy = true;
6801}
6802
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006803struct cgroup_subsys mem_cgroup_subsys = {
6804 .name = "memory",
6805 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006806 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006807 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006808 .css_offline = mem_cgroup_css_offline,
6809 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006810 .can_attach = mem_cgroup_can_attach,
6811 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006812 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006813 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006814 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006815 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006816 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006817};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006818
Andrew Mortonc255a452012-07-31 16:43:02 -07006819#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006820static int __init enable_swap_account(char *s)
6821{
6822 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006823 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006824 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006825 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006826 really_do_swap_account = 0;
6827 return 1;
6828}
Michal Hockoa2c89902011-05-24 17:12:50 -07006829__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006830
Michal Hocko2d110852013-02-22 16:34:43 -08006831static void __init memsw_file_init(void)
6832{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006833 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006834}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006835
6836static void __init enable_swap_cgroup(void)
6837{
6838 if (!mem_cgroup_disabled() && really_do_swap_account) {
6839 do_swap_account = 1;
6840 memsw_file_init();
6841 }
6842}
6843
Michal Hocko2d110852013-02-22 16:34:43 -08006844#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006845static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006846{
6847}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006848#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006849
6850/*
Michal Hocko10813122013-02-22 16:35:41 -08006851 * subsys_initcall() for memory controller.
6852 *
6853 * Some parts like hotcpu_notifier() have to be initialized from this context
6854 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6855 * everything that doesn't depend on a specific mem_cgroup structure should
6856 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006857 */
6858static int __init mem_cgroup_init(void)
6859{
6860 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006861 enable_swap_cgroup();
Michal Hocko8787a1d2013-02-22 16:35:39 -08006862 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08006863 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006864 return 0;
6865}
6866subsys_initcall(mem_cgroup_init);