blob: eaa3accb01e708a11986135a8df1146f7dbc1b4b [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>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070052#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070053#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070054#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080055#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070056#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080057#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000058#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070059#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000060#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080061
Balbir Singh8697d332008-02-07 00:13:59 -080062#include <asm/uaccess.h>
63
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070064#include <trace/events/vmscan.h>
65
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070066struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080067EXPORT_SYMBOL(mem_cgroup_subsys);
68
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070069#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070070static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080071
Andrew Mortonc255a452012-07-31 16:43:02 -070072#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070073/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080074int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080075
76/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070077#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080078static int really_do_swap_account __initdata = 1;
79#else
80static int really_do_swap_account __initdata = 0;
81#endif
82
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080083#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070084#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080085#endif
86
87
Balbir Singh8cdea7c2008-02-07 00:13:50 -080088/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080089 * Statistics for memory cgroup.
90 */
91enum mem_cgroup_stat_index {
92 /*
93 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
94 */
David Rientjesb070e652013-05-07 16:18:09 -070095 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
96 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
97 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
98 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
99 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100 MEM_CGROUP_STAT_NSTATS,
101};
102
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700103static const char * const mem_cgroup_stat_names[] = {
104 "cache",
105 "rss",
David Rientjesb070e652013-05-07 16:18:09 -0700106 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700107 "mapped_file",
108 "swap",
109};
110
Johannes Weinere9f89742011-03-23 16:42:37 -0700111enum mem_cgroup_events_index {
112 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
113 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700114 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
115 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700116 MEM_CGROUP_EVENTS_NSTATS,
117};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700118
119static const char * const mem_cgroup_events_names[] = {
120 "pgpgin",
121 "pgpgout",
122 "pgfault",
123 "pgmajfault",
124};
125
Sha Zhengju58cf1882013-02-22 16:32:05 -0800126static const char * const mem_cgroup_lru_names[] = {
127 "inactive_anon",
128 "active_anon",
129 "inactive_file",
130 "active_file",
131 "unevictable",
132};
133
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700134/*
135 * Per memcg event counter is incremented at every pagein/pageout. With THP,
136 * it will be incremated by the number of pages. This counter is used for
137 * for trigger some periodic events. This is straightforward and better
138 * than using jiffies etc. to handle periodic memcg event.
139 */
140enum mem_cgroup_events_target {
141 MEM_CGROUP_TARGET_THRESH,
142 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700143 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700144 MEM_CGROUP_NTARGETS,
145};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700146#define THRESHOLDS_EVENTS_TARGET 128
147#define SOFTLIMIT_EVENTS_TARGET 1024
148#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700149
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800150struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700151 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700152 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700153 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700154 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800155};
156
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800157struct mem_cgroup_reclaim_iter {
Michal Hocko5f578162013-04-29 15:07:17 -0700158 /*
159 * last scanned hierarchy member. Valid only if last_dead_count
160 * matches memcg->dead_count of the hierarchy root group.
161 */
Michal Hocko542f85f2013-04-29 15:07:15 -0700162 struct mem_cgroup *last_visited;
Michal Hocko5f578162013-04-29 15:07:17 -0700163 unsigned long last_dead_count;
164
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800165 /* scan generation, increased every round-trip */
166 unsigned int generation;
167};
168
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800169/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800170 * per-zone information in memory controller.
171 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800172struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800173 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700174 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800175
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800176 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
177
Balbir Singhf64c3f52009-09-23 15:56:37 -0700178 struct rb_node tree_node; /* RB tree node */
179 unsigned long long usage_in_excess;/* Set to the value by which */
180 /* the soft limit is exceeded*/
181 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700182 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700183 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800184};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800185
186struct mem_cgroup_per_node {
187 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
188};
189
190struct mem_cgroup_lru_info {
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800191 struct mem_cgroup_per_node *nodeinfo[0];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800192};
193
194/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700195 * Cgroups above their limits are maintained in a RB-Tree, independent of
196 * their hierarchy representation
197 */
198
199struct mem_cgroup_tree_per_zone {
200 struct rb_root rb_root;
201 spinlock_t lock;
202};
203
204struct mem_cgroup_tree_per_node {
205 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
206};
207
208struct mem_cgroup_tree {
209 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
210};
211
212static struct mem_cgroup_tree soft_limit_tree __read_mostly;
213
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800214struct mem_cgroup_threshold {
215 struct eventfd_ctx *eventfd;
216 u64 threshold;
217};
218
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700219/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800220struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700221 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700222 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800223 /* Size of entries[] */
224 unsigned int size;
225 /* Array of thresholds */
226 struct mem_cgroup_threshold entries[0];
227};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700228
229struct mem_cgroup_thresholds {
230 /* Primary thresholds array */
231 struct mem_cgroup_threshold_ary *primary;
232 /*
233 * Spare threshold array.
234 * This is needed to make mem_cgroup_unregister_event() "never fail".
235 * It must be able to store at least primary->size - 1 entries.
236 */
237 struct mem_cgroup_threshold_ary *spare;
238};
239
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700240/* for OOM */
241struct mem_cgroup_eventfd_list {
242 struct list_head list;
243 struct eventfd_ctx *eventfd;
244};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800245
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700246static void mem_cgroup_threshold(struct mem_cgroup *memcg);
247static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800248
Balbir Singhf64c3f52009-09-23 15:56:37 -0700249/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800250 * The memory controller data structure. The memory controller controls both
251 * page cache and RSS per cgroup. We would eventually like to provide
252 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
253 * to help the administrator determine what knobs to tune.
254 *
255 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800256 * we hit the water mark. May be even add a low water mark, such that
257 * no reclaim occurs from a cgroup at it's low water mark, this is
258 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800259 */
260struct mem_cgroup {
261 struct cgroup_subsys_state css;
262 /*
263 * the counter to account for memory usage
264 */
265 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700266
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700267 /* vmpressure notifications */
268 struct vmpressure vmpressure;
269
Hugh Dickins59927fb2012-03-15 15:17:07 -0700270 union {
271 /*
272 * the counter to account for mem+swap usage.
273 */
274 struct res_counter memsw;
275
276 /*
277 * rcu_freeing is used only when freeing struct mem_cgroup,
278 * so put it into a union to avoid wasting more memory.
279 * It must be disjoint from the css field. It could be
280 * in a union with the res field, but res plays a much
281 * larger part in mem_cgroup life than memsw, and might
282 * be of interest, even at time of free, when debugging.
283 * So share rcu_head with the less interesting memsw.
284 */
285 struct rcu_head rcu_freeing;
286 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700287 * We also need some space for a worker in deferred freeing.
288 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700289 */
290 struct work_struct work_freeing;
291 };
292
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800293 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800294 * the counter to account for kernel memory usage.
295 */
296 struct res_counter kmem;
297 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800298 * Should the accounting and control be hierarchical, per subtree?
299 */
300 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800301 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700302
303 bool oom_lock;
304 atomic_t under_oom;
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700305 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700306
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800307 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800308
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700309 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700310 /* OOM-Killer disable */
311 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800312
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700313 /* set when res.limit == memsw.limit */
314 bool memsw_is_minimum;
315
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800316 /* protect arrays of thresholds */
317 struct mutex thresholds_lock;
318
319 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700320 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700321
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800322 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700323 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700324
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700325 /* For oom notifier event fd */
326 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700327
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800328 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800329 * Should we move charges of a task when a task is moved into this
330 * mem_cgroup ? And what type of charges should we move ?
331 */
332 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800333 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700334 * set > 0 if pages under this cgroup are moving to other cgroup.
335 */
336 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700337 /* taken only while moving_account > 0 */
338 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700339 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800340 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800341 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700342 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700343 /*
344 * used when a cpu is offlined or other synchronizations
345 * See mem_cgroup_read_stat().
346 */
347 struct mem_cgroup_stat_cpu nocpu_base;
348 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000349
Michal Hocko5f578162013-04-29 15:07:17 -0700350 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700351#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000352 struct tcp_memcontrol tcp_mem;
353#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800354#if defined(CONFIG_MEMCG_KMEM)
355 /* analogous to slab_common's slab_caches list. per-memcg */
356 struct list_head memcg_slab_caches;
357 /* Not a spinlock, we can take a lot of time walking the list */
358 struct mutex slab_caches_mutex;
359 /* Index in the kmem_cache->memcg_params->memcg_caches array */
360 int kmemcg_id;
361#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800362
363 int last_scanned_node;
364#if MAX_NUMNODES > 1
365 nodemask_t scan_nodes;
366 atomic_t numainfo_events;
367 atomic_t numainfo_updating;
368#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700369
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800370 /*
371 * Per cgroup active and inactive list, similar to the
372 * per zone LRU lists.
373 *
374 * WARNING: This has to be the last element of the struct. Don't
375 * add new fields after this point.
376 */
377 struct mem_cgroup_lru_info info;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800378};
379
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800380static size_t memcg_size(void)
381{
382 return sizeof(struct mem_cgroup) +
Vladimir Davydov76fca222014-01-02 12:58:47 -0800383 nr_node_ids * sizeof(struct mem_cgroup_per_node *);
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800384}
385
Glauber Costa510fc4e2012-12-18 14:21:47 -0800386/* internal only representation about the status of kmem accounting. */
387enum {
388 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800389 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800390 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800391};
392
Glauber Costaa8964b92012-12-18 14:22:09 -0800393/* We account when limit is on, but only after call sites are patched */
394#define KMEM_ACCOUNTED_MASK \
395 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800396
397#ifdef CONFIG_MEMCG_KMEM
398static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
399{
400 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
401}
Glauber Costa7de37682012-12-18 14:22:07 -0800402
403static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
404{
405 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
406}
407
Glauber Costaa8964b92012-12-18 14:22:09 -0800408static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
409{
410 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
411}
412
Glauber Costa55007d82012-12-18 14:22:38 -0800413static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
414{
415 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
416}
417
Glauber Costa7de37682012-12-18 14:22:07 -0800418static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
419{
420 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
421 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
422}
423
424static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
425{
426 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
427 &memcg->kmem_account_flags);
428}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800429#endif
430
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800431/* Stuffs for move charges at task migration. */
432/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800433 * Types of charges to be moved. "move_charge_at_immitgrate" and
434 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800435 */
436enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800437 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700438 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800439 NR_MOVE_TYPE,
440};
441
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800442/* "mc" and its members are protected by cgroup_mutex */
443static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800444 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800445 struct mem_cgroup *from;
446 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800447 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800448 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800449 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800450 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800451 struct task_struct *moving_task; /* a task moving charges */
452 wait_queue_head_t waitq; /* a waitq for other context */
453} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700454 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800455 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
456};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800457
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700458static bool move_anon(void)
459{
Glauber Costaee5e8472013-02-22 16:34:50 -0800460 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700461}
462
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700463static bool move_file(void)
464{
Glauber Costaee5e8472013-02-22 16:34:50 -0800465 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700466}
467
Balbir Singh4e416952009-09-23 15:56:39 -0700468/*
469 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
470 * limit reclaim to prevent infinite loops, if they ever occur.
471 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700472#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
473#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700474
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800475enum charge_type {
476 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700477 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800478 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700479 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700480 NR_CHARGE_TYPE,
481};
482
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800483/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800484enum res_type {
485 _MEM,
486 _MEMSWAP,
487 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800488 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800489};
490
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700491#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
492#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800493#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700494/* Used for OOM nofiier */
495#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800496
Balbir Singh75822b42009-09-23 15:56:38 -0700497/*
498 * Reclaim flags for mem_cgroup_hierarchical_reclaim
499 */
500#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
501#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
502#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
503#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
504
Glauber Costa09998212013-02-22 16:34:55 -0800505/*
506 * The memcg_create_mutex will be held whenever a new cgroup is created.
507 * As a consequence, any change that needs to protect against new child cgroups
508 * appearing has to hold it as well.
509 */
510static DEFINE_MUTEX(memcg_create_mutex);
511
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700512static void mem_cgroup_get(struct mem_cgroup *memcg);
513static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000514
Wanpeng Lib2145142012-07-31 16:46:01 -0700515static inline
516struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
517{
518 return container_of(s, struct mem_cgroup, css);
519}
520
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700521/* Some nice accessors for the vmpressure. */
522struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
523{
524 if (!memcg)
525 memcg = root_mem_cgroup;
526 return &memcg->vmpressure;
527}
528
529struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
530{
531 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
532}
533
534struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
535{
536 return &mem_cgroup_from_css(css)->vmpressure;
537}
538
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700539static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
540{
541 return (memcg == root_mem_cgroup);
542}
543
Glauber Costae1aab162011-12-11 21:47:03 +0000544/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700545#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000546
Glauber Costae1aab162011-12-11 21:47:03 +0000547void sock_update_memcg(struct sock *sk)
548{
Glauber Costa376be5f2012-01-20 04:57:14 +0000549 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000550 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700551 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000552
553 BUG_ON(!sk->sk_prot->proto_cgroup);
554
Glauber Costaf3f511e2012-01-05 20:16:39 +0000555 /* Socket cloning can throw us here with sk_cgrp already
556 * filled. It won't however, necessarily happen from
557 * process context. So the test for root memcg given
558 * the current task's memcg won't help us in this case.
559 *
560 * Respecting the original socket's memcg is a better
561 * decision in this case.
562 */
563 if (sk->sk_cgrp) {
564 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
565 mem_cgroup_get(sk->sk_cgrp->memcg);
566 return;
567 }
568
Glauber Costae1aab162011-12-11 21:47:03 +0000569 rcu_read_lock();
570 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700571 cg_proto = sk->sk_prot->proto_cgroup(memcg);
572 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000573 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700574 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000575 }
576 rcu_read_unlock();
577 }
578}
579EXPORT_SYMBOL(sock_update_memcg);
580
581void sock_release_memcg(struct sock *sk)
582{
Glauber Costa376be5f2012-01-20 04:57:14 +0000583 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000584 struct mem_cgroup *memcg;
585 WARN_ON(!sk->sk_cgrp->memcg);
586 memcg = sk->sk_cgrp->memcg;
587 mem_cgroup_put(memcg);
588 }
589}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000590
591struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
592{
593 if (!memcg || mem_cgroup_is_root(memcg))
594 return NULL;
595
596 return &memcg->tcp_mem.cg_proto;
597}
598EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000599
Glauber Costa3f134612012-05-29 15:07:11 -0700600static void disarm_sock_keys(struct mem_cgroup *memcg)
601{
602 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
603 return;
604 static_key_slow_dec(&memcg_socket_limit_enabled);
605}
606#else
607static void disarm_sock_keys(struct mem_cgroup *memcg)
608{
609}
610#endif
611
Glauber Costaa8964b92012-12-18 14:22:09 -0800612#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800613/*
614 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
615 * There are two main reasons for not using the css_id for this:
616 * 1) this works better in sparse environments, where we have a lot of memcgs,
617 * but only a few kmem-limited. Or also, if we have, for instance, 200
618 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
619 * 200 entry array for that.
620 *
621 * 2) In order not to violate the cgroup API, we would like to do all memory
622 * allocation in ->create(). At that point, we haven't yet allocated the
623 * css_id. Having a separate index prevents us from messing with the cgroup
624 * core for this
625 *
626 * The current size of the caches array is stored in
627 * memcg_limited_groups_array_size. It will double each time we have to
628 * increase it.
629 */
630static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800631int memcg_limited_groups_array_size;
632
Glauber Costa55007d82012-12-18 14:22:38 -0800633/*
634 * MIN_SIZE is different than 1, because we would like to avoid going through
635 * the alloc/free process all the time. In a small machine, 4 kmem-limited
636 * cgroups is a reasonable guess. In the future, it could be a parameter or
637 * tunable, but that is strictly not necessary.
638 *
639 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
640 * this constant directly from cgroup, but it is understandable that this is
641 * better kept as an internal representation in cgroup.c. In any case, the
642 * css_id space is not getting any smaller, and we don't have to necessarily
643 * increase ours as well if it increases.
644 */
645#define MEMCG_CACHES_MIN_SIZE 4
646#define MEMCG_CACHES_MAX_SIZE 65535
647
Glauber Costad7f25f82012-12-18 14:22:40 -0800648/*
649 * A lot of the calls to the cache allocation functions are expected to be
650 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
651 * conditional to this static branch, we'll have to allow modules that does
652 * kmem_cache_alloc and the such to see this symbol as well
653 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800654struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800655EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800656
657static void disarm_kmem_keys(struct mem_cgroup *memcg)
658{
Glauber Costa55007d82012-12-18 14:22:38 -0800659 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800660 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800661 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
662 }
Glauber Costabea207c2012-12-18 14:22:11 -0800663 /*
664 * This check can't live in kmem destruction function,
665 * since the charges will outlive the cgroup
666 */
667 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800668}
669#else
670static void disarm_kmem_keys(struct mem_cgroup *memcg)
671{
672}
673#endif /* CONFIG_MEMCG_KMEM */
674
675static void disarm_static_keys(struct mem_cgroup *memcg)
676{
677 disarm_sock_keys(memcg);
678 disarm_kmem_keys(memcg);
679}
680
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700681static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800682
Balbir Singhf64c3f52009-09-23 15:56:37 -0700683static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700684mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700685{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800686 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700687 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700688}
689
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700690struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100691{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700692 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100693}
694
Balbir Singhf64c3f52009-09-23 15:56:37 -0700695static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700696page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700697{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700698 int nid = page_to_nid(page);
699 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700700
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700701 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700702}
703
704static struct mem_cgroup_tree_per_zone *
705soft_limit_tree_node_zone(int nid, int zid)
706{
707 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
708}
709
710static struct mem_cgroup_tree_per_zone *
711soft_limit_tree_from_page(struct page *page)
712{
713 int nid = page_to_nid(page);
714 int zid = page_zonenum(page);
715
716 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
717}
718
719static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700720__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700721 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700722 struct mem_cgroup_tree_per_zone *mctz,
723 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700724{
725 struct rb_node **p = &mctz->rb_root.rb_node;
726 struct rb_node *parent = NULL;
727 struct mem_cgroup_per_zone *mz_node;
728
729 if (mz->on_tree)
730 return;
731
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700732 mz->usage_in_excess = new_usage_in_excess;
733 if (!mz->usage_in_excess)
734 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700735 while (*p) {
736 parent = *p;
737 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
738 tree_node);
739 if (mz->usage_in_excess < mz_node->usage_in_excess)
740 p = &(*p)->rb_left;
741 /*
742 * We can't avoid mem cgroups that are over their soft
743 * limit by the same amount
744 */
745 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
746 p = &(*p)->rb_right;
747 }
748 rb_link_node(&mz->tree_node, parent, p);
749 rb_insert_color(&mz->tree_node, &mctz->rb_root);
750 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700751}
752
753static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700754__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700755 struct mem_cgroup_per_zone *mz,
756 struct mem_cgroup_tree_per_zone *mctz)
757{
758 if (!mz->on_tree)
759 return;
760 rb_erase(&mz->tree_node, &mctz->rb_root);
761 mz->on_tree = false;
762}
763
764static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700765mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700766 struct mem_cgroup_per_zone *mz,
767 struct mem_cgroup_tree_per_zone *mctz)
768{
769 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700770 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700771 spin_unlock(&mctz->lock);
772}
773
Balbir Singhf64c3f52009-09-23 15:56:37 -0700774
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700776{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700777 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700778 struct mem_cgroup_per_zone *mz;
779 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700780 int nid = page_to_nid(page);
781 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700782 mctz = soft_limit_tree_from_page(page);
783
784 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700785 * Necessary to update all ancestors when hierarchy is used.
786 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700787 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
789 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
790 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700791 /*
792 * We have to update the tree if mz is on RB-tree or
793 * mem is over its softlimit.
794 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700795 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700796 spin_lock(&mctz->lock);
797 /* if on-tree, remove it */
798 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700799 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700800 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700801 * Insert again. mz->usage_in_excess will be updated.
802 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700803 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700804 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700805 spin_unlock(&mctz->lock);
806 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700807 }
808}
809
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700810static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700811{
812 int node, zone;
813 struct mem_cgroup_per_zone *mz;
814 struct mem_cgroup_tree_per_zone *mctz;
815
Bob Liu3ed28fa2012-01-12 17:19:04 -0800816 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700817 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700818 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700819 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700820 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700821 }
822 }
823}
824
Balbir Singh4e416952009-09-23 15:56:39 -0700825static struct mem_cgroup_per_zone *
826__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
827{
828 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700829 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700830
831retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700832 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700833 rightmost = rb_last(&mctz->rb_root);
834 if (!rightmost)
835 goto done; /* Nothing to reclaim from */
836
837 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
838 /*
839 * Remove the node now but someone else can add it back,
840 * we will to add it back at the end of reclaim to its correct
841 * position in the tree.
842 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700843 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
844 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
845 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700846 goto retry;
847done:
848 return mz;
849}
850
851static struct mem_cgroup_per_zone *
852mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
853{
854 struct mem_cgroup_per_zone *mz;
855
856 spin_lock(&mctz->lock);
857 mz = __mem_cgroup_largest_soft_limit_node(mctz);
858 spin_unlock(&mctz->lock);
859 return mz;
860}
861
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700862/*
863 * Implementation Note: reading percpu statistics for memcg.
864 *
865 * Both of vmstat[] and percpu_counter has threshold and do periodic
866 * synchronization to implement "quick" read. There are trade-off between
867 * reading cost and precision of value. Then, we may have a chance to implement
868 * a periodic synchronizion of counter in memcg's counter.
869 *
870 * But this _read() function is used for user interface now. The user accounts
871 * memory usage by memory cgroup and he _always_ requires exact value because
872 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
873 * have to visit all online cpus and make sum. So, for now, unnecessary
874 * synchronization is not implemented. (just implemented for cpu hotplug)
875 *
876 * If there are kernel internal actions which can make use of some not-exact
877 * value, and reading all cpu value can be performance bottleneck in some
878 * common workload, threashold and synchonization as vmstat[] should be
879 * implemented.
880 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700881static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700882 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800883{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700884 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800885 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800886
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700887 get_online_cpus();
888 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700889 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700890#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700891 spin_lock(&memcg->pcp_counter_lock);
892 val += memcg->nocpu_base.count[idx];
893 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700894#endif
895 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800896 return val;
897}
898
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700899static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700900 bool charge)
901{
902 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700903 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700904}
905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700906static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700907 enum mem_cgroup_events_index idx)
908{
909 unsigned long val = 0;
910 int cpu;
911
912 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700913 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700914#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700915 spin_lock(&memcg->pcp_counter_lock);
916 val += memcg->nocpu_base.events[idx];
917 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700918#endif
919 return val;
920}
921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700922static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700923 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700924 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800925{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800926 preempt_disable();
927
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700928 /*
929 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
930 * counted as CACHE even if it's on ANON LRU.
931 */
932 if (anon)
933 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700934 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800935 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700936 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700937 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700938
David Rientjesb070e652013-05-07 16:18:09 -0700939 if (PageTransHuge(page))
940 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
941 nr_pages);
942
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800943 /* pagein of a big page is an event. So, ignore page size */
944 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700945 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800946 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700947 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800948 nr_pages = -nr_pages; /* for event */
949 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800950
Johannes Weiner13114712012-05-29 15:07:07 -0700951 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800952
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800953 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800954}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800955
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700956unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700957mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700958{
959 struct mem_cgroup_per_zone *mz;
960
961 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
962 return mz->lru_size[lru];
963}
964
965static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700966mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700967 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700968{
969 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700970 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700971 unsigned long ret = 0;
972
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700973 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700974
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700975 for_each_lru(lru) {
976 if (BIT(lru) & lru_mask)
977 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700978 }
979 return ret;
980}
981
982static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700983mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700984 int nid, unsigned int lru_mask)
985{
Ying Han889976d2011-05-26 16:25:33 -0700986 u64 total = 0;
987 int zid;
988
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700989 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700990 total += mem_cgroup_zone_nr_lru_pages(memcg,
991 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700992
Ying Han889976d2011-05-26 16:25:33 -0700993 return total;
994}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700995
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700996static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700997 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800998{
Ying Han889976d2011-05-26 16:25:33 -0700999 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001000 u64 total = 0;
1001
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001002 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001003 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001004 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -08001005}
1006
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001007static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
1008 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001009{
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001010 unsigned long val, next;
1011
Johannes Weiner13114712012-05-29 15:07:07 -07001012 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -07001013 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001014 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001015 if ((long)next - (long)val < 0) {
1016 switch (target) {
1017 case MEM_CGROUP_TARGET_THRESH:
1018 next = val + THRESHOLDS_EVENTS_TARGET;
1019 break;
1020 case MEM_CGROUP_TARGET_SOFTLIMIT:
1021 next = val + SOFTLIMIT_EVENTS_TARGET;
1022 break;
1023 case MEM_CGROUP_TARGET_NUMAINFO:
1024 next = val + NUMAINFO_EVENTS_TARGET;
1025 break;
1026 default:
1027 break;
1028 }
1029 __this_cpu_write(memcg->stat->targets[target], next);
1030 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001031 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001032 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001033}
1034
1035/*
1036 * Check events in order.
1037 *
1038 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001039static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001040{
Steven Rostedt47994012011-11-02 13:38:33 -07001041 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001042 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001043 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1044 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001045 bool do_softlimit;
1046 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001047
1048 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1049 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001050#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001051 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1052 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001053#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001054 preempt_enable();
1055
1056 mem_cgroup_threshold(memcg);
1057 if (unlikely(do_softlimit))
1058 mem_cgroup_update_tree(memcg, page);
1059#if MAX_NUMNODES > 1
1060 if (unlikely(do_numainfo))
1061 atomic_inc(&memcg->numainfo_events);
1062#endif
1063 } else
1064 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001065}
1066
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001067struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001068{
Wanpeng Lib2145142012-07-31 16:46:01 -07001069 return mem_cgroup_from_css(
1070 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001071}
1072
Balbir Singhcf475ad2008-04-29 01:00:16 -07001073struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001074{
Balbir Singh31a78f22008-09-28 23:09:31 +01001075 /*
1076 * mm_update_next_owner() may clear mm->owner to NULL
1077 * if it races with swapoff, page migration, etc.
1078 * So this can be called with p == NULL.
1079 */
1080 if (unlikely(!p))
1081 return NULL;
1082
Wanpeng Lib2145142012-07-31 16:46:01 -07001083 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001084}
1085
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001086struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001087{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001088 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001089
1090 if (!mm)
1091 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001092 /*
1093 * Because we have no locks, mm->owner's may be being moved to other
1094 * cgroup. We use css_tryget() here even if this looks
1095 * pessimistic (rather than adding locks here).
1096 */
1097 rcu_read_lock();
1098 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001099 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1100 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001101 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001102 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001103 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001104 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001105}
1106
Michal Hocko16248d82013-04-29 15:07:19 -07001107/*
1108 * Returns a next (in a pre-order walk) alive memcg (with elevated css
1109 * ref. count) or NULL if the whole root's subtree has been visited.
1110 *
1111 * helper function to be used by mem_cgroup_iter
1112 */
1113static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
1114 struct mem_cgroup *last_visited)
1115{
1116 struct cgroup *prev_cgroup, *next_cgroup;
1117
1118 /*
1119 * Root is not visited by cgroup iterators so it needs an
1120 * explicit visit.
1121 */
1122 if (!last_visited)
1123 return root;
1124
1125 prev_cgroup = (last_visited == root) ? NULL
1126 : last_visited->css.cgroup;
1127skip_node:
1128 next_cgroup = cgroup_next_descendant_pre(
1129 prev_cgroup, root->css.cgroup);
1130
1131 /*
1132 * Even if we found a group we have to make sure it is
1133 * alive. css && !memcg means that the groups should be
1134 * skipped and we should continue the tree walk.
1135 * last_visited css is safe to use because it is
1136 * protected by css_get and the tree walk is rcu safe.
1137 */
1138 if (next_cgroup) {
1139 struct mem_cgroup *mem = mem_cgroup_from_cont(
1140 next_cgroup);
1141 if (css_tryget(&mem->css))
1142 return mem;
1143 else {
1144 prev_cgroup = next_cgroup;
1145 goto skip_node;
1146 }
1147 }
1148
1149 return NULL;
1150}
1151
Johannes Weiner56600482012-01-12 17:17:59 -08001152/**
1153 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1154 * @root: hierarchy root
1155 * @prev: previously returned memcg, NULL on first invocation
1156 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1157 *
1158 * Returns references to children of the hierarchy below @root, or
1159 * @root itself, or %NULL after a full round-trip.
1160 *
1161 * Caller must pass the return value in @prev on subsequent
1162 * invocations for reference counting, or use mem_cgroup_iter_break()
1163 * to cancel a hierarchy walk before the round-trip is complete.
1164 *
1165 * Reclaimers can specify a zone and a priority level in @reclaim to
1166 * divide up the memcgs in the hierarchy among all concurrent
1167 * reclaimers operating on the same zone and priority.
1168 */
1169struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1170 struct mem_cgroup *prev,
1171 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001172{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001173 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001174 struct mem_cgroup *last_visited = NULL;
Michal Hocko5f578162013-04-29 15:07:17 -07001175 unsigned long uninitialized_var(dead_count);
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001176
Johannes Weiner56600482012-01-12 17:17:59 -08001177 if (mem_cgroup_disabled())
1178 return NULL;
1179
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001180 if (!root)
1181 root = root_mem_cgroup;
1182
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001183 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001184 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001185
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001186 if (!root->use_hierarchy && root != root_mem_cgroup) {
1187 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001188 goto out_css_put;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001189 return root;
1190 }
1191
Michal Hocko542f85f2013-04-29 15:07:15 -07001192 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001193 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001194 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001195
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001196 if (reclaim) {
1197 int nid = zone_to_nid(reclaim->zone);
1198 int zid = zone_idx(reclaim->zone);
1199 struct mem_cgroup_per_zone *mz;
1200
1201 mz = mem_cgroup_zoneinfo(root, nid, zid);
1202 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001203 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001204 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001205 goto out_unlock;
1206 }
Michal Hocko5f578162013-04-29 15:07:17 -07001207
1208 /*
1209 * If the dead_count mismatches, a destruction
1210 * has happened or is happening concurrently.
1211 * If the dead_count matches, a destruction
1212 * might still happen concurrently, but since
1213 * we checked under RCU, that destruction
1214 * won't free the object until we release the
1215 * RCU reader lock. Thus, the dead_count
1216 * check verifies the pointer is still valid,
1217 * css_tryget() verifies the cgroup pointed to
1218 * is alive.
1219 */
1220 dead_count = atomic_read(&root->dead_count);
Johannes Weiner89dc9912013-06-12 14:05:09 -07001221 if (dead_count == iter->last_dead_count) {
1222 smp_rmb();
1223 last_visited = iter->last_visited;
Michal Hocko5fb67b92014-01-23 15:53:35 -08001224 if (last_visited && last_visited != root &&
Johannes Weiner89dc9912013-06-12 14:05:09 -07001225 !css_tryget(&last_visited->css))
Michal Hocko5f578162013-04-29 15:07:17 -07001226 last_visited = NULL;
Michal Hocko5f578162013-04-29 15:07:17 -07001227 }
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001228 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001229
Michal Hocko16248d82013-04-29 15:07:19 -07001230 memcg = __mem_cgroup_iter_next(root, last_visited);
Michal Hocko542f85f2013-04-29 15:07:15 -07001231
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001232 if (reclaim) {
Michal Hocko5fb67b92014-01-23 15:53:35 -08001233 if (last_visited && last_visited != root)
Michal Hocko542f85f2013-04-29 15:07:15 -07001234 css_put(&last_visited->css);
1235
Michal Hocko19f39402013-04-29 15:07:18 -07001236 iter->last_visited = memcg;
Michal Hocko5f578162013-04-29 15:07:17 -07001237 smp_wmb();
1238 iter->last_dead_count = dead_count;
Michal Hocko542f85f2013-04-29 15:07:15 -07001239
Michal Hocko19f39402013-04-29 15:07:18 -07001240 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001241 iter->generation++;
1242 else if (!prev && memcg)
1243 reclaim->generation = iter->generation;
1244 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001245
Michal Hocko19f39402013-04-29 15:07:18 -07001246 if (prev && !memcg)
Michal Hocko542f85f2013-04-29 15:07:15 -07001247 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001248 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001249out_unlock:
1250 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001251out_css_put:
1252 if (prev && prev != root)
1253 css_put(&prev->css);
1254
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001255 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001256}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001257
Johannes Weiner56600482012-01-12 17:17:59 -08001258/**
1259 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1260 * @root: hierarchy root
1261 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1262 */
1263void mem_cgroup_iter_break(struct mem_cgroup *root,
1264 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001265{
1266 if (!root)
1267 root = root_mem_cgroup;
1268 if (prev && prev != root)
1269 css_put(&prev->css);
1270}
1271
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001272/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001273 * Iteration constructs for visiting all cgroups (under a tree). If
1274 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1275 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001276 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001277#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001278 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001279 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001280 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001281
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001282#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001283 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001284 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001285 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001286
David Rientjes68ae5642012-12-12 13:51:57 -08001287void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001288{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001289 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001290
Ying Han456f9982011-05-26 16:25:38 -07001291 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001292 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1293 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001294 goto out;
1295
1296 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001297 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001298 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1299 break;
1300 case PGMAJFAULT:
1301 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001302 break;
1303 default:
1304 BUG();
1305 }
1306out:
1307 rcu_read_unlock();
1308}
David Rientjes68ae5642012-12-12 13:51:57 -08001309EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001310
Johannes Weiner925b7672012-01-12 17:18:15 -08001311/**
1312 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1313 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001314 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001315 *
1316 * Returns the lru list vector holding pages for the given @zone and
1317 * @mem. This can be the global zone lruvec, if the memory controller
1318 * is disabled.
1319 */
1320struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1321 struct mem_cgroup *memcg)
1322{
1323 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001324 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001325
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001326 if (mem_cgroup_disabled()) {
1327 lruvec = &zone->lruvec;
1328 goto out;
1329 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001330
1331 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001332 lruvec = &mz->lruvec;
1333out:
1334 /*
1335 * Since a node can be onlined after the mem_cgroup was created,
1336 * we have to be prepared to initialize lruvec->zone here;
1337 * and if offlined then reonlined, we need to reinitialize it.
1338 */
1339 if (unlikely(lruvec->zone != zone))
1340 lruvec->zone = zone;
1341 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001342}
1343
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001344/*
1345 * Following LRU functions are allowed to be used without PCG_LOCK.
1346 * Operations are called by routine of global LRU independently from memcg.
1347 * What we have to take care of here is validness of pc->mem_cgroup.
1348 *
1349 * Changes to pc->mem_cgroup happens when
1350 * 1. charge
1351 * 2. moving account
1352 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1353 * It is added to LRU before charge.
1354 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1355 * When moving account, the page is not on LRU. It's isolated.
1356 */
1357
Johannes Weiner925b7672012-01-12 17:18:15 -08001358/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001359 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001360 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001361 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001362 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001363struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001364{
1365 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001366 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001367 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001368 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001369
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001370 if (mem_cgroup_disabled()) {
1371 lruvec = &zone->lruvec;
1372 goto out;
1373 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001374
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001375 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001376 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001377
1378 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001379 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001380 * an uncharged page off lru does nothing to secure
1381 * its former mem_cgroup from sudden removal.
1382 *
1383 * Our caller holds lru_lock, and PageCgroupUsed is updated
1384 * under page_cgroup lock: between them, they make all uses
1385 * of pc->mem_cgroup safe.
1386 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001387 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001388 pc->mem_cgroup = memcg = root_mem_cgroup;
1389
Johannes Weiner925b7672012-01-12 17:18:15 -08001390 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001391 lruvec = &mz->lruvec;
1392out:
1393 /*
1394 * Since a node can be onlined after the mem_cgroup was created,
1395 * we have to be prepared to initialize lruvec->zone here;
1396 * and if offlined then reonlined, we need to reinitialize it.
1397 */
1398 if (unlikely(lruvec->zone != zone))
1399 lruvec->zone = zone;
1400 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001401}
1402
1403/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001404 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1405 * @lruvec: mem_cgroup per zone lru vector
1406 * @lru: index of lru list the page is sitting on
1407 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001408 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001409 * This function must be called when a page is added to or removed from an
1410 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001411 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001412void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1413 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001414{
1415 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001416 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001417
1418 if (mem_cgroup_disabled())
1419 return;
1420
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001421 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1422 lru_size = mz->lru_size + lru;
1423 *lru_size += nr_pages;
1424 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001425}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001426
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001427/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001428 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001429 * hierarchy subtree
1430 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001431bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1432 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001433{
Johannes Weiner91c637342012-05-29 15:06:24 -07001434 if (root_memcg == memcg)
1435 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001436 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001437 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001438 return css_is_ancestor(&memcg->css, &root_memcg->css);
1439}
1440
1441static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1442 struct mem_cgroup *memcg)
1443{
1444 bool ret;
1445
Johannes Weiner91c637342012-05-29 15:06:24 -07001446 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001447 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001448 rcu_read_unlock();
1449 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001450}
1451
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001452int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001453{
1454 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001455 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001456 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001457
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001458 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001459 if (p) {
1460 curr = try_get_mem_cgroup_from_mm(p->mm);
1461 task_unlock(p);
1462 } else {
1463 /*
1464 * All threads may have already detached their mm's, but the oom
1465 * killer still needs to detect if they have already been oom
1466 * killed to prevent needlessly killing additional tasks.
1467 */
1468 task_lock(task);
1469 curr = mem_cgroup_from_task(task);
1470 if (curr)
1471 css_get(&curr->css);
1472 task_unlock(task);
1473 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001474 if (!curr)
1475 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001476 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001477 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001478 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001479 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1480 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001481 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001482 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001483 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001484 return ret;
1485}
1486
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001487int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001488{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001489 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001490 unsigned long inactive;
1491 unsigned long active;
1492 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001493
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001494 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1495 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001496
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001497 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1498 if (gb)
1499 inactive_ratio = int_sqrt(10 * gb);
1500 else
1501 inactive_ratio = 1;
1502
Johannes Weiner9b272972011-11-02 13:38:23 -07001503 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001504}
1505
Balbir Singh6d61ef42009-01-07 18:08:06 -08001506#define mem_cgroup_from_res_counter(counter, member) \
1507 container_of(counter, struct mem_cgroup, member)
1508
Johannes Weiner19942822011-02-01 15:52:43 -08001509/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001510 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001511 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001512 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001513 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001514 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001515 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001517{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001518 unsigned long long margin;
1519
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001520 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001521 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001522 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001523 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001524}
1525
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001526int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001527{
1528 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001529
1530 /* root ? */
1531 if (cgrp->parent == NULL)
1532 return vm_swappiness;
1533
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001534 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001535}
1536
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001537/*
1538 * memcg->moving_account is used for checking possibility that some thread is
1539 * calling move_account(). When a thread on CPU-A starts moving pages under
1540 * a memcg, other threads should check memcg->moving_account under
1541 * rcu_read_lock(), like this:
1542 *
1543 * CPU-A CPU-B
1544 * rcu_read_lock()
1545 * memcg->moving_account+1 if (memcg->mocing_account)
1546 * take heavy locks.
1547 * synchronize_rcu() update something.
1548 * rcu_read_unlock()
1549 * start move here.
1550 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001551
1552/* for quick checking without looking up memcg */
1553atomic_t memcg_moving __read_mostly;
1554
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001555static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001556{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001557 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001558 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001559 synchronize_rcu();
1560}
1561
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001562static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001563{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001564 /*
1565 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1566 * We check NULL in callee rather than caller.
1567 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001568 if (memcg) {
1569 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001570 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001571 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001572}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001573
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001574/*
1575 * 2 routines for checking "mem" is under move_account() or not.
1576 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001577 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1578 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001579 * pc->mem_cgroup may be overwritten.
1580 *
1581 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1582 * under hierarchy of moving cgroups. This is for
1583 * waiting at hith-memory prressure caused by "move".
1584 */
1585
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001586static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001587{
1588 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001589 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001590}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001592static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001593{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001594 struct mem_cgroup *from;
1595 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001596 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001597 /*
1598 * Unlike task_move routines, we access mc.to, mc.from not under
1599 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1600 */
1601 spin_lock(&mc.lock);
1602 from = mc.from;
1603 to = mc.to;
1604 if (!from)
1605 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001606
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001607 ret = mem_cgroup_same_or_subtree(memcg, from)
1608 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001609unlock:
1610 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001611 return ret;
1612}
1613
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001614static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001615{
1616 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001617 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001618 DEFINE_WAIT(wait);
1619 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1620 /* moving charge context might have finished. */
1621 if (mc.moving_task)
1622 schedule();
1623 finish_wait(&mc.waitq, &wait);
1624 return true;
1625 }
1626 }
1627 return false;
1628}
1629
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001630/*
1631 * Take this lock when
1632 * - a code tries to modify page's memcg while it's USED.
1633 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001634 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001635 */
1636static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1637 unsigned long *flags)
1638{
1639 spin_lock_irqsave(&memcg->move_lock, *flags);
1640}
1641
1642static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1643 unsigned long *flags)
1644{
1645 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1646}
1647
Sha Zhengju58cf1882013-02-22 16:32:05 -08001648#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001649/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001650 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001651 * @memcg: The memory cgroup that went over limit
1652 * @p: Task that is going to be killed
1653 *
1654 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1655 * enabled
1656 */
1657void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1658{
1659 struct cgroup *task_cgrp;
1660 struct cgroup *mem_cgrp;
1661 /*
1662 * Need a buffer in BSS, can't rely on allocations. The code relies
1663 * on the assumption that OOM is serialized for memory controller.
1664 * If this assumption is broken, revisit this code.
1665 */
1666 static char memcg_name[PATH_MAX];
1667 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001668 struct mem_cgroup *iter;
1669 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001670
Sha Zhengju58cf1882013-02-22 16:32:05 -08001671 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001672 return;
1673
Balbir Singhe2224322009-04-02 16:57:39 -07001674 rcu_read_lock();
1675
1676 mem_cgrp = memcg->css.cgroup;
1677 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1678
1679 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1680 if (ret < 0) {
1681 /*
1682 * Unfortunately, we are unable to convert to a useful name
1683 * But we'll still print out the usage information
1684 */
1685 rcu_read_unlock();
1686 goto done;
1687 }
1688 rcu_read_unlock();
1689
Andrew Mortond0451972013-02-22 16:32:06 -08001690 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001691
1692 rcu_read_lock();
1693 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1694 if (ret < 0) {
1695 rcu_read_unlock();
1696 goto done;
1697 }
1698 rcu_read_unlock();
1699
1700 /*
1701 * Continues from above, so we don't need an KERN_ level
1702 */
Andrew Mortond0451972013-02-22 16:32:06 -08001703 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001704done:
1705
Andrew Mortond0451972013-02-22 16:32:06 -08001706 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001707 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1708 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1709 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001710 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001711 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1712 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1713 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001714 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001715 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1716 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1717 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001718
1719 for_each_mem_cgroup_tree(iter, memcg) {
1720 pr_info("Memory cgroup stats");
1721
1722 rcu_read_lock();
1723 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1724 if (!ret)
1725 pr_cont(" for %s", memcg_name);
1726 rcu_read_unlock();
1727 pr_cont(":");
1728
1729 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1730 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1731 continue;
1732 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1733 K(mem_cgroup_read_stat(iter, i)));
1734 }
1735
1736 for (i = 0; i < NR_LRU_LISTS; i++)
1737 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1738 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1739
1740 pr_cont("\n");
1741 }
Balbir Singhe2224322009-04-02 16:57:39 -07001742}
1743
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001744/*
1745 * This function returns the number of memcg under hierarchy tree. Returns
1746 * 1(self count) if no children.
1747 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001748static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001749{
1750 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001751 struct mem_cgroup *iter;
1752
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001753 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001754 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001755 return num;
1756}
1757
Balbir Singh6d61ef42009-01-07 18:08:06 -08001758/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001759 * Return the memory (and swap, if configured) limit for a memcg.
1760 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001761static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001762{
1763 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001764
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001765 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001766
David Rientjesa63d83f2010-08-09 17:19:46 -07001767 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001768 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001769 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001770 if (mem_cgroup_swappiness(memcg)) {
1771 u64 memsw;
1772
1773 limit += total_swap_pages << PAGE_SHIFT;
1774 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1775
1776 /*
1777 * If memsw is finite and limits the amount of swap space
1778 * available to this memcg, return that limit.
1779 */
1780 limit = min(limit, memsw);
1781 }
1782
1783 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001784}
1785
David Rientjes19965462012-12-11 16:00:26 -08001786static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1787 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001788{
1789 struct mem_cgroup *iter;
1790 unsigned long chosen_points = 0;
1791 unsigned long totalpages;
1792 unsigned int points = 0;
1793 struct task_struct *chosen = NULL;
1794
David Rientjes876aafb2012-07-31 16:43:48 -07001795 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001796 * If current has a pending SIGKILL or is exiting, then automatically
1797 * select it. The goal is to allow it to allocate so that it may
1798 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001799 */
David Rientjes465adcf2013-04-29 15:08:45 -07001800 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001801 set_thread_flag(TIF_MEMDIE);
1802 return;
1803 }
1804
1805 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001806 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1807 for_each_mem_cgroup_tree(iter, memcg) {
1808 struct cgroup *cgroup = iter->css.cgroup;
1809 struct cgroup_iter it;
1810 struct task_struct *task;
1811
1812 cgroup_iter_start(cgroup, &it);
1813 while ((task = cgroup_iter_next(cgroup, &it))) {
1814 switch (oom_scan_process_thread(task, totalpages, NULL,
1815 false)) {
1816 case OOM_SCAN_SELECT:
1817 if (chosen)
1818 put_task_struct(chosen);
1819 chosen = task;
1820 chosen_points = ULONG_MAX;
1821 get_task_struct(chosen);
1822 /* fall through */
1823 case OOM_SCAN_CONTINUE:
1824 continue;
1825 case OOM_SCAN_ABORT:
1826 cgroup_iter_end(cgroup, &it);
1827 mem_cgroup_iter_break(memcg, iter);
1828 if (chosen)
1829 put_task_struct(chosen);
1830 return;
1831 case OOM_SCAN_OK:
1832 break;
1833 };
1834 points = oom_badness(task, memcg, NULL, totalpages);
1835 if (points > chosen_points) {
1836 if (chosen)
1837 put_task_struct(chosen);
1838 chosen = task;
1839 chosen_points = points;
1840 get_task_struct(chosen);
1841 }
1842 }
1843 cgroup_iter_end(cgroup, &it);
1844 }
1845
1846 if (!chosen)
1847 return;
1848 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001849 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1850 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001851}
1852
Johannes Weiner56600482012-01-12 17:17:59 -08001853static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1854 gfp_t gfp_mask,
1855 unsigned long flags)
1856{
1857 unsigned long total = 0;
1858 bool noswap = false;
1859 int loop;
1860
1861 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1862 noswap = true;
1863 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1864 noswap = true;
1865
1866 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1867 if (loop)
1868 drain_all_stock_async(memcg);
1869 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1870 /*
1871 * Allow limit shrinkers, which are triggered directly
1872 * by userspace, to catch signals and stop reclaim
1873 * after minimal progress, regardless of the margin.
1874 */
1875 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1876 break;
1877 if (mem_cgroup_margin(memcg))
1878 break;
1879 /*
1880 * If nothing was reclaimed after two attempts, there
1881 * may be no reclaimable pages in this hierarchy.
1882 */
1883 if (loop && !total)
1884 break;
1885 }
1886 return total;
1887}
1888
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001889/**
1890 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001891 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001892 * @nid: the node ID to be checked.
1893 * @noswap : specify true here if the user wants flle only information.
1894 *
1895 * This function returns whether the specified memcg contains any
1896 * reclaimable pages on a node. Returns true if there are any reclaimable
1897 * pages in the node.
1898 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001899static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001900 int nid, bool noswap)
1901{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001903 return true;
1904 if (noswap || !total_swap_pages)
1905 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001906 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001907 return true;
1908 return false;
1909
1910}
Ying Han889976d2011-05-26 16:25:33 -07001911#if MAX_NUMNODES > 1
1912
1913/*
1914 * Always updating the nodemask is not very good - even if we have an empty
1915 * list or the wrong list here, we can start from some node and traverse all
1916 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1917 *
1918 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001919static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001920{
1921 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001922 /*
1923 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1924 * pagein/pageout changes since the last update.
1925 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001926 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001927 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001928 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001929 return;
1930
Ying Han889976d2011-05-26 16:25:33 -07001931 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001932 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001933
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001934 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001935
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001936 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1937 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001938 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001939
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001940 atomic_set(&memcg->numainfo_events, 0);
1941 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001942}
1943
1944/*
1945 * Selecting a node where we start reclaim from. Because what we need is just
1946 * reducing usage counter, start from anywhere is O,K. Considering
1947 * memory reclaim from current node, there are pros. and cons.
1948 *
1949 * Freeing memory from current node means freeing memory from a node which
1950 * we'll use or we've used. So, it may make LRU bad. And if several threads
1951 * hit limits, it will see a contention on a node. But freeing from remote
1952 * node means more costs for memory reclaim because of memory latency.
1953 *
1954 * Now, we use round-robin. Better algorithm is welcomed.
1955 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001956int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001957{
1958 int node;
1959
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001960 mem_cgroup_may_update_nodemask(memcg);
1961 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001962
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001963 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001964 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001965 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001966 /*
1967 * We call this when we hit limit, not when pages are added to LRU.
1968 * No LRU may hold pages because all pages are UNEVICTABLE or
1969 * memcg is too small and all pages are not on LRU. In that case,
1970 * we use curret node.
1971 */
1972 if (unlikely(node == MAX_NUMNODES))
1973 node = numa_node_id();
1974
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001975 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001976 return node;
1977}
1978
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001979/*
1980 * Check all nodes whether it contains reclaimable pages or not.
1981 * For quick scan, we make use of scan_nodes. This will allow us to skip
1982 * unused nodes. But scan_nodes is lazily updated and may not cotain
1983 * enough new information. We need to do double check.
1984 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001985static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001986{
1987 int nid;
1988
1989 /*
1990 * quick check...making use of scan_node.
1991 * We can skip unused nodes.
1992 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001993 if (!nodes_empty(memcg->scan_nodes)) {
1994 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001995 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001996 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001997
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001998 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001999 return true;
2000 }
2001 }
2002 /*
2003 * Check rest of nodes.
2004 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08002005 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002006 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002007 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002008 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002009 return true;
2010 }
2011 return false;
2012}
2013
Ying Han889976d2011-05-26 16:25:33 -07002014#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002015int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07002016{
2017 return 0;
2018}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002019
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002020static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002021{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002022 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002023}
Ying Han889976d2011-05-26 16:25:33 -07002024#endif
2025
Johannes Weiner56600482012-01-12 17:17:59 -08002026static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
2027 struct zone *zone,
2028 gfp_t gfp_mask,
2029 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08002030{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002031 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08002032 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002033 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07002034 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07002035 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08002036 struct mem_cgroup_reclaim_cookie reclaim = {
2037 .zone = zone,
2038 .priority = 0,
2039 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07002040
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002041 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002042
Balbir Singh4e416952009-09-23 15:56:39 -07002043 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08002044 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002045 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002046 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07002047 if (loop >= 2) {
2048 /*
2049 * If we have not been able to reclaim
2050 * anything, it might because there are
2051 * no reclaimable pages under this hierarchy
2052 */
Johannes Weiner56600482012-01-12 17:17:59 -08002053 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07002054 break;
Balbir Singh4e416952009-09-23 15:56:39 -07002055 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002056 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07002057 * excess >> 2 is not to excessive so as to
2058 * reclaim too much, nor too less that we keep
2059 * coming back to reclaim from this cgroup
2060 */
2061 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002062 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07002063 break;
Balbir Singh4e416952009-09-23 15:56:39 -07002064 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002065 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07002066 }
Johannes Weiner56600482012-01-12 17:17:59 -08002067 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08002068 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08002069 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2070 zone, &nr_scanned);
2071 *total_scanned += nr_scanned;
2072 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002073 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002074 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002075 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002076 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002077}
2078
Johannes Weiner7a147e02013-09-12 15:13:43 -07002079static DEFINE_SPINLOCK(memcg_oom_lock);
2080
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002081/*
2082 * Check OOM-Killer is already running under our hierarchy.
2083 * If someone is running, return false.
2084 */
Johannes Weiner7a147e02013-09-12 15:13:43 -07002085static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002086{
Michal Hocko79dfdac2011-07-26 16:08:23 -07002087 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002088
Johannes Weiner7a147e02013-09-12 15:13:43 -07002089 spin_lock(&memcg_oom_lock);
2090
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002091 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07002092 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002093 /*
2094 * this subtree of our hierarchy is already locked
2095 * so we cannot give a lock.
2096 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07002097 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002098 mem_cgroup_iter_break(memcg, iter);
2099 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07002100 } else
2101 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002102 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002103
Johannes Weiner7a147e02013-09-12 15:13:43 -07002104 if (failed) {
2105 /*
2106 * OK, we failed to lock the whole subtree so we have
2107 * to clean up what we set up to the failing subtree
2108 */
2109 for_each_mem_cgroup_tree(iter, memcg) {
2110 if (iter == failed) {
2111 mem_cgroup_iter_break(memcg, iter);
2112 break;
2113 }
2114 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002115 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07002116 }
Johannes Weiner7a147e02013-09-12 15:13:43 -07002117
2118 spin_unlock(&memcg_oom_lock);
2119
2120 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002121}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002122
Johannes Weiner7a147e02013-09-12 15:13:43 -07002123static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002124{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002125 struct mem_cgroup *iter;
2126
Johannes Weiner7a147e02013-09-12 15:13:43 -07002127 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002128 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002129 iter->oom_lock = false;
Johannes Weiner7a147e02013-09-12 15:13:43 -07002130 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002131}
2132
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002133static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002134{
2135 struct mem_cgroup *iter;
2136
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002137 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002138 atomic_inc(&iter->under_oom);
2139}
2140
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002141static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002142{
2143 struct mem_cgroup *iter;
2144
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002145 /*
2146 * When a new child is created while the hierarchy is under oom,
2147 * mem_cgroup_oom_lock() may not be called. We have to use
2148 * atomic_add_unless() here.
2149 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002151 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002152}
2153
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002154static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2155
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002156struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002157 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002158 wait_queue_t wait;
2159};
2160
2161static int memcg_oom_wake_function(wait_queue_t *wait,
2162 unsigned mode, int sync, void *arg)
2163{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002164 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2165 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002166 struct oom_wait_info *oom_wait_info;
2167
2168 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002169 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002170
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002171 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002172 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002173 * Then we can use css_is_ancestor without taking care of RCU.
2174 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002175 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2176 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002177 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002178 return autoremove_wake_function(wait, mode, sync, arg);
2179}
2180
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002181static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002182{
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002183 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002184 /* for filtering, pass "memcg" as argument. */
2185 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002186}
2187
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002188static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002189{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002190 if (memcg && atomic_read(&memcg->under_oom))
2191 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002192}
2193
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002194static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002195{
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002196 if (!current->memcg_oom.may_oom)
2197 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002198 /*
Johannes Weinerf8a51172013-10-16 13:46:59 -07002199 * We are in the middle of the charge context here, so we
2200 * don't want to block when potentially sitting on a callstack
2201 * that holds all kinds of filesystem and mm locks.
2202 *
2203 * Also, the caller may handle a failed allocation gracefully
2204 * (like optional page cache readahead) and so an OOM killer
2205 * invocation might not even be necessary.
2206 *
2207 * That's why we don't do anything here except remember the
2208 * OOM context and then deal with it at the end of the page
2209 * fault when the stack is unwound, the locks are released,
2210 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002211 */
Johannes Weinerf8a51172013-10-16 13:46:59 -07002212 css_get(&memcg->css);
2213 current->memcg_oom.memcg = memcg;
2214 current->memcg_oom.gfp_mask = mask;
2215 current->memcg_oom.order = order;
2216}
2217
2218/**
2219 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2220 * @handle: actually kill/wait or just clean up the OOM state
2221 *
2222 * This has to be called at the end of a page fault if the memcg OOM
2223 * handler was enabled.
2224 *
2225 * Memcg supports userspace OOM handling where failed allocations must
2226 * sleep on a waitqueue until the userspace task resolves the
2227 * situation. Sleeping directly in the charge context with all kinds
2228 * of locks held is not a good idea, instead we remember an OOM state
2229 * in the task and mem_cgroup_oom_synchronize() has to be called at
2230 * the end of the page fault to complete the OOM handling.
2231 *
2232 * Returns %true if an ongoing memcg OOM situation was detected and
2233 * completed, %false otherwise.
2234 */
2235bool mem_cgroup_oom_synchronize(bool handle)
2236{
2237 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2238 struct oom_wait_info owait;
2239 bool locked;
2240
2241 /* OOM is global, do not handle */
2242 if (!memcg)
2243 return false;
2244
2245 if (!handle)
2246 goto cleanup;
2247
2248 owait.memcg = memcg;
2249 owait.wait.flags = 0;
2250 owait.wait.func = memcg_oom_wake_function;
2251 owait.wait.private = current;
2252 INIT_LIST_HEAD(&owait.wait.task_list);
2253
2254 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weiner7a147e02013-09-12 15:13:43 -07002255 mem_cgroup_mark_under_oom(memcg);
2256
2257 locked = mem_cgroup_oom_trylock(memcg);
2258
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002259 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002260 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002261
Johannes Weiner7a147e02013-09-12 15:13:43 -07002262 if (locked && !memcg->oom_kill_disable) {
2263 mem_cgroup_unmark_under_oom(memcg);
Johannes Weinerf8a51172013-10-16 13:46:59 -07002264 finish_wait(&memcg_oom_waitq, &owait.wait);
2265 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2266 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002267 } else {
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002268 schedule();
Johannes Weinerf8a51172013-10-16 13:46:59 -07002269 mem_cgroup_unmark_under_oom(memcg);
2270 finish_wait(&memcg_oom_waitq, &owait.wait);
2271 }
2272
2273 if (locked) {
Johannes Weiner7a147e02013-09-12 15:13:43 -07002274 mem_cgroup_oom_unlock(memcg);
2275 /*
2276 * There is no guarantee that an OOM-lock contender
2277 * sees the wakeups triggered by the OOM kill
2278 * uncharges. Wake any sleepers explicitely.
2279 */
2280 memcg_oom_recover(memcg);
2281 }
Johannes Weinerf8a51172013-10-16 13:46:59 -07002282cleanup:
2283 current->memcg_oom.memcg = NULL;
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002284 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002285 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002286}
2287
Balbir Singhd69b0422009-06-17 16:26:34 -07002288/*
2289 * Currently used to update mapped file statistics, but the routine can be
2290 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002291 *
2292 * Notes: Race condition
2293 *
2294 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2295 * it tends to be costly. But considering some conditions, we doesn't need
2296 * to do so _always_.
2297 *
2298 * Considering "charge", lock_page_cgroup() is not required because all
2299 * file-stat operations happen after a page is attached to radix-tree. There
2300 * are no race with "charge".
2301 *
2302 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2303 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2304 * if there are race with "uncharge". Statistics itself is properly handled
2305 * by flags.
2306 *
2307 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002308 * small, we check mm->moving_account and detect there are possibility of race
2309 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002310 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002311
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002312void __mem_cgroup_begin_update_page_stat(struct page *page,
2313 bool *locked, unsigned long *flags)
2314{
2315 struct mem_cgroup *memcg;
2316 struct page_cgroup *pc;
2317
2318 pc = lookup_page_cgroup(page);
2319again:
2320 memcg = pc->mem_cgroup;
2321 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2322 return;
2323 /*
2324 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002325 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002326 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002327 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002328 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002329 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002330 return;
2331
2332 move_lock_mem_cgroup(memcg, flags);
2333 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2334 move_unlock_mem_cgroup(memcg, flags);
2335 goto again;
2336 }
2337 *locked = true;
2338}
2339
2340void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2341{
2342 struct page_cgroup *pc = lookup_page_cgroup(page);
2343
2344 /*
2345 * It's guaranteed that pc->mem_cgroup never changes while
2346 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002347 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002348 */
2349 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2350}
2351
Greg Thelen2a7106f2011-01-13 15:47:37 -08002352void mem_cgroup_update_page_stat(struct page *page,
2353 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002354{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002355 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002356 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002357 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002358
Johannes Weinercfa44942012-01-12 17:18:38 -08002359 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002360 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002361
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002362 memcg = pc->mem_cgroup;
2363 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002364 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002365
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002366 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002367 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002368 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002369 break;
2370 default:
2371 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002372 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002373
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002375}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002376
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002377/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002378 * size of first charge trial. "32" comes from vmscan.c's magic value.
2379 * TODO: maybe necessary to use big numbers in big irons.
2380 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002381#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002382struct memcg_stock_pcp {
2383 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002384 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002385 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002386 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002387#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002388};
2389static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002390static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002391
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002392/**
2393 * consume_stock: Try to consume stocked charge on this cpu.
2394 * @memcg: memcg to consume from.
2395 * @nr_pages: how many pages to charge.
2396 *
2397 * The charges will only happen if @memcg matches the current cpu's memcg
2398 * stock, and at least @nr_pages are available in that stock. Failure to
2399 * service an allocation will refill the stock.
2400 *
2401 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002402 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002403static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002404{
2405 struct memcg_stock_pcp *stock;
2406 bool ret = true;
2407
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002408 if (nr_pages > CHARGE_BATCH)
2409 return false;
2410
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002411 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002412 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2413 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002414 else /* need to call res_counter_charge */
2415 ret = false;
2416 put_cpu_var(memcg_stock);
2417 return ret;
2418}
2419
2420/*
2421 * Returns stocks cached in percpu to res_counter and reset cached information.
2422 */
2423static void drain_stock(struct memcg_stock_pcp *stock)
2424{
2425 struct mem_cgroup *old = stock->cached;
2426
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002427 if (stock->nr_pages) {
2428 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2429
2430 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002431 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002432 res_counter_uncharge(&old->memsw, bytes);
2433 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002434 }
2435 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002436}
2437
2438/*
2439 * This must be called under preempt disabled or must be called by
2440 * a thread which is pinned to local cpu.
2441 */
2442static void drain_local_stock(struct work_struct *dummy)
2443{
2444 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2445 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002446 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002447}
2448
Michal Hockoe4777492013-02-22 16:35:40 -08002449static void __init memcg_stock_init(void)
2450{
2451 int cpu;
2452
2453 for_each_possible_cpu(cpu) {
2454 struct memcg_stock_pcp *stock =
2455 &per_cpu(memcg_stock, cpu);
2456 INIT_WORK(&stock->work, drain_local_stock);
2457 }
2458}
2459
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002460/*
2461 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002462 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002463 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002465{
2466 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2467
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002468 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002469 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002471 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002472 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002473 put_cpu_var(memcg_stock);
2474}
2475
2476/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002478 * of the hierarchy under it. sync flag says whether we should block
2479 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002480 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002481static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002482{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002483 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002484
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002485 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002486 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002487 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002488 for_each_online_cpu(cpu) {
2489 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002490 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002491
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002492 memcg = stock->cached;
2493 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002494 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002495 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002496 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002497 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2498 if (cpu == curcpu)
2499 drain_local_stock(&stock->work);
2500 else
2501 schedule_work_on(cpu, &stock->work);
2502 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002503 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002504 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002505
2506 if (!sync)
2507 goto out;
2508
2509 for_each_online_cpu(cpu) {
2510 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002511 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002512 flush_work(&stock->work);
2513 }
2514out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002515 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002516}
2517
2518/*
2519 * Tries to drain stocked charges in other cpus. This function is asynchronous
2520 * and just put a work per cpu for draining localy on each cpu. Caller can
2521 * expects some charges will be back to res_counter later but cannot wait for
2522 * it.
2523 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002524static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002525{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002526 /*
2527 * If someone calls draining, avoid adding more kworker runs.
2528 */
2529 if (!mutex_trylock(&percpu_charge_mutex))
2530 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002531 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002532 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002533}
2534
2535/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002536static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002537{
2538 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002539 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002540 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002541 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002542}
2543
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002544/*
2545 * This function drains percpu counter value from DEAD cpu and
2546 * move it to local cpu. Note that this function can be preempted.
2547 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002548static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002549{
2550 int i;
2551
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002552 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002553 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002554 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002555
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002556 per_cpu(memcg->stat->count[i], cpu) = 0;
2557 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002558 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002559 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002560 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002561
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002562 per_cpu(memcg->stat->events[i], cpu) = 0;
2563 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002564 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002565 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002566}
2567
2568static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002569 unsigned long action,
2570 void *hcpu)
2571{
2572 int cpu = (unsigned long)hcpu;
2573 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002574 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002575
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002576 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002577 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002578
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002579 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002580 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002581
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002582 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002583 mem_cgroup_drain_pcp_counter(iter, cpu);
2584
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002585 stock = &per_cpu(memcg_stock, cpu);
2586 drain_stock(stock);
2587 return NOTIFY_OK;
2588}
2589
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002590
2591/* See __mem_cgroup_try_charge() for details */
2592enum {
2593 CHARGE_OK, /* success */
2594 CHARGE_RETRY, /* need to retry but retry is not bad */
2595 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2596 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002597};
2598
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002599static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002600 unsigned int nr_pages, unsigned int min_pages,
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002601 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002602{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002603 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002604 struct mem_cgroup *mem_over_limit;
2605 struct res_counter *fail_res;
2606 unsigned long flags = 0;
2607 int ret;
2608
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002609 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002610
2611 if (likely(!ret)) {
2612 if (!do_swap_account)
2613 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002614 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002615 if (likely(!ret))
2616 return CHARGE_OK;
2617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002618 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002619 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2620 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2621 } else
2622 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002623 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002624 * Never reclaim on behalf of optional batching, retry with a
2625 * single page instead.
2626 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002627 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002628 return CHARGE_RETRY;
2629
2630 if (!(gfp_mask & __GFP_WAIT))
2631 return CHARGE_WOULDBLOCK;
2632
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002633 if (gfp_mask & __GFP_NORETRY)
2634 return CHARGE_NOMEM;
2635
Johannes Weiner56600482012-01-12 17:17:59 -08002636 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002637 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002638 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002639 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002640 * Even though the limit is exceeded at this point, reclaim
2641 * may have been able to free some pages. Retry the charge
2642 * before killing the task.
2643 *
2644 * Only for regular pages, though: huge pages are rather
2645 * unlikely to succeed so close to the limit, and we fall back
2646 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002647 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002648 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002649 return CHARGE_RETRY;
2650
2651 /*
2652 * At task move, charge accounts can be doubly counted. So, it's
2653 * better to wait until the end of task_move if something is going on.
2654 */
2655 if (mem_cgroup_wait_acct_move(mem_over_limit))
2656 return CHARGE_RETRY;
2657
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002658 if (invoke_oom)
2659 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002660
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002661 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002662}
2663
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002664/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002665 * __mem_cgroup_try_charge() does
2666 * 1. detect memcg to be charged against from passed *mm and *ptr,
2667 * 2. update res_counter
2668 * 3. call memory reclaim if necessary.
2669 *
2670 * In some special case, if the task is fatal, fatal_signal_pending() or
2671 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2672 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2673 * as possible without any hazards. 2: all pages should have a valid
2674 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2675 * pointer, that is treated as a charge to root_mem_cgroup.
2676 *
2677 * So __mem_cgroup_try_charge() will return
2678 * 0 ... on success, filling *ptr with a valid memcg pointer.
2679 * -ENOMEM ... charge failure because of resource limits.
2680 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2681 *
2682 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2683 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002684 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002685static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002686 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002687 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002688 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002689 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002690{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002691 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002692 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002693 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002694 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002695
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002696 /*
2697 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2698 * in system level. So, allow to go ahead dying process in addition to
2699 * MEMDIE process.
2700 */
2701 if (unlikely(test_thread_flag(TIF_MEMDIE)
2702 || fatal_signal_pending(current)))
2703 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002704
Johannes Weinerf8a51172013-10-16 13:46:59 -07002705 if (unlikely(task_in_memcg_oom(current)))
2706 goto bypass;
2707
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002708 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002709 * We always charge the cgroup the mm_struct belongs to.
2710 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002711 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002712 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002713 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002714 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002715 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002716again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002717 if (*ptr) { /* css should be a valid one */
2718 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002719 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002720 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002721 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002722 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002723 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002724 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002725 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002726
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002727 rcu_read_lock();
2728 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002729 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002730 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002731 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002732 * race with swapoff. Then, we have small risk of mis-accouning.
2733 * But such kind of mis-account by race always happens because
2734 * we don't have cgroup_mutex(). It's overkill and we allo that
2735 * small race, here.
2736 * (*) swapoff at el will charge against mm-struct not against
2737 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002738 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002739 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002740 if (!memcg)
2741 memcg = root_mem_cgroup;
2742 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002743 rcu_read_unlock();
2744 goto done;
2745 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002746 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002747 /*
2748 * It seems dagerous to access memcg without css_get().
2749 * But considering how consume_stok works, it's not
2750 * necessary. If consume_stock success, some charges
2751 * from this memcg are cached on this cpu. So, we
2752 * don't need to call css_get()/css_tryget() before
2753 * calling consume_stock().
2754 */
2755 rcu_read_unlock();
2756 goto done;
2757 }
2758 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002759 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002760 rcu_read_unlock();
2761 goto again;
2762 }
2763 rcu_read_unlock();
2764 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002765
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002766 do {
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002767 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002768
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002769 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002770 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002771 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002772 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002773 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002774
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002775 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2776 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002777 switch (ret) {
2778 case CHARGE_OK:
2779 break;
2780 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002781 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002782 css_put(&memcg->css);
2783 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002784 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002785 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002786 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002787 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002788 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002789 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002790 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002791 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002792 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002793 nr_oom_retries--;
2794 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002795 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002796 } while (ret != CHARGE_OK);
2797
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002798 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 refill_stock(memcg, batch - nr_pages);
2800 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002801done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002802 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002803 return 0;
2804nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002805 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002806 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002807bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002808 *ptr = root_mem_cgroup;
2809 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002810}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002811
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002812/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002813 * Somemtimes we have to undo a charge we got by try_charge().
2814 * This function is for that and do uncharge, put css's refcnt.
2815 * gotten by try_charge().
2816 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002817static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002818 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002819{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002820 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002821 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002822
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002823 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002824 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002825 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002826 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002827}
2828
2829/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002830 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2831 * This is useful when moving usage to parent cgroup.
2832 */
2833static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2834 unsigned int nr_pages)
2835{
2836 unsigned long bytes = nr_pages * PAGE_SIZE;
2837
2838 if (mem_cgroup_is_root(memcg))
2839 return;
2840
2841 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2842 if (do_swap_account)
2843 res_counter_uncharge_until(&memcg->memsw,
2844 memcg->memsw.parent, bytes);
2845}
2846
2847/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002848 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002849 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2850 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2851 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002852 */
2853static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2854{
2855 struct cgroup_subsys_state *css;
2856
2857 /* ID 0 is unused ID */
2858 if (!id)
2859 return NULL;
2860 css = css_lookup(&mem_cgroup_subsys, id);
2861 if (!css)
2862 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002863 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002864}
2865
Wu Fengguange42d9d52009-12-16 12:19:59 +01002866struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002867{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002868 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002869 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002870 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002871 swp_entry_t ent;
2872
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002873 VM_BUG_ON(!PageLocked(page));
2874
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002875 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002876 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002877 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002878 memcg = pc->mem_cgroup;
2879 if (memcg && !css_tryget(&memcg->css))
2880 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002881 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002882 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002883 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002884 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002885 memcg = mem_cgroup_lookup(id);
2886 if (memcg && !css_tryget(&memcg->css))
2887 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002888 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002889 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002890 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002891 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002892}
2893
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002894static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002895 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002896 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002897 enum charge_type ctype,
2898 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002899{
Johannes Weinerce587e62012-04-24 20:22:33 +02002900 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002901 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002902 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002903 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002904 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002905
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002906 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002907 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002908 /*
2909 * we don't need page_cgroup_lock about tail pages, becase they are not
2910 * accessed by any other context at this point.
2911 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002912
2913 /*
2914 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2915 * may already be on some other mem_cgroup's LRU. Take care of it.
2916 */
2917 if (lrucare) {
2918 zone = page_zone(page);
2919 spin_lock_irq(&zone->lru_lock);
2920 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002921 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002922 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002923 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002924 was_on_lru = true;
2925 }
2926 }
2927
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002928 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002929 /*
2930 * We access a page_cgroup asynchronously without lock_page_cgroup().
2931 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2932 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2933 * before USED bit, we need memory barrier here.
2934 * See mem_cgroup_add_lru_list(), etc.
2935 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002936 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002937 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002938
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002939 if (lrucare) {
2940 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002941 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002942 VM_BUG_ON(PageLRU(page));
2943 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002944 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002945 }
2946 spin_unlock_irq(&zone->lru_lock);
2947 }
2948
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002949 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002950 anon = true;
2951 else
2952 anon = false;
2953
David Rientjesb070e652013-05-07 16:18:09 -07002954 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002955 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002956
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002957 /*
2958 * "charge_statistics" updated event counter. Then, check it.
2959 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2960 * if they exceeds softlimit.
2961 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002962 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002963}
2964
Glauber Costa7cf27982012-12-18 14:22:55 -08002965static DEFINE_MUTEX(set_limit_mutex);
2966
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002967#ifdef CONFIG_MEMCG_KMEM
2968static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2969{
2970 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2971 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2972}
2973
Glauber Costa1f458cb2012-12-18 14:22:50 -08002974/*
2975 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2976 * in the memcg_cache_params struct.
2977 */
2978static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2979{
2980 struct kmem_cache *cachep;
2981
2982 VM_BUG_ON(p->is_root_cache);
2983 cachep = p->root_cache;
2984 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2985}
2986
Glauber Costa749c5412012-12-18 14:23:01 -08002987#ifdef CONFIG_SLABINFO
2988static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2989 struct seq_file *m)
2990{
2991 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2992 struct memcg_cache_params *params;
2993
2994 if (!memcg_can_account_kmem(memcg))
2995 return -EIO;
2996
2997 print_slabinfo_header(m);
2998
2999 mutex_lock(&memcg->slab_caches_mutex);
3000 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
3001 cache_show(memcg_params_to_cache(params), m);
3002 mutex_unlock(&memcg->slab_caches_mutex);
3003
3004 return 0;
3005}
3006#endif
3007
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003008static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
3009{
3010 struct res_counter *fail_res;
3011 struct mem_cgroup *_memcg;
3012 int ret = 0;
3013 bool may_oom;
3014
3015 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
3016 if (ret)
3017 return ret;
3018
3019 /*
3020 * Conditions under which we can wait for the oom_killer. Those are
3021 * the same conditions tested by the core page allocator
3022 */
3023 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
3024
3025 _memcg = memcg;
3026 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
3027 &_memcg, may_oom);
3028
3029 if (ret == -EINTR) {
3030 /*
3031 * __mem_cgroup_try_charge() chosed to bypass to root due to
3032 * OOM kill or fatal signal. Since our only options are to
3033 * either fail the allocation or charge it to this cgroup, do
3034 * it as a temporary condition. But we can't fail. From a
3035 * kmem/slab perspective, the cache has already been selected,
3036 * by mem_cgroup_kmem_get_cache(), so it is too late to change
3037 * our minds.
3038 *
3039 * This condition will only trigger if the task entered
3040 * memcg_charge_kmem in a sane state, but was OOM-killed during
3041 * __mem_cgroup_try_charge() above. Tasks that were already
3042 * dying when the allocation triggers should have been already
3043 * directed to the root cgroup in memcontrol.h
3044 */
3045 res_counter_charge_nofail(&memcg->res, size, &fail_res);
3046 if (do_swap_account)
3047 res_counter_charge_nofail(&memcg->memsw, size,
3048 &fail_res);
3049 ret = 0;
3050 } else if (ret)
3051 res_counter_uncharge(&memcg->kmem, size);
3052
3053 return ret;
3054}
3055
3056static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
3057{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003058 res_counter_uncharge(&memcg->res, size);
3059 if (do_swap_account)
3060 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08003061
3062 /* Not down to 0 */
3063 if (res_counter_uncharge(&memcg->kmem, size))
3064 return;
3065
3066 if (memcg_kmem_test_and_clear_dead(memcg))
3067 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003068}
3069
Glauber Costa2633d7a2012-12-18 14:22:34 -08003070void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
3071{
3072 if (!memcg)
3073 return;
3074
3075 mutex_lock(&memcg->slab_caches_mutex);
3076 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
3077 mutex_unlock(&memcg->slab_caches_mutex);
3078}
3079
3080/*
3081 * helper for acessing a memcg's index. It will be used as an index in the
3082 * child cache array in kmem_cache, and also to derive its name. This function
3083 * will return -1 when this is not a kmem-limited memcg.
3084 */
3085int memcg_cache_id(struct mem_cgroup *memcg)
3086{
3087 return memcg ? memcg->kmemcg_id : -1;
3088}
3089
Glauber Costa55007d82012-12-18 14:22:38 -08003090/*
3091 * This ends up being protected by the set_limit mutex, during normal
3092 * operation, because that is its main call site.
3093 *
3094 * But when we create a new cache, we can call this as well if its parent
3095 * is kmem-limited. That will have to hold set_limit_mutex as well.
3096 */
3097int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3098{
3099 int num, ret;
3100
3101 num = ida_simple_get(&kmem_limited_groups,
3102 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3103 if (num < 0)
3104 return num;
3105 /*
3106 * After this point, kmem_accounted (that we test atomically in
3107 * the beginning of this conditional), is no longer 0. This
3108 * guarantees only one process will set the following boolean
3109 * to true. We don't need test_and_set because we're protected
3110 * by the set_limit_mutex anyway.
3111 */
3112 memcg_kmem_set_activated(memcg);
3113
3114 ret = memcg_update_all_caches(num+1);
3115 if (ret) {
3116 ida_simple_remove(&kmem_limited_groups, num);
3117 memcg_kmem_clear_activated(memcg);
3118 return ret;
3119 }
3120
3121 memcg->kmemcg_id = num;
3122 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3123 mutex_init(&memcg->slab_caches_mutex);
3124 return 0;
3125}
3126
3127static size_t memcg_caches_array_size(int num_groups)
3128{
3129 ssize_t size;
3130 if (num_groups <= 0)
3131 return 0;
3132
3133 size = 2 * num_groups;
3134 if (size < MEMCG_CACHES_MIN_SIZE)
3135 size = MEMCG_CACHES_MIN_SIZE;
3136 else if (size > MEMCG_CACHES_MAX_SIZE)
3137 size = MEMCG_CACHES_MAX_SIZE;
3138
3139 return size;
3140}
3141
3142/*
3143 * We should update the current array size iff all caches updates succeed. This
3144 * can only be done from the slab side. The slab mutex needs to be held when
3145 * calling this.
3146 */
3147void memcg_update_array_size(int num)
3148{
3149 if (num > memcg_limited_groups_array_size)
3150 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3151}
3152
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003153static void kmem_cache_destroy_work_func(struct work_struct *w);
3154
Glauber Costa55007d82012-12-18 14:22:38 -08003155int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3156{
3157 struct memcg_cache_params *cur_params = s->memcg_params;
3158
3159 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3160
3161 if (num_groups > memcg_limited_groups_array_size) {
3162 int i;
3163 ssize_t size = memcg_caches_array_size(num_groups);
3164
3165 size *= sizeof(void *);
3166 size += sizeof(struct memcg_cache_params);
3167
3168 s->memcg_params = kzalloc(size, GFP_KERNEL);
3169 if (!s->memcg_params) {
3170 s->memcg_params = cur_params;
3171 return -ENOMEM;
3172 }
3173
3174 s->memcg_params->is_root_cache = true;
3175
3176 /*
3177 * There is the chance it will be bigger than
3178 * memcg_limited_groups_array_size, if we failed an allocation
3179 * in a cache, in which case all caches updated before it, will
3180 * have a bigger array.
3181 *
3182 * But if that is the case, the data after
3183 * memcg_limited_groups_array_size is certainly unused
3184 */
3185 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3186 if (!cur_params->memcg_caches[i])
3187 continue;
3188 s->memcg_params->memcg_caches[i] =
3189 cur_params->memcg_caches[i];
3190 }
3191
3192 /*
3193 * Ideally, we would wait until all caches succeed, and only
3194 * then free the old one. But this is not worth the extra
3195 * pointer per-cache we'd have to have for this.
3196 *
3197 * It is not a big deal if some caches are left with a size
3198 * bigger than the others. And all updates will reset this
3199 * anyway.
3200 */
3201 kfree(cur_params);
3202 }
3203 return 0;
3204}
3205
Glauber Costa943a4512012-12-18 14:23:03 -08003206int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3207 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003208{
3209 size_t size = sizeof(struct memcg_cache_params);
3210
3211 if (!memcg_kmem_enabled())
3212 return 0;
3213
Glauber Costa55007d82012-12-18 14:22:38 -08003214 if (!memcg)
3215 size += memcg_limited_groups_array_size * sizeof(void *);
3216
Glauber Costa2633d7a2012-12-18 14:22:34 -08003217 s->memcg_params = kzalloc(size, GFP_KERNEL);
3218 if (!s->memcg_params)
3219 return -ENOMEM;
3220
Glauber Costa943a4512012-12-18 14:23:03 -08003221 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003222 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003223 s->memcg_params->root_cache = root_cache;
Andrey Vagin0dcf19b2013-08-13 16:00:47 -07003224 INIT_WORK(&s->memcg_params->destroy,
3225 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003226 } else
3227 s->memcg_params->is_root_cache = true;
3228
Glauber Costa2633d7a2012-12-18 14:22:34 -08003229 return 0;
3230}
3231
3232void memcg_release_cache(struct kmem_cache *s)
3233{
Glauber Costad7f25f82012-12-18 14:22:40 -08003234 struct kmem_cache *root;
3235 struct mem_cgroup *memcg;
3236 int id;
3237
3238 /*
3239 * This happens, for instance, when a root cache goes away before we
3240 * add any memcg.
3241 */
3242 if (!s->memcg_params)
3243 return;
3244
3245 if (s->memcg_params->is_root_cache)
3246 goto out;
3247
3248 memcg = s->memcg_params->memcg;
3249 id = memcg_cache_id(memcg);
3250
3251 root = s->memcg_params->root_cache;
3252 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003253
3254 mutex_lock(&memcg->slab_caches_mutex);
3255 list_del(&s->memcg_params->list);
3256 mutex_unlock(&memcg->slab_caches_mutex);
3257
Li Zefanfd0ccaf2013-04-29 15:08:43 -07003258 mem_cgroup_put(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08003259out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003260 kfree(s->memcg_params);
3261}
3262
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003263/*
3264 * During the creation a new cache, we need to disable our accounting mechanism
3265 * altogether. This is true even if we are not creating, but rather just
3266 * enqueing new caches to be created.
3267 *
3268 * This is because that process will trigger allocations; some visible, like
3269 * explicit kmallocs to auxiliary data structures, name strings and internal
3270 * cache structures; some well concealed, like INIT_WORK() that can allocate
3271 * objects during debug.
3272 *
3273 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3274 * to it. This may not be a bounded recursion: since the first cache creation
3275 * failed to complete (waiting on the allocation), we'll just try to create the
3276 * cache again, failing at the same point.
3277 *
3278 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3279 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3280 * inside the following two functions.
3281 */
3282static inline void memcg_stop_kmem_account(void)
3283{
3284 VM_BUG_ON(!current->mm);
3285 current->memcg_kmem_skip_account++;
3286}
3287
3288static inline void memcg_resume_kmem_account(void)
3289{
3290 VM_BUG_ON(!current->mm);
3291 current->memcg_kmem_skip_account--;
3292}
3293
Glauber Costa1f458cb2012-12-18 14:22:50 -08003294static void kmem_cache_destroy_work_func(struct work_struct *w)
3295{
3296 struct kmem_cache *cachep;
3297 struct memcg_cache_params *p;
3298
3299 p = container_of(w, struct memcg_cache_params, destroy);
3300
3301 cachep = memcg_params_to_cache(p);
3302
Glauber Costa22933152012-12-18 14:22:59 -08003303 /*
3304 * If we get down to 0 after shrink, we could delete right away.
3305 * However, memcg_release_pages() already puts us back in the workqueue
3306 * in that case. If we proceed deleting, we'll get a dangling
3307 * reference, and removing the object from the workqueue in that case
3308 * is unnecessary complication. We are not a fast path.
3309 *
3310 * Note that this case is fundamentally different from racing with
3311 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3312 * kmem_cache_shrink, not only we would be reinserting a dead cache
3313 * into the queue, but doing so from inside the worker racing to
3314 * destroy it.
3315 *
3316 * So if we aren't down to zero, we'll just schedule a worker and try
3317 * again
3318 */
3319 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3320 kmem_cache_shrink(cachep);
3321 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3322 return;
3323 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003324 kmem_cache_destroy(cachep);
3325}
3326
3327void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3328{
3329 if (!cachep->memcg_params->dead)
3330 return;
3331
3332 /*
Glauber Costa22933152012-12-18 14:22:59 -08003333 * There are many ways in which we can get here.
3334 *
3335 * We can get to a memory-pressure situation while the delayed work is
3336 * still pending to run. The vmscan shrinkers can then release all
3337 * cache memory and get us to destruction. If this is the case, we'll
3338 * be executed twice, which is a bug (the second time will execute over
3339 * bogus data). In this case, cancelling the work should be fine.
3340 *
3341 * But we can also get here from the worker itself, if
3342 * kmem_cache_shrink is enough to shake all the remaining objects and
3343 * get the page count to 0. In this case, we'll deadlock if we try to
3344 * cancel the work (the worker runs with an internal lock held, which
3345 * is the same lock we would hold for cancel_work_sync().)
3346 *
3347 * Since we can't possibly know who got us here, just refrain from
3348 * running if there is already work pending
3349 */
3350 if (work_pending(&cachep->memcg_params->destroy))
3351 return;
3352 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003353 * We have to defer the actual destroying to a workqueue, because
3354 * we might currently be in a context that cannot sleep.
3355 */
3356 schedule_work(&cachep->memcg_params->destroy);
3357}
3358
Glauber Costad7f25f82012-12-18 14:22:40 -08003359/*
3360 * This lock protects updaters, not readers. We want readers to be as fast as
3361 * they can, and they will either see NULL or a valid cache value. Our model
3362 * allow them to see NULL, in which case the root memcg will be selected.
3363 *
3364 * We need this lock because multiple allocations to the same cache from a non
3365 * will span more than one worker. Only one of them can create the cache.
3366 */
3367static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003368
3369/*
3370 * Called with memcg_cache_mutex held
3371 */
3372static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3373 struct kmem_cache *s)
3374{
3375 struct kmem_cache *new;
3376 static char *tmp_name = NULL;
3377
3378 lockdep_assert_held(&memcg_cache_mutex);
3379
3380 /*
3381 * kmem_cache_create_memcg duplicates the given name and
3382 * cgroup_name for this name requires RCU context.
3383 * This static temporary buffer is used to prevent from
3384 * pointless shortliving allocation.
3385 */
3386 if (!tmp_name) {
3387 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3388 if (!tmp_name)
3389 return NULL;
3390 }
3391
3392 rcu_read_lock();
3393 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3394 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3395 rcu_read_unlock();
3396
3397 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3398 (s->flags & ~SLAB_PANIC), s->ctor, s);
3399
3400 if (new)
3401 new->allocflags |= __GFP_KMEMCG;
3402
3403 return new;
3404}
3405
Glauber Costad7f25f82012-12-18 14:22:40 -08003406static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3407 struct kmem_cache *cachep)
3408{
3409 struct kmem_cache *new_cachep;
3410 int idx;
3411
3412 BUG_ON(!memcg_can_account_kmem(memcg));
3413
3414 idx = memcg_cache_id(memcg);
3415
3416 mutex_lock(&memcg_cache_mutex);
3417 new_cachep = cachep->memcg_params->memcg_caches[idx];
3418 if (new_cachep)
3419 goto out;
3420
3421 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003422 if (new_cachep == NULL) {
3423 new_cachep = cachep;
3424 goto out;
3425 }
3426
3427 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003428 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003429
3430 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3431 /*
3432 * the readers won't lock, make sure everybody sees the updated value,
3433 * so they won't put stuff in the queue again for no reason
3434 */
3435 wmb();
3436out:
3437 mutex_unlock(&memcg_cache_mutex);
3438 return new_cachep;
3439}
3440
Glauber Costa7cf27982012-12-18 14:22:55 -08003441void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3442{
3443 struct kmem_cache *c;
3444 int i;
3445
3446 if (!s->memcg_params)
3447 return;
3448 if (!s->memcg_params->is_root_cache)
3449 return;
3450
3451 /*
3452 * If the cache is being destroyed, we trust that there is no one else
3453 * requesting objects from it. Even if there are, the sanity checks in
3454 * kmem_cache_destroy should caught this ill-case.
3455 *
3456 * Still, we don't want anyone else freeing memcg_caches under our
3457 * noses, which can happen if a new memcg comes to life. As usual,
3458 * we'll take the set_limit_mutex to protect ourselves against this.
3459 */
3460 mutex_lock(&set_limit_mutex);
3461 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3462 c = s->memcg_params->memcg_caches[i];
3463 if (!c)
3464 continue;
3465
3466 /*
3467 * We will now manually delete the caches, so to avoid races
3468 * we need to cancel all pending destruction workers and
3469 * proceed with destruction ourselves.
3470 *
3471 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3472 * and that could spawn the workers again: it is likely that
3473 * the cache still have active pages until this very moment.
3474 * This would lead us back to mem_cgroup_destroy_cache.
3475 *
3476 * But that will not execute at all if the "dead" flag is not
3477 * set, so flip it down to guarantee we are in control.
3478 */
3479 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003480 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003481 kmem_cache_destroy(c);
3482 }
3483 mutex_unlock(&set_limit_mutex);
3484}
3485
Glauber Costad7f25f82012-12-18 14:22:40 -08003486struct create_work {
3487 struct mem_cgroup *memcg;
3488 struct kmem_cache *cachep;
3489 struct work_struct work;
3490};
3491
Glauber Costa1f458cb2012-12-18 14:22:50 -08003492static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3493{
3494 struct kmem_cache *cachep;
3495 struct memcg_cache_params *params;
3496
3497 if (!memcg_kmem_is_active(memcg))
3498 return;
3499
3500 mutex_lock(&memcg->slab_caches_mutex);
3501 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3502 cachep = memcg_params_to_cache(params);
3503 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003504 schedule_work(&cachep->memcg_params->destroy);
3505 }
3506 mutex_unlock(&memcg->slab_caches_mutex);
3507}
3508
Glauber Costad7f25f82012-12-18 14:22:40 -08003509static void memcg_create_cache_work_func(struct work_struct *w)
3510{
3511 struct create_work *cw;
3512
3513 cw = container_of(w, struct create_work, work);
3514 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3515 /* Drop the reference gotten when we enqueued. */
3516 css_put(&cw->memcg->css);
3517 kfree(cw);
3518}
3519
3520/*
3521 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003522 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003523static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3524 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003525{
3526 struct create_work *cw;
3527
3528 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003529 if (cw == NULL) {
3530 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003531 return;
3532 }
3533
3534 cw->memcg = memcg;
3535 cw->cachep = cachep;
3536
3537 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3538 schedule_work(&cw->work);
3539}
3540
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003541static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3542 struct kmem_cache *cachep)
3543{
3544 /*
3545 * We need to stop accounting when we kmalloc, because if the
3546 * corresponding kmalloc cache is not yet created, the first allocation
3547 * in __memcg_create_cache_enqueue will recurse.
3548 *
3549 * However, it is better to enclose the whole function. Depending on
3550 * the debugging options enabled, INIT_WORK(), for instance, can
3551 * trigger an allocation. This too, will make us recurse. Because at
3552 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3553 * the safest choice is to do it like this, wrapping the whole function.
3554 */
3555 memcg_stop_kmem_account();
3556 __memcg_create_cache_enqueue(memcg, cachep);
3557 memcg_resume_kmem_account();
3558}
Glauber Costad7f25f82012-12-18 14:22:40 -08003559/*
3560 * Return the kmem_cache we're supposed to use for a slab allocation.
3561 * We try to use the current memcg's version of the cache.
3562 *
3563 * If the cache does not exist yet, if we are the first user of it,
3564 * we either create it immediately, if possible, or create it asynchronously
3565 * in a workqueue.
3566 * In the latter case, we will let the current allocation go through with
3567 * the original cache.
3568 *
3569 * Can't be called in interrupt context or from kernel threads.
3570 * This function needs to be called with rcu_read_lock() held.
3571 */
3572struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3573 gfp_t gfp)
3574{
3575 struct mem_cgroup *memcg;
3576 int idx;
3577
3578 VM_BUG_ON(!cachep->memcg_params);
3579 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3580
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003581 if (!current->mm || current->memcg_kmem_skip_account)
3582 return cachep;
3583
Glauber Costad7f25f82012-12-18 14:22:40 -08003584 rcu_read_lock();
3585 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003586
3587 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003588 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003589
3590 idx = memcg_cache_id(memcg);
3591
3592 /*
3593 * barrier to mare sure we're always seeing the up to date value. The
3594 * code updating memcg_caches will issue a write barrier to match this.
3595 */
3596 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003597 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3598 cachep = cachep->memcg_params->memcg_caches[idx];
3599 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003600 }
3601
Li Zefanca0dde92013-04-29 15:08:57 -07003602 /* The corresponding put will be done in the workqueue. */
3603 if (!css_tryget(&memcg->css))
3604 goto out;
3605 rcu_read_unlock();
3606
3607 /*
3608 * If we are in a safe context (can wait, and not in interrupt
3609 * context), we could be be predictable and return right away.
3610 * This would guarantee that the allocation being performed
3611 * already belongs in the new cache.
3612 *
3613 * However, there are some clashes that can arrive from locking.
3614 * For instance, because we acquire the slab_mutex while doing
3615 * kmem_cache_dup, this means no further allocation could happen
3616 * with the slab_mutex held.
3617 *
3618 * Also, because cache creation issue get_online_cpus(), this
3619 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3620 * that ends up reversed during cpu hotplug. (cpuset allocates
3621 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3622 * better to defer everything.
3623 */
3624 memcg_create_cache_enqueue(memcg, cachep);
3625 return cachep;
3626out:
3627 rcu_read_unlock();
3628 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003629}
3630EXPORT_SYMBOL(__memcg_kmem_get_cache);
3631
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003632/*
3633 * We need to verify if the allocation against current->mm->owner's memcg is
3634 * possible for the given order. But the page is not allocated yet, so we'll
3635 * need a further commit step to do the final arrangements.
3636 *
3637 * It is possible for the task to switch cgroups in this mean time, so at
3638 * commit time, we can't rely on task conversion any longer. We'll then use
3639 * the handle argument to return to the caller which cgroup we should commit
3640 * against. We could also return the memcg directly and avoid the pointer
3641 * passing, but a boolean return value gives better semantics considering
3642 * the compiled-out case as well.
3643 *
3644 * Returning true means the allocation is possible.
3645 */
3646bool
3647__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3648{
3649 struct mem_cgroup *memcg;
3650 int ret;
3651
3652 *_memcg = NULL;
3653 memcg = try_get_mem_cgroup_from_mm(current->mm);
3654
3655 /*
3656 * very rare case described in mem_cgroup_from_task. Unfortunately there
3657 * isn't much we can do without complicating this too much, and it would
3658 * be gfp-dependent anyway. Just let it go
3659 */
3660 if (unlikely(!memcg))
3661 return true;
3662
3663 if (!memcg_can_account_kmem(memcg)) {
3664 css_put(&memcg->css);
3665 return true;
3666 }
3667
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003668 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3669 if (!ret)
3670 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003671
3672 css_put(&memcg->css);
3673 return (ret == 0);
3674}
3675
3676void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3677 int order)
3678{
3679 struct page_cgroup *pc;
3680
3681 VM_BUG_ON(mem_cgroup_is_root(memcg));
3682
3683 /* The page allocation failed. Revert */
3684 if (!page) {
3685 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003686 return;
3687 }
3688
3689 pc = lookup_page_cgroup(page);
3690 lock_page_cgroup(pc);
3691 pc->mem_cgroup = memcg;
3692 SetPageCgroupUsed(pc);
3693 unlock_page_cgroup(pc);
3694}
3695
3696void __memcg_kmem_uncharge_pages(struct page *page, int order)
3697{
3698 struct mem_cgroup *memcg = NULL;
3699 struct page_cgroup *pc;
3700
3701
3702 pc = lookup_page_cgroup(page);
3703 /*
3704 * Fast unlocked return. Theoretically might have changed, have to
3705 * check again after locking.
3706 */
3707 if (!PageCgroupUsed(pc))
3708 return;
3709
3710 lock_page_cgroup(pc);
3711 if (PageCgroupUsed(pc)) {
3712 memcg = pc->mem_cgroup;
3713 ClearPageCgroupUsed(pc);
3714 }
3715 unlock_page_cgroup(pc);
3716
3717 /*
3718 * We trust that only if there is a memcg associated with the page, it
3719 * is a valid allocation
3720 */
3721 if (!memcg)
3722 return;
3723
3724 VM_BUG_ON(mem_cgroup_is_root(memcg));
3725 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003726}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003727#else
3728static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3729{
3730}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003731#endif /* CONFIG_MEMCG_KMEM */
3732
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003733#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3734
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003735#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003736/*
3737 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003738 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3739 * charge/uncharge will be never happen and move_account() is done under
3740 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003741 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003742void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003743{
3744 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003745 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003746 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003747 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003748
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003749 if (mem_cgroup_disabled())
3750 return;
David Rientjesb070e652013-05-07 16:18:09 -07003751
3752 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003753 for (i = 1; i < HPAGE_PMD_NR; i++) {
3754 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003755 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003756 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003757 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3758 }
David Rientjesb070e652013-05-07 16:18:09 -07003759 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3760 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003761}
Hugh Dickins12d27102012-01-12 17:19:52 -08003762#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003763
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003764/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003765 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003766 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003767 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003768 * @pc: page_cgroup of the page.
3769 * @from: mem_cgroup which the page is moved from.
3770 * @to: mem_cgroup which the page is moved to. @from != @to.
3771 *
3772 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003773 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003774 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003775 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003776 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3777 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003778 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003779static int mem_cgroup_move_account(struct page *page,
3780 unsigned int nr_pages,
3781 struct page_cgroup *pc,
3782 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003783 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003784{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003785 unsigned long flags;
3786 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003787 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003788
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003789 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003790 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003791 /*
3792 * The page is isolated from LRU. So, collapse function
3793 * will not handle this page. But page splitting can happen.
3794 * Do this check under compound_page_lock(). The caller should
3795 * hold it.
3796 */
3797 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003798 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003799 goto out;
3800
3801 lock_page_cgroup(pc);
3802
3803 ret = -EINVAL;
3804 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3805 goto unlock;
3806
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003807 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003808
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003809 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003810 /* Update mapped_file data for mem_cgroup */
3811 preempt_disable();
3812 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3813 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3814 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003815 }
David Rientjesb070e652013-05-07 16:18:09 -07003816 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003817
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003818 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003819 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003820 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003821 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003822 ret = 0;
3823unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003824 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003825 /*
3826 * check events
3827 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003828 memcg_check_events(to, page);
3829 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003830out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831 return ret;
3832}
3833
Michal Hocko2ef37d32012-10-26 13:37:30 +02003834/**
3835 * mem_cgroup_move_parent - moves page to the parent group
3836 * @page: the page to move
3837 * @pc: page_cgroup of the page
3838 * @child: page's cgroup
3839 *
3840 * move charges to its parent or the root cgroup if the group has no
3841 * parent (aka use_hierarchy==0).
3842 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3843 * mem_cgroup_move_account fails) the failure is always temporary and
3844 * it signals a race with a page removal/uncharge or migration. In the
3845 * first case the page is on the way out and it will vanish from the LRU
3846 * on the next attempt and the call should be retried later.
3847 * Isolation from the LRU fails only if page has been isolated from
3848 * the LRU since we looked at it and that usually means either global
3849 * reclaim or migration going on. The page will either get back to the
3850 * LRU or vanish.
3851 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3852 * (!PageCgroupUsed) or moved to a different group. The page will
3853 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003854 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003855static int mem_cgroup_move_parent(struct page *page,
3856 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003857 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003858{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003859 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003860 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003861 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003862 int ret;
3863
Michal Hockod8423012012-10-26 13:37:29 +02003864 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003865
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003866 ret = -EBUSY;
3867 if (!get_page_unless_zero(page))
3868 goto out;
3869 if (isolate_lru_page(page))
3870 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003871
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003872 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003873
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003874 parent = parent_mem_cgroup(child);
3875 /*
3876 * If no parent, move charges to root cgroup.
3877 */
3878 if (!parent)
3879 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003880
Michal Hocko2ef37d32012-10-26 13:37:30 +02003881 if (nr_pages > 1) {
3882 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003883 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003884 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003885
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003886 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003887 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003888 if (!ret)
3889 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003890
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003891 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003892 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003893 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003894put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003895 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003896out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003897 return ret;
3898}
3899
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003900/*
3901 * Charge the memory controller for page usage.
3902 * Return
3903 * 0 if the charge was successful
3904 * < 0 if the cgroup is over its limit
3905 */
3906static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003907 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003908{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003909 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003910 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003911 bool oom = true;
3912 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003913
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003914 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003915 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003916 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003917 /*
3918 * Never OOM-kill a process for a huge page. The
3919 * fault handler will fall back to regular pages.
3920 */
3921 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003922 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003923
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003924 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003925 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003926 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003927 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003928 return 0;
3929}
3930
3931int mem_cgroup_newpage_charge(struct page *page,
3932 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003933{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003934 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003935 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003936 VM_BUG_ON(page_mapped(page));
3937 VM_BUG_ON(page->mapping && !PageAnon(page));
3938 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003939 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003940 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003941}
3942
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003943/*
3944 * While swap-in, try_charge -> commit or cancel, the page is locked.
3945 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003946 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003947 * "commit()" or removed by "cancel()"
3948 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003949static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3950 struct page *page,
3951 gfp_t mask,
3952 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003953{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003954 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003955 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003956 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003957
Johannes Weiner90deb782012-07-31 16:45:47 -07003958 pc = lookup_page_cgroup(page);
3959 /*
3960 * Every swap fault against a single page tries to charge the
3961 * page, bail as early as possible. shmem_unuse() encounters
3962 * already charged pages, too. The USED bit is protected by
3963 * the page lock, which serializes swap cache removal, which
3964 * in turn serializes uncharging.
3965 */
3966 if (PageCgroupUsed(pc))
3967 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003968 if (!do_swap_account)
3969 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003970 memcg = try_get_mem_cgroup_from_page(page);
3971 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003972 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003973 *memcgp = memcg;
3974 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003975 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003976 if (ret == -EINTR)
3977 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003978 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003979charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003980 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3981 if (ret == -EINTR)
3982 ret = 0;
3983 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003984}
3985
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003986int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3987 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3988{
3989 *memcgp = NULL;
3990 if (mem_cgroup_disabled())
3991 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003992 /*
3993 * A racing thread's fault, or swapoff, may have already
3994 * updated the pte, and even removed page from swap cache: in
3995 * those cases unuse_pte()'s pte_same() test will fail; but
3996 * there's also a KSM case which does need to charge the page.
3997 */
3998 if (!PageSwapCache(page)) {
3999 int ret;
4000
4001 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
4002 if (ret == -EINTR)
4003 ret = 0;
4004 return ret;
4005 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004006 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
4007}
4008
Johannes Weiner827a03d2012-07-31 16:45:36 -07004009void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
4010{
4011 if (mem_cgroup_disabled())
4012 return;
4013 if (!memcg)
4014 return;
4015 __mem_cgroup_cancel_charge(memcg, 1);
4016}
4017
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004018static void
Johannes Weiner72835c82012-01-12 17:18:32 -08004019__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004020 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004021{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004022 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004023 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08004024 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004025 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07004026
Johannes Weinerce587e62012-04-24 20:22:33 +02004027 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004028 /*
4029 * Now swap is on-memory. This means this page may be
4030 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004031 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
4032 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
4033 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004034 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004035 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004036 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07004037 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004038 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004039}
4040
Johannes Weiner72835c82012-01-12 17:18:32 -08004041void mem_cgroup_commit_charge_swapin(struct page *page,
4042 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004043{
Johannes Weiner72835c82012-01-12 17:18:32 -08004044 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004045 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004046}
4047
Johannes Weiner827a03d2012-07-31 16:45:36 -07004048int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
4049 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004050{
Johannes Weiner827a03d2012-07-31 16:45:36 -07004051 struct mem_cgroup *memcg = NULL;
4052 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4053 int ret;
4054
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004055 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07004056 return 0;
4057 if (PageCompound(page))
4058 return 0;
4059
Johannes Weiner827a03d2012-07-31 16:45:36 -07004060 if (!PageSwapCache(page))
4061 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
4062 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004063 ret = __mem_cgroup_try_charge_swapin(mm, page,
4064 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07004065 if (!ret)
4066 __mem_cgroup_commit_charge_swapin(page, memcg, type);
4067 }
4068 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004069}
4070
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004071static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004072 unsigned int nr_pages,
4073 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004074{
4075 struct memcg_batch_info *batch = NULL;
4076 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004077
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004078 /* If swapout, usage of swap doesn't decrease */
4079 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4080 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004081
4082 batch = &current->memcg_batch;
4083 /*
4084 * In usual, we do css_get() when we remember memcg pointer.
4085 * But in this case, we keep res->usage until end of a series of
4086 * uncharges. Then, it's ok to ignore memcg's refcnt.
4087 */
4088 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004089 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004090 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004091 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004092 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004093 * the same cgroup and we have chance to coalesce uncharges.
4094 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4095 * because we want to do uncharge as soon as possible.
4096 */
4097
4098 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4099 goto direct_uncharge;
4100
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004101 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004102 goto direct_uncharge;
4103
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004104 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004105 * In typical case, batch->memcg == mem. This means we can
4106 * merge a series of uncharges to an uncharge of res_counter.
4107 * If not, we uncharge res_counter ony by one.
4108 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004109 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004110 goto direct_uncharge;
4111 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004112 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004113 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004114 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004115 return;
4116direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004117 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004118 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004119 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4120 if (unlikely(batch->memcg != memcg))
4121 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004122}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004123
Balbir Singh8697d332008-02-07 00:13:59 -08004124/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004125 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004126 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004127static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004128__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4129 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004130{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004131 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004132 unsigned int nr_pages = 1;
4133 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004134 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004135
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004136 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004137 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004138
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004139 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004140 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004141 VM_BUG_ON(!PageTransHuge(page));
4142 }
Balbir Singh8697d332008-02-07 00:13:59 -08004143 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004144 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004145 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004146 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004147 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004148 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004149
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004150 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004151
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004152 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004153
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004154 if (!PageCgroupUsed(pc))
4155 goto unlock_out;
4156
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004157 anon = PageAnon(page);
4158
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004159 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004160 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004161 /*
4162 * Generally PageAnon tells if it's the anon statistics to be
4163 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4164 * used before page reached the stage of being marked PageAnon.
4165 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004166 anon = true;
4167 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004168 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004169 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004170 if (page_mapped(page))
4171 goto unlock_out;
4172 /*
4173 * Pages under migration may not be uncharged. But
4174 * end_migration() /must/ be the one uncharging the
4175 * unused post-migration page and so it has to call
4176 * here with the migration bit still set. See the
4177 * res_counter handling below.
4178 */
4179 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004180 goto unlock_out;
4181 break;
4182 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4183 if (!PageAnon(page)) { /* Shared memory */
4184 if (page->mapping && !page_is_file_cache(page))
4185 goto unlock_out;
4186 } else if (page_mapped(page)) /* Anon */
4187 goto unlock_out;
4188 break;
4189 default:
4190 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004191 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004192
David Rientjesb070e652013-05-07 16:18:09 -07004193 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004194
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004195 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004196 /*
4197 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4198 * freed from LRU. This is safe because uncharged page is expected not
4199 * to be reused (freed soon). Exception is SwapCache, it's handled by
4200 * special functions.
4201 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004202
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004203 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004204 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004205 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004206 * will never be freed.
4207 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004208 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004209 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004210 mem_cgroup_swap_statistics(memcg, true);
4211 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004212 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004213 /*
4214 * Migration does not charge the res_counter for the
4215 * replacement page, so leave it alone when phasing out the
4216 * page that is unused after the migration.
4217 */
4218 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004219 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004220
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004221 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004222
4223unlock_out:
4224 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004225 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004226}
4227
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004228void mem_cgroup_uncharge_page(struct page *page)
4229{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004230 /* early check. */
4231 if (page_mapped(page))
4232 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004233 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004234 /*
4235 * If the page is in swap cache, uncharge should be deferred
4236 * to the swap path, which also properly accounts swap usage
4237 * and handles memcg lifetime.
4238 *
4239 * Note that this check is not stable and reclaim may add the
4240 * page to swap cache at any time after this. However, if the
4241 * page is not in swap cache by the time page->mapcount hits
4242 * 0, there won't be any page table references to the swap
4243 * slot, and reclaim will free it and not actually write the
4244 * page to disk.
4245 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004246 if (PageSwapCache(page))
4247 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004248 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004249}
4250
4251void mem_cgroup_uncharge_cache_page(struct page *page)
4252{
4253 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004254 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004255 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004256}
4257
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004258/*
4259 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4260 * In that cases, pages are freed continuously and we can expect pages
4261 * are in the same memcg. All these calls itself limits the number of
4262 * pages freed at once, then uncharge_start/end() is called properly.
4263 * This may be called prural(2) times in a context,
4264 */
4265
4266void mem_cgroup_uncharge_start(void)
4267{
4268 current->memcg_batch.do_batch++;
4269 /* We can do nest. */
4270 if (current->memcg_batch.do_batch == 1) {
4271 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004272 current->memcg_batch.nr_pages = 0;
4273 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004274 }
4275}
4276
4277void mem_cgroup_uncharge_end(void)
4278{
4279 struct memcg_batch_info *batch = &current->memcg_batch;
4280
4281 if (!batch->do_batch)
4282 return;
4283
4284 batch->do_batch--;
4285 if (batch->do_batch) /* If stacked, do nothing. */
4286 return;
4287
4288 if (!batch->memcg)
4289 return;
4290 /*
4291 * This "batch->memcg" is valid without any css_get/put etc...
4292 * bacause we hide charges behind us.
4293 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004294 if (batch->nr_pages)
4295 res_counter_uncharge(&batch->memcg->res,
4296 batch->nr_pages * PAGE_SIZE);
4297 if (batch->memsw_nr_pages)
4298 res_counter_uncharge(&batch->memcg->memsw,
4299 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004300 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004301 /* forget this pointer (for sanity check) */
4302 batch->memcg = NULL;
4303}
4304
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004305#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004306/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004307 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004308 * memcg information is recorded to swap_cgroup of "ent"
4309 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004310void
4311mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004312{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004313 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004314 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004315
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004316 if (!swapout) /* this was a swap cache but the swap is unused ! */
4317 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4318
Johannes Weiner0030f532012-07-31 16:45:25 -07004319 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004320
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004321 /*
4322 * record memcg information, if swapout && memcg != NULL,
4323 * mem_cgroup_get() was called in uncharge().
4324 */
4325 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004326 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004327}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004328#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004329
Andrew Mortonc255a452012-07-31 16:43:02 -07004330#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004331/*
4332 * called from swap_entry_free(). remove record in swap_cgroup and
4333 * uncharge "memsw" account.
4334 */
4335void mem_cgroup_uncharge_swap(swp_entry_t ent)
4336{
4337 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004338 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004339
4340 if (!do_swap_account)
4341 return;
4342
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004343 id = swap_cgroup_record(ent, 0);
4344 rcu_read_lock();
4345 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004346 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004347 /*
4348 * We uncharge this because swap is freed.
4349 * This memcg can be obsolete one. We avoid calling css_tryget
4350 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004351 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004352 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004353 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004354 mem_cgroup_put(memcg);
4355 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004356 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004357}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004358
4359/**
4360 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4361 * @entry: swap entry to be moved
4362 * @from: mem_cgroup which the entry is moved from
4363 * @to: mem_cgroup which the entry is moved to
4364 *
4365 * It succeeds only when the swap_cgroup's record for this entry is the same
4366 * as the mem_cgroup's id of @from.
4367 *
4368 * Returns 0 on success, -EINVAL on failure.
4369 *
4370 * The caller must have charged to @to, IOW, called res_counter_charge() about
4371 * both res and memsw, and called css_get().
4372 */
4373static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004374 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004375{
4376 unsigned short old_id, new_id;
4377
4378 old_id = css_id(&from->css);
4379 new_id = css_id(&to->css);
4380
4381 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004382 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004383 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004384 /*
4385 * This function is only called from task migration context now.
4386 * It postpones res_counter and refcount handling till the end
4387 * of task migration(mem_cgroup_clear_mc()) for performance
4388 * improvement. But we cannot postpone mem_cgroup_get(to)
4389 * because if the process that has been moved to @to does
4390 * swap-in, the refcount of @to might be decreased to 0.
4391 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004392 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004393 return 0;
4394 }
4395 return -EINVAL;
4396}
4397#else
4398static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004399 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004400{
4401 return -EINVAL;
4402}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004403#endif
4404
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004405/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004406 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4407 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004408 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004409void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4410 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004411{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004412 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004413 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004414 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004415 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004416
Johannes Weiner72835c82012-01-12 17:18:32 -08004417 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004418
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004419 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004420 return;
Balbir Singh40779602008-04-04 14:29:59 -07004421
Mel Gormanb32967f2012-11-19 12:35:47 +00004422 if (PageTransHuge(page))
4423 nr_pages <<= compound_order(page);
4424
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004425 pc = lookup_page_cgroup(page);
4426 lock_page_cgroup(pc);
4427 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004428 memcg = pc->mem_cgroup;
4429 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004430 /*
4431 * At migrating an anonymous page, its mapcount goes down
4432 * to 0 and uncharge() will be called. But, even if it's fully
4433 * unmapped, migration may fail and this page has to be
4434 * charged again. We set MIGRATION flag here and delay uncharge
4435 * until end_migration() is called
4436 *
4437 * Corner Case Thinking
4438 * A)
4439 * When the old page was mapped as Anon and it's unmap-and-freed
4440 * while migration was ongoing.
4441 * If unmap finds the old page, uncharge() of it will be delayed
4442 * until end_migration(). If unmap finds a new page, it's
4443 * uncharged when it make mapcount to be 1->0. If unmap code
4444 * finds swap_migration_entry, the new page will not be mapped
4445 * and end_migration() will find it(mapcount==0).
4446 *
4447 * B)
4448 * When the old page was mapped but migraion fails, the kernel
4449 * remaps it. A charge for it is kept by MIGRATION flag even
4450 * if mapcount goes down to 0. We can do remap successfully
4451 * without charging it again.
4452 *
4453 * C)
4454 * The "old" page is under lock_page() until the end of
4455 * migration, so, the old page itself will not be swapped-out.
4456 * If the new page is swapped out before end_migraton, our
4457 * hook to usual swap-out path will catch the event.
4458 */
4459 if (PageAnon(page))
4460 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004461 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004462 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004463 /*
4464 * If the page is not charged at this point,
4465 * we return here.
4466 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004467 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004468 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004469
Johannes Weiner72835c82012-01-12 17:18:32 -08004470 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004471 /*
4472 * We charge new page before it's used/mapped. So, even if unlock_page()
4473 * is called before end_migration, we can catch all events on this new
4474 * page. In the case new page is migrated but not remapped, new page's
4475 * mapcount will be finally 0 and we call uncharge in end_migration().
4476 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004477 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004478 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004479 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004480 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004481 /*
4482 * The page is committed to the memcg, but it's not actually
4483 * charged to the res_counter since we plan on replacing the
4484 * old one and only one page is going to be left afterwards.
4485 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004486 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004487}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004488
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004489/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004490void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004491 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004492{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004493 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004494 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004495 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004496
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004497 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004498 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004499
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004500 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004501 used = oldpage;
4502 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004503 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004504 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004505 unused = oldpage;
4506 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004507 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004508 __mem_cgroup_uncharge_common(unused,
4509 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4510 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4511 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004512 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004513 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004514 * We disallowed uncharge of pages under migration because mapcount
4515 * of the page goes down to zero, temporarly.
4516 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004517 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004518 pc = lookup_page_cgroup(oldpage);
4519 lock_page_cgroup(pc);
4520 ClearPageCgroupMigration(pc);
4521 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004522
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004523 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004524 * If a page is a file cache, radix-tree replacement is very atomic
4525 * and we can skip this check. When it was an Anon page, its mapcount
4526 * goes down to 0. But because we added MIGRATION flage, it's not
4527 * uncharged yet. There are several case but page->mapcount check
4528 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4529 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004530 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004531 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004532 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004533}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004534
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004535/*
4536 * At replace page cache, newpage is not under any memcg but it's on
4537 * LRU. So, this function doesn't touch res_counter but handles LRU
4538 * in correct way. Both pages are locked so we cannot race with uncharge.
4539 */
4540void mem_cgroup_replace_page_cache(struct page *oldpage,
4541 struct page *newpage)
4542{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004543 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004544 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004545 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004546
4547 if (mem_cgroup_disabled())
4548 return;
4549
4550 pc = lookup_page_cgroup(oldpage);
4551 /* fix accounting on old pages */
4552 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004553 if (PageCgroupUsed(pc)) {
4554 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004555 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004556 ClearPageCgroupUsed(pc);
4557 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004558 unlock_page_cgroup(pc);
4559
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004560 /*
4561 * When called from shmem_replace_page(), in some cases the
4562 * oldpage has already been charged, and in some cases not.
4563 */
4564 if (!memcg)
4565 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004566 /*
4567 * Even if newpage->mapping was NULL before starting replacement,
4568 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4569 * LRU while we overwrite pc->mem_cgroup.
4570 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004571 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004572}
4573
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004574#ifdef CONFIG_DEBUG_VM
4575static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4576{
4577 struct page_cgroup *pc;
4578
4579 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004580 /*
4581 * Can be NULL while feeding pages into the page allocator for
4582 * the first time, i.e. during boot or memory hotplug;
4583 * or when mem_cgroup_disabled().
4584 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004585 if (likely(pc) && PageCgroupUsed(pc))
4586 return pc;
4587 return NULL;
4588}
4589
4590bool mem_cgroup_bad_page_check(struct page *page)
4591{
4592 if (mem_cgroup_disabled())
4593 return false;
4594
4595 return lookup_page_cgroup_used(page) != NULL;
4596}
4597
4598void mem_cgroup_print_bad_page(struct page *page)
4599{
4600 struct page_cgroup *pc;
4601
4602 pc = lookup_page_cgroup_used(page);
4603 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004604 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4605 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004606 }
4607}
4608#endif
4609
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004610static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004611 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004612{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004613 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004614 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004615 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004616 int children = mem_cgroup_count_children(memcg);
4617 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004618 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004619
4620 /*
4621 * For keeping hierarchical_reclaim simple, how long we should retry
4622 * is depends on callers. We set our retry-count to be function
4623 * of # of children which we should visit in this loop.
4624 */
4625 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4626
4627 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004628
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004629 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004630 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004631 if (signal_pending(current)) {
4632 ret = -EINTR;
4633 break;
4634 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004635 /*
4636 * Rather than hide all in some function, I do this in
4637 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004638 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004639 */
4640 mutex_lock(&set_limit_mutex);
4641 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4642 if (memswlimit < val) {
4643 ret = -EINVAL;
4644 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004645 break;
4646 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004647
4648 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4649 if (memlimit < val)
4650 enlarge = 1;
4651
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004652 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004653 if (!ret) {
4654 if (memswlimit == val)
4655 memcg->memsw_is_minimum = true;
4656 else
4657 memcg->memsw_is_minimum = false;
4658 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004659 mutex_unlock(&set_limit_mutex);
4660
4661 if (!ret)
4662 break;
4663
Johannes Weiner56600482012-01-12 17:17:59 -08004664 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4665 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004666 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4667 /* Usage is reduced ? */
4668 if (curusage >= oldusage)
4669 retry_count--;
4670 else
4671 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004672 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004673 if (!ret && enlarge)
4674 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004675
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004676 return ret;
4677}
4678
Li Zefan338c8432009-06-17 16:27:15 -07004679static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4680 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004681{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004682 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004683 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004684 int children = mem_cgroup_count_children(memcg);
4685 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004686 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004687
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004688 /* see mem_cgroup_resize_res_limit */
4689 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4690 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004691 while (retry_count) {
4692 if (signal_pending(current)) {
4693 ret = -EINTR;
4694 break;
4695 }
4696 /*
4697 * Rather than hide all in some function, I do this in
4698 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004699 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004700 */
4701 mutex_lock(&set_limit_mutex);
4702 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4703 if (memlimit > val) {
4704 ret = -EINVAL;
4705 mutex_unlock(&set_limit_mutex);
4706 break;
4707 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004708 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4709 if (memswlimit < val)
4710 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004711 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004712 if (!ret) {
4713 if (memlimit == val)
4714 memcg->memsw_is_minimum = true;
4715 else
4716 memcg->memsw_is_minimum = false;
4717 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004718 mutex_unlock(&set_limit_mutex);
4719
4720 if (!ret)
4721 break;
4722
Johannes Weiner56600482012-01-12 17:17:59 -08004723 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4724 MEM_CGROUP_RECLAIM_NOSWAP |
4725 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004726 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004727 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004728 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004729 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004730 else
4731 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004732 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004733 if (!ret && enlarge)
4734 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004735 return ret;
4736}
4737
Balbir Singh4e416952009-09-23 15:56:39 -07004738unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004739 gfp_t gfp_mask,
4740 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004741{
4742 unsigned long nr_reclaimed = 0;
4743 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4744 unsigned long reclaimed;
4745 int loop = 0;
4746 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004747 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004748 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004749
4750 if (order > 0)
4751 return 0;
4752
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004753 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004754 /*
4755 * This loop can run a while, specially if mem_cgroup's continuously
4756 * keep exceeding their soft limit and putting the system under
4757 * pressure
4758 */
4759 do {
4760 if (next_mz)
4761 mz = next_mz;
4762 else
4763 mz = mem_cgroup_largest_soft_limit_node(mctz);
4764 if (!mz)
4765 break;
4766
Ying Han0ae5e892011-05-26 16:25:25 -07004767 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004768 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004769 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004770 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004771 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004772 spin_lock(&mctz->lock);
4773
4774 /*
4775 * If we failed to reclaim anything from this memory cgroup
4776 * it is time to move on to the next cgroup
4777 */
4778 next_mz = NULL;
4779 if (!reclaimed) {
4780 do {
4781 /*
4782 * Loop until we find yet another one.
4783 *
4784 * By the time we get the soft_limit lock
4785 * again, someone might have aded the
4786 * group back on the RB tree. Iterate to
4787 * make sure we get a different mem.
4788 * mem_cgroup_largest_soft_limit_node returns
4789 * NULL if no other cgroup is present on
4790 * the tree
4791 */
4792 next_mz =
4793 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004794 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004795 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004796 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004797 break;
4798 } while (1);
4799 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004800 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4801 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004802 /*
4803 * One school of thought says that we should not add
4804 * back the node to the tree if reclaim returns 0.
4805 * But our reclaim could return 0, simply because due
4806 * to priority we are exposing a smaller subset of
4807 * memory to reclaim from. Consider this as a longer
4808 * term TODO.
4809 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004810 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004811 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004812 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004813 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004814 loop++;
4815 /*
4816 * Could not reclaim anything and there are no more
4817 * mem cgroups to try or we seem to be looping without
4818 * reclaiming anything.
4819 */
4820 if (!nr_reclaimed &&
4821 (next_mz == NULL ||
4822 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4823 break;
4824 } while (!nr_reclaimed);
4825 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004826 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004827 return nr_reclaimed;
4828}
4829
Michal Hocko2ef37d32012-10-26 13:37:30 +02004830/**
4831 * mem_cgroup_force_empty_list - clears LRU of a group
4832 * @memcg: group to clear
4833 * @node: NUMA node
4834 * @zid: zone id
4835 * @lru: lru to to clear
4836 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004837 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004838 * reclaim the pages page themselves - pages are moved to the parent (or root)
4839 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004840 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004841static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004842 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004843{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004844 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004845 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004846 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004847 struct page *busy;
4848 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004849
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004850 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004851 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4852 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004853
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004854 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004855 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004856 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004857 struct page *page;
4858
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004859 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004860 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004861 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004862 break;
4863 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004864 page = list_entry(list->prev, struct page, lru);
4865 if (busy == page) {
4866 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004867 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004868 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004869 continue;
4870 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004871 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004872
Johannes Weiner925b7672012-01-12 17:18:15 -08004873 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004874
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004875 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004876 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004877 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004878 cond_resched();
4879 } else
4880 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004881 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004882}
4883
4884/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004885 * make mem_cgroup's charge to be 0 if there is no task by moving
4886 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004887 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004888 *
4889 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004890 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004891static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004892{
Michal Hockoc26251f2012-10-26 13:37:28 +02004893 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004894 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004895
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004896 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004897 /* This is for making all *used* pages to be on LRU. */
4898 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004899 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004900 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004901 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004902 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004903 enum lru_list lru;
4904 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004905 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004906 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004907 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004908 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004909 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004910 mem_cgroup_end_move(memcg);
4911 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004912 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004913
Michal Hocko2ef37d32012-10-26 13:37:30 +02004914 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004915 * Kernel memory may not necessarily be trackable to a specific
4916 * process. So they are not migrated, and therefore we can't
4917 * expect their value to drop to 0 here.
4918 * Having res filled up with kmem only is enough.
4919 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004920 * This is a safety check because mem_cgroup_force_empty_list
4921 * could have raced with mem_cgroup_replace_page_cache callers
4922 * so the lru seemed empty but the page could have been added
4923 * right after the check. RES_USAGE should be safe as we always
4924 * charge before adding to the LRU.
4925 */
Glauber Costabea207c2012-12-18 14:22:11 -08004926 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4927 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4928 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004929}
4930
4931/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004932 * This mainly exists for tests during the setting of set of use_hierarchy.
4933 * Since this is the very setting we are changing, the current hierarchy value
4934 * is meaningless
4935 */
4936static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4937{
4938 struct cgroup *pos;
4939
4940 /* bounce at first found */
4941 cgroup_for_each_child(pos, memcg->css.cgroup)
4942 return true;
4943 return false;
4944}
4945
4946/*
Glauber Costa09998212013-02-22 16:34:55 -08004947 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4948 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004949 * from mem_cgroup_count_children(), in the sense that we don't really care how
4950 * many children we have; we only need to know if we have any. It also counts
4951 * any memcg without hierarchy as infertile.
4952 */
4953static inline bool memcg_has_children(struct mem_cgroup *memcg)
4954{
4955 return memcg->use_hierarchy && __memcg_has_children(memcg);
4956}
4957
4958/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004959 * Reclaims as many pages from the given memcg as possible and moves
4960 * the rest to the parent.
4961 *
4962 * Caller is responsible for holding css reference for memcg.
4963 */
4964static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4965{
4966 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4967 struct cgroup *cgrp = memcg->css.cgroup;
4968
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004969 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004970 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4971 return -EBUSY;
4972
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004973 /* we call try-to-free pages for make this cgroup empty */
4974 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004975 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004976 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004977 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004978
Michal Hockoc26251f2012-10-26 13:37:28 +02004979 if (signal_pending(current))
4980 return -EINTR;
4981
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004982 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004983 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004984 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004985 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004986 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004987 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004988 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004989
4990 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004991 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004992 mem_cgroup_reparent_charges(memcg);
4993
4994 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004995}
4996
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004997static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004998{
Michal Hockoc26251f2012-10-26 13:37:28 +02004999 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5000 int ret;
5001
Michal Hockod8423012012-10-26 13:37:29 +02005002 if (mem_cgroup_is_root(memcg))
5003 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02005004 css_get(&memcg->css);
5005 ret = mem_cgroup_force_empty(memcg);
5006 css_put(&memcg->css);
5007
5008 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005009}
5010
5011
Balbir Singh18f59ea2009-01-07 18:08:07 -08005012static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
5013{
5014 return mem_cgroup_from_cont(cont)->use_hierarchy;
5015}
5016
5017static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
5018 u64 val)
5019{
5020 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005021 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005022 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005023 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005024
5025 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005026 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005027
Glauber Costa09998212013-02-22 16:34:55 -08005028 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07005029
5030 if (memcg->use_hierarchy == val)
5031 goto out;
5032
Balbir Singh18f59ea2009-01-07 18:08:07 -08005033 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005034 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08005035 * in the child subtrees. If it is unset, then the change can
5036 * occur, provided the current cgroup has no children.
5037 *
5038 * For the root cgroup, parent_mem is NULL, we allow value to be
5039 * set if there are no children.
5040 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005041 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08005042 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08005043 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005044 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005045 else
5046 retval = -EBUSY;
5047 } else
5048 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07005049
5050out:
Glauber Costa09998212013-02-22 16:34:55 -08005051 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005052
5053 return retval;
5054}
5055
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005056
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005057static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005058 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005059{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005060 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005061 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005062
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005063 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005064 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005065 val += mem_cgroup_read_stat(iter, idx);
5066
5067 if (val < 0) /* race ? */
5068 val = 0;
5069 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005070}
5071
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005072static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005073{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005074 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005076 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005077 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00005078 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005079 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00005080 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005081 }
5082
David Rientjesb070e652013-05-07 16:18:09 -07005083 /*
5084 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
5085 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
5086 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005087 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
5088 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005089
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005090 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005091 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005092
5093 return val << PAGE_SHIFT;
5094}
5095
Tejun Heoaf36f902012-04-01 12:09:55 -07005096static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
5097 struct file *file, char __user *buf,
5098 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005099{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005100 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07005101 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005102 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005103 int name, len;
5104 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005105
5106 type = MEMFILE_TYPE(cft->private);
5107 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005108
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005109 switch (type) {
5110 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005111 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005112 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005113 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005114 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005115 break;
5116 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005117 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005118 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005119 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005120 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005121 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005122 case _KMEM:
5123 val = res_counter_read_u64(&memcg->kmem, name);
5124 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005125 default:
5126 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005127 }
Tejun Heoaf36f902012-04-01 12:09:55 -07005128
5129 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5130 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005131}
Glauber Costa510fc4e2012-12-18 14:21:47 -08005132
5133static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
5134{
5135 int ret = -EINVAL;
5136#ifdef CONFIG_MEMCG_KMEM
5137 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5138 /*
5139 * For simplicity, we won't allow this to be disabled. It also can't
5140 * be changed if the cgroup has children already, or if tasks had
5141 * already joined.
5142 *
5143 * If tasks join before we set the limit, a person looking at
5144 * kmem.usage_in_bytes will have no way to determine when it took
5145 * place, which makes the value quite meaningless.
5146 *
5147 * After it first became limited, changes in the value of the limit are
5148 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08005149 */
Glauber Costa09998212013-02-22 16:34:55 -08005150 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005151 mutex_lock(&set_limit_mutex);
5152 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08005153 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08005154 ret = -EBUSY;
5155 goto out;
5156 }
5157 ret = res_counter_set_limit(&memcg->kmem, val);
5158 VM_BUG_ON(ret);
5159
Glauber Costa55007d82012-12-18 14:22:38 -08005160 ret = memcg_update_cache_sizes(memcg);
5161 if (ret) {
5162 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5163 goto out;
5164 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005165 static_key_slow_inc(&memcg_kmem_enabled_key);
5166 /*
5167 * setting the active bit after the inc will guarantee no one
5168 * starts accounting before all call sites are patched
5169 */
5170 memcg_kmem_set_active(memcg);
5171
Glauber Costa7de37682012-12-18 14:22:07 -08005172 /*
5173 * kmem charges can outlive the cgroup. In the case of slab
5174 * pages, for instance, a page contain objects from various
5175 * processes, so it is unfeasible to migrate them away. We
5176 * need to reference count the memcg because of that.
5177 */
5178 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005179 } else
5180 ret = res_counter_set_limit(&memcg->kmem, val);
5181out:
5182 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005183 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005184#endif
5185 return ret;
5186}
5187
Hugh Dickins6d0439902013-02-22 16:35:50 -08005188#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005189static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005190{
Glauber Costa55007d82012-12-18 14:22:38 -08005191 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005192 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5193 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005194 goto out;
5195
Glauber Costa510fc4e2012-12-18 14:21:47 -08005196 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005197 /*
5198 * When that happen, we need to disable the static branch only on those
5199 * memcgs that enabled it. To achieve this, we would be forced to
5200 * complicate the code by keeping track of which memcgs were the ones
5201 * that actually enabled limits, and which ones got it from its
5202 * parents.
5203 *
5204 * It is a lot simpler just to do static_key_slow_inc() on every child
5205 * that is accounted.
5206 */
Glauber Costa55007d82012-12-18 14:22:38 -08005207 if (!memcg_kmem_is_active(memcg))
5208 goto out;
5209
5210 /*
5211 * destroy(), called if we fail, will issue static_key_slow_inc() and
5212 * mem_cgroup_put() if kmem is enabled. We have to either call them
5213 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5214 * this more consistent, since it always leads to the same destroy path
5215 */
5216 mem_cgroup_get(memcg);
5217 static_key_slow_inc(&memcg_kmem_enabled_key);
5218
5219 mutex_lock(&set_limit_mutex);
5220 ret = memcg_update_cache_sizes(memcg);
5221 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005222out:
5223 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005224}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005225#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005226
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005227/*
5228 * The user of this function is...
5229 * RES_LIMIT.
5230 */
Paul Menage856c13a2008-07-25 01:47:04 -07005231static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5232 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005233{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005234 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005235 enum res_type type;
5236 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005237 unsigned long long val;
5238 int ret;
5239
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005240 type = MEMFILE_TYPE(cft->private);
5241 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005242
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005243 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005244 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005245 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5246 ret = -EINVAL;
5247 break;
5248 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005249 /* This function does all necessary parse...reuse it */
5250 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005251 if (ret)
5252 break;
5253 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005254 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005255 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005256 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005257 else if (type == _KMEM)
5258 ret = memcg_update_kmem_limit(cont, val);
5259 else
5260 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005261 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005262 case RES_SOFT_LIMIT:
5263 ret = res_counter_memparse_write_strategy(buffer, &val);
5264 if (ret)
5265 break;
5266 /*
5267 * For memsw, soft limits are hard to implement in terms
5268 * of semantics, for now, we support soft limits for
5269 * control without swap
5270 */
5271 if (type == _MEM)
5272 ret = res_counter_set_soft_limit(&memcg->res, val);
5273 else
5274 ret = -EINVAL;
5275 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005276 default:
5277 ret = -EINVAL; /* should be BUG() ? */
5278 break;
5279 }
5280 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005281}
5282
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005283static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5284 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5285{
5286 struct cgroup *cgroup;
5287 unsigned long long min_limit, min_memsw_limit, tmp;
5288
5289 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5290 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5291 cgroup = memcg->css.cgroup;
5292 if (!memcg->use_hierarchy)
5293 goto out;
5294
5295 while (cgroup->parent) {
5296 cgroup = cgroup->parent;
5297 memcg = mem_cgroup_from_cont(cgroup);
5298 if (!memcg->use_hierarchy)
5299 break;
5300 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5301 min_limit = min(min_limit, tmp);
5302 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5303 min_memsw_limit = min(min_memsw_limit, tmp);
5304 }
5305out:
5306 *mem_limit = min_limit;
5307 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005308}
5309
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005310static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005311{
Tejun Heoaf36f902012-04-01 12:09:55 -07005312 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005313 int name;
5314 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005315
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005316 type = MEMFILE_TYPE(event);
5317 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005318
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005319 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005320 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005321 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005322 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005323 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005324 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005325 else if (type == _KMEM)
5326 res_counter_reset_max(&memcg->kmem);
5327 else
5328 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005329 break;
5330 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005331 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005332 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005333 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005334 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005335 else if (type == _KMEM)
5336 res_counter_reset_failcnt(&memcg->kmem);
5337 else
5338 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005339 break;
5340 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005341
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005342 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005343}
5344
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005345static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5346 struct cftype *cft)
5347{
5348 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5349}
5350
Daisuke Nishimura02491442010-03-10 15:22:17 -08005351#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005352static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5353 struct cftype *cft, u64 val)
5354{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005355 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005356
5357 if (val >= (1 << NR_MOVE_TYPE))
5358 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005359
Glauber Costaee5e8472013-02-22 16:34:50 -08005360 /*
5361 * No kind of locking is needed in here, because ->can_attach() will
5362 * check this value once in the beginning of the process, and then carry
5363 * on with stale data. This means that changes to this value will only
5364 * affect task migrations starting after the change.
5365 */
5366 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005367 return 0;
5368}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005369#else
5370static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5371 struct cftype *cft, u64 val)
5372{
5373 return -ENOSYS;
5374}
5375#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005376
Ying Han406eb0c2011-05-26 16:25:37 -07005377#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005378static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005379 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005380{
5381 int nid;
5382 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5383 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005384 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005385
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005386 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005387 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005388 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005389 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005390 seq_printf(m, " N%d=%lu", nid, node_nr);
5391 }
5392 seq_putc(m, '\n');
5393
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005394 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005395 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005396 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005397 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005398 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005399 seq_printf(m, " N%d=%lu", nid, node_nr);
5400 }
5401 seq_putc(m, '\n');
5402
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005403 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005404 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005405 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005406 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005407 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005408 seq_printf(m, " N%d=%lu", nid, node_nr);
5409 }
5410 seq_putc(m, '\n');
5411
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005412 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005413 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005414 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005415 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005416 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005417 seq_printf(m, " N%d=%lu", nid, node_nr);
5418 }
5419 seq_putc(m, '\n');
5420 return 0;
5421}
5422#endif /* CONFIG_NUMA */
5423
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005424static inline void mem_cgroup_lru_names_not_uptodate(void)
5425{
5426 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5427}
5428
Wanpeng Liab215882012-07-31 16:43:09 -07005429static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005430 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005431{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005432 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005433 struct mem_cgroup *mi;
5434 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005435
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005436 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005437 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005438 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005439 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5440 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005441 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005442
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005443 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5444 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5445 mem_cgroup_read_events(memcg, i));
5446
5447 for (i = 0; i < NR_LRU_LISTS; i++)
5448 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5449 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5450
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005451 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005452 {
5453 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005454 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005455 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005456 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005457 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5458 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005459 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005460
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005461 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5462 long long val = 0;
5463
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005464 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005465 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005466 for_each_mem_cgroup_tree(mi, memcg)
5467 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5468 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5469 }
5470
5471 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5472 unsigned long long val = 0;
5473
5474 for_each_mem_cgroup_tree(mi, memcg)
5475 val += mem_cgroup_read_events(mi, i);
5476 seq_printf(m, "total_%s %llu\n",
5477 mem_cgroup_events_names[i], val);
5478 }
5479
5480 for (i = 0; i < NR_LRU_LISTS; i++) {
5481 unsigned long long val = 0;
5482
5483 for_each_mem_cgroup_tree(mi, memcg)
5484 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5485 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005486 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005487
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005488#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005489 {
5490 int nid, zid;
5491 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005492 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005493 unsigned long recent_rotated[2] = {0, 0};
5494 unsigned long recent_scanned[2] = {0, 0};
5495
5496 for_each_online_node(nid)
5497 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005498 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005499 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005500
Hugh Dickins89abfab2012-05-29 15:06:53 -07005501 recent_rotated[0] += rstat->recent_rotated[0];
5502 recent_rotated[1] += rstat->recent_rotated[1];
5503 recent_scanned[0] += rstat->recent_scanned[0];
5504 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005505 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005506 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5507 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5508 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5509 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005510 }
5511#endif
5512
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005513 return 0;
5514}
5515
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005516static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5517{
5518 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5519
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005520 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005521}
5522
5523static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5524 u64 val)
5525{
5526 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5527 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005528
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005529 if (val > 100)
5530 return -EINVAL;
5531
5532 if (cgrp->parent == NULL)
5533 return -EINVAL;
5534
5535 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005536
Glauber Costa09998212013-02-22 16:34:55 -08005537 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005538
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005539 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005540 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005541 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005542 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005543 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005544
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005545 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005546
Glauber Costa09998212013-02-22 16:34:55 -08005547 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005548
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005549 return 0;
5550}
5551
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005552static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5553{
5554 struct mem_cgroup_threshold_ary *t;
5555 u64 usage;
5556 int i;
5557
5558 rcu_read_lock();
5559 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005560 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005561 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005562 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563
5564 if (!t)
5565 goto unlock;
5566
5567 usage = mem_cgroup_usage(memcg, swap);
5568
5569 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005570 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005571 * If it's not true, a threshold was crossed after last
5572 * call of __mem_cgroup_threshold().
5573 */
Phil Carmody5407a562010-05-26 14:42:42 -07005574 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005575
5576 /*
5577 * Iterate backward over array of thresholds starting from
5578 * current_threshold and check if a threshold is crossed.
5579 * If none of thresholds below usage is crossed, we read
5580 * only one element of the array here.
5581 */
5582 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5583 eventfd_signal(t->entries[i].eventfd, 1);
5584
5585 /* i = current_threshold + 1 */
5586 i++;
5587
5588 /*
5589 * Iterate forward over array of thresholds starting from
5590 * current_threshold+1 and check if a threshold is crossed.
5591 * If none of thresholds above usage is crossed, we read
5592 * only one element of the array here.
5593 */
5594 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5595 eventfd_signal(t->entries[i].eventfd, 1);
5596
5597 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005598 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005599unlock:
5600 rcu_read_unlock();
5601}
5602
5603static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5604{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005605 while (memcg) {
5606 __mem_cgroup_threshold(memcg, false);
5607 if (do_swap_account)
5608 __mem_cgroup_threshold(memcg, true);
5609
5610 memcg = parent_mem_cgroup(memcg);
5611 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005612}
5613
5614static int compare_thresholds(const void *a, const void *b)
5615{
5616 const struct mem_cgroup_threshold *_a = a;
5617 const struct mem_cgroup_threshold *_b = b;
5618
Greg Thelend96fa172013-09-11 14:23:08 -07005619 if (_a->threshold > _b->threshold)
5620 return 1;
5621
5622 if (_a->threshold < _b->threshold)
5623 return -1;
5624
5625 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005626}
5627
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005628static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005629{
5630 struct mem_cgroup_eventfd_list *ev;
5631
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005632 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005633 eventfd_signal(ev->eventfd, 1);
5634 return 0;
5635}
5636
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005637static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005638{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005639 struct mem_cgroup *iter;
5640
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005641 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005642 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005643}
5644
5645static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5646 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005647{
5648 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005649 struct mem_cgroup_thresholds *thresholds;
5650 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005651 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005652 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005653 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005654
5655 ret = res_counter_memparse_write_strategy(args, &threshold);
5656 if (ret)
5657 return ret;
5658
5659 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005660
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005661 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005662 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005663 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005664 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005665 else
5666 BUG();
5667
5668 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5669
5670 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005671 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005672 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5673
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005674 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005675
5676 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005677 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005678 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005679 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005680 ret = -ENOMEM;
5681 goto unlock;
5682 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005683 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005684
5685 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005686 if (thresholds->primary) {
5687 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005688 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005689 }
5690
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005691 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005692 new->entries[size - 1].eventfd = eventfd;
5693 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005694
5695 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005696 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005697 compare_thresholds, NULL);
5698
5699 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005700 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005701 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005702 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005703 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005704 * new->current_threshold will not be used until
5705 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005706 * it here.
5707 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005708 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005709 } else
5710 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005711 }
5712
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005713 /* Free old spare buffer and save old primary buffer as spare */
5714 kfree(thresholds->spare);
5715 thresholds->spare = thresholds->primary;
5716
5717 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005718
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005719 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005720 synchronize_rcu();
5721
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005722unlock:
5723 mutex_unlock(&memcg->thresholds_lock);
5724
5725 return ret;
5726}
5727
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005728static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005729 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005730{
5731 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005732 struct mem_cgroup_thresholds *thresholds;
5733 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005734 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005735 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005736 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005737
5738 mutex_lock(&memcg->thresholds_lock);
5739 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005740 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005741 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005742 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005743 else
5744 BUG();
5745
Anton Vorontsov371528c2012-02-24 05:14:46 +04005746 if (!thresholds->primary)
5747 goto unlock;
5748
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005749 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5750
5751 /* Check if a threshold crossed before removing */
5752 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5753
5754 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005755 size = 0;
5756 for (i = 0; i < thresholds->primary->size; i++) {
5757 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005758 size++;
5759 }
5760
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005761 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005762
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005763 /* Set thresholds array to NULL if we don't have thresholds */
5764 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005765 kfree(new);
5766 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005767 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005768 }
5769
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005770 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005771
5772 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005773 new->current_threshold = -1;
5774 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5775 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005776 continue;
5777
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005778 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005779 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005780 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005781 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005782 * until rcu_assign_pointer(), so it's safe to increment
5783 * it here.
5784 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005785 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005786 }
5787 j++;
5788 }
5789
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005790swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005791 /* Swap primary and spare array */
5792 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005793 /* If all events are unregistered, free the spare array */
5794 if (!new) {
5795 kfree(thresholds->spare);
5796 thresholds->spare = NULL;
5797 }
5798
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005799 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005800
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005801 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005802 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005803unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005804 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005805}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005806
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005807static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5808 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5809{
5810 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5811 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005812 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005813
5814 BUG_ON(type != _OOM_TYPE);
5815 event = kmalloc(sizeof(*event), GFP_KERNEL);
5816 if (!event)
5817 return -ENOMEM;
5818
Michal Hocko1af8efe2011-07-26 16:08:24 -07005819 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005820
5821 event->eventfd = eventfd;
5822 list_add(&event->list, &memcg->oom_notify);
5823
5824 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005825 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005826 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005827 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005828
5829 return 0;
5830}
5831
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005832static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005833 struct cftype *cft, struct eventfd_ctx *eventfd)
5834{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005835 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005836 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005837 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005838
5839 BUG_ON(type != _OOM_TYPE);
5840
Michal Hocko1af8efe2011-07-26 16:08:24 -07005841 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005842
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005843 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005844 if (ev->eventfd == eventfd) {
5845 list_del(&ev->list);
5846 kfree(ev);
5847 }
5848 }
5849
Michal Hocko1af8efe2011-07-26 16:08:24 -07005850 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005851}
5852
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005853static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5854 struct cftype *cft, struct cgroup_map_cb *cb)
5855{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005856 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005857
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005858 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005860 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005861 cb->fill(cb, "under_oom", 1);
5862 else
5863 cb->fill(cb, "under_oom", 0);
5864 return 0;
5865}
5866
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005867static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5868 struct cftype *cft, u64 val)
5869{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005870 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005871 struct mem_cgroup *parent;
5872
5873 /* cannot set to root cgroup and only 0 and 1 are allowed */
5874 if (!cgrp->parent || !((val == 0) || (val == 1)))
5875 return -EINVAL;
5876
5877 parent = mem_cgroup_from_cont(cgrp->parent);
5878
Glauber Costa09998212013-02-22 16:34:55 -08005879 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005880 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005881 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005882 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005883 return -EINVAL;
5884 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005885 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005886 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005887 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005888 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005889 return 0;
5890}
5891
Andrew Mortonc255a452012-07-31 16:43:02 -07005892#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005893static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005894{
Glauber Costa55007d82012-12-18 14:22:38 -08005895 int ret;
5896
Glauber Costa2633d7a2012-12-18 14:22:34 -08005897 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005898 ret = memcg_propagate_kmem(memcg);
5899 if (ret)
5900 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005901
Glauber Costa1d62e432012-04-09 19:36:33 -03005902 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005903}
Glauber Costae5671df2011-12-11 21:47:01 +00005904
Glauber Costa1d62e432012-04-09 19:36:33 -03005905static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005906{
Glauber Costa1d62e432012-04-09 19:36:33 -03005907 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005908
5909 memcg_kmem_mark_dead(memcg);
5910
5911 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5912 return;
5913
5914 /*
5915 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5916 * path here, being careful not to race with memcg_uncharge_kmem: it is
5917 * possible that the charges went down to 0 between mark_dead and the
5918 * res_counter read, so in that case, we don't need the put
5919 */
5920 if (memcg_kmem_test_and_clear_dead(memcg))
5921 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005922}
Glauber Costae5671df2011-12-11 21:47:01 +00005923#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005924static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005925{
5926 return 0;
5927}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005928
Glauber Costa1d62e432012-04-09 19:36:33 -03005929static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005930{
5931}
Glauber Costae5671df2011-12-11 21:47:01 +00005932#endif
5933
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005934static struct cftype mem_cgroup_files[] = {
5935 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005936 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005937 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005938 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005939 .register_event = mem_cgroup_usage_register_event,
5940 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005941 },
5942 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005943 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005944 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005945 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005946 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005947 },
5948 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005949 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005950 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005951 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005952 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005953 },
5954 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005955 .name = "soft_limit_in_bytes",
5956 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5957 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005958 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005959 },
5960 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005961 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005962 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005963 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005964 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005965 },
Balbir Singh8697d332008-02-07 00:13:59 -08005966 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005967 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005968 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005969 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005970 {
5971 .name = "force_empty",
5972 .trigger = mem_cgroup_force_empty_write,
5973 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005974 {
5975 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005976 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005977 .write_u64 = mem_cgroup_hierarchy_write,
5978 .read_u64 = mem_cgroup_hierarchy_read,
5979 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005980 {
5981 .name = "swappiness",
5982 .read_u64 = mem_cgroup_swappiness_read,
5983 .write_u64 = mem_cgroup_swappiness_write,
5984 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005985 {
5986 .name = "move_charge_at_immigrate",
5987 .read_u64 = mem_cgroup_move_charge_read,
5988 .write_u64 = mem_cgroup_move_charge_write,
5989 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005990 {
5991 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005992 .read_map = mem_cgroup_oom_control_read,
5993 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005994 .register_event = mem_cgroup_oom_register_event,
5995 .unregister_event = mem_cgroup_oom_unregister_event,
5996 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5997 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005998 {
5999 .name = "pressure_level",
6000 .register_event = vmpressure_register_event,
6001 .unregister_event = vmpressure_unregister_event,
6002 },
Ying Han406eb0c2011-05-26 16:25:37 -07006003#ifdef CONFIG_NUMA
6004 {
6005 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006006 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07006007 },
6008#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006009#ifdef CONFIG_MEMCG_KMEM
6010 {
6011 .name = "kmem.limit_in_bytes",
6012 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
6013 .write_string = mem_cgroup_write,
6014 .read = mem_cgroup_read,
6015 },
6016 {
6017 .name = "kmem.usage_in_bytes",
6018 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
6019 .read = mem_cgroup_read,
6020 },
6021 {
6022 .name = "kmem.failcnt",
6023 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
6024 .trigger = mem_cgroup_reset,
6025 .read = mem_cgroup_read,
6026 },
6027 {
6028 .name = "kmem.max_usage_in_bytes",
6029 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6030 .trigger = mem_cgroup_reset,
6031 .read = mem_cgroup_read,
6032 },
Glauber Costa749c5412012-12-18 14:23:01 -08006033#ifdef CONFIG_SLABINFO
6034 {
6035 .name = "kmem.slabinfo",
6036 .read_seq_string = mem_cgroup_slabinfo_read,
6037 },
6038#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006039#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07006040 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07006041};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006042
Michal Hocko2d110852013-02-22 16:34:43 -08006043#ifdef CONFIG_MEMCG_SWAP
6044static struct cftype memsw_cgroup_files[] = {
6045 {
6046 .name = "memsw.usage_in_bytes",
6047 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6048 .read = mem_cgroup_read,
6049 .register_event = mem_cgroup_usage_register_event,
6050 .unregister_event = mem_cgroup_usage_unregister_event,
6051 },
6052 {
6053 .name = "memsw.max_usage_in_bytes",
6054 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6055 .trigger = mem_cgroup_reset,
6056 .read = mem_cgroup_read,
6057 },
6058 {
6059 .name = "memsw.limit_in_bytes",
6060 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6061 .write_string = mem_cgroup_write,
6062 .read = mem_cgroup_read,
6063 },
6064 {
6065 .name = "memsw.failcnt",
6066 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6067 .trigger = mem_cgroup_reset,
6068 .read = mem_cgroup_read,
6069 },
6070 { }, /* terminate */
6071};
6072#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006073static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006074{
6075 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006076 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006077 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006078 /*
6079 * This routine is called against possible nodes.
6080 * But it's BUG to call kmalloc() against offline node.
6081 *
6082 * TODO: this routine can waste much memory for nodes which will
6083 * never be onlined. It's better to use memory hotplug callback
6084 * function.
6085 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006086 if (!node_state(node, N_NORMAL_MEMORY))
6087 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08006088 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006089 if (!pn)
6090 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006091
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006092 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6093 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08006094 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006095 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07006096 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006097 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006098 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07006099 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006100 return 0;
6101}
6102
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006103static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006104{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006105 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006106}
6107
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006108static struct mem_cgroup *mem_cgroup_alloc(void)
6109{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006110 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006111 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006112
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006113 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08006114 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006115 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006116 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006117 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006118
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006119 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07006120 return NULL;
6121
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006122 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6123 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006124 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006125 spin_lock_init(&memcg->pcp_counter_lock);
6126 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006127
6128out_free:
6129 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006130 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006131 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006132 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006133 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006134}
6135
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006136/*
Glauber Costac8b2a362012-12-18 14:22:13 -08006137 * At destroying mem_cgroup, references from swap_cgroup can remain.
6138 * (scanning all at force_empty is too costly...)
6139 *
6140 * Instead of clearing all references at force_empty, we remember
6141 * the number of reference from swap_cgroup and free mem_cgroup when
6142 * it goes down to 0.
6143 *
6144 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07006145 */
Glauber Costac8b2a362012-12-18 14:22:13 -08006146
6147static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006148{
Glauber Costac8b2a362012-12-18 14:22:13 -08006149 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006150 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006151
Glauber Costac8b2a362012-12-18 14:22:13 -08006152 mem_cgroup_remove_from_trees(memcg);
6153 free_css_id(&mem_cgroup_subsys, &memcg->css);
6154
6155 for_each_node(node)
6156 free_mem_cgroup_per_zone_info(memcg, node);
6157
6158 free_percpu(memcg->stat);
6159
Glauber Costa3f134612012-05-29 15:07:11 -07006160 /*
6161 * We need to make sure that (at least for now), the jump label
6162 * destruction code runs outside of the cgroup lock. This is because
6163 * get_online_cpus(), which is called from the static_branch update,
6164 * can't be called inside the cgroup_lock. cpusets are the ones
6165 * enforcing this dependency, so if they ever change, we might as well.
6166 *
6167 * schedule_work() will guarantee this happens. Be careful if you need
6168 * to move this code around, and make sure it is outside
6169 * the cgroup_lock.
6170 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006171 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006172 if (size < PAGE_SIZE)
6173 kfree(memcg);
6174 else
6175 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006176}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006177
Glauber Costac8b2a362012-12-18 14:22:13 -08006178
6179/*
6180 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6181 * but in process context. The work_freeing structure is overlaid
6182 * on the rcu_freeing structure, which itself is overlaid on memsw.
6183 */
6184static void free_work(struct work_struct *work)
6185{
6186 struct mem_cgroup *memcg;
6187
6188 memcg = container_of(work, struct mem_cgroup, work_freeing);
6189 __mem_cgroup_free(memcg);
6190}
6191
Glauber Costa3afe36b2012-05-29 15:07:10 -07006192static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006193{
6194 struct mem_cgroup *memcg;
6195
6196 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006197 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006198 schedule_work(&memcg->work_freeing);
6199}
6200
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006201static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006202{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006203 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006204}
6205
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006206static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006207{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006208 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6209 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006210 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006211 if (parent)
6212 mem_cgroup_put(parent);
6213 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006214}
6215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006216static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006217{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006218 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006219}
6220
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006221/*
6222 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6223 */
Glauber Costae1aab162011-12-11 21:47:03 +00006224struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006225{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006226 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006227 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006228 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006229}
Glauber Costae1aab162011-12-11 21:47:03 +00006230EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006231
Michal Hocko8787a1d2013-02-22 16:35:39 -08006232static void __init mem_cgroup_soft_limit_tree_init(void)
Balbir Singhf64c3f52009-09-23 15:56:37 -07006233{
6234 struct mem_cgroup_tree_per_node *rtpn;
6235 struct mem_cgroup_tree_per_zone *rtpz;
6236 int tmp, node, zone;
6237
Bob Liu3ed28fa2012-01-12 17:19:04 -08006238 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006239 tmp = node;
6240 if (!node_state(node, N_NORMAL_MEMORY))
6241 tmp = -1;
6242 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
Michal Hocko8787a1d2013-02-22 16:35:39 -08006243 BUG_ON(!rtpn);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006244
6245 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6246
6247 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6248 rtpz = &rtpn->rb_tree_per_zone[zone];
6249 rtpz->rb_root = RB_ROOT;
6250 spin_lock_init(&rtpz->lock);
6251 }
6252 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07006253}
6254
Li Zefan0eb253e2009-01-15 13:51:25 -08006255static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006256mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006257{
Glauber Costad142e3e2013-02-22 16:34:52 -08006258 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006259 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006260 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006261
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006262 memcg = mem_cgroup_alloc();
6263 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006264 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006265
Bob Liu3ed28fa2012-01-12 17:19:04 -08006266 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006267 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006268 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006269
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006270 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006271 if (cont->parent == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006272 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006273 res_counter_init(&memcg->res, NULL);
6274 res_counter_init(&memcg->memsw, NULL);
6275 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006276 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006277
Glauber Costad142e3e2013-02-22 16:34:52 -08006278 memcg->last_scanned_node = MAX_NUMNODES;
6279 INIT_LIST_HEAD(&memcg->oom_notify);
6280 atomic_set(&memcg->refcnt, 1);
6281 memcg->move_charge_at_immigrate = 0;
6282 mutex_init(&memcg->thresholds_lock);
6283 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006284 vmpressure_init(&memcg->vmpressure);
Glauber Costad142e3e2013-02-22 16:34:52 -08006285
6286 return &memcg->css;
6287
6288free_out:
6289 __mem_cgroup_free(memcg);
6290 return ERR_PTR(error);
6291}
6292
6293static int
6294mem_cgroup_css_online(struct cgroup *cont)
6295{
6296 struct mem_cgroup *memcg, *parent;
6297 int error = 0;
6298
6299 if (!cont->parent)
6300 return 0;
6301
Glauber Costa09998212013-02-22 16:34:55 -08006302 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006303 memcg = mem_cgroup_from_cont(cont);
6304 parent = mem_cgroup_from_cont(cont->parent);
6305
6306 memcg->use_hierarchy = parent->use_hierarchy;
6307 memcg->oom_kill_disable = parent->oom_kill_disable;
6308 memcg->swappiness = mem_cgroup_swappiness(parent);
6309
6310 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006311 res_counter_init(&memcg->res, &parent->res);
6312 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006313 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006314
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006315 /*
6316 * We increment refcnt of the parent to ensure that we can
6317 * safely access it on res_counter_charge/uncharge.
6318 * This refcnt will be decremented when freeing this
6319 * mem_cgroup(see mem_cgroup_put).
6320 */
6321 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006322 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006323 res_counter_init(&memcg->res, NULL);
6324 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006325 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006326 /*
6327 * Deeper hierachy with use_hierarchy == false doesn't make
6328 * much sense so let cgroup subsystem know about this
6329 * unfortunate state in our controller.
6330 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006331 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006332 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006333 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006334
6335 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006336 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006337 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006338}
6339
Michal Hocko5f578162013-04-29 15:07:17 -07006340/*
6341 * Announce all parents that a group from their hierarchy is gone.
6342 */
6343static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6344{
6345 struct mem_cgroup *parent = memcg;
6346
6347 while ((parent = parent_mem_cgroup(parent)))
6348 atomic_inc(&parent->dead_count);
6349
6350 /*
6351 * if the root memcg is not hierarchical we have to check it
6352 * explicitely.
6353 */
6354 if (!root_mem_cgroup->use_hierarchy)
6355 atomic_inc(&root_mem_cgroup->dead_count);
6356}
6357
Tejun Heo92fb9742012-11-19 08:13:38 -08006358static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006359{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006360 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006361 struct cgroup *iter;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006362
Michal Hocko5f578162013-04-29 15:07:17 -07006363 mem_cgroup_invalidate_reclaim_iterators(memcg);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006364
6365 /*
6366 * This requires that offlining is serialized. Right now that is
6367 * guaranteed because css_killed_work_fn() holds the cgroup_mutex.
6368 */
6369 rcu_read_lock();
6370 cgroup_for_each_descendant_post(iter, cont) {
6371 rcu_read_unlock();
6372 mem_cgroup_reparent_charges(mem_cgroup_from_cont(iter));
6373 rcu_read_lock();
6374 }
6375 rcu_read_unlock();
Michal Hockoab5196c2012-10-26 13:37:32 +02006376 mem_cgroup_reparent_charges(memcg);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006377
Glauber Costa1f458cb2012-12-18 14:22:50 -08006378 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006379}
6380
Tejun Heo92fb9742012-11-19 08:13:38 -08006381static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006382{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006383 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006384
Glauber Costa1d62e432012-04-09 19:36:33 -03006385 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006387 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006388}
6389
Daisuke Nishimura02491442010-03-10 15:22:17 -08006390#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006391/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006392#define PRECHARGE_COUNT_AT_ONCE 256
6393static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006394{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006395 int ret = 0;
6396 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006397 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006398
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006399 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006400 mc.precharge += count;
6401 /* we don't need css_get for root */
6402 return ret;
6403 }
6404 /* try to charge at once */
6405 if (count > 1) {
6406 struct res_counter *dummy;
6407 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006408 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006409 * by cgroup_lock_live_cgroup() that it is not removed and we
6410 * are still under the same cgroup_mutex. So we can postpone
6411 * css_get().
6412 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006413 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006414 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006415 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006416 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006417 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006418 goto one_by_one;
6419 }
6420 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006421 return ret;
6422 }
6423one_by_one:
6424 /* fall back to one by one charge */
6425 while (count--) {
6426 if (signal_pending(current)) {
6427 ret = -EINTR;
6428 break;
6429 }
6430 if (!batch_count--) {
6431 batch_count = PRECHARGE_COUNT_AT_ONCE;
6432 cond_resched();
6433 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006434 ret = __mem_cgroup_try_charge(NULL,
6435 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006436 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006437 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006438 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006439 mc.precharge++;
6440 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006441 return ret;
6442}
6443
6444/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006445 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006446 * @vma: the vma the pte to be checked belongs
6447 * @addr: the address corresponding to the pte to be checked
6448 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006449 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006450 *
6451 * Returns
6452 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6453 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6454 * move charge. if @target is not NULL, the page is stored in target->page
6455 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006456 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6457 * target for charge migration. if @target is not NULL, the entry is stored
6458 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006459 *
6460 * Called with pte lock held.
6461 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006462union mc_target {
6463 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006464 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006465};
6466
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006467enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006468 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006469 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006470 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006471};
6472
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006473static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6474 unsigned long addr, pte_t ptent)
6475{
6476 struct page *page = vm_normal_page(vma, addr, ptent);
6477
6478 if (!page || !page_mapped(page))
6479 return NULL;
6480 if (PageAnon(page)) {
6481 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006482 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006483 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006484 } else if (!move_file())
6485 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006486 return NULL;
6487 if (!get_page_unless_zero(page))
6488 return NULL;
6489
6490 return page;
6491}
6492
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006493#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006494static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6495 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6496{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006497 struct page *page = NULL;
6498 swp_entry_t ent = pte_to_swp_entry(ptent);
6499
6500 if (!move_anon() || non_swap_entry(ent))
6501 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006502 /*
6503 * Because lookup_swap_cache() updates some statistics counter,
6504 * we call find_get_page() with swapper_space directly.
6505 */
Shaohua Li33806f02013-02-22 16:34:37 -08006506 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006507 if (do_swap_account)
6508 entry->val = ent.val;
6509
6510 return page;
6511}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006512#else
6513static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6514 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6515{
6516 return NULL;
6517}
6518#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006519
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006520static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6521 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6522{
6523 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006524 struct address_space *mapping;
6525 pgoff_t pgoff;
6526
6527 if (!vma->vm_file) /* anonymous vma */
6528 return NULL;
6529 if (!move_file())
6530 return NULL;
6531
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006532 mapping = vma->vm_file->f_mapping;
6533 if (pte_none(ptent))
6534 pgoff = linear_page_index(vma, addr);
6535 else /* pte_file(ptent) is true */
6536 pgoff = pte_to_pgoff(ptent);
6537
6538 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006539 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006540
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006541#ifdef CONFIG_SWAP
6542 /* shmem/tmpfs may report page out on swap: account for that too. */
6543 if (radix_tree_exceptional_entry(page)) {
6544 swp_entry_t swap = radix_to_swp_entry(page);
6545 if (do_swap_account)
6546 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006547 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006548 }
6549#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006550 return page;
6551}
6552
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006553static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006554 unsigned long addr, pte_t ptent, union mc_target *target)
6555{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006556 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006557 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006558 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006559 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006560
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006561 if (pte_present(ptent))
6562 page = mc_handle_present_pte(vma, addr, ptent);
6563 else if (is_swap_pte(ptent))
6564 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006565 else if (pte_none(ptent) || pte_file(ptent))
6566 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006567
6568 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006569 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006570 if (page) {
6571 pc = lookup_page_cgroup(page);
6572 /*
6573 * Do only loose check w/o page_cgroup lock.
6574 * mem_cgroup_move_account() checks the pc is valid or not under
6575 * the lock.
6576 */
6577 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6578 ret = MC_TARGET_PAGE;
6579 if (target)
6580 target->page = page;
6581 }
6582 if (!ret || !target)
6583 put_page(page);
6584 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006585 /* There is a swap entry and a page doesn't exist or isn't charged */
6586 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006587 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006588 ret = MC_TARGET_SWAP;
6589 if (target)
6590 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006591 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006592 return ret;
6593}
6594
Naoya Horiguchi12724852012-03-21 16:34:28 -07006595#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6596/*
6597 * We don't consider swapping or file mapped pages because THP does not
6598 * support them for now.
6599 * Caller should make sure that pmd_trans_huge(pmd) is true.
6600 */
6601static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6602 unsigned long addr, pmd_t pmd, union mc_target *target)
6603{
6604 struct page *page = NULL;
6605 struct page_cgroup *pc;
6606 enum mc_target_type ret = MC_TARGET_NONE;
6607
6608 page = pmd_page(pmd);
6609 VM_BUG_ON(!page || !PageHead(page));
6610 if (!move_anon())
6611 return ret;
6612 pc = lookup_page_cgroup(page);
6613 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6614 ret = MC_TARGET_PAGE;
6615 if (target) {
6616 get_page(page);
6617 target->page = page;
6618 }
6619 }
6620 return ret;
6621}
6622#else
6623static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6624 unsigned long addr, pmd_t pmd, union mc_target *target)
6625{
6626 return MC_TARGET_NONE;
6627}
6628#endif
6629
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006630static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6631 unsigned long addr, unsigned long end,
6632 struct mm_walk *walk)
6633{
6634 struct vm_area_struct *vma = walk->private;
6635 pte_t *pte;
6636 spinlock_t *ptl;
6637
Naoya Horiguchi12724852012-03-21 16:34:28 -07006638 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6639 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6640 mc.precharge += HPAGE_PMD_NR;
6641 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006642 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006643 }
Dave Hansen03319322011-03-22 16:32:56 -07006644
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006645 if (pmd_trans_unstable(pmd))
6646 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006647 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6648 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006649 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006650 mc.precharge++; /* increment precharge temporarily */
6651 pte_unmap_unlock(pte - 1, ptl);
6652 cond_resched();
6653
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006654 return 0;
6655}
6656
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006657static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6658{
6659 unsigned long precharge;
6660 struct vm_area_struct *vma;
6661
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006662 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006663 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6664 struct mm_walk mem_cgroup_count_precharge_walk = {
6665 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6666 .mm = mm,
6667 .private = vma,
6668 };
6669 if (is_vm_hugetlb_page(vma))
6670 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006671 walk_page_range(vma->vm_start, vma->vm_end,
6672 &mem_cgroup_count_precharge_walk);
6673 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006674 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006675
6676 precharge = mc.precharge;
6677 mc.precharge = 0;
6678
6679 return precharge;
6680}
6681
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006682static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6683{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006684 unsigned long precharge = mem_cgroup_count_precharge(mm);
6685
6686 VM_BUG_ON(mc.moving_task);
6687 mc.moving_task = current;
6688 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006689}
6690
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006691/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6692static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006693{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006694 struct mem_cgroup *from = mc.from;
6695 struct mem_cgroup *to = mc.to;
6696
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006697 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006698 if (mc.precharge) {
6699 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6700 mc.precharge = 0;
6701 }
6702 /*
6703 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6704 * we must uncharge here.
6705 */
6706 if (mc.moved_charge) {
6707 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6708 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006709 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006710 /* we must fixup refcnts and charges */
6711 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006712 /* uncharge swap account from the old cgroup */
6713 if (!mem_cgroup_is_root(mc.from))
6714 res_counter_uncharge(&mc.from->memsw,
6715 PAGE_SIZE * mc.moved_swap);
6716 __mem_cgroup_put(mc.from, mc.moved_swap);
6717
6718 if (!mem_cgroup_is_root(mc.to)) {
6719 /*
6720 * we charged both to->res and to->memsw, so we should
6721 * uncharge to->res.
6722 */
6723 res_counter_uncharge(&mc.to->res,
6724 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006725 }
6726 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006727 mc.moved_swap = 0;
6728 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006729 memcg_oom_recover(from);
6730 memcg_oom_recover(to);
6731 wake_up_all(&mc.waitq);
6732}
6733
6734static void mem_cgroup_clear_mc(void)
6735{
6736 struct mem_cgroup *from = mc.from;
6737
6738 /*
6739 * we must clear moving_task before waking up waiters at the end of
6740 * task migration.
6741 */
6742 mc.moving_task = NULL;
6743 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006744 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006745 mc.from = NULL;
6746 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006747 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006748 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006749}
6750
Li Zefan761b3ef2012-01-31 13:47:36 +08006751static int mem_cgroup_can_attach(struct cgroup *cgroup,
6752 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006753{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006754 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006755 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006756 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006757 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006758
Glauber Costaee5e8472013-02-22 16:34:50 -08006759 /*
6760 * We are now commited to this value whatever it is. Changes in this
6761 * tunable will only affect upcoming migrations, not the current one.
6762 * So we need to save it, and keep it going.
6763 */
6764 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6765 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006766 struct mm_struct *mm;
6767 struct mem_cgroup *from = mem_cgroup_from_task(p);
6768
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006769 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006770
6771 mm = get_task_mm(p);
6772 if (!mm)
6773 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006774 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006775 if (mm->owner == p) {
6776 VM_BUG_ON(mc.from);
6777 VM_BUG_ON(mc.to);
6778 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006779 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006780 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006781 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006782 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006783 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006784 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006785 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006786 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006787 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006788
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006789 ret = mem_cgroup_precharge_mc(mm);
6790 if (ret)
6791 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006792 }
6793 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006794 }
6795 return ret;
6796}
6797
Li Zefan761b3ef2012-01-31 13:47:36 +08006798static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6799 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006800{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006801 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006802}
6803
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006804static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6805 unsigned long addr, unsigned long end,
6806 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006807{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006808 int ret = 0;
6809 struct vm_area_struct *vma = walk->private;
6810 pte_t *pte;
6811 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006812 enum mc_target_type target_type;
6813 union mc_target target;
6814 struct page *page;
6815 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006816
Naoya Horiguchi12724852012-03-21 16:34:28 -07006817 /*
6818 * We don't take compound_lock() here but no race with splitting thp
6819 * happens because:
6820 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6821 * under splitting, which means there's no concurrent thp split,
6822 * - if another thread runs into split_huge_page() just after we
6823 * entered this if-block, the thread must wait for page table lock
6824 * to be unlocked in __split_huge_page_splitting(), where the main
6825 * part of thp split is not executed yet.
6826 */
6827 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006828 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006829 spin_unlock(&vma->vm_mm->page_table_lock);
6830 return 0;
6831 }
6832 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6833 if (target_type == MC_TARGET_PAGE) {
6834 page = target.page;
6835 if (!isolate_lru_page(page)) {
6836 pc = lookup_page_cgroup(page);
6837 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006838 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006839 mc.precharge -= HPAGE_PMD_NR;
6840 mc.moved_charge += HPAGE_PMD_NR;
6841 }
6842 putback_lru_page(page);
6843 }
6844 put_page(page);
6845 }
6846 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006847 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006848 }
6849
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006850 if (pmd_trans_unstable(pmd))
6851 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006852retry:
6853 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6854 for (; addr != end; addr += PAGE_SIZE) {
6855 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006856 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006857
6858 if (!mc.precharge)
6859 break;
6860
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006861 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006862 case MC_TARGET_PAGE:
6863 page = target.page;
6864 if (isolate_lru_page(page))
6865 goto put;
6866 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006867 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006868 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006869 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006870 /* we uncharge from mc.from later. */
6871 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006872 }
6873 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006874put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006875 put_page(page);
6876 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006877 case MC_TARGET_SWAP:
6878 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006879 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006880 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006881 /* we fixup refcnts and charges later. */
6882 mc.moved_swap++;
6883 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006884 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006885 default:
6886 break;
6887 }
6888 }
6889 pte_unmap_unlock(pte - 1, ptl);
6890 cond_resched();
6891
6892 if (addr != end) {
6893 /*
6894 * We have consumed all precharges we got in can_attach().
6895 * We try charge one by one, but don't do any additional
6896 * charges to mc.to if we have failed in charge once in attach()
6897 * phase.
6898 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006899 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006900 if (!ret)
6901 goto retry;
6902 }
6903
6904 return ret;
6905}
6906
6907static void mem_cgroup_move_charge(struct mm_struct *mm)
6908{
6909 struct vm_area_struct *vma;
6910
6911 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006912retry:
6913 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6914 /*
6915 * Someone who are holding the mmap_sem might be waiting in
6916 * waitq. So we cancel all extra charges, wake up all waiters,
6917 * and retry. Because we cancel precharges, we might not be able
6918 * to move enough charges, but moving charge is a best-effort
6919 * feature anyway, so it wouldn't be a big problem.
6920 */
6921 __mem_cgroup_clear_mc();
6922 cond_resched();
6923 goto retry;
6924 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006925 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6926 int ret;
6927 struct mm_walk mem_cgroup_move_charge_walk = {
6928 .pmd_entry = mem_cgroup_move_charge_pte_range,
6929 .mm = mm,
6930 .private = vma,
6931 };
6932 if (is_vm_hugetlb_page(vma))
6933 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006934 ret = walk_page_range(vma->vm_start, vma->vm_end,
6935 &mem_cgroup_move_charge_walk);
6936 if (ret)
6937 /*
6938 * means we have consumed all precharges and failed in
6939 * doing additional charge. Just abandon here.
6940 */
6941 break;
6942 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006943 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006944}
6945
Li Zefan761b3ef2012-01-31 13:47:36 +08006946static void mem_cgroup_move_task(struct cgroup *cont,
6947 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006948{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006949 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006950 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006951
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006952 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006953 if (mc.to)
6954 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006955 mmput(mm);
6956 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006957 if (mc.to)
6958 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006959}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006960#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006961static int mem_cgroup_can_attach(struct cgroup *cgroup,
6962 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006963{
6964 return 0;
6965}
Li Zefan761b3ef2012-01-31 13:47:36 +08006966static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6967 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006968{
6969}
Li Zefan761b3ef2012-01-31 13:47:36 +08006970static void mem_cgroup_move_task(struct cgroup *cont,
6971 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006972{
6973}
6974#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006975
Tejun Heof00baae2013-04-15 13:41:15 -07006976/*
6977 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6978 * to verify sane_behavior flag on each mount attempt.
6979 */
6980static void mem_cgroup_bind(struct cgroup *root)
6981{
6982 /*
6983 * use_hierarchy is forced with sane_behavior. cgroup core
6984 * guarantees that @root doesn't have any children, so turning it
6985 * on for the root memcg is enough.
6986 */
6987 if (cgroup_sane_behavior(root))
6988 mem_cgroup_from_cont(root)->use_hierarchy = true;
6989}
6990
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006991struct cgroup_subsys mem_cgroup_subsys = {
6992 .name = "memory",
6993 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006994 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006995 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006996 .css_offline = mem_cgroup_css_offline,
6997 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006998 .can_attach = mem_cgroup_can_attach,
6999 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08007000 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07007001 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07007002 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08007003 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07007004 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007005};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007006
Andrew Mortonc255a452012-07-31 16:43:02 -07007007#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08007008static int __init enable_swap_account(char *s)
7009{
7010 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07007011 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007012 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07007013 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007014 really_do_swap_account = 0;
7015 return 1;
7016}
Michal Hockoa2c89902011-05-24 17:12:50 -07007017__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007018
Michal Hocko2d110852013-02-22 16:34:43 -08007019static void __init memsw_file_init(void)
7020{
Michal Hocko6acc8b02013-02-22 16:34:45 -08007021 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08007022}
Michal Hocko6acc8b02013-02-22 16:34:45 -08007023
7024static void __init enable_swap_cgroup(void)
7025{
7026 if (!mem_cgroup_disabled() && really_do_swap_account) {
7027 do_swap_account = 1;
7028 memsw_file_init();
7029 }
7030}
7031
Michal Hocko2d110852013-02-22 16:34:43 -08007032#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08007033static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08007034{
7035}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007036#endif
Michal Hocko2d110852013-02-22 16:34:43 -08007037
7038/*
Michal Hocko10813122013-02-22 16:35:41 -08007039 * subsys_initcall() for memory controller.
7040 *
7041 * Some parts like hotcpu_notifier() have to be initialized from this context
7042 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
7043 * everything that doesn't depend on a specific mem_cgroup structure should
7044 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08007045 */
7046static int __init mem_cgroup_init(void)
7047{
7048 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08007049 enable_swap_cgroup();
Michal Hocko8787a1d2013-02-22 16:35:39 -08007050 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08007051 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08007052 return 0;
7053}
7054subsys_initcall(mem_cgroup_init);