blob: 7849660665d711f1eedfe97c5ebd66975afa34cb [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
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002194/*
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002195 * try to call OOM killer
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002196 */
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002197static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002198{
Johannes Weiner7a147e02013-09-12 15:13:43 -07002199 bool locked;
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002200 int wakeups;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002201
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002202 if (!current->memcg_oom.may_oom)
2203 return;
2204
2205 current->memcg_oom.in_memcg_oom = 1;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002206
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002207 /*
Johannes Weiner7a147e02013-09-12 15:13:43 -07002208 * As with any blocking lock, a contender needs to start
2209 * listening for wakeups before attempting the trylock,
2210 * otherwise it can miss the wakeup from the unlock and sleep
2211 * indefinitely. This is just open-coded because our locking
2212 * is so particular to memcg hierarchies.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002213 */
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002214 wakeups = atomic_read(&memcg->oom_wakeups);
Johannes Weiner7a147e02013-09-12 15:13:43 -07002215 mem_cgroup_mark_under_oom(memcg);
2216
2217 locked = mem_cgroup_oom_trylock(memcg);
2218
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002219 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002220 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002221
Johannes Weiner7a147e02013-09-12 15:13:43 -07002222 if (locked && !memcg->oom_kill_disable) {
2223 mem_cgroup_unmark_under_oom(memcg);
David Rientjese845e192012-03-21 16:34:10 -07002224 mem_cgroup_out_of_memory(memcg, mask, order);
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002225 mem_cgroup_oom_unlock(memcg);
2226 /*
2227 * There is no guarantee that an OOM-lock contender
2228 * sees the wakeups triggered by the OOM kill
2229 * uncharges. Wake any sleepers explicitely.
2230 */
2231 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002232 } else {
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002233 /*
2234 * A system call can just return -ENOMEM, but if this
2235 * is a page fault and somebody else is handling the
2236 * OOM already, we need to sleep on the OOM waitqueue
2237 * for this memcg until the situation is resolved.
2238 * Which can take some time because it might be
2239 * handled by a userspace task.
2240 *
2241 * However, this is the charge context, which means
2242 * that we may sit on a large call stack and hold
2243 * various filesystem locks, the mmap_sem etc. and we
2244 * don't want the OOM handler to deadlock on them
2245 * while we sit here and wait. Store the current OOM
2246 * context in the task_struct, then return -ENOMEM.
2247 * At the end of the page fault handler, with the
2248 * stack unwound, pagefault_out_of_memory() will check
2249 * back with us by calling
2250 * mem_cgroup_oom_synchronize(), possibly putting the
2251 * task to sleep.
2252 */
2253 current->memcg_oom.oom_locked = locked;
2254 current->memcg_oom.wakeups = wakeups;
2255 css_get(&memcg->css);
2256 current->memcg_oom.wait_on_memcg = memcg;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002257 }
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002258}
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002259
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002260/**
2261 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2262 *
2263 * This has to be called at the end of a page fault if the the memcg
2264 * OOM handler was enabled and the fault is returning %VM_FAULT_OOM.
2265 *
2266 * Memcg supports userspace OOM handling, so failed allocations must
2267 * sleep on a waitqueue until the userspace task resolves the
2268 * situation. Sleeping directly in the charge context with all kinds
2269 * of locks held is not a good idea, instead we remember an OOM state
2270 * in the task and mem_cgroup_oom_synchronize() has to be called at
2271 * the end of the page fault to put the task to sleep and clean up the
2272 * OOM state.
2273 *
2274 * Returns %true if an ongoing memcg OOM situation was detected and
2275 * finalized, %false otherwise.
2276 */
2277bool mem_cgroup_oom_synchronize(void)
2278{
2279 struct oom_wait_info owait;
2280 struct mem_cgroup *memcg;
2281
2282 /* OOM is global, do not handle */
2283 if (!current->memcg_oom.in_memcg_oom)
2284 return false;
2285
2286 /*
2287 * We invoked the OOM killer but there is a chance that a kill
2288 * did not free up any charges. Everybody else might already
2289 * be sleeping, so restart the fault and keep the rampage
2290 * going until some charges are released.
2291 */
2292 memcg = current->memcg_oom.wait_on_memcg;
2293 if (!memcg)
2294 goto out;
2295
2296 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2297 goto out_memcg;
2298
2299 owait.memcg = memcg;
2300 owait.wait.flags = 0;
2301 owait.wait.func = memcg_oom_wake_function;
2302 owait.wait.private = current;
2303 INIT_LIST_HEAD(&owait.wait.task_list);
2304
2305 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
2306 /* Only sleep if we didn't miss any wakeups since OOM */
2307 if (atomic_read(&memcg->oom_wakeups) == current->memcg_oom.wakeups)
2308 schedule();
2309 finish_wait(&memcg_oom_waitq, &owait.wait);
2310out_memcg:
2311 mem_cgroup_unmark_under_oom(memcg);
2312 if (current->memcg_oom.oom_locked) {
Johannes Weiner7a147e02013-09-12 15:13:43 -07002313 mem_cgroup_oom_unlock(memcg);
2314 /*
2315 * There is no guarantee that an OOM-lock contender
2316 * sees the wakeups triggered by the OOM kill
2317 * uncharges. Wake any sleepers explicitely.
2318 */
2319 memcg_oom_recover(memcg);
2320 }
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002321 css_put(&memcg->css);
2322 current->memcg_oom.wait_on_memcg = NULL;
2323out:
2324 current->memcg_oom.in_memcg_oom = 0;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002325 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002326}
2327
Balbir Singhd69b0422009-06-17 16:26:34 -07002328/*
2329 * Currently used to update mapped file statistics, but the routine can be
2330 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002331 *
2332 * Notes: Race condition
2333 *
2334 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2335 * it tends to be costly. But considering some conditions, we doesn't need
2336 * to do so _always_.
2337 *
2338 * Considering "charge", lock_page_cgroup() is not required because all
2339 * file-stat operations happen after a page is attached to radix-tree. There
2340 * are no race with "charge".
2341 *
2342 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2343 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2344 * if there are race with "uncharge". Statistics itself is properly handled
2345 * by flags.
2346 *
2347 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002348 * small, we check mm->moving_account and detect there are possibility of race
2349 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002350 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002351
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002352void __mem_cgroup_begin_update_page_stat(struct page *page,
2353 bool *locked, unsigned long *flags)
2354{
2355 struct mem_cgroup *memcg;
2356 struct page_cgroup *pc;
2357
2358 pc = lookup_page_cgroup(page);
2359again:
2360 memcg = pc->mem_cgroup;
2361 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2362 return;
2363 /*
2364 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002365 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002366 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002367 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002368 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002369 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002370 return;
2371
2372 move_lock_mem_cgroup(memcg, flags);
2373 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2374 move_unlock_mem_cgroup(memcg, flags);
2375 goto again;
2376 }
2377 *locked = true;
2378}
2379
2380void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2381{
2382 struct page_cgroup *pc = lookup_page_cgroup(page);
2383
2384 /*
2385 * It's guaranteed that pc->mem_cgroup never changes while
2386 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002387 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002388 */
2389 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2390}
2391
Greg Thelen2a7106f2011-01-13 15:47:37 -08002392void mem_cgroup_update_page_stat(struct page *page,
2393 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002394{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002396 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002397 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002398
Johannes Weinercfa44942012-01-12 17:18:38 -08002399 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002400 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002401
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 memcg = pc->mem_cgroup;
2403 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002404 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002405
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002406 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002407 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002408 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002409 break;
2410 default:
2411 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002412 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002414 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002415}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002416
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002417/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002418 * size of first charge trial. "32" comes from vmscan.c's magic value.
2419 * TODO: maybe necessary to use big numbers in big irons.
2420 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002421#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002422struct memcg_stock_pcp {
2423 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002424 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002425 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002426 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002427#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002428};
2429static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002430static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002431
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002432/**
2433 * consume_stock: Try to consume stocked charge on this cpu.
2434 * @memcg: memcg to consume from.
2435 * @nr_pages: how many pages to charge.
2436 *
2437 * The charges will only happen if @memcg matches the current cpu's memcg
2438 * stock, and at least @nr_pages are available in that stock. Failure to
2439 * service an allocation will refill the stock.
2440 *
2441 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002442 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002443static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002444{
2445 struct memcg_stock_pcp *stock;
2446 bool ret = true;
2447
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002448 if (nr_pages > CHARGE_BATCH)
2449 return false;
2450
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002451 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002452 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2453 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002454 else /* need to call res_counter_charge */
2455 ret = false;
2456 put_cpu_var(memcg_stock);
2457 return ret;
2458}
2459
2460/*
2461 * Returns stocks cached in percpu to res_counter and reset cached information.
2462 */
2463static void drain_stock(struct memcg_stock_pcp *stock)
2464{
2465 struct mem_cgroup *old = stock->cached;
2466
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002467 if (stock->nr_pages) {
2468 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2469
2470 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002471 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002472 res_counter_uncharge(&old->memsw, bytes);
2473 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002474 }
2475 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002476}
2477
2478/*
2479 * This must be called under preempt disabled or must be called by
2480 * a thread which is pinned to local cpu.
2481 */
2482static void drain_local_stock(struct work_struct *dummy)
2483{
2484 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2485 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002486 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002487}
2488
Michal Hockoe4777492013-02-22 16:35:40 -08002489static void __init memcg_stock_init(void)
2490{
2491 int cpu;
2492
2493 for_each_possible_cpu(cpu) {
2494 struct memcg_stock_pcp *stock =
2495 &per_cpu(memcg_stock, cpu);
2496 INIT_WORK(&stock->work, drain_local_stock);
2497 }
2498}
2499
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002500/*
2501 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002502 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002503 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002504static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002505{
2506 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2507
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002509 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002510 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002511 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002512 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002513 put_cpu_var(memcg_stock);
2514}
2515
2516/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002517 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002518 * of the hierarchy under it. sync flag says whether we should block
2519 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002520 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002521static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002522{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002523 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002524
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002525 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002526 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002527 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002528 for_each_online_cpu(cpu) {
2529 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002530 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002531
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002532 memcg = stock->cached;
2533 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002534 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002535 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002536 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002537 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2538 if (cpu == curcpu)
2539 drain_local_stock(&stock->work);
2540 else
2541 schedule_work_on(cpu, &stock->work);
2542 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002543 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002544 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002545
2546 if (!sync)
2547 goto out;
2548
2549 for_each_online_cpu(cpu) {
2550 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002551 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002552 flush_work(&stock->work);
2553 }
2554out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002555 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002556}
2557
2558/*
2559 * Tries to drain stocked charges in other cpus. This function is asynchronous
2560 * and just put a work per cpu for draining localy on each cpu. Caller can
2561 * expects some charges will be back to res_counter later but cannot wait for
2562 * it.
2563 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002564static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002565{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002566 /*
2567 * If someone calls draining, avoid adding more kworker runs.
2568 */
2569 if (!mutex_trylock(&percpu_charge_mutex))
2570 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002572 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002573}
2574
2575/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002576static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002577{
2578 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002579 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002580 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002581 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002582}
2583
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002584/*
2585 * This function drains percpu counter value from DEAD cpu and
2586 * move it to local cpu. Note that this function can be preempted.
2587 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002588static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002589{
2590 int i;
2591
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002592 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002593 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002594 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002595
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002596 per_cpu(memcg->stat->count[i], cpu) = 0;
2597 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002598 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002599 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002600 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002601
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002602 per_cpu(memcg->stat->events[i], cpu) = 0;
2603 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002604 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002605 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002606}
2607
2608static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002609 unsigned long action,
2610 void *hcpu)
2611{
2612 int cpu = (unsigned long)hcpu;
2613 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002614 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002615
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002616 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002617 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002618
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002619 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002620 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002621
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002622 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002623 mem_cgroup_drain_pcp_counter(iter, cpu);
2624
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002625 stock = &per_cpu(memcg_stock, cpu);
2626 drain_stock(stock);
2627 return NOTIFY_OK;
2628}
2629
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002630
2631/* See __mem_cgroup_try_charge() for details */
2632enum {
2633 CHARGE_OK, /* success */
2634 CHARGE_RETRY, /* need to retry but retry is not bad */
2635 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2636 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002637};
2638
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002639static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002640 unsigned int nr_pages, unsigned int min_pages,
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002641 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002642{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002643 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002644 struct mem_cgroup *mem_over_limit;
2645 struct res_counter *fail_res;
2646 unsigned long flags = 0;
2647 int ret;
2648
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002649 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002650
2651 if (likely(!ret)) {
2652 if (!do_swap_account)
2653 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002654 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002655 if (likely(!ret))
2656 return CHARGE_OK;
2657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002658 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002659 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2660 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2661 } else
2662 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002663 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002664 * Never reclaim on behalf of optional batching, retry with a
2665 * single page instead.
2666 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002667 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002668 return CHARGE_RETRY;
2669
2670 if (!(gfp_mask & __GFP_WAIT))
2671 return CHARGE_WOULDBLOCK;
2672
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002673 if (gfp_mask & __GFP_NORETRY)
2674 return CHARGE_NOMEM;
2675
Johannes Weiner56600482012-01-12 17:17:59 -08002676 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002677 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002678 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002679 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002680 * Even though the limit is exceeded at this point, reclaim
2681 * may have been able to free some pages. Retry the charge
2682 * before killing the task.
2683 *
2684 * Only for regular pages, though: huge pages are rather
2685 * unlikely to succeed so close to the limit, and we fall back
2686 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002687 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002688 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002689 return CHARGE_RETRY;
2690
2691 /*
2692 * At task move, charge accounts can be doubly counted. So, it's
2693 * better to wait until the end of task_move if something is going on.
2694 */
2695 if (mem_cgroup_wait_acct_move(mem_over_limit))
2696 return CHARGE_RETRY;
2697
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002698 if (invoke_oom)
2699 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002700
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002701 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002702}
2703
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002704/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002705 * __mem_cgroup_try_charge() does
2706 * 1. detect memcg to be charged against from passed *mm and *ptr,
2707 * 2. update res_counter
2708 * 3. call memory reclaim if necessary.
2709 *
2710 * In some special case, if the task is fatal, fatal_signal_pending() or
2711 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2712 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2713 * as possible without any hazards. 2: all pages should have a valid
2714 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2715 * pointer, that is treated as a charge to root_mem_cgroup.
2716 *
2717 * So __mem_cgroup_try_charge() will return
2718 * 0 ... on success, filling *ptr with a valid memcg pointer.
2719 * -ENOMEM ... charge failure because of resource limits.
2720 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2721 *
2722 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2723 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002724 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002725static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002726 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002727 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002728 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002729 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002730{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002731 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002732 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002733 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002734 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002735
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002736 /*
2737 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2738 * in system level. So, allow to go ahead dying process in addition to
2739 * MEMDIE process.
2740 */
2741 if (unlikely(test_thread_flag(TIF_MEMDIE)
2742 || fatal_signal_pending(current)))
2743 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002744
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002745 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002746 * We always charge the cgroup the mm_struct belongs to.
2747 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002748 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002749 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002750 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002751 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002752 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002753again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002754 if (*ptr) { /* css should be a valid one */
2755 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002756 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002757 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002758 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002759 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002760 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002761 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002762 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002763
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002764 rcu_read_lock();
2765 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002766 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002767 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002768 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002769 * race with swapoff. Then, we have small risk of mis-accouning.
2770 * But such kind of mis-account by race always happens because
2771 * we don't have cgroup_mutex(). It's overkill and we allo that
2772 * small race, here.
2773 * (*) swapoff at el will charge against mm-struct not against
2774 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002775 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002776 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002777 if (!memcg)
2778 memcg = root_mem_cgroup;
2779 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002780 rcu_read_unlock();
2781 goto done;
2782 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002783 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002784 /*
2785 * It seems dagerous to access memcg without css_get().
2786 * But considering how consume_stok works, it's not
2787 * necessary. If consume_stock success, some charges
2788 * from this memcg are cached on this cpu. So, we
2789 * don't need to call css_get()/css_tryget() before
2790 * calling consume_stock().
2791 */
2792 rcu_read_unlock();
2793 goto done;
2794 }
2795 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002796 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002797 rcu_read_unlock();
2798 goto again;
2799 }
2800 rcu_read_unlock();
2801 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002802
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002803 do {
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002804 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002805
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002806 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002807 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002808 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002809 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002810 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002811
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002812 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2813 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002814 switch (ret) {
2815 case CHARGE_OK:
2816 break;
2817 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002818 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002819 css_put(&memcg->css);
2820 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002821 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002822 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002823 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002824 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002825 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weinerf79d6a42013-09-12 15:13:44 -07002826 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002827 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002828 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002829 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002830 nr_oom_retries--;
2831 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002832 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002833 } while (ret != CHARGE_OK);
2834
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002835 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002836 refill_stock(memcg, batch - nr_pages);
2837 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002838done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002839 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002840 return 0;
2841nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002842 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002843 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002844bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002845 *ptr = root_mem_cgroup;
2846 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002847}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002848
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002849/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002850 * Somemtimes we have to undo a charge we got by try_charge().
2851 * This function is for that and do uncharge, put css's refcnt.
2852 * gotten by try_charge().
2853 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002854static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002855 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002856{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002857 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002858 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002860 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002861 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002862 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002863 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002864}
2865
2866/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002867 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2868 * This is useful when moving usage to parent cgroup.
2869 */
2870static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2871 unsigned int nr_pages)
2872{
2873 unsigned long bytes = nr_pages * PAGE_SIZE;
2874
2875 if (mem_cgroup_is_root(memcg))
2876 return;
2877
2878 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2879 if (do_swap_account)
2880 res_counter_uncharge_until(&memcg->memsw,
2881 memcg->memsw.parent, bytes);
2882}
2883
2884/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002885 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002886 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2887 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2888 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002889 */
2890static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2891{
2892 struct cgroup_subsys_state *css;
2893
2894 /* ID 0 is unused ID */
2895 if (!id)
2896 return NULL;
2897 css = css_lookup(&mem_cgroup_subsys, id);
2898 if (!css)
2899 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002900 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002901}
2902
Wu Fengguange42d9d52009-12-16 12:19:59 +01002903struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002904{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002905 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002906 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002907 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002908 swp_entry_t ent;
2909
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002910 VM_BUG_ON(!PageLocked(page));
2911
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002912 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002913 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002914 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002915 memcg = pc->mem_cgroup;
2916 if (memcg && !css_tryget(&memcg->css))
2917 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002918 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002919 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002920 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002921 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002922 memcg = mem_cgroup_lookup(id);
2923 if (memcg && !css_tryget(&memcg->css))
2924 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002925 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002926 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002927 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002928 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002929}
2930
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002931static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002932 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002933 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002934 enum charge_type ctype,
2935 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002936{
Johannes Weinerce587e62012-04-24 20:22:33 +02002937 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002938 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002939 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002940 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002941 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002942
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002943 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002944 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002945 /*
2946 * we don't need page_cgroup_lock about tail pages, becase they are not
2947 * accessed by any other context at this point.
2948 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002949
2950 /*
2951 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2952 * may already be on some other mem_cgroup's LRU. Take care of it.
2953 */
2954 if (lrucare) {
2955 zone = page_zone(page);
2956 spin_lock_irq(&zone->lru_lock);
2957 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002958 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002959 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002960 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002961 was_on_lru = true;
2962 }
2963 }
2964
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002965 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002966 /*
2967 * We access a page_cgroup asynchronously without lock_page_cgroup().
2968 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2969 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2970 * before USED bit, we need memory barrier here.
2971 * See mem_cgroup_add_lru_list(), etc.
2972 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002973 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002974 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002975
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002976 if (lrucare) {
2977 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002978 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002979 VM_BUG_ON(PageLRU(page));
2980 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002981 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002982 }
2983 spin_unlock_irq(&zone->lru_lock);
2984 }
2985
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002986 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002987 anon = true;
2988 else
2989 anon = false;
2990
David Rientjesb070e652013-05-07 16:18:09 -07002991 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002992 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002993
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002994 /*
2995 * "charge_statistics" updated event counter. Then, check it.
2996 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2997 * if they exceeds softlimit.
2998 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002999 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003000}
3001
Glauber Costa7cf27982012-12-18 14:22:55 -08003002static DEFINE_MUTEX(set_limit_mutex);
3003
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003004#ifdef CONFIG_MEMCG_KMEM
3005static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
3006{
3007 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
3008 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
3009}
3010
Glauber Costa1f458cb2012-12-18 14:22:50 -08003011/*
3012 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
3013 * in the memcg_cache_params struct.
3014 */
3015static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
3016{
3017 struct kmem_cache *cachep;
3018
3019 VM_BUG_ON(p->is_root_cache);
3020 cachep = p->root_cache;
3021 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
3022}
3023
Glauber Costa749c5412012-12-18 14:23:01 -08003024#ifdef CONFIG_SLABINFO
3025static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
3026 struct seq_file *m)
3027{
3028 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
3029 struct memcg_cache_params *params;
3030
3031 if (!memcg_can_account_kmem(memcg))
3032 return -EIO;
3033
3034 print_slabinfo_header(m);
3035
3036 mutex_lock(&memcg->slab_caches_mutex);
3037 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
3038 cache_show(memcg_params_to_cache(params), m);
3039 mutex_unlock(&memcg->slab_caches_mutex);
3040
3041 return 0;
3042}
3043#endif
3044
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003045static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
3046{
3047 struct res_counter *fail_res;
3048 struct mem_cgroup *_memcg;
3049 int ret = 0;
3050 bool may_oom;
3051
3052 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
3053 if (ret)
3054 return ret;
3055
3056 /*
3057 * Conditions under which we can wait for the oom_killer. Those are
3058 * the same conditions tested by the core page allocator
3059 */
3060 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
3061
3062 _memcg = memcg;
3063 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
3064 &_memcg, may_oom);
3065
3066 if (ret == -EINTR) {
3067 /*
3068 * __mem_cgroup_try_charge() chosed to bypass to root due to
3069 * OOM kill or fatal signal. Since our only options are to
3070 * either fail the allocation or charge it to this cgroup, do
3071 * it as a temporary condition. But we can't fail. From a
3072 * kmem/slab perspective, the cache has already been selected,
3073 * by mem_cgroup_kmem_get_cache(), so it is too late to change
3074 * our minds.
3075 *
3076 * This condition will only trigger if the task entered
3077 * memcg_charge_kmem in a sane state, but was OOM-killed during
3078 * __mem_cgroup_try_charge() above. Tasks that were already
3079 * dying when the allocation triggers should have been already
3080 * directed to the root cgroup in memcontrol.h
3081 */
3082 res_counter_charge_nofail(&memcg->res, size, &fail_res);
3083 if (do_swap_account)
3084 res_counter_charge_nofail(&memcg->memsw, size,
3085 &fail_res);
3086 ret = 0;
3087 } else if (ret)
3088 res_counter_uncharge(&memcg->kmem, size);
3089
3090 return ret;
3091}
3092
3093static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
3094{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003095 res_counter_uncharge(&memcg->res, size);
3096 if (do_swap_account)
3097 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08003098
3099 /* Not down to 0 */
3100 if (res_counter_uncharge(&memcg->kmem, size))
3101 return;
3102
3103 if (memcg_kmem_test_and_clear_dead(memcg))
3104 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003105}
3106
Glauber Costa2633d7a2012-12-18 14:22:34 -08003107void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
3108{
3109 if (!memcg)
3110 return;
3111
3112 mutex_lock(&memcg->slab_caches_mutex);
3113 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
3114 mutex_unlock(&memcg->slab_caches_mutex);
3115}
3116
3117/*
3118 * helper for acessing a memcg's index. It will be used as an index in the
3119 * child cache array in kmem_cache, and also to derive its name. This function
3120 * will return -1 when this is not a kmem-limited memcg.
3121 */
3122int memcg_cache_id(struct mem_cgroup *memcg)
3123{
3124 return memcg ? memcg->kmemcg_id : -1;
3125}
3126
Glauber Costa55007d82012-12-18 14:22:38 -08003127/*
3128 * This ends up being protected by the set_limit mutex, during normal
3129 * operation, because that is its main call site.
3130 *
3131 * But when we create a new cache, we can call this as well if its parent
3132 * is kmem-limited. That will have to hold set_limit_mutex as well.
3133 */
3134int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3135{
3136 int num, ret;
3137
3138 num = ida_simple_get(&kmem_limited_groups,
3139 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3140 if (num < 0)
3141 return num;
3142 /*
3143 * After this point, kmem_accounted (that we test atomically in
3144 * the beginning of this conditional), is no longer 0. This
3145 * guarantees only one process will set the following boolean
3146 * to true. We don't need test_and_set because we're protected
3147 * by the set_limit_mutex anyway.
3148 */
3149 memcg_kmem_set_activated(memcg);
3150
3151 ret = memcg_update_all_caches(num+1);
3152 if (ret) {
3153 ida_simple_remove(&kmem_limited_groups, num);
3154 memcg_kmem_clear_activated(memcg);
3155 return ret;
3156 }
3157
3158 memcg->kmemcg_id = num;
3159 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3160 mutex_init(&memcg->slab_caches_mutex);
3161 return 0;
3162}
3163
3164static size_t memcg_caches_array_size(int num_groups)
3165{
3166 ssize_t size;
3167 if (num_groups <= 0)
3168 return 0;
3169
3170 size = 2 * num_groups;
3171 if (size < MEMCG_CACHES_MIN_SIZE)
3172 size = MEMCG_CACHES_MIN_SIZE;
3173 else if (size > MEMCG_CACHES_MAX_SIZE)
3174 size = MEMCG_CACHES_MAX_SIZE;
3175
3176 return size;
3177}
3178
3179/*
3180 * We should update the current array size iff all caches updates succeed. This
3181 * can only be done from the slab side. The slab mutex needs to be held when
3182 * calling this.
3183 */
3184void memcg_update_array_size(int num)
3185{
3186 if (num > memcg_limited_groups_array_size)
3187 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3188}
3189
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003190static void kmem_cache_destroy_work_func(struct work_struct *w);
3191
Glauber Costa55007d82012-12-18 14:22:38 -08003192int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3193{
3194 struct memcg_cache_params *cur_params = s->memcg_params;
3195
3196 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3197
3198 if (num_groups > memcg_limited_groups_array_size) {
3199 int i;
3200 ssize_t size = memcg_caches_array_size(num_groups);
3201
3202 size *= sizeof(void *);
3203 size += sizeof(struct memcg_cache_params);
3204
3205 s->memcg_params = kzalloc(size, GFP_KERNEL);
3206 if (!s->memcg_params) {
3207 s->memcg_params = cur_params;
3208 return -ENOMEM;
3209 }
3210
3211 s->memcg_params->is_root_cache = true;
3212
3213 /*
3214 * There is the chance it will be bigger than
3215 * memcg_limited_groups_array_size, if we failed an allocation
3216 * in a cache, in which case all caches updated before it, will
3217 * have a bigger array.
3218 *
3219 * But if that is the case, the data after
3220 * memcg_limited_groups_array_size is certainly unused
3221 */
3222 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3223 if (!cur_params->memcg_caches[i])
3224 continue;
3225 s->memcg_params->memcg_caches[i] =
3226 cur_params->memcg_caches[i];
3227 }
3228
3229 /*
3230 * Ideally, we would wait until all caches succeed, and only
3231 * then free the old one. But this is not worth the extra
3232 * pointer per-cache we'd have to have for this.
3233 *
3234 * It is not a big deal if some caches are left with a size
3235 * bigger than the others. And all updates will reset this
3236 * anyway.
3237 */
3238 kfree(cur_params);
3239 }
3240 return 0;
3241}
3242
Glauber Costa943a4512012-12-18 14:23:03 -08003243int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3244 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003245{
3246 size_t size = sizeof(struct memcg_cache_params);
3247
3248 if (!memcg_kmem_enabled())
3249 return 0;
3250
Glauber Costa55007d82012-12-18 14:22:38 -08003251 if (!memcg)
3252 size += memcg_limited_groups_array_size * sizeof(void *);
3253
Glauber Costa2633d7a2012-12-18 14:22:34 -08003254 s->memcg_params = kzalloc(size, GFP_KERNEL);
3255 if (!s->memcg_params)
3256 return -ENOMEM;
3257
Glauber Costa943a4512012-12-18 14:23:03 -08003258 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003259 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003260 s->memcg_params->root_cache = root_cache;
Andrey Vagin0dcf19b2013-08-13 16:00:47 -07003261 INIT_WORK(&s->memcg_params->destroy,
3262 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003263 } else
3264 s->memcg_params->is_root_cache = true;
3265
Glauber Costa2633d7a2012-12-18 14:22:34 -08003266 return 0;
3267}
3268
3269void memcg_release_cache(struct kmem_cache *s)
3270{
Glauber Costad7f25f82012-12-18 14:22:40 -08003271 struct kmem_cache *root;
3272 struct mem_cgroup *memcg;
3273 int id;
3274
3275 /*
3276 * This happens, for instance, when a root cache goes away before we
3277 * add any memcg.
3278 */
3279 if (!s->memcg_params)
3280 return;
3281
3282 if (s->memcg_params->is_root_cache)
3283 goto out;
3284
3285 memcg = s->memcg_params->memcg;
3286 id = memcg_cache_id(memcg);
3287
3288 root = s->memcg_params->root_cache;
3289 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003290
3291 mutex_lock(&memcg->slab_caches_mutex);
3292 list_del(&s->memcg_params->list);
3293 mutex_unlock(&memcg->slab_caches_mutex);
3294
Li Zefanfd0ccaf2013-04-29 15:08:43 -07003295 mem_cgroup_put(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08003296out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003297 kfree(s->memcg_params);
3298}
3299
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003300/*
3301 * During the creation a new cache, we need to disable our accounting mechanism
3302 * altogether. This is true even if we are not creating, but rather just
3303 * enqueing new caches to be created.
3304 *
3305 * This is because that process will trigger allocations; some visible, like
3306 * explicit kmallocs to auxiliary data structures, name strings and internal
3307 * cache structures; some well concealed, like INIT_WORK() that can allocate
3308 * objects during debug.
3309 *
3310 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3311 * to it. This may not be a bounded recursion: since the first cache creation
3312 * failed to complete (waiting on the allocation), we'll just try to create the
3313 * cache again, failing at the same point.
3314 *
3315 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3316 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3317 * inside the following two functions.
3318 */
3319static inline void memcg_stop_kmem_account(void)
3320{
3321 VM_BUG_ON(!current->mm);
3322 current->memcg_kmem_skip_account++;
3323}
3324
3325static inline void memcg_resume_kmem_account(void)
3326{
3327 VM_BUG_ON(!current->mm);
3328 current->memcg_kmem_skip_account--;
3329}
3330
Glauber Costa1f458cb2012-12-18 14:22:50 -08003331static void kmem_cache_destroy_work_func(struct work_struct *w)
3332{
3333 struct kmem_cache *cachep;
3334 struct memcg_cache_params *p;
3335
3336 p = container_of(w, struct memcg_cache_params, destroy);
3337
3338 cachep = memcg_params_to_cache(p);
3339
Glauber Costa22933152012-12-18 14:22:59 -08003340 /*
3341 * If we get down to 0 after shrink, we could delete right away.
3342 * However, memcg_release_pages() already puts us back in the workqueue
3343 * in that case. If we proceed deleting, we'll get a dangling
3344 * reference, and removing the object from the workqueue in that case
3345 * is unnecessary complication. We are not a fast path.
3346 *
3347 * Note that this case is fundamentally different from racing with
3348 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3349 * kmem_cache_shrink, not only we would be reinserting a dead cache
3350 * into the queue, but doing so from inside the worker racing to
3351 * destroy it.
3352 *
3353 * So if we aren't down to zero, we'll just schedule a worker and try
3354 * again
3355 */
3356 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3357 kmem_cache_shrink(cachep);
3358 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3359 return;
3360 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003361 kmem_cache_destroy(cachep);
3362}
3363
3364void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3365{
3366 if (!cachep->memcg_params->dead)
3367 return;
3368
3369 /*
Glauber Costa22933152012-12-18 14:22:59 -08003370 * There are many ways in which we can get here.
3371 *
3372 * We can get to a memory-pressure situation while the delayed work is
3373 * still pending to run. The vmscan shrinkers can then release all
3374 * cache memory and get us to destruction. If this is the case, we'll
3375 * be executed twice, which is a bug (the second time will execute over
3376 * bogus data). In this case, cancelling the work should be fine.
3377 *
3378 * But we can also get here from the worker itself, if
3379 * kmem_cache_shrink is enough to shake all the remaining objects and
3380 * get the page count to 0. In this case, we'll deadlock if we try to
3381 * cancel the work (the worker runs with an internal lock held, which
3382 * is the same lock we would hold for cancel_work_sync().)
3383 *
3384 * Since we can't possibly know who got us here, just refrain from
3385 * running if there is already work pending
3386 */
3387 if (work_pending(&cachep->memcg_params->destroy))
3388 return;
3389 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003390 * We have to defer the actual destroying to a workqueue, because
3391 * we might currently be in a context that cannot sleep.
3392 */
3393 schedule_work(&cachep->memcg_params->destroy);
3394}
3395
Glauber Costad7f25f82012-12-18 14:22:40 -08003396/*
3397 * This lock protects updaters, not readers. We want readers to be as fast as
3398 * they can, and they will either see NULL or a valid cache value. Our model
3399 * allow them to see NULL, in which case the root memcg will be selected.
3400 *
3401 * We need this lock because multiple allocations to the same cache from a non
3402 * will span more than one worker. Only one of them can create the cache.
3403 */
3404static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003405
3406/*
3407 * Called with memcg_cache_mutex held
3408 */
3409static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3410 struct kmem_cache *s)
3411{
3412 struct kmem_cache *new;
3413 static char *tmp_name = NULL;
3414
3415 lockdep_assert_held(&memcg_cache_mutex);
3416
3417 /*
3418 * kmem_cache_create_memcg duplicates the given name and
3419 * cgroup_name for this name requires RCU context.
3420 * This static temporary buffer is used to prevent from
3421 * pointless shortliving allocation.
3422 */
3423 if (!tmp_name) {
3424 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3425 if (!tmp_name)
3426 return NULL;
3427 }
3428
3429 rcu_read_lock();
3430 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3431 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3432 rcu_read_unlock();
3433
3434 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3435 (s->flags & ~SLAB_PANIC), s->ctor, s);
3436
3437 if (new)
3438 new->allocflags |= __GFP_KMEMCG;
3439
3440 return new;
3441}
3442
Glauber Costad7f25f82012-12-18 14:22:40 -08003443static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3444 struct kmem_cache *cachep)
3445{
3446 struct kmem_cache *new_cachep;
3447 int idx;
3448
3449 BUG_ON(!memcg_can_account_kmem(memcg));
3450
3451 idx = memcg_cache_id(memcg);
3452
3453 mutex_lock(&memcg_cache_mutex);
3454 new_cachep = cachep->memcg_params->memcg_caches[idx];
3455 if (new_cachep)
3456 goto out;
3457
3458 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003459 if (new_cachep == NULL) {
3460 new_cachep = cachep;
3461 goto out;
3462 }
3463
3464 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003465 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003466
3467 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3468 /*
3469 * the readers won't lock, make sure everybody sees the updated value,
3470 * so they won't put stuff in the queue again for no reason
3471 */
3472 wmb();
3473out:
3474 mutex_unlock(&memcg_cache_mutex);
3475 return new_cachep;
3476}
3477
Glauber Costa7cf27982012-12-18 14:22:55 -08003478void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3479{
3480 struct kmem_cache *c;
3481 int i;
3482
3483 if (!s->memcg_params)
3484 return;
3485 if (!s->memcg_params->is_root_cache)
3486 return;
3487
3488 /*
3489 * If the cache is being destroyed, we trust that there is no one else
3490 * requesting objects from it. Even if there are, the sanity checks in
3491 * kmem_cache_destroy should caught this ill-case.
3492 *
3493 * Still, we don't want anyone else freeing memcg_caches under our
3494 * noses, which can happen if a new memcg comes to life. As usual,
3495 * we'll take the set_limit_mutex to protect ourselves against this.
3496 */
3497 mutex_lock(&set_limit_mutex);
3498 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3499 c = s->memcg_params->memcg_caches[i];
3500 if (!c)
3501 continue;
3502
3503 /*
3504 * We will now manually delete the caches, so to avoid races
3505 * we need to cancel all pending destruction workers and
3506 * proceed with destruction ourselves.
3507 *
3508 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3509 * and that could spawn the workers again: it is likely that
3510 * the cache still have active pages until this very moment.
3511 * This would lead us back to mem_cgroup_destroy_cache.
3512 *
3513 * But that will not execute at all if the "dead" flag is not
3514 * set, so flip it down to guarantee we are in control.
3515 */
3516 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003517 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003518 kmem_cache_destroy(c);
3519 }
3520 mutex_unlock(&set_limit_mutex);
3521}
3522
Glauber Costad7f25f82012-12-18 14:22:40 -08003523struct create_work {
3524 struct mem_cgroup *memcg;
3525 struct kmem_cache *cachep;
3526 struct work_struct work;
3527};
3528
Glauber Costa1f458cb2012-12-18 14:22:50 -08003529static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3530{
3531 struct kmem_cache *cachep;
3532 struct memcg_cache_params *params;
3533
3534 if (!memcg_kmem_is_active(memcg))
3535 return;
3536
3537 mutex_lock(&memcg->slab_caches_mutex);
3538 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3539 cachep = memcg_params_to_cache(params);
3540 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003541 schedule_work(&cachep->memcg_params->destroy);
3542 }
3543 mutex_unlock(&memcg->slab_caches_mutex);
3544}
3545
Glauber Costad7f25f82012-12-18 14:22:40 -08003546static void memcg_create_cache_work_func(struct work_struct *w)
3547{
3548 struct create_work *cw;
3549
3550 cw = container_of(w, struct create_work, work);
3551 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3552 /* Drop the reference gotten when we enqueued. */
3553 css_put(&cw->memcg->css);
3554 kfree(cw);
3555}
3556
3557/*
3558 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003559 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003560static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3561 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003562{
3563 struct create_work *cw;
3564
3565 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003566 if (cw == NULL) {
3567 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003568 return;
3569 }
3570
3571 cw->memcg = memcg;
3572 cw->cachep = cachep;
3573
3574 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3575 schedule_work(&cw->work);
3576}
3577
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003578static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3579 struct kmem_cache *cachep)
3580{
3581 /*
3582 * We need to stop accounting when we kmalloc, because if the
3583 * corresponding kmalloc cache is not yet created, the first allocation
3584 * in __memcg_create_cache_enqueue will recurse.
3585 *
3586 * However, it is better to enclose the whole function. Depending on
3587 * the debugging options enabled, INIT_WORK(), for instance, can
3588 * trigger an allocation. This too, will make us recurse. Because at
3589 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3590 * the safest choice is to do it like this, wrapping the whole function.
3591 */
3592 memcg_stop_kmem_account();
3593 __memcg_create_cache_enqueue(memcg, cachep);
3594 memcg_resume_kmem_account();
3595}
Glauber Costad7f25f82012-12-18 14:22:40 -08003596/*
3597 * Return the kmem_cache we're supposed to use for a slab allocation.
3598 * We try to use the current memcg's version of the cache.
3599 *
3600 * If the cache does not exist yet, if we are the first user of it,
3601 * we either create it immediately, if possible, or create it asynchronously
3602 * in a workqueue.
3603 * In the latter case, we will let the current allocation go through with
3604 * the original cache.
3605 *
3606 * Can't be called in interrupt context or from kernel threads.
3607 * This function needs to be called with rcu_read_lock() held.
3608 */
3609struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3610 gfp_t gfp)
3611{
3612 struct mem_cgroup *memcg;
3613 int idx;
3614
3615 VM_BUG_ON(!cachep->memcg_params);
3616 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3617
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003618 if (!current->mm || current->memcg_kmem_skip_account)
3619 return cachep;
3620
Glauber Costad7f25f82012-12-18 14:22:40 -08003621 rcu_read_lock();
3622 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003623
3624 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003625 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003626
3627 idx = memcg_cache_id(memcg);
3628
3629 /*
3630 * barrier to mare sure we're always seeing the up to date value. The
3631 * code updating memcg_caches will issue a write barrier to match this.
3632 */
3633 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003634 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3635 cachep = cachep->memcg_params->memcg_caches[idx];
3636 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003637 }
3638
Li Zefanca0dde92013-04-29 15:08:57 -07003639 /* The corresponding put will be done in the workqueue. */
3640 if (!css_tryget(&memcg->css))
3641 goto out;
3642 rcu_read_unlock();
3643
3644 /*
3645 * If we are in a safe context (can wait, and not in interrupt
3646 * context), we could be be predictable and return right away.
3647 * This would guarantee that the allocation being performed
3648 * already belongs in the new cache.
3649 *
3650 * However, there are some clashes that can arrive from locking.
3651 * For instance, because we acquire the slab_mutex while doing
3652 * kmem_cache_dup, this means no further allocation could happen
3653 * with the slab_mutex held.
3654 *
3655 * Also, because cache creation issue get_online_cpus(), this
3656 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3657 * that ends up reversed during cpu hotplug. (cpuset allocates
3658 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3659 * better to defer everything.
3660 */
3661 memcg_create_cache_enqueue(memcg, cachep);
3662 return cachep;
3663out:
3664 rcu_read_unlock();
3665 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003666}
3667EXPORT_SYMBOL(__memcg_kmem_get_cache);
3668
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003669/*
3670 * We need to verify if the allocation against current->mm->owner's memcg is
3671 * possible for the given order. But the page is not allocated yet, so we'll
3672 * need a further commit step to do the final arrangements.
3673 *
3674 * It is possible for the task to switch cgroups in this mean time, so at
3675 * commit time, we can't rely on task conversion any longer. We'll then use
3676 * the handle argument to return to the caller which cgroup we should commit
3677 * against. We could also return the memcg directly and avoid the pointer
3678 * passing, but a boolean return value gives better semantics considering
3679 * the compiled-out case as well.
3680 *
3681 * Returning true means the allocation is possible.
3682 */
3683bool
3684__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3685{
3686 struct mem_cgroup *memcg;
3687 int ret;
3688
3689 *_memcg = NULL;
3690 memcg = try_get_mem_cgroup_from_mm(current->mm);
3691
3692 /*
3693 * very rare case described in mem_cgroup_from_task. Unfortunately there
3694 * isn't much we can do without complicating this too much, and it would
3695 * be gfp-dependent anyway. Just let it go
3696 */
3697 if (unlikely(!memcg))
3698 return true;
3699
3700 if (!memcg_can_account_kmem(memcg)) {
3701 css_put(&memcg->css);
3702 return true;
3703 }
3704
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003705 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3706 if (!ret)
3707 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003708
3709 css_put(&memcg->css);
3710 return (ret == 0);
3711}
3712
3713void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3714 int order)
3715{
3716 struct page_cgroup *pc;
3717
3718 VM_BUG_ON(mem_cgroup_is_root(memcg));
3719
3720 /* The page allocation failed. Revert */
3721 if (!page) {
3722 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003723 return;
3724 }
3725
3726 pc = lookup_page_cgroup(page);
3727 lock_page_cgroup(pc);
3728 pc->mem_cgroup = memcg;
3729 SetPageCgroupUsed(pc);
3730 unlock_page_cgroup(pc);
3731}
3732
3733void __memcg_kmem_uncharge_pages(struct page *page, int order)
3734{
3735 struct mem_cgroup *memcg = NULL;
3736 struct page_cgroup *pc;
3737
3738
3739 pc = lookup_page_cgroup(page);
3740 /*
3741 * Fast unlocked return. Theoretically might have changed, have to
3742 * check again after locking.
3743 */
3744 if (!PageCgroupUsed(pc))
3745 return;
3746
3747 lock_page_cgroup(pc);
3748 if (PageCgroupUsed(pc)) {
3749 memcg = pc->mem_cgroup;
3750 ClearPageCgroupUsed(pc);
3751 }
3752 unlock_page_cgroup(pc);
3753
3754 /*
3755 * We trust that only if there is a memcg associated with the page, it
3756 * is a valid allocation
3757 */
3758 if (!memcg)
3759 return;
3760
3761 VM_BUG_ON(mem_cgroup_is_root(memcg));
3762 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003763}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003764#else
3765static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3766{
3767}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003768#endif /* CONFIG_MEMCG_KMEM */
3769
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003770#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3771
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003772#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003773/*
3774 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003775 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3776 * charge/uncharge will be never happen and move_account() is done under
3777 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003778 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003779void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003780{
3781 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003782 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003783 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003784 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003785
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003786 if (mem_cgroup_disabled())
3787 return;
David Rientjesb070e652013-05-07 16:18:09 -07003788
3789 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003790 for (i = 1; i < HPAGE_PMD_NR; i++) {
3791 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003792 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003793 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003794 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3795 }
David Rientjesb070e652013-05-07 16:18:09 -07003796 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3797 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003798}
Hugh Dickins12d27102012-01-12 17:19:52 -08003799#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003800
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003801/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003802 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003803 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003804 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003805 * @pc: page_cgroup of the page.
3806 * @from: mem_cgroup which the page is moved from.
3807 * @to: mem_cgroup which the page is moved to. @from != @to.
3808 *
3809 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003810 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003811 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003812 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003813 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3814 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003815 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003816static int mem_cgroup_move_account(struct page *page,
3817 unsigned int nr_pages,
3818 struct page_cgroup *pc,
3819 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003820 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003821{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003822 unsigned long flags;
3823 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003824 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003825
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003826 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003827 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003828 /*
3829 * The page is isolated from LRU. So, collapse function
3830 * will not handle this page. But page splitting can happen.
3831 * Do this check under compound_page_lock(). The caller should
3832 * hold it.
3833 */
3834 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003835 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003836 goto out;
3837
3838 lock_page_cgroup(pc);
3839
3840 ret = -EINVAL;
3841 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3842 goto unlock;
3843
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003844 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003845
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003846 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003847 /* Update mapped_file data for mem_cgroup */
3848 preempt_disable();
3849 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3850 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3851 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003852 }
David Rientjesb070e652013-05-07 16:18:09 -07003853 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003854
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003855 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003856 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003857 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003858 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003859 ret = 0;
3860unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003861 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003862 /*
3863 * check events
3864 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003865 memcg_check_events(to, page);
3866 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003867out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003868 return ret;
3869}
3870
Michal Hocko2ef37d32012-10-26 13:37:30 +02003871/**
3872 * mem_cgroup_move_parent - moves page to the parent group
3873 * @page: the page to move
3874 * @pc: page_cgroup of the page
3875 * @child: page's cgroup
3876 *
3877 * move charges to its parent or the root cgroup if the group has no
3878 * parent (aka use_hierarchy==0).
3879 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3880 * mem_cgroup_move_account fails) the failure is always temporary and
3881 * it signals a race with a page removal/uncharge or migration. In the
3882 * first case the page is on the way out and it will vanish from the LRU
3883 * on the next attempt and the call should be retried later.
3884 * Isolation from the LRU fails only if page has been isolated from
3885 * the LRU since we looked at it and that usually means either global
3886 * reclaim or migration going on. The page will either get back to the
3887 * LRU or vanish.
3888 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3889 * (!PageCgroupUsed) or moved to a different group. The page will
3890 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003891 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003892static int mem_cgroup_move_parent(struct page *page,
3893 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003894 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003895{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003896 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003897 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003898 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003899 int ret;
3900
Michal Hockod8423012012-10-26 13:37:29 +02003901 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003902
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003903 ret = -EBUSY;
3904 if (!get_page_unless_zero(page))
3905 goto out;
3906 if (isolate_lru_page(page))
3907 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003908
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003909 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003910
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003911 parent = parent_mem_cgroup(child);
3912 /*
3913 * If no parent, move charges to root cgroup.
3914 */
3915 if (!parent)
3916 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003917
Michal Hocko2ef37d32012-10-26 13:37:30 +02003918 if (nr_pages > 1) {
3919 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003920 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003921 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003922
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003923 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003924 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003925 if (!ret)
3926 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003927
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003928 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003929 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003930 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003931put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003932 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003933out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003934 return ret;
3935}
3936
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003937/*
3938 * Charge the memory controller for page usage.
3939 * Return
3940 * 0 if the charge was successful
3941 * < 0 if the cgroup is over its limit
3942 */
3943static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003944 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003945{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003946 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003947 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003948 bool oom = true;
3949 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003950
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003951 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003952 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003953 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003954 /*
3955 * Never OOM-kill a process for a huge page. The
3956 * fault handler will fall back to regular pages.
3957 */
3958 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003959 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003960
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003961 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003962 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003963 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003964 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003965 return 0;
3966}
3967
3968int mem_cgroup_newpage_charge(struct page *page,
3969 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003970{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003971 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003972 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003973 VM_BUG_ON(page_mapped(page));
3974 VM_BUG_ON(page->mapping && !PageAnon(page));
3975 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003976 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003977 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003978}
3979
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003980/*
3981 * While swap-in, try_charge -> commit or cancel, the page is locked.
3982 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003983 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003984 * "commit()" or removed by "cancel()"
3985 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003986static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3987 struct page *page,
3988 gfp_t mask,
3989 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003990{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003991 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003992 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003993 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003994
Johannes Weiner90deb782012-07-31 16:45:47 -07003995 pc = lookup_page_cgroup(page);
3996 /*
3997 * Every swap fault against a single page tries to charge the
3998 * page, bail as early as possible. shmem_unuse() encounters
3999 * already charged pages, too. The USED bit is protected by
4000 * the page lock, which serializes swap cache removal, which
4001 * in turn serializes uncharging.
4002 */
4003 if (PageCgroupUsed(pc))
4004 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004005 if (!do_swap_account)
4006 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004007 memcg = try_get_mem_cgroup_from_page(page);
4008 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004009 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08004010 *memcgp = memcg;
4011 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004012 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004013 if (ret == -EINTR)
4014 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004015 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004016charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004017 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
4018 if (ret == -EINTR)
4019 ret = 0;
4020 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004021}
4022
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004023int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
4024 gfp_t gfp_mask, struct mem_cgroup **memcgp)
4025{
4026 *memcgp = NULL;
4027 if (mem_cgroup_disabled())
4028 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07004029 /*
4030 * A racing thread's fault, or swapoff, may have already
4031 * updated the pte, and even removed page from swap cache: in
4032 * those cases unuse_pte()'s pte_same() test will fail; but
4033 * there's also a KSM case which does need to charge the page.
4034 */
4035 if (!PageSwapCache(page)) {
4036 int ret;
4037
4038 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
4039 if (ret == -EINTR)
4040 ret = 0;
4041 return ret;
4042 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004043 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
4044}
4045
Johannes Weiner827a03d2012-07-31 16:45:36 -07004046void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
4047{
4048 if (mem_cgroup_disabled())
4049 return;
4050 if (!memcg)
4051 return;
4052 __mem_cgroup_cancel_charge(memcg, 1);
4053}
4054
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004055static void
Johannes Weiner72835c82012-01-12 17:18:32 -08004056__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004057 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004058{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004059 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004060 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08004061 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004062 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07004063
Johannes Weinerce587e62012-04-24 20:22:33 +02004064 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004065 /*
4066 * Now swap is on-memory. This means this page may be
4067 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004068 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
4069 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
4070 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004071 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004072 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004073 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07004074 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004075 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004076}
4077
Johannes Weiner72835c82012-01-12 17:18:32 -08004078void mem_cgroup_commit_charge_swapin(struct page *page,
4079 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004080{
Johannes Weiner72835c82012-01-12 17:18:32 -08004081 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004082 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004083}
4084
Johannes Weiner827a03d2012-07-31 16:45:36 -07004085int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
4086 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004087{
Johannes Weiner827a03d2012-07-31 16:45:36 -07004088 struct mem_cgroup *memcg = NULL;
4089 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4090 int ret;
4091
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004092 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07004093 return 0;
4094 if (PageCompound(page))
4095 return 0;
4096
Johannes Weiner827a03d2012-07-31 16:45:36 -07004097 if (!PageSwapCache(page))
4098 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
4099 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004100 ret = __mem_cgroup_try_charge_swapin(mm, page,
4101 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07004102 if (!ret)
4103 __mem_cgroup_commit_charge_swapin(page, memcg, type);
4104 }
4105 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004106}
4107
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004108static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004109 unsigned int nr_pages,
4110 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004111{
4112 struct memcg_batch_info *batch = NULL;
4113 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004114
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004115 /* If swapout, usage of swap doesn't decrease */
4116 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4117 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004118
4119 batch = &current->memcg_batch;
4120 /*
4121 * In usual, we do css_get() when we remember memcg pointer.
4122 * But in this case, we keep res->usage until end of a series of
4123 * uncharges. Then, it's ok to ignore memcg's refcnt.
4124 */
4125 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004126 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004127 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004128 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004129 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004130 * the same cgroup and we have chance to coalesce uncharges.
4131 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4132 * because we want to do uncharge as soon as possible.
4133 */
4134
4135 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4136 goto direct_uncharge;
4137
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004138 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004139 goto direct_uncharge;
4140
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004141 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004142 * In typical case, batch->memcg == mem. This means we can
4143 * merge a series of uncharges to an uncharge of res_counter.
4144 * If not, we uncharge res_counter ony by one.
4145 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004146 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004147 goto direct_uncharge;
4148 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004149 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004150 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004151 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004152 return;
4153direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004154 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004155 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004156 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4157 if (unlikely(batch->memcg != memcg))
4158 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004159}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004160
Balbir Singh8697d332008-02-07 00:13:59 -08004161/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004162 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004163 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004164static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004165__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4166 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004167{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004168 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004169 unsigned int nr_pages = 1;
4170 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004171 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004172
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004173 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004174 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004175
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004176 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004177 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004178 VM_BUG_ON(!PageTransHuge(page));
4179 }
Balbir Singh8697d332008-02-07 00:13:59 -08004180 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004181 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004182 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004183 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004184 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004185 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004186
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004187 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004188
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004189 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004190
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004191 if (!PageCgroupUsed(pc))
4192 goto unlock_out;
4193
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004194 anon = PageAnon(page);
4195
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004196 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004197 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004198 /*
4199 * Generally PageAnon tells if it's the anon statistics to be
4200 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4201 * used before page reached the stage of being marked PageAnon.
4202 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004203 anon = true;
4204 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004205 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004206 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004207 if (page_mapped(page))
4208 goto unlock_out;
4209 /*
4210 * Pages under migration may not be uncharged. But
4211 * end_migration() /must/ be the one uncharging the
4212 * unused post-migration page and so it has to call
4213 * here with the migration bit still set. See the
4214 * res_counter handling below.
4215 */
4216 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004217 goto unlock_out;
4218 break;
4219 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4220 if (!PageAnon(page)) { /* Shared memory */
4221 if (page->mapping && !page_is_file_cache(page))
4222 goto unlock_out;
4223 } else if (page_mapped(page)) /* Anon */
4224 goto unlock_out;
4225 break;
4226 default:
4227 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004228 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004229
David Rientjesb070e652013-05-07 16:18:09 -07004230 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004231
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004232 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004233 /*
4234 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4235 * freed from LRU. This is safe because uncharged page is expected not
4236 * to be reused (freed soon). Exception is SwapCache, it's handled by
4237 * special functions.
4238 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004239
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004240 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004241 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004242 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004243 * will never be freed.
4244 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004245 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004246 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004247 mem_cgroup_swap_statistics(memcg, true);
4248 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004249 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004250 /*
4251 * Migration does not charge the res_counter for the
4252 * replacement page, so leave it alone when phasing out the
4253 * page that is unused after the migration.
4254 */
4255 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004256 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004257
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004258 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004259
4260unlock_out:
4261 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004262 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004263}
4264
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004265void mem_cgroup_uncharge_page(struct page *page)
4266{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004267 /* early check. */
4268 if (page_mapped(page))
4269 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004270 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004271 /*
4272 * If the page is in swap cache, uncharge should be deferred
4273 * to the swap path, which also properly accounts swap usage
4274 * and handles memcg lifetime.
4275 *
4276 * Note that this check is not stable and reclaim may add the
4277 * page to swap cache at any time after this. However, if the
4278 * page is not in swap cache by the time page->mapcount hits
4279 * 0, there won't be any page table references to the swap
4280 * slot, and reclaim will free it and not actually write the
4281 * page to disk.
4282 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004283 if (PageSwapCache(page))
4284 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004285 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004286}
4287
4288void mem_cgroup_uncharge_cache_page(struct page *page)
4289{
4290 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004291 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004292 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004293}
4294
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004295/*
4296 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4297 * In that cases, pages are freed continuously and we can expect pages
4298 * are in the same memcg. All these calls itself limits the number of
4299 * pages freed at once, then uncharge_start/end() is called properly.
4300 * This may be called prural(2) times in a context,
4301 */
4302
4303void mem_cgroup_uncharge_start(void)
4304{
4305 current->memcg_batch.do_batch++;
4306 /* We can do nest. */
4307 if (current->memcg_batch.do_batch == 1) {
4308 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004309 current->memcg_batch.nr_pages = 0;
4310 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004311 }
4312}
4313
4314void mem_cgroup_uncharge_end(void)
4315{
4316 struct memcg_batch_info *batch = &current->memcg_batch;
4317
4318 if (!batch->do_batch)
4319 return;
4320
4321 batch->do_batch--;
4322 if (batch->do_batch) /* If stacked, do nothing. */
4323 return;
4324
4325 if (!batch->memcg)
4326 return;
4327 /*
4328 * This "batch->memcg" is valid without any css_get/put etc...
4329 * bacause we hide charges behind us.
4330 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004331 if (batch->nr_pages)
4332 res_counter_uncharge(&batch->memcg->res,
4333 batch->nr_pages * PAGE_SIZE);
4334 if (batch->memsw_nr_pages)
4335 res_counter_uncharge(&batch->memcg->memsw,
4336 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004337 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004338 /* forget this pointer (for sanity check) */
4339 batch->memcg = NULL;
4340}
4341
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004342#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004343/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004344 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004345 * memcg information is recorded to swap_cgroup of "ent"
4346 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004347void
4348mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004349{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004350 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004351 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004352
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004353 if (!swapout) /* this was a swap cache but the swap is unused ! */
4354 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4355
Johannes Weiner0030f532012-07-31 16:45:25 -07004356 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004357
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004358 /*
4359 * record memcg information, if swapout && memcg != NULL,
4360 * mem_cgroup_get() was called in uncharge().
4361 */
4362 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004363 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004364}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004365#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004366
Andrew Mortonc255a452012-07-31 16:43:02 -07004367#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004368/*
4369 * called from swap_entry_free(). remove record in swap_cgroup and
4370 * uncharge "memsw" account.
4371 */
4372void mem_cgroup_uncharge_swap(swp_entry_t ent)
4373{
4374 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004375 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004376
4377 if (!do_swap_account)
4378 return;
4379
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004380 id = swap_cgroup_record(ent, 0);
4381 rcu_read_lock();
4382 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004383 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004384 /*
4385 * We uncharge this because swap is freed.
4386 * This memcg can be obsolete one. We avoid calling css_tryget
4387 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004388 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004389 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004390 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004391 mem_cgroup_put(memcg);
4392 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004393 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004394}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004395
4396/**
4397 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4398 * @entry: swap entry to be moved
4399 * @from: mem_cgroup which the entry is moved from
4400 * @to: mem_cgroup which the entry is moved to
4401 *
4402 * It succeeds only when the swap_cgroup's record for this entry is the same
4403 * as the mem_cgroup's id of @from.
4404 *
4405 * Returns 0 on success, -EINVAL on failure.
4406 *
4407 * The caller must have charged to @to, IOW, called res_counter_charge() about
4408 * both res and memsw, and called css_get().
4409 */
4410static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004411 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004412{
4413 unsigned short old_id, new_id;
4414
4415 old_id = css_id(&from->css);
4416 new_id = css_id(&to->css);
4417
4418 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004419 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004420 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004421 /*
4422 * This function is only called from task migration context now.
4423 * It postpones res_counter and refcount handling till the end
4424 * of task migration(mem_cgroup_clear_mc()) for performance
4425 * improvement. But we cannot postpone mem_cgroup_get(to)
4426 * because if the process that has been moved to @to does
4427 * swap-in, the refcount of @to might be decreased to 0.
4428 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004429 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004430 return 0;
4431 }
4432 return -EINVAL;
4433}
4434#else
4435static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004436 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004437{
4438 return -EINVAL;
4439}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004440#endif
4441
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004442/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004443 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4444 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004445 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004446void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4447 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004448{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004449 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004450 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004451 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004452 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004453
Johannes Weiner72835c82012-01-12 17:18:32 -08004454 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004455
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004456 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004457 return;
Balbir Singh40779602008-04-04 14:29:59 -07004458
Mel Gormanb32967f2012-11-19 12:35:47 +00004459 if (PageTransHuge(page))
4460 nr_pages <<= compound_order(page);
4461
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004462 pc = lookup_page_cgroup(page);
4463 lock_page_cgroup(pc);
4464 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004465 memcg = pc->mem_cgroup;
4466 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004467 /*
4468 * At migrating an anonymous page, its mapcount goes down
4469 * to 0 and uncharge() will be called. But, even if it's fully
4470 * unmapped, migration may fail and this page has to be
4471 * charged again. We set MIGRATION flag here and delay uncharge
4472 * until end_migration() is called
4473 *
4474 * Corner Case Thinking
4475 * A)
4476 * When the old page was mapped as Anon and it's unmap-and-freed
4477 * while migration was ongoing.
4478 * If unmap finds the old page, uncharge() of it will be delayed
4479 * until end_migration(). If unmap finds a new page, it's
4480 * uncharged when it make mapcount to be 1->0. If unmap code
4481 * finds swap_migration_entry, the new page will not be mapped
4482 * and end_migration() will find it(mapcount==0).
4483 *
4484 * B)
4485 * When the old page was mapped but migraion fails, the kernel
4486 * remaps it. A charge for it is kept by MIGRATION flag even
4487 * if mapcount goes down to 0. We can do remap successfully
4488 * without charging it again.
4489 *
4490 * C)
4491 * The "old" page is under lock_page() until the end of
4492 * migration, so, the old page itself will not be swapped-out.
4493 * If the new page is swapped out before end_migraton, our
4494 * hook to usual swap-out path will catch the event.
4495 */
4496 if (PageAnon(page))
4497 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004498 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004499 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004500 /*
4501 * If the page is not charged at this point,
4502 * we return here.
4503 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004504 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004505 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004506
Johannes Weiner72835c82012-01-12 17:18:32 -08004507 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004508 /*
4509 * We charge new page before it's used/mapped. So, even if unlock_page()
4510 * is called before end_migration, we can catch all events on this new
4511 * page. In the case new page is migrated but not remapped, new page's
4512 * mapcount will be finally 0 and we call uncharge in end_migration().
4513 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004514 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004515 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004516 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004517 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004518 /*
4519 * The page is committed to the memcg, but it's not actually
4520 * charged to the res_counter since we plan on replacing the
4521 * old one and only one page is going to be left afterwards.
4522 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004523 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004524}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004525
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004526/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004527void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004528 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004529{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004530 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004531 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004532 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004533
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004534 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004535 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004536
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004537 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004538 used = oldpage;
4539 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004540 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004541 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004542 unused = oldpage;
4543 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004544 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004545 __mem_cgroup_uncharge_common(unused,
4546 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4547 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4548 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004549 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004550 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004551 * We disallowed uncharge of pages under migration because mapcount
4552 * of the page goes down to zero, temporarly.
4553 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004554 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004555 pc = lookup_page_cgroup(oldpage);
4556 lock_page_cgroup(pc);
4557 ClearPageCgroupMigration(pc);
4558 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004559
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004560 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004561 * If a page is a file cache, radix-tree replacement is very atomic
4562 * and we can skip this check. When it was an Anon page, its mapcount
4563 * goes down to 0. But because we added MIGRATION flage, it's not
4564 * uncharged yet. There are several case but page->mapcount check
4565 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4566 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004567 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004568 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004569 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004570}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004571
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004572/*
4573 * At replace page cache, newpage is not under any memcg but it's on
4574 * LRU. So, this function doesn't touch res_counter but handles LRU
4575 * in correct way. Both pages are locked so we cannot race with uncharge.
4576 */
4577void mem_cgroup_replace_page_cache(struct page *oldpage,
4578 struct page *newpage)
4579{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004580 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004581 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004582 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004583
4584 if (mem_cgroup_disabled())
4585 return;
4586
4587 pc = lookup_page_cgroup(oldpage);
4588 /* fix accounting on old pages */
4589 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004590 if (PageCgroupUsed(pc)) {
4591 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004592 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004593 ClearPageCgroupUsed(pc);
4594 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004595 unlock_page_cgroup(pc);
4596
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004597 /*
4598 * When called from shmem_replace_page(), in some cases the
4599 * oldpage has already been charged, and in some cases not.
4600 */
4601 if (!memcg)
4602 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004603 /*
4604 * Even if newpage->mapping was NULL before starting replacement,
4605 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4606 * LRU while we overwrite pc->mem_cgroup.
4607 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004608 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004609}
4610
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004611#ifdef CONFIG_DEBUG_VM
4612static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4613{
4614 struct page_cgroup *pc;
4615
4616 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004617 /*
4618 * Can be NULL while feeding pages into the page allocator for
4619 * the first time, i.e. during boot or memory hotplug;
4620 * or when mem_cgroup_disabled().
4621 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004622 if (likely(pc) && PageCgroupUsed(pc))
4623 return pc;
4624 return NULL;
4625}
4626
4627bool mem_cgroup_bad_page_check(struct page *page)
4628{
4629 if (mem_cgroup_disabled())
4630 return false;
4631
4632 return lookup_page_cgroup_used(page) != NULL;
4633}
4634
4635void mem_cgroup_print_bad_page(struct page *page)
4636{
4637 struct page_cgroup *pc;
4638
4639 pc = lookup_page_cgroup_used(page);
4640 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004641 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4642 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004643 }
4644}
4645#endif
4646
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004647static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004648 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004649{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004650 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004651 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004652 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004653 int children = mem_cgroup_count_children(memcg);
4654 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004655 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004656
4657 /*
4658 * For keeping hierarchical_reclaim simple, how long we should retry
4659 * is depends on callers. We set our retry-count to be function
4660 * of # of children which we should visit in this loop.
4661 */
4662 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4663
4664 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004665
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004666 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004667 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004668 if (signal_pending(current)) {
4669 ret = -EINTR;
4670 break;
4671 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004672 /*
4673 * Rather than hide all in some function, I do this in
4674 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004675 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004676 */
4677 mutex_lock(&set_limit_mutex);
4678 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4679 if (memswlimit < val) {
4680 ret = -EINVAL;
4681 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004682 break;
4683 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004684
4685 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4686 if (memlimit < val)
4687 enlarge = 1;
4688
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004689 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004690 if (!ret) {
4691 if (memswlimit == val)
4692 memcg->memsw_is_minimum = true;
4693 else
4694 memcg->memsw_is_minimum = false;
4695 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004696 mutex_unlock(&set_limit_mutex);
4697
4698 if (!ret)
4699 break;
4700
Johannes Weiner56600482012-01-12 17:17:59 -08004701 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4702 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004703 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4704 /* Usage is reduced ? */
4705 if (curusage >= oldusage)
4706 retry_count--;
4707 else
4708 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004709 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004710 if (!ret && enlarge)
4711 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004712
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004713 return ret;
4714}
4715
Li Zefan338c8432009-06-17 16:27:15 -07004716static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4717 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004718{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004719 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004720 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004721 int children = mem_cgroup_count_children(memcg);
4722 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004723 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004724
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004725 /* see mem_cgroup_resize_res_limit */
4726 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4727 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004728 while (retry_count) {
4729 if (signal_pending(current)) {
4730 ret = -EINTR;
4731 break;
4732 }
4733 /*
4734 * Rather than hide all in some function, I do this in
4735 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004736 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004737 */
4738 mutex_lock(&set_limit_mutex);
4739 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4740 if (memlimit > val) {
4741 ret = -EINVAL;
4742 mutex_unlock(&set_limit_mutex);
4743 break;
4744 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004745 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4746 if (memswlimit < val)
4747 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004748 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004749 if (!ret) {
4750 if (memlimit == val)
4751 memcg->memsw_is_minimum = true;
4752 else
4753 memcg->memsw_is_minimum = false;
4754 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004755 mutex_unlock(&set_limit_mutex);
4756
4757 if (!ret)
4758 break;
4759
Johannes Weiner56600482012-01-12 17:17:59 -08004760 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4761 MEM_CGROUP_RECLAIM_NOSWAP |
4762 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004763 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004764 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004765 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004766 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004767 else
4768 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004769 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004770 if (!ret && enlarge)
4771 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004772 return ret;
4773}
4774
Balbir Singh4e416952009-09-23 15:56:39 -07004775unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004776 gfp_t gfp_mask,
4777 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004778{
4779 unsigned long nr_reclaimed = 0;
4780 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4781 unsigned long reclaimed;
4782 int loop = 0;
4783 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004784 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004785 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004786
4787 if (order > 0)
4788 return 0;
4789
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004790 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004791 /*
4792 * This loop can run a while, specially if mem_cgroup's continuously
4793 * keep exceeding their soft limit and putting the system under
4794 * pressure
4795 */
4796 do {
4797 if (next_mz)
4798 mz = next_mz;
4799 else
4800 mz = mem_cgroup_largest_soft_limit_node(mctz);
4801 if (!mz)
4802 break;
4803
Ying Han0ae5e892011-05-26 16:25:25 -07004804 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004805 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004806 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004807 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004808 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004809 spin_lock(&mctz->lock);
4810
4811 /*
4812 * If we failed to reclaim anything from this memory cgroup
4813 * it is time to move on to the next cgroup
4814 */
4815 next_mz = NULL;
4816 if (!reclaimed) {
4817 do {
4818 /*
4819 * Loop until we find yet another one.
4820 *
4821 * By the time we get the soft_limit lock
4822 * again, someone might have aded the
4823 * group back on the RB tree. Iterate to
4824 * make sure we get a different mem.
4825 * mem_cgroup_largest_soft_limit_node returns
4826 * NULL if no other cgroup is present on
4827 * the tree
4828 */
4829 next_mz =
4830 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004831 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004832 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004833 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004834 break;
4835 } while (1);
4836 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004837 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4838 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004839 /*
4840 * One school of thought says that we should not add
4841 * back the node to the tree if reclaim returns 0.
4842 * But our reclaim could return 0, simply because due
4843 * to priority we are exposing a smaller subset of
4844 * memory to reclaim from. Consider this as a longer
4845 * term TODO.
4846 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004847 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004848 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004849 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004850 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004851 loop++;
4852 /*
4853 * Could not reclaim anything and there are no more
4854 * mem cgroups to try or we seem to be looping without
4855 * reclaiming anything.
4856 */
4857 if (!nr_reclaimed &&
4858 (next_mz == NULL ||
4859 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4860 break;
4861 } while (!nr_reclaimed);
4862 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004863 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004864 return nr_reclaimed;
4865}
4866
Michal Hocko2ef37d32012-10-26 13:37:30 +02004867/**
4868 * mem_cgroup_force_empty_list - clears LRU of a group
4869 * @memcg: group to clear
4870 * @node: NUMA node
4871 * @zid: zone id
4872 * @lru: lru to to clear
4873 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004874 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004875 * reclaim the pages page themselves - pages are moved to the parent (or root)
4876 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004877 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004878static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004879 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004880{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004881 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004882 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004883 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004884 struct page *busy;
4885 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004886
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004887 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004888 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4889 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004890
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004891 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004892 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004893 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004894 struct page *page;
4895
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004896 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004897 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004898 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004899 break;
4900 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004901 page = list_entry(list->prev, struct page, lru);
4902 if (busy == page) {
4903 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004904 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004905 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004906 continue;
4907 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004908 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004909
Johannes Weiner925b7672012-01-12 17:18:15 -08004910 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004911
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004912 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004913 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004914 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004915 cond_resched();
4916 } else
4917 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004918 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004919}
4920
4921/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004922 * make mem_cgroup's charge to be 0 if there is no task by moving
4923 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004924 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004925 *
4926 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004927 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004928static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004929{
Michal Hockoc26251f2012-10-26 13:37:28 +02004930 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004931 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004932
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004933 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004934 /* This is for making all *used* pages to be on LRU. */
4935 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004936 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004937 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004938 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004939 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004940 enum lru_list lru;
4941 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004942 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004943 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004944 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004945 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004946 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004947 mem_cgroup_end_move(memcg);
4948 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004949 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004950
Michal Hocko2ef37d32012-10-26 13:37:30 +02004951 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004952 * Kernel memory may not necessarily be trackable to a specific
4953 * process. So they are not migrated, and therefore we can't
4954 * expect their value to drop to 0 here.
4955 * Having res filled up with kmem only is enough.
4956 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004957 * This is a safety check because mem_cgroup_force_empty_list
4958 * could have raced with mem_cgroup_replace_page_cache callers
4959 * so the lru seemed empty but the page could have been added
4960 * right after the check. RES_USAGE should be safe as we always
4961 * charge before adding to the LRU.
4962 */
Glauber Costabea207c2012-12-18 14:22:11 -08004963 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4964 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4965 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004966}
4967
4968/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004969 * This mainly exists for tests during the setting of set of use_hierarchy.
4970 * Since this is the very setting we are changing, the current hierarchy value
4971 * is meaningless
4972 */
4973static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4974{
4975 struct cgroup *pos;
4976
4977 /* bounce at first found */
4978 cgroup_for_each_child(pos, memcg->css.cgroup)
4979 return true;
4980 return false;
4981}
4982
4983/*
Glauber Costa09998212013-02-22 16:34:55 -08004984 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4985 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004986 * from mem_cgroup_count_children(), in the sense that we don't really care how
4987 * many children we have; we only need to know if we have any. It also counts
4988 * any memcg without hierarchy as infertile.
4989 */
4990static inline bool memcg_has_children(struct mem_cgroup *memcg)
4991{
4992 return memcg->use_hierarchy && __memcg_has_children(memcg);
4993}
4994
4995/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004996 * Reclaims as many pages from the given memcg as possible and moves
4997 * the rest to the parent.
4998 *
4999 * Caller is responsible for holding css reference for memcg.
5000 */
5001static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
5002{
5003 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
5004 struct cgroup *cgrp = memcg->css.cgroup;
5005
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005006 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02005007 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
5008 return -EBUSY;
5009
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005010 /* we call try-to-free pages for make this cgroup empty */
5011 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005012 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07005013 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005014 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005015
Michal Hockoc26251f2012-10-26 13:37:28 +02005016 if (signal_pending(current))
5017 return -EINTR;
5018
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005019 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07005020 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005021 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005022 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005023 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02005024 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005025 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005026
5027 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005028 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02005029 mem_cgroup_reparent_charges(memcg);
5030
5031 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08005032}
5033
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07005034static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005035{
Michal Hockoc26251f2012-10-26 13:37:28 +02005036 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5037 int ret;
5038
Michal Hockod8423012012-10-26 13:37:29 +02005039 if (mem_cgroup_is_root(memcg))
5040 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02005041 css_get(&memcg->css);
5042 ret = mem_cgroup_force_empty(memcg);
5043 css_put(&memcg->css);
5044
5045 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005046}
5047
5048
Balbir Singh18f59ea2009-01-07 18:08:07 -08005049static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
5050{
5051 return mem_cgroup_from_cont(cont)->use_hierarchy;
5052}
5053
5054static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
5055 u64 val)
5056{
5057 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005058 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005059 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005060 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005061
5062 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005063 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005064
Glauber Costa09998212013-02-22 16:34:55 -08005065 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07005066
5067 if (memcg->use_hierarchy == val)
5068 goto out;
5069
Balbir Singh18f59ea2009-01-07 18:08:07 -08005070 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005071 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08005072 * in the child subtrees. If it is unset, then the change can
5073 * occur, provided the current cgroup has no children.
5074 *
5075 * For the root cgroup, parent_mem is NULL, we allow value to be
5076 * set if there are no children.
5077 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005078 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08005079 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08005080 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005081 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005082 else
5083 retval = -EBUSY;
5084 } else
5085 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07005086
5087out:
Glauber Costa09998212013-02-22 16:34:55 -08005088 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005089
5090 return retval;
5091}
5092
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005093
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005094static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005095 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005096{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005097 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005098 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005099
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005100 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005101 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005102 val += mem_cgroup_read_stat(iter, idx);
5103
5104 if (val < 0) /* race ? */
5105 val = 0;
5106 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005107}
5108
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005109static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005110{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005111 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005112
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005113 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005114 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00005115 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005116 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00005117 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005118 }
5119
David Rientjesb070e652013-05-07 16:18:09 -07005120 /*
5121 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
5122 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
5123 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005124 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
5125 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005126
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005127 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005128 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005129
5130 return val << PAGE_SHIFT;
5131}
5132
Tejun Heoaf36f902012-04-01 12:09:55 -07005133static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
5134 struct file *file, char __user *buf,
5135 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005136{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005137 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07005138 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005139 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005140 int name, len;
5141 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005142
5143 type = MEMFILE_TYPE(cft->private);
5144 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005145
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005146 switch (type) {
5147 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005148 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005149 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005150 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005151 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005152 break;
5153 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005154 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005155 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005156 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005157 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005158 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005159 case _KMEM:
5160 val = res_counter_read_u64(&memcg->kmem, name);
5161 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005162 default:
5163 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005164 }
Tejun Heoaf36f902012-04-01 12:09:55 -07005165
5166 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5167 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005168}
Glauber Costa510fc4e2012-12-18 14:21:47 -08005169
5170static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
5171{
5172 int ret = -EINVAL;
5173#ifdef CONFIG_MEMCG_KMEM
5174 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5175 /*
5176 * For simplicity, we won't allow this to be disabled. It also can't
5177 * be changed if the cgroup has children already, or if tasks had
5178 * already joined.
5179 *
5180 * If tasks join before we set the limit, a person looking at
5181 * kmem.usage_in_bytes will have no way to determine when it took
5182 * place, which makes the value quite meaningless.
5183 *
5184 * After it first became limited, changes in the value of the limit are
5185 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08005186 */
Glauber Costa09998212013-02-22 16:34:55 -08005187 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005188 mutex_lock(&set_limit_mutex);
5189 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08005190 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08005191 ret = -EBUSY;
5192 goto out;
5193 }
5194 ret = res_counter_set_limit(&memcg->kmem, val);
5195 VM_BUG_ON(ret);
5196
Glauber Costa55007d82012-12-18 14:22:38 -08005197 ret = memcg_update_cache_sizes(memcg);
5198 if (ret) {
5199 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5200 goto out;
5201 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005202 static_key_slow_inc(&memcg_kmem_enabled_key);
5203 /*
5204 * setting the active bit after the inc will guarantee no one
5205 * starts accounting before all call sites are patched
5206 */
5207 memcg_kmem_set_active(memcg);
5208
Glauber Costa7de37682012-12-18 14:22:07 -08005209 /*
5210 * kmem charges can outlive the cgroup. In the case of slab
5211 * pages, for instance, a page contain objects from various
5212 * processes, so it is unfeasible to migrate them away. We
5213 * need to reference count the memcg because of that.
5214 */
5215 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005216 } else
5217 ret = res_counter_set_limit(&memcg->kmem, val);
5218out:
5219 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005220 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005221#endif
5222 return ret;
5223}
5224
Hugh Dickins6d0439902013-02-22 16:35:50 -08005225#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005226static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005227{
Glauber Costa55007d82012-12-18 14:22:38 -08005228 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005229 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5230 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005231 goto out;
5232
Glauber Costa510fc4e2012-12-18 14:21:47 -08005233 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005234 /*
5235 * When that happen, we need to disable the static branch only on those
5236 * memcgs that enabled it. To achieve this, we would be forced to
5237 * complicate the code by keeping track of which memcgs were the ones
5238 * that actually enabled limits, and which ones got it from its
5239 * parents.
5240 *
5241 * It is a lot simpler just to do static_key_slow_inc() on every child
5242 * that is accounted.
5243 */
Glauber Costa55007d82012-12-18 14:22:38 -08005244 if (!memcg_kmem_is_active(memcg))
5245 goto out;
5246
5247 /*
5248 * destroy(), called if we fail, will issue static_key_slow_inc() and
5249 * mem_cgroup_put() if kmem is enabled. We have to either call them
5250 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5251 * this more consistent, since it always leads to the same destroy path
5252 */
5253 mem_cgroup_get(memcg);
5254 static_key_slow_inc(&memcg_kmem_enabled_key);
5255
5256 mutex_lock(&set_limit_mutex);
5257 ret = memcg_update_cache_sizes(memcg);
5258 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005259out:
5260 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005261}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005262#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005263
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005264/*
5265 * The user of this function is...
5266 * RES_LIMIT.
5267 */
Paul Menage856c13a2008-07-25 01:47:04 -07005268static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5269 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005270{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005271 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005272 enum res_type type;
5273 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005274 unsigned long long val;
5275 int ret;
5276
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005277 type = MEMFILE_TYPE(cft->private);
5278 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005279
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005280 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005281 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005282 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5283 ret = -EINVAL;
5284 break;
5285 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005286 /* This function does all necessary parse...reuse it */
5287 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005288 if (ret)
5289 break;
5290 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005291 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005292 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005293 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005294 else if (type == _KMEM)
5295 ret = memcg_update_kmem_limit(cont, val);
5296 else
5297 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005298 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005299 case RES_SOFT_LIMIT:
5300 ret = res_counter_memparse_write_strategy(buffer, &val);
5301 if (ret)
5302 break;
5303 /*
5304 * For memsw, soft limits are hard to implement in terms
5305 * of semantics, for now, we support soft limits for
5306 * control without swap
5307 */
5308 if (type == _MEM)
5309 ret = res_counter_set_soft_limit(&memcg->res, val);
5310 else
5311 ret = -EINVAL;
5312 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005313 default:
5314 ret = -EINVAL; /* should be BUG() ? */
5315 break;
5316 }
5317 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005318}
5319
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005320static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5321 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5322{
5323 struct cgroup *cgroup;
5324 unsigned long long min_limit, min_memsw_limit, tmp;
5325
5326 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5327 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5328 cgroup = memcg->css.cgroup;
5329 if (!memcg->use_hierarchy)
5330 goto out;
5331
5332 while (cgroup->parent) {
5333 cgroup = cgroup->parent;
5334 memcg = mem_cgroup_from_cont(cgroup);
5335 if (!memcg->use_hierarchy)
5336 break;
5337 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5338 min_limit = min(min_limit, tmp);
5339 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5340 min_memsw_limit = min(min_memsw_limit, tmp);
5341 }
5342out:
5343 *mem_limit = min_limit;
5344 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005345}
5346
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005347static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005348{
Tejun Heoaf36f902012-04-01 12:09:55 -07005349 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005350 int name;
5351 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005352
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005353 type = MEMFILE_TYPE(event);
5354 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005355
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005356 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005357 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005358 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005359 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005360 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005361 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005362 else if (type == _KMEM)
5363 res_counter_reset_max(&memcg->kmem);
5364 else
5365 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005366 break;
5367 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005368 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005369 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005370 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005371 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005372 else if (type == _KMEM)
5373 res_counter_reset_failcnt(&memcg->kmem);
5374 else
5375 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005376 break;
5377 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005378
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005379 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005380}
5381
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005382static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5383 struct cftype *cft)
5384{
5385 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5386}
5387
Daisuke Nishimura02491442010-03-10 15:22:17 -08005388#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005389static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5390 struct cftype *cft, u64 val)
5391{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005392 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005393
5394 if (val >= (1 << NR_MOVE_TYPE))
5395 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005396
Glauber Costaee5e8472013-02-22 16:34:50 -08005397 /*
5398 * No kind of locking is needed in here, because ->can_attach() will
5399 * check this value once in the beginning of the process, and then carry
5400 * on with stale data. This means that changes to this value will only
5401 * affect task migrations starting after the change.
5402 */
5403 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005404 return 0;
5405}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005406#else
5407static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5408 struct cftype *cft, u64 val)
5409{
5410 return -ENOSYS;
5411}
5412#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005413
Ying Han406eb0c2011-05-26 16:25:37 -07005414#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005415static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005416 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005417{
5418 int nid;
5419 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5420 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005421 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005422
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005423 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005424 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005425 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005426 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005427 seq_printf(m, " N%d=%lu", nid, node_nr);
5428 }
5429 seq_putc(m, '\n');
5430
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005431 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005432 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005433 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005434 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005435 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005436 seq_printf(m, " N%d=%lu", nid, node_nr);
5437 }
5438 seq_putc(m, '\n');
5439
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005440 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005441 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005442 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005443 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005444 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005445 seq_printf(m, " N%d=%lu", nid, node_nr);
5446 }
5447 seq_putc(m, '\n');
5448
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005449 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005450 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005451 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005452 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005453 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005454 seq_printf(m, " N%d=%lu", nid, node_nr);
5455 }
5456 seq_putc(m, '\n');
5457 return 0;
5458}
5459#endif /* CONFIG_NUMA */
5460
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005461static inline void mem_cgroup_lru_names_not_uptodate(void)
5462{
5463 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5464}
5465
Wanpeng Liab215882012-07-31 16:43:09 -07005466static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005467 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005468{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005469 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005470 struct mem_cgroup *mi;
5471 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005472
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005473 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005474 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005475 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005476 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5477 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005478 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005479
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005480 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5481 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5482 mem_cgroup_read_events(memcg, i));
5483
5484 for (i = 0; i < NR_LRU_LISTS; i++)
5485 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5486 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5487
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005488 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005489 {
5490 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005491 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005492 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005493 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005494 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5495 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005496 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005497
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005498 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5499 long long val = 0;
5500
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005501 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005502 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005503 for_each_mem_cgroup_tree(mi, memcg)
5504 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5505 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5506 }
5507
5508 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5509 unsigned long long val = 0;
5510
5511 for_each_mem_cgroup_tree(mi, memcg)
5512 val += mem_cgroup_read_events(mi, i);
5513 seq_printf(m, "total_%s %llu\n",
5514 mem_cgroup_events_names[i], val);
5515 }
5516
5517 for (i = 0; i < NR_LRU_LISTS; i++) {
5518 unsigned long long val = 0;
5519
5520 for_each_mem_cgroup_tree(mi, memcg)
5521 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5522 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005523 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005524
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005525#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005526 {
5527 int nid, zid;
5528 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005529 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005530 unsigned long recent_rotated[2] = {0, 0};
5531 unsigned long recent_scanned[2] = {0, 0};
5532
5533 for_each_online_node(nid)
5534 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005535 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005536 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005537
Hugh Dickins89abfab2012-05-29 15:06:53 -07005538 recent_rotated[0] += rstat->recent_rotated[0];
5539 recent_rotated[1] += rstat->recent_rotated[1];
5540 recent_scanned[0] += rstat->recent_scanned[0];
5541 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005542 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005543 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5544 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5545 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5546 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005547 }
5548#endif
5549
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005550 return 0;
5551}
5552
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005553static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5554{
5555 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5556
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005557 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005558}
5559
5560static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5561 u64 val)
5562{
5563 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5564 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005565
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005566 if (val > 100)
5567 return -EINVAL;
5568
5569 if (cgrp->parent == NULL)
5570 return -EINVAL;
5571
5572 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005573
Glauber Costa09998212013-02-22 16:34:55 -08005574 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005575
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005576 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005577 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005578 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005579 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005580 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005581
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005582 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005583
Glauber Costa09998212013-02-22 16:34:55 -08005584 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005585
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005586 return 0;
5587}
5588
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005589static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5590{
5591 struct mem_cgroup_threshold_ary *t;
5592 u64 usage;
5593 int i;
5594
5595 rcu_read_lock();
5596 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005597 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005598 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005599 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005600
5601 if (!t)
5602 goto unlock;
5603
5604 usage = mem_cgroup_usage(memcg, swap);
5605
5606 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005607 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005608 * If it's not true, a threshold was crossed after last
5609 * call of __mem_cgroup_threshold().
5610 */
Phil Carmody5407a562010-05-26 14:42:42 -07005611 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005612
5613 /*
5614 * Iterate backward over array of thresholds starting from
5615 * current_threshold and check if a threshold is crossed.
5616 * If none of thresholds below usage is crossed, we read
5617 * only one element of the array here.
5618 */
5619 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5620 eventfd_signal(t->entries[i].eventfd, 1);
5621
5622 /* i = current_threshold + 1 */
5623 i++;
5624
5625 /*
5626 * Iterate forward over array of thresholds starting from
5627 * current_threshold+1 and check if a threshold is crossed.
5628 * If none of thresholds above usage is crossed, we read
5629 * only one element of the array here.
5630 */
5631 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5632 eventfd_signal(t->entries[i].eventfd, 1);
5633
5634 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005635 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005636unlock:
5637 rcu_read_unlock();
5638}
5639
5640static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5641{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005642 while (memcg) {
5643 __mem_cgroup_threshold(memcg, false);
5644 if (do_swap_account)
5645 __mem_cgroup_threshold(memcg, true);
5646
5647 memcg = parent_mem_cgroup(memcg);
5648 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005649}
5650
5651static int compare_thresholds(const void *a, const void *b)
5652{
5653 const struct mem_cgroup_threshold *_a = a;
5654 const struct mem_cgroup_threshold *_b = b;
5655
Greg Thelend96fa172013-09-11 14:23:08 -07005656 if (_a->threshold > _b->threshold)
5657 return 1;
5658
5659 if (_a->threshold < _b->threshold)
5660 return -1;
5661
5662 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005663}
5664
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005665static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005666{
5667 struct mem_cgroup_eventfd_list *ev;
5668
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005669 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005670 eventfd_signal(ev->eventfd, 1);
5671 return 0;
5672}
5673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005674static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005675{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005676 struct mem_cgroup *iter;
5677
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005678 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005679 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005680}
5681
5682static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5683 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005684{
5685 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005686 struct mem_cgroup_thresholds *thresholds;
5687 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005688 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005689 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005690 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005691
5692 ret = res_counter_memparse_write_strategy(args, &threshold);
5693 if (ret)
5694 return ret;
5695
5696 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005697
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005698 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005699 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005700 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005701 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005702 else
5703 BUG();
5704
5705 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5706
5707 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005708 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005709 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5710
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005711 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005712
5713 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005714 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005715 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005716 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005717 ret = -ENOMEM;
5718 goto unlock;
5719 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005720 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005721
5722 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005723 if (thresholds->primary) {
5724 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005725 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005726 }
5727
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005728 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005729 new->entries[size - 1].eventfd = eventfd;
5730 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005731
5732 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005733 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005734 compare_thresholds, NULL);
5735
5736 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005737 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005738 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005739 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005740 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005741 * new->current_threshold will not be used until
5742 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005743 * it here.
5744 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005745 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005746 } else
5747 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005748 }
5749
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005750 /* Free old spare buffer and save old primary buffer as spare */
5751 kfree(thresholds->spare);
5752 thresholds->spare = thresholds->primary;
5753
5754 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005755
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005756 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005757 synchronize_rcu();
5758
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005759unlock:
5760 mutex_unlock(&memcg->thresholds_lock);
5761
5762 return ret;
5763}
5764
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005765static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005766 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005767{
5768 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005769 struct mem_cgroup_thresholds *thresholds;
5770 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005771 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005772 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005773 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005774
5775 mutex_lock(&memcg->thresholds_lock);
5776 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005777 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005778 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005779 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005780 else
5781 BUG();
5782
Anton Vorontsov371528c2012-02-24 05:14:46 +04005783 if (!thresholds->primary)
5784 goto unlock;
5785
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005786 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5787
5788 /* Check if a threshold crossed before removing */
5789 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5790
5791 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005792 size = 0;
5793 for (i = 0; i < thresholds->primary->size; i++) {
5794 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005795 size++;
5796 }
5797
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005798 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005799
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005800 /* Set thresholds array to NULL if we don't have thresholds */
5801 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005802 kfree(new);
5803 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005804 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005805 }
5806
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005807 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005808
5809 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005810 new->current_threshold = -1;
5811 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5812 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005813 continue;
5814
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005815 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005816 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005817 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005818 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005819 * until rcu_assign_pointer(), so it's safe to increment
5820 * it here.
5821 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005822 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005823 }
5824 j++;
5825 }
5826
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005827swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005828 /* Swap primary and spare array */
5829 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005830 /* If all events are unregistered, free the spare array */
5831 if (!new) {
5832 kfree(thresholds->spare);
5833 thresholds->spare = NULL;
5834 }
5835
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005836 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005837
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005838 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005839 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005840unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005841 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005842}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005843
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005844static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5845 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5846{
5847 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5848 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005849 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005850
5851 BUG_ON(type != _OOM_TYPE);
5852 event = kmalloc(sizeof(*event), GFP_KERNEL);
5853 if (!event)
5854 return -ENOMEM;
5855
Michal Hocko1af8efe2011-07-26 16:08:24 -07005856 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005857
5858 event->eventfd = eventfd;
5859 list_add(&event->list, &memcg->oom_notify);
5860
5861 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005862 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005863 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005864 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005865
5866 return 0;
5867}
5868
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005869static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005870 struct cftype *cft, struct eventfd_ctx *eventfd)
5871{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005872 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005873 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005874 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005875
5876 BUG_ON(type != _OOM_TYPE);
5877
Michal Hocko1af8efe2011-07-26 16:08:24 -07005878 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005879
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005880 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005881 if (ev->eventfd == eventfd) {
5882 list_del(&ev->list);
5883 kfree(ev);
5884 }
5885 }
5886
Michal Hocko1af8efe2011-07-26 16:08:24 -07005887 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005888}
5889
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005890static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5891 struct cftype *cft, struct cgroup_map_cb *cb)
5892{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005893 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005894
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005895 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005896
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005897 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005898 cb->fill(cb, "under_oom", 1);
5899 else
5900 cb->fill(cb, "under_oom", 0);
5901 return 0;
5902}
5903
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005904static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5905 struct cftype *cft, u64 val)
5906{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005907 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005908 struct mem_cgroup *parent;
5909
5910 /* cannot set to root cgroup and only 0 and 1 are allowed */
5911 if (!cgrp->parent || !((val == 0) || (val == 1)))
5912 return -EINVAL;
5913
5914 parent = mem_cgroup_from_cont(cgrp->parent);
5915
Glauber Costa09998212013-02-22 16:34:55 -08005916 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005917 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005918 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005919 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005920 return -EINVAL;
5921 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005922 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005923 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005924 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005925 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005926 return 0;
5927}
5928
Andrew Mortonc255a452012-07-31 16:43:02 -07005929#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005930static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005931{
Glauber Costa55007d82012-12-18 14:22:38 -08005932 int ret;
5933
Glauber Costa2633d7a2012-12-18 14:22:34 -08005934 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005935 ret = memcg_propagate_kmem(memcg);
5936 if (ret)
5937 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005938
Glauber Costa1d62e432012-04-09 19:36:33 -03005939 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005940}
Glauber Costae5671df2011-12-11 21:47:01 +00005941
Glauber Costa1d62e432012-04-09 19:36:33 -03005942static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005943{
Glauber Costa1d62e432012-04-09 19:36:33 -03005944 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005945
5946 memcg_kmem_mark_dead(memcg);
5947
5948 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5949 return;
5950
5951 /*
5952 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5953 * path here, being careful not to race with memcg_uncharge_kmem: it is
5954 * possible that the charges went down to 0 between mark_dead and the
5955 * res_counter read, so in that case, we don't need the put
5956 */
5957 if (memcg_kmem_test_and_clear_dead(memcg))
5958 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005959}
Glauber Costae5671df2011-12-11 21:47:01 +00005960#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005961static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005962{
5963 return 0;
5964}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005965
Glauber Costa1d62e432012-04-09 19:36:33 -03005966static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005967{
5968}
Glauber Costae5671df2011-12-11 21:47:01 +00005969#endif
5970
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005971static struct cftype mem_cgroup_files[] = {
5972 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005973 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005974 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005975 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005976 .register_event = mem_cgroup_usage_register_event,
5977 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005978 },
5979 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005980 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005981 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005982 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005983 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005984 },
5985 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005986 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005987 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005988 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005989 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005990 },
5991 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005992 .name = "soft_limit_in_bytes",
5993 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5994 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005995 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005996 },
5997 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005998 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005999 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07006000 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07006001 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006002 },
Balbir Singh8697d332008-02-07 00:13:59 -08006003 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006004 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006005 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006006 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08006007 {
6008 .name = "force_empty",
6009 .trigger = mem_cgroup_force_empty_write,
6010 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08006011 {
6012 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07006013 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08006014 .write_u64 = mem_cgroup_hierarchy_write,
6015 .read_u64 = mem_cgroup_hierarchy_read,
6016 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006017 {
6018 .name = "swappiness",
6019 .read_u64 = mem_cgroup_swappiness_read,
6020 .write_u64 = mem_cgroup_swappiness_write,
6021 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006022 {
6023 .name = "move_charge_at_immigrate",
6024 .read_u64 = mem_cgroup_move_charge_read,
6025 .write_u64 = mem_cgroup_move_charge_write,
6026 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006027 {
6028 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07006029 .read_map = mem_cgroup_oom_control_read,
6030 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006031 .register_event = mem_cgroup_oom_register_event,
6032 .unregister_event = mem_cgroup_oom_unregister_event,
6033 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
6034 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006035 {
6036 .name = "pressure_level",
6037 .register_event = vmpressure_register_event,
6038 .unregister_event = vmpressure_unregister_event,
6039 },
Ying Han406eb0c2011-05-26 16:25:37 -07006040#ifdef CONFIG_NUMA
6041 {
6042 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006043 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07006044 },
6045#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006046#ifdef CONFIG_MEMCG_KMEM
6047 {
6048 .name = "kmem.limit_in_bytes",
6049 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
6050 .write_string = mem_cgroup_write,
6051 .read = mem_cgroup_read,
6052 },
6053 {
6054 .name = "kmem.usage_in_bytes",
6055 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
6056 .read = mem_cgroup_read,
6057 },
6058 {
6059 .name = "kmem.failcnt",
6060 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
6061 .trigger = mem_cgroup_reset,
6062 .read = mem_cgroup_read,
6063 },
6064 {
6065 .name = "kmem.max_usage_in_bytes",
6066 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6067 .trigger = mem_cgroup_reset,
6068 .read = mem_cgroup_read,
6069 },
Glauber Costa749c5412012-12-18 14:23:01 -08006070#ifdef CONFIG_SLABINFO
6071 {
6072 .name = "kmem.slabinfo",
6073 .read_seq_string = mem_cgroup_slabinfo_read,
6074 },
6075#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006076#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07006077 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07006078};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006079
Michal Hocko2d110852013-02-22 16:34:43 -08006080#ifdef CONFIG_MEMCG_SWAP
6081static struct cftype memsw_cgroup_files[] = {
6082 {
6083 .name = "memsw.usage_in_bytes",
6084 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6085 .read = mem_cgroup_read,
6086 .register_event = mem_cgroup_usage_register_event,
6087 .unregister_event = mem_cgroup_usage_unregister_event,
6088 },
6089 {
6090 .name = "memsw.max_usage_in_bytes",
6091 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6092 .trigger = mem_cgroup_reset,
6093 .read = mem_cgroup_read,
6094 },
6095 {
6096 .name = "memsw.limit_in_bytes",
6097 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6098 .write_string = mem_cgroup_write,
6099 .read = mem_cgroup_read,
6100 },
6101 {
6102 .name = "memsw.failcnt",
6103 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6104 .trigger = mem_cgroup_reset,
6105 .read = mem_cgroup_read,
6106 },
6107 { }, /* terminate */
6108};
6109#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006110static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006111{
6112 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006113 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006114 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006115 /*
6116 * This routine is called against possible nodes.
6117 * But it's BUG to call kmalloc() against offline node.
6118 *
6119 * TODO: this routine can waste much memory for nodes which will
6120 * never be onlined. It's better to use memory hotplug callback
6121 * function.
6122 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006123 if (!node_state(node, N_NORMAL_MEMORY))
6124 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08006125 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006126 if (!pn)
6127 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006128
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006129 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6130 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08006131 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006132 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07006133 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006134 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006135 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07006136 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006137 return 0;
6138}
6139
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006140static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006141{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006142 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006143}
6144
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006145static struct mem_cgroup *mem_cgroup_alloc(void)
6146{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006147 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006148 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006149
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006150 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08006151 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006152 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006153 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006154 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006155
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006156 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07006157 return NULL;
6158
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006159 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6160 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006161 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006162 spin_lock_init(&memcg->pcp_counter_lock);
6163 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006164
6165out_free:
6166 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006167 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006168 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006169 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006170 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006171}
6172
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006173/*
Glauber Costac8b2a362012-12-18 14:22:13 -08006174 * At destroying mem_cgroup, references from swap_cgroup can remain.
6175 * (scanning all at force_empty is too costly...)
6176 *
6177 * Instead of clearing all references at force_empty, we remember
6178 * the number of reference from swap_cgroup and free mem_cgroup when
6179 * it goes down to 0.
6180 *
6181 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07006182 */
Glauber Costac8b2a362012-12-18 14:22:13 -08006183
6184static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006185{
Glauber Costac8b2a362012-12-18 14:22:13 -08006186 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006187 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006188
Glauber Costac8b2a362012-12-18 14:22:13 -08006189 mem_cgroup_remove_from_trees(memcg);
6190 free_css_id(&mem_cgroup_subsys, &memcg->css);
6191
6192 for_each_node(node)
6193 free_mem_cgroup_per_zone_info(memcg, node);
6194
6195 free_percpu(memcg->stat);
6196
Glauber Costa3f134612012-05-29 15:07:11 -07006197 /*
6198 * We need to make sure that (at least for now), the jump label
6199 * destruction code runs outside of the cgroup lock. This is because
6200 * get_online_cpus(), which is called from the static_branch update,
6201 * can't be called inside the cgroup_lock. cpusets are the ones
6202 * enforcing this dependency, so if they ever change, we might as well.
6203 *
6204 * schedule_work() will guarantee this happens. Be careful if you need
6205 * to move this code around, and make sure it is outside
6206 * the cgroup_lock.
6207 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006208 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006209 if (size < PAGE_SIZE)
6210 kfree(memcg);
6211 else
6212 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006213}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006214
Glauber Costac8b2a362012-12-18 14:22:13 -08006215
6216/*
6217 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6218 * but in process context. The work_freeing structure is overlaid
6219 * on the rcu_freeing structure, which itself is overlaid on memsw.
6220 */
6221static void free_work(struct work_struct *work)
6222{
6223 struct mem_cgroup *memcg;
6224
6225 memcg = container_of(work, struct mem_cgroup, work_freeing);
6226 __mem_cgroup_free(memcg);
6227}
6228
Glauber Costa3afe36b2012-05-29 15:07:10 -07006229static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006230{
6231 struct mem_cgroup *memcg;
6232
6233 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006234 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006235 schedule_work(&memcg->work_freeing);
6236}
6237
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006238static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006239{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006240 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006241}
6242
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006243static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006244{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006245 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6246 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006247 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006248 if (parent)
6249 mem_cgroup_put(parent);
6250 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006251}
6252
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006253static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006254{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006255 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006256}
6257
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006258/*
6259 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6260 */
Glauber Costae1aab162011-12-11 21:47:03 +00006261struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006262{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006263 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006264 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006265 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006266}
Glauber Costae1aab162011-12-11 21:47:03 +00006267EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006268
Michal Hocko8787a1d2013-02-22 16:35:39 -08006269static void __init mem_cgroup_soft_limit_tree_init(void)
Balbir Singhf64c3f52009-09-23 15:56:37 -07006270{
6271 struct mem_cgroup_tree_per_node *rtpn;
6272 struct mem_cgroup_tree_per_zone *rtpz;
6273 int tmp, node, zone;
6274
Bob Liu3ed28fa2012-01-12 17:19:04 -08006275 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006276 tmp = node;
6277 if (!node_state(node, N_NORMAL_MEMORY))
6278 tmp = -1;
6279 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
Michal Hocko8787a1d2013-02-22 16:35:39 -08006280 BUG_ON(!rtpn);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006281
6282 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6283
6284 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6285 rtpz = &rtpn->rb_tree_per_zone[zone];
6286 rtpz->rb_root = RB_ROOT;
6287 spin_lock_init(&rtpz->lock);
6288 }
6289 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07006290}
6291
Li Zefan0eb253e2009-01-15 13:51:25 -08006292static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006293mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006294{
Glauber Costad142e3e2013-02-22 16:34:52 -08006295 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006296 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006297 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006298
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006299 memcg = mem_cgroup_alloc();
6300 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006301 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006302
Bob Liu3ed28fa2012-01-12 17:19:04 -08006303 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006304 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006305 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006306
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006307 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006308 if (cont->parent == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006309 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006310 res_counter_init(&memcg->res, NULL);
6311 res_counter_init(&memcg->memsw, NULL);
6312 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006313 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006314
Glauber Costad142e3e2013-02-22 16:34:52 -08006315 memcg->last_scanned_node = MAX_NUMNODES;
6316 INIT_LIST_HEAD(&memcg->oom_notify);
6317 atomic_set(&memcg->refcnt, 1);
6318 memcg->move_charge_at_immigrate = 0;
6319 mutex_init(&memcg->thresholds_lock);
6320 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006321 vmpressure_init(&memcg->vmpressure);
Glauber Costad142e3e2013-02-22 16:34:52 -08006322
6323 return &memcg->css;
6324
6325free_out:
6326 __mem_cgroup_free(memcg);
6327 return ERR_PTR(error);
6328}
6329
6330static int
6331mem_cgroup_css_online(struct cgroup *cont)
6332{
6333 struct mem_cgroup *memcg, *parent;
6334 int error = 0;
6335
6336 if (!cont->parent)
6337 return 0;
6338
Glauber Costa09998212013-02-22 16:34:55 -08006339 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006340 memcg = mem_cgroup_from_cont(cont);
6341 parent = mem_cgroup_from_cont(cont->parent);
6342
6343 memcg->use_hierarchy = parent->use_hierarchy;
6344 memcg->oom_kill_disable = parent->oom_kill_disable;
6345 memcg->swappiness = mem_cgroup_swappiness(parent);
6346
6347 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006348 res_counter_init(&memcg->res, &parent->res);
6349 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006350 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006351
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006352 /*
6353 * We increment refcnt of the parent to ensure that we can
6354 * safely access it on res_counter_charge/uncharge.
6355 * This refcnt will be decremented when freeing this
6356 * mem_cgroup(see mem_cgroup_put).
6357 */
6358 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006359 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006360 res_counter_init(&memcg->res, NULL);
6361 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006362 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006363 /*
6364 * Deeper hierachy with use_hierarchy == false doesn't make
6365 * much sense so let cgroup subsystem know about this
6366 * unfortunate state in our controller.
6367 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006368 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006369 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006370 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006371
6372 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006373 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006374 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006375}
6376
Michal Hocko5f578162013-04-29 15:07:17 -07006377/*
6378 * Announce all parents that a group from their hierarchy is gone.
6379 */
6380static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6381{
6382 struct mem_cgroup *parent = memcg;
6383
6384 while ((parent = parent_mem_cgroup(parent)))
6385 atomic_inc(&parent->dead_count);
6386
6387 /*
6388 * if the root memcg is not hierarchical we have to check it
6389 * explicitely.
6390 */
6391 if (!root_mem_cgroup->use_hierarchy)
6392 atomic_inc(&root_mem_cgroup->dead_count);
6393}
6394
Tejun Heo92fb9742012-11-19 08:13:38 -08006395static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006396{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006397 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006398 struct cgroup *iter;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006399
Michal Hocko5f578162013-04-29 15:07:17 -07006400 mem_cgroup_invalidate_reclaim_iterators(memcg);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006401
6402 /*
6403 * This requires that offlining is serialized. Right now that is
6404 * guaranteed because css_killed_work_fn() holds the cgroup_mutex.
6405 */
6406 rcu_read_lock();
6407 cgroup_for_each_descendant_post(iter, cont) {
6408 rcu_read_unlock();
6409 mem_cgroup_reparent_charges(mem_cgroup_from_cont(iter));
6410 rcu_read_lock();
6411 }
6412 rcu_read_unlock();
Michal Hockoab5196c2012-10-26 13:37:32 +02006413 mem_cgroup_reparent_charges(memcg);
Filipe Brandenburgerba9a7292014-03-03 15:38:25 -08006414
Glauber Costa1f458cb2012-12-18 14:22:50 -08006415 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006416}
6417
Tejun Heo92fb9742012-11-19 08:13:38 -08006418static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006419{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006420 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006421
Glauber Costa1d62e432012-04-09 19:36:33 -03006422 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006423
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006424 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006425}
6426
Daisuke Nishimura02491442010-03-10 15:22:17 -08006427#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006428/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006429#define PRECHARGE_COUNT_AT_ONCE 256
6430static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006431{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006432 int ret = 0;
6433 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006434 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006436 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006437 mc.precharge += count;
6438 /* we don't need css_get for root */
6439 return ret;
6440 }
6441 /* try to charge at once */
6442 if (count > 1) {
6443 struct res_counter *dummy;
6444 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006445 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006446 * by cgroup_lock_live_cgroup() that it is not removed and we
6447 * are still under the same cgroup_mutex. So we can postpone
6448 * css_get().
6449 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006450 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006451 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006452 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006453 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006454 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006455 goto one_by_one;
6456 }
6457 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006458 return ret;
6459 }
6460one_by_one:
6461 /* fall back to one by one charge */
6462 while (count--) {
6463 if (signal_pending(current)) {
6464 ret = -EINTR;
6465 break;
6466 }
6467 if (!batch_count--) {
6468 batch_count = PRECHARGE_COUNT_AT_ONCE;
6469 cond_resched();
6470 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006471 ret = __mem_cgroup_try_charge(NULL,
6472 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006473 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006474 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006475 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006476 mc.precharge++;
6477 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006478 return ret;
6479}
6480
6481/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006482 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006483 * @vma: the vma the pte to be checked belongs
6484 * @addr: the address corresponding to the pte to be checked
6485 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006486 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006487 *
6488 * Returns
6489 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6490 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6491 * move charge. if @target is not NULL, the page is stored in target->page
6492 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006493 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6494 * target for charge migration. if @target is not NULL, the entry is stored
6495 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006496 *
6497 * Called with pte lock held.
6498 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006499union mc_target {
6500 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006501 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006502};
6503
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006504enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006505 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006506 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006507 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006508};
6509
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006510static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6511 unsigned long addr, pte_t ptent)
6512{
6513 struct page *page = vm_normal_page(vma, addr, ptent);
6514
6515 if (!page || !page_mapped(page))
6516 return NULL;
6517 if (PageAnon(page)) {
6518 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006519 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006520 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006521 } else if (!move_file())
6522 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006523 return NULL;
6524 if (!get_page_unless_zero(page))
6525 return NULL;
6526
6527 return page;
6528}
6529
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006530#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006531static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6532 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6533{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006534 struct page *page = NULL;
6535 swp_entry_t ent = pte_to_swp_entry(ptent);
6536
6537 if (!move_anon() || non_swap_entry(ent))
6538 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006539 /*
6540 * Because lookup_swap_cache() updates some statistics counter,
6541 * we call find_get_page() with swapper_space directly.
6542 */
Shaohua Li33806f02013-02-22 16:34:37 -08006543 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006544 if (do_swap_account)
6545 entry->val = ent.val;
6546
6547 return page;
6548}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006549#else
6550static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6551 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6552{
6553 return NULL;
6554}
6555#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006556
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006557static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6558 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6559{
6560 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006561 struct address_space *mapping;
6562 pgoff_t pgoff;
6563
6564 if (!vma->vm_file) /* anonymous vma */
6565 return NULL;
6566 if (!move_file())
6567 return NULL;
6568
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006569 mapping = vma->vm_file->f_mapping;
6570 if (pte_none(ptent))
6571 pgoff = linear_page_index(vma, addr);
6572 else /* pte_file(ptent) is true */
6573 pgoff = pte_to_pgoff(ptent);
6574
6575 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006576 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006577
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006578#ifdef CONFIG_SWAP
6579 /* shmem/tmpfs may report page out on swap: account for that too. */
6580 if (radix_tree_exceptional_entry(page)) {
6581 swp_entry_t swap = radix_to_swp_entry(page);
6582 if (do_swap_account)
6583 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006584 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006585 }
6586#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006587 return page;
6588}
6589
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006590static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006591 unsigned long addr, pte_t ptent, union mc_target *target)
6592{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006593 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006594 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006595 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006596 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006597
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006598 if (pte_present(ptent))
6599 page = mc_handle_present_pte(vma, addr, ptent);
6600 else if (is_swap_pte(ptent))
6601 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006602 else if (pte_none(ptent) || pte_file(ptent))
6603 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006604
6605 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006606 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006607 if (page) {
6608 pc = lookup_page_cgroup(page);
6609 /*
6610 * Do only loose check w/o page_cgroup lock.
6611 * mem_cgroup_move_account() checks the pc is valid or not under
6612 * the lock.
6613 */
6614 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6615 ret = MC_TARGET_PAGE;
6616 if (target)
6617 target->page = page;
6618 }
6619 if (!ret || !target)
6620 put_page(page);
6621 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006622 /* There is a swap entry and a page doesn't exist or isn't charged */
6623 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006624 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006625 ret = MC_TARGET_SWAP;
6626 if (target)
6627 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006628 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006629 return ret;
6630}
6631
Naoya Horiguchi12724852012-03-21 16:34:28 -07006632#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6633/*
6634 * We don't consider swapping or file mapped pages because THP does not
6635 * support them for now.
6636 * Caller should make sure that pmd_trans_huge(pmd) is true.
6637 */
6638static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6639 unsigned long addr, pmd_t pmd, union mc_target *target)
6640{
6641 struct page *page = NULL;
6642 struct page_cgroup *pc;
6643 enum mc_target_type ret = MC_TARGET_NONE;
6644
6645 page = pmd_page(pmd);
6646 VM_BUG_ON(!page || !PageHead(page));
6647 if (!move_anon())
6648 return ret;
6649 pc = lookup_page_cgroup(page);
6650 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6651 ret = MC_TARGET_PAGE;
6652 if (target) {
6653 get_page(page);
6654 target->page = page;
6655 }
6656 }
6657 return ret;
6658}
6659#else
6660static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6661 unsigned long addr, pmd_t pmd, union mc_target *target)
6662{
6663 return MC_TARGET_NONE;
6664}
6665#endif
6666
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006667static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6668 unsigned long addr, unsigned long end,
6669 struct mm_walk *walk)
6670{
6671 struct vm_area_struct *vma = walk->private;
6672 pte_t *pte;
6673 spinlock_t *ptl;
6674
Naoya Horiguchi12724852012-03-21 16:34:28 -07006675 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6676 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6677 mc.precharge += HPAGE_PMD_NR;
6678 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006679 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006680 }
Dave Hansen03319322011-03-22 16:32:56 -07006681
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006682 if (pmd_trans_unstable(pmd))
6683 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006684 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6685 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006686 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006687 mc.precharge++; /* increment precharge temporarily */
6688 pte_unmap_unlock(pte - 1, ptl);
6689 cond_resched();
6690
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006691 return 0;
6692}
6693
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006694static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6695{
6696 unsigned long precharge;
6697 struct vm_area_struct *vma;
6698
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006699 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006700 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6701 struct mm_walk mem_cgroup_count_precharge_walk = {
6702 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6703 .mm = mm,
6704 .private = vma,
6705 };
6706 if (is_vm_hugetlb_page(vma))
6707 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006708 walk_page_range(vma->vm_start, vma->vm_end,
6709 &mem_cgroup_count_precharge_walk);
6710 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006711 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006712
6713 precharge = mc.precharge;
6714 mc.precharge = 0;
6715
6716 return precharge;
6717}
6718
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006719static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6720{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006721 unsigned long precharge = mem_cgroup_count_precharge(mm);
6722
6723 VM_BUG_ON(mc.moving_task);
6724 mc.moving_task = current;
6725 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006726}
6727
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006728/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6729static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006730{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006731 struct mem_cgroup *from = mc.from;
6732 struct mem_cgroup *to = mc.to;
6733
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006734 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006735 if (mc.precharge) {
6736 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6737 mc.precharge = 0;
6738 }
6739 /*
6740 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6741 * we must uncharge here.
6742 */
6743 if (mc.moved_charge) {
6744 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6745 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006746 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006747 /* we must fixup refcnts and charges */
6748 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006749 /* uncharge swap account from the old cgroup */
6750 if (!mem_cgroup_is_root(mc.from))
6751 res_counter_uncharge(&mc.from->memsw,
6752 PAGE_SIZE * mc.moved_swap);
6753 __mem_cgroup_put(mc.from, mc.moved_swap);
6754
6755 if (!mem_cgroup_is_root(mc.to)) {
6756 /*
6757 * we charged both to->res and to->memsw, so we should
6758 * uncharge to->res.
6759 */
6760 res_counter_uncharge(&mc.to->res,
6761 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006762 }
6763 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006764 mc.moved_swap = 0;
6765 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006766 memcg_oom_recover(from);
6767 memcg_oom_recover(to);
6768 wake_up_all(&mc.waitq);
6769}
6770
6771static void mem_cgroup_clear_mc(void)
6772{
6773 struct mem_cgroup *from = mc.from;
6774
6775 /*
6776 * we must clear moving_task before waking up waiters at the end of
6777 * task migration.
6778 */
6779 mc.moving_task = NULL;
6780 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006781 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006782 mc.from = NULL;
6783 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006784 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006785 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006786}
6787
Li Zefan761b3ef2012-01-31 13:47:36 +08006788static int mem_cgroup_can_attach(struct cgroup *cgroup,
6789 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006790{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006791 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006792 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006793 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006794 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006795
Glauber Costaee5e8472013-02-22 16:34:50 -08006796 /*
6797 * We are now commited to this value whatever it is. Changes in this
6798 * tunable will only affect upcoming migrations, not the current one.
6799 * So we need to save it, and keep it going.
6800 */
6801 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6802 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006803 struct mm_struct *mm;
6804 struct mem_cgroup *from = mem_cgroup_from_task(p);
6805
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006806 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006807
6808 mm = get_task_mm(p);
6809 if (!mm)
6810 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006811 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006812 if (mm->owner == p) {
6813 VM_BUG_ON(mc.from);
6814 VM_BUG_ON(mc.to);
6815 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006816 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006817 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006818 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006819 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006820 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006821 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006822 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006823 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006824 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006825
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006826 ret = mem_cgroup_precharge_mc(mm);
6827 if (ret)
6828 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006829 }
6830 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006831 }
6832 return ret;
6833}
6834
Li Zefan761b3ef2012-01-31 13:47:36 +08006835static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6836 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006837{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006838 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006839}
6840
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006841static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6842 unsigned long addr, unsigned long end,
6843 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006844{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006845 int ret = 0;
6846 struct vm_area_struct *vma = walk->private;
6847 pte_t *pte;
6848 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006849 enum mc_target_type target_type;
6850 union mc_target target;
6851 struct page *page;
6852 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006853
Naoya Horiguchi12724852012-03-21 16:34:28 -07006854 /*
6855 * We don't take compound_lock() here but no race with splitting thp
6856 * happens because:
6857 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6858 * under splitting, which means there's no concurrent thp split,
6859 * - if another thread runs into split_huge_page() just after we
6860 * entered this if-block, the thread must wait for page table lock
6861 * to be unlocked in __split_huge_page_splitting(), where the main
6862 * part of thp split is not executed yet.
6863 */
6864 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006865 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006866 spin_unlock(&vma->vm_mm->page_table_lock);
6867 return 0;
6868 }
6869 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6870 if (target_type == MC_TARGET_PAGE) {
6871 page = target.page;
6872 if (!isolate_lru_page(page)) {
6873 pc = lookup_page_cgroup(page);
6874 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006875 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006876 mc.precharge -= HPAGE_PMD_NR;
6877 mc.moved_charge += HPAGE_PMD_NR;
6878 }
6879 putback_lru_page(page);
6880 }
6881 put_page(page);
6882 }
6883 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006884 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006885 }
6886
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006887 if (pmd_trans_unstable(pmd))
6888 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006889retry:
6890 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6891 for (; addr != end; addr += PAGE_SIZE) {
6892 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006893 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006894
6895 if (!mc.precharge)
6896 break;
6897
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006898 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006899 case MC_TARGET_PAGE:
6900 page = target.page;
6901 if (isolate_lru_page(page))
6902 goto put;
6903 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006904 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006905 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006906 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006907 /* we uncharge from mc.from later. */
6908 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006909 }
6910 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006911put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006912 put_page(page);
6913 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006914 case MC_TARGET_SWAP:
6915 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006916 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006917 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006918 /* we fixup refcnts and charges later. */
6919 mc.moved_swap++;
6920 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006921 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006922 default:
6923 break;
6924 }
6925 }
6926 pte_unmap_unlock(pte - 1, ptl);
6927 cond_resched();
6928
6929 if (addr != end) {
6930 /*
6931 * We have consumed all precharges we got in can_attach().
6932 * We try charge one by one, but don't do any additional
6933 * charges to mc.to if we have failed in charge once in attach()
6934 * phase.
6935 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006936 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006937 if (!ret)
6938 goto retry;
6939 }
6940
6941 return ret;
6942}
6943
6944static void mem_cgroup_move_charge(struct mm_struct *mm)
6945{
6946 struct vm_area_struct *vma;
6947
6948 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006949retry:
6950 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6951 /*
6952 * Someone who are holding the mmap_sem might be waiting in
6953 * waitq. So we cancel all extra charges, wake up all waiters,
6954 * and retry. Because we cancel precharges, we might not be able
6955 * to move enough charges, but moving charge is a best-effort
6956 * feature anyway, so it wouldn't be a big problem.
6957 */
6958 __mem_cgroup_clear_mc();
6959 cond_resched();
6960 goto retry;
6961 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006962 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6963 int ret;
6964 struct mm_walk mem_cgroup_move_charge_walk = {
6965 .pmd_entry = mem_cgroup_move_charge_pte_range,
6966 .mm = mm,
6967 .private = vma,
6968 };
6969 if (is_vm_hugetlb_page(vma))
6970 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006971 ret = walk_page_range(vma->vm_start, vma->vm_end,
6972 &mem_cgroup_move_charge_walk);
6973 if (ret)
6974 /*
6975 * means we have consumed all precharges and failed in
6976 * doing additional charge. Just abandon here.
6977 */
6978 break;
6979 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006980 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006981}
6982
Li Zefan761b3ef2012-01-31 13:47:36 +08006983static void mem_cgroup_move_task(struct cgroup *cont,
6984 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006985{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006986 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006987 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006988
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006989 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006990 if (mc.to)
6991 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006992 mmput(mm);
6993 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006994 if (mc.to)
6995 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006996}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006997#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006998static int mem_cgroup_can_attach(struct cgroup *cgroup,
6999 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007000{
7001 return 0;
7002}
Li Zefan761b3ef2012-01-31 13:47:36 +08007003static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
7004 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007005{
7006}
Li Zefan761b3ef2012-01-31 13:47:36 +08007007static void mem_cgroup_move_task(struct cgroup *cont,
7008 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007009{
7010}
7011#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08007012
Tejun Heof00baae2013-04-15 13:41:15 -07007013/*
7014 * Cgroup retains root cgroups across [un]mount cycles making it necessary
7015 * to verify sane_behavior flag on each mount attempt.
7016 */
7017static void mem_cgroup_bind(struct cgroup *root)
7018{
7019 /*
7020 * use_hierarchy is forced with sane_behavior. cgroup core
7021 * guarantees that @root doesn't have any children, so turning it
7022 * on for the root memcg is enough.
7023 */
7024 if (cgroup_sane_behavior(root))
7025 mem_cgroup_from_cont(root)->use_hierarchy = true;
7026}
7027
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007028struct cgroup_subsys mem_cgroup_subsys = {
7029 .name = "memory",
7030 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007031 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08007032 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08007033 .css_offline = mem_cgroup_css_offline,
7034 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007035 .can_attach = mem_cgroup_can_attach,
7036 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08007037 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07007038 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07007039 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08007040 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07007041 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007042};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007043
Andrew Mortonc255a452012-07-31 16:43:02 -07007044#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08007045static int __init enable_swap_account(char *s)
7046{
7047 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07007048 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007049 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07007050 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007051 really_do_swap_account = 0;
7052 return 1;
7053}
Michal Hockoa2c89902011-05-24 17:12:50 -07007054__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007055
Michal Hocko2d110852013-02-22 16:34:43 -08007056static void __init memsw_file_init(void)
7057{
Michal Hocko6acc8b02013-02-22 16:34:45 -08007058 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08007059}
Michal Hocko6acc8b02013-02-22 16:34:45 -08007060
7061static void __init enable_swap_cgroup(void)
7062{
7063 if (!mem_cgroup_disabled() && really_do_swap_account) {
7064 do_swap_account = 1;
7065 memsw_file_init();
7066 }
7067}
7068
Michal Hocko2d110852013-02-22 16:34:43 -08007069#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08007070static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08007071{
7072}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007073#endif
Michal Hocko2d110852013-02-22 16:34:43 -08007074
7075/*
Michal Hocko10813122013-02-22 16:35:41 -08007076 * subsys_initcall() for memory controller.
7077 *
7078 * Some parts like hotcpu_notifier() have to be initialized from this context
7079 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
7080 * everything that doesn't depend on a specific mem_cgroup structure should
7081 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08007082 */
7083static int __init mem_cgroup_init(void)
7084{
7085 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08007086 enable_swap_cgroup();
Michal Hocko8787a1d2013-02-22 16:35:39 -08007087 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08007088 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08007089 return 0;
7090}
7091subsys_initcall(mem_cgroup_init);