blob: d00368110b08367f6bf4d06e43ea847d0f5d4c73 [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>
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -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>
Tejun Heo79bd9812013-11-22 18:20:42 -050048#include <linux/poll.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080049#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080050#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080051#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070052#include <linux/vmalloc.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070053#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070054#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070055#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080056#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070057#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070058#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050059#include <linux/file.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080060#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000061#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070062#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000063#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080064
Balbir Singh8697d332008-02-07 00:13:59 -080065#include <asm/uaccess.h>
66
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070067#include <trace/events/vmscan.h>
68
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070069struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080070EXPORT_SYMBOL(mem_cgroup_subsys);
71
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070072#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070073static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080074
Andrew Mortonc255a452012-07-31 16:43:02 -070075#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070076/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080077int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080078
79/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070080#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080081static int really_do_swap_account __initdata = 1;
82#else
83static int really_do_swap_account __initdata = 0;
84#endif
85
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080086#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070087#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080088#endif
89
90
Johannes Weineraf7c4b02012-05-29 15:07:08 -070091static const char * const mem_cgroup_stat_names[] = {
92 "cache",
93 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070094 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070095 "mapped_file",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070096 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070097 "swap",
98};
99
Johannes Weinere9f89742011-03-23 16:42:37 -0700100enum mem_cgroup_events_index {
101 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
102 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700103 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
104 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700105 MEM_CGROUP_EVENTS_NSTATS,
106};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700107
108static const char * const mem_cgroup_events_names[] = {
109 "pgpgin",
110 "pgpgout",
111 "pgfault",
112 "pgmajfault",
113};
114
Sha Zhengju58cf1882013-02-22 16:32:05 -0800115static const char * const mem_cgroup_lru_names[] = {
116 "inactive_anon",
117 "active_anon",
118 "inactive_file",
119 "active_file",
120 "unevictable",
121};
122
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123/*
124 * Per memcg event counter is incremented at every pagein/pageout. With THP,
125 * it will be incremated by the number of pages. This counter is used for
126 * for trigger some periodic events. This is straightforward and better
127 * than using jiffies etc. to handle periodic memcg event.
128 */
129enum mem_cgroup_events_target {
130 MEM_CGROUP_TARGET_THRESH,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700131 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700132 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133 MEM_CGROUP_NTARGETS,
134};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700135#define THRESHOLDS_EVENTS_TARGET 128
136#define SOFTLIMIT_EVENTS_TARGET 1024
137#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700138
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800139struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700140 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700141 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700142 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700143 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800144};
145
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800146struct mem_cgroup_reclaim_iter {
Michal Hocko5f578162013-04-29 15:07:17 -0700147 /*
148 * last scanned hierarchy member. Valid only if last_dead_count
149 * matches memcg->dead_count of the hierarchy root group.
150 */
Michal Hocko542f85f2013-04-29 15:07:15 -0700151 struct mem_cgroup *last_visited;
Michal Hocko5f578162013-04-29 15:07:17 -0700152 unsigned long last_dead_count;
153
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800154 /* scan generation, increased every round-trip */
155 unsigned int generation;
156};
157
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800158/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800159 * per-zone information in memory controller.
160 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800161struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800162 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700163 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800164
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800165 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
166
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700167 struct rb_node tree_node; /* RB tree node */
168 unsigned long long usage_in_excess;/* Set to the value by which */
169 /* the soft limit is exceeded*/
170 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700171 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700172 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800173};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800174
175struct mem_cgroup_per_node {
176 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
177};
178
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700179/*
180 * Cgroups above their limits are maintained in a RB-Tree, independent of
181 * their hierarchy representation
182 */
183
184struct mem_cgroup_tree_per_zone {
185 struct rb_root rb_root;
186 spinlock_t lock;
187};
188
189struct mem_cgroup_tree_per_node {
190 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
191};
192
193struct mem_cgroup_tree {
194 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
195};
196
197static struct mem_cgroup_tree soft_limit_tree __read_mostly;
198
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800199struct mem_cgroup_threshold {
200 struct eventfd_ctx *eventfd;
201 u64 threshold;
202};
203
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700204/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800205struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700206 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700207 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800208 /* Size of entries[] */
209 unsigned int size;
210 /* Array of thresholds */
211 struct mem_cgroup_threshold entries[0];
212};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700213
214struct mem_cgroup_thresholds {
215 /* Primary thresholds array */
216 struct mem_cgroup_threshold_ary *primary;
217 /*
218 * Spare threshold array.
219 * This is needed to make mem_cgroup_unregister_event() "never fail".
220 * It must be able to store at least primary->size - 1 entries.
221 */
222 struct mem_cgroup_threshold_ary *spare;
223};
224
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700225/* for OOM */
226struct mem_cgroup_eventfd_list {
227 struct list_head list;
228 struct eventfd_ctx *eventfd;
229};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800230
Tejun Heo79bd9812013-11-22 18:20:42 -0500231/*
232 * cgroup_event represents events which userspace want to receive.
233 */
234struct cgroup_event {
235 /*
236 * css which the event belongs to.
237 */
238 struct cgroup_subsys_state *css;
239 /*
240 * Control file which the event associated.
241 */
242 struct cftype *cft;
243 /*
244 * eventfd to signal userspace about the event.
245 */
246 struct eventfd_ctx *eventfd;
247 /*
248 * Each of these stored in a list by the cgroup.
249 */
250 struct list_head list;
251 /*
252 * All fields below needed to unregister event when
253 * userspace closes eventfd.
254 */
255 poll_table pt;
256 wait_queue_head_t *wqh;
257 wait_queue_t wait;
258 struct work_struct remove;
259};
260
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700261static void mem_cgroup_threshold(struct mem_cgroup *memcg);
262static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800263
Balbir Singhf64c3f52009-09-23 15:56:37 -0700264/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800265 * The memory controller data structure. The memory controller controls both
266 * page cache and RSS per cgroup. We would eventually like to provide
267 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
268 * to help the administrator determine what knobs to tune.
269 *
270 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800271 * we hit the water mark. May be even add a low water mark, such that
272 * no reclaim occurs from a cgroup at it's low water mark, this is
273 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800274 */
275struct mem_cgroup {
276 struct cgroup_subsys_state css;
277 /*
278 * the counter to account for memory usage
279 */
280 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700281
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700282 /* vmpressure notifications */
283 struct vmpressure vmpressure;
284
Li Zefan465939a2013-07-08 16:00:38 -0700285 /*
286 * the counter to account for mem+swap usage.
287 */
288 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700289
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800290 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800291 * the counter to account for kernel memory usage.
292 */
293 struct res_counter kmem;
294 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800295 * Should the accounting and control be hierarchical, per subtree?
296 */
297 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800298 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700299
300 bool oom_lock;
301 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700302 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700303
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700304 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700305 /* OOM-Killer disable */
306 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800307
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700308 /* set when res.limit == memsw.limit */
309 bool memsw_is_minimum;
310
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800311 /* protect arrays of thresholds */
312 struct mutex thresholds_lock;
313
314 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700315 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700316
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800317 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700318 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700319
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700320 /* For oom notifier event fd */
321 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700322
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800323 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800324 * Should we move charges of a task when a task is moved into this
325 * mem_cgroup ? And what type of charges should we move ?
326 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700327 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800328 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700329 * set > 0 if pages under this cgroup are moving to other cgroup.
330 */
331 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700332 /* taken only while moving_account > 0 */
333 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700334 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800335 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800336 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700337 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700338 /*
339 * used when a cpu is offlined or other synchronizations
340 * See mem_cgroup_read_stat().
341 */
342 struct mem_cgroup_stat_cpu nocpu_base;
343 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000344
Michal Hocko5f578162013-04-29 15:07:17 -0700345 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700346#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000347 struct tcp_memcontrol tcp_mem;
348#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800349#if defined(CONFIG_MEMCG_KMEM)
350 /* analogous to slab_common's slab_caches list. per-memcg */
351 struct list_head memcg_slab_caches;
352 /* Not a spinlock, we can take a lot of time walking the list */
353 struct mutex slab_caches_mutex;
354 /* Index in the kmem_cache->memcg_params->memcg_caches array */
355 int kmemcg_id;
356#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800357
358 int last_scanned_node;
359#if MAX_NUMNODES > 1
360 nodemask_t scan_nodes;
361 atomic_t numainfo_events;
362 atomic_t numainfo_updating;
363#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700364
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700365 struct mem_cgroup_per_node *nodeinfo[0];
366 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800367};
368
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800369static size_t memcg_size(void)
370{
371 return sizeof(struct mem_cgroup) +
372 nr_node_ids * sizeof(struct mem_cgroup_per_node);
373}
374
Glauber Costa510fc4e2012-12-18 14:21:47 -0800375/* internal only representation about the status of kmem accounting. */
376enum {
377 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800378 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800379 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800380};
381
Glauber Costaa8964b92012-12-18 14:22:09 -0800382/* We account when limit is on, but only after call sites are patched */
383#define KMEM_ACCOUNTED_MASK \
384 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800385
386#ifdef CONFIG_MEMCG_KMEM
387static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
388{
389 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
390}
Glauber Costa7de37682012-12-18 14:22:07 -0800391
392static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
393{
394 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
395}
396
Glauber Costaa8964b92012-12-18 14:22:09 -0800397static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
398{
399 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
400}
401
Glauber Costa55007d82012-12-18 14:22:38 -0800402static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
403{
404 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
405}
406
Glauber Costa7de37682012-12-18 14:22:07 -0800407static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
408{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700409 /*
410 * Our caller must use css_get() first, because memcg_uncharge_kmem()
411 * will call css_put() if it sees the memcg is dead.
412 */
413 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800414 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
415 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
416}
417
418static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
419{
420 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
421 &memcg->kmem_account_flags);
422}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800423#endif
424
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800425/* Stuffs for move charges at task migration. */
426/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800427 * Types of charges to be moved. "move_charge_at_immitgrate" and
428 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800429 */
430enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800431 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700432 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800433 NR_MOVE_TYPE,
434};
435
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800436/* "mc" and its members are protected by cgroup_mutex */
437static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800438 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800439 struct mem_cgroup *from;
440 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800441 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800442 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800443 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800444 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800445 struct task_struct *moving_task; /* a task moving charges */
446 wait_queue_head_t waitq; /* a waitq for other context */
447} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700448 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800449 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
450};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800451
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700452static bool move_anon(void)
453{
Glauber Costaee5e8472013-02-22 16:34:50 -0800454 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700455}
456
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700457static bool move_file(void)
458{
Glauber Costaee5e8472013-02-22 16:34:50 -0800459 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700460}
461
Balbir Singh4e416952009-09-23 15:56:39 -0700462/*
463 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
464 * limit reclaim to prevent infinite loops, if they ever occur.
465 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700466#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700467#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700468
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800469enum charge_type {
470 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700471 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800472 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700473 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700474 NR_CHARGE_TYPE,
475};
476
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800477/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800478enum res_type {
479 _MEM,
480 _MEMSWAP,
481 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800482 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800483};
484
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700485#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
486#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800487#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700488/* Used for OOM nofiier */
489#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800490
Balbir Singh75822b42009-09-23 15:56:38 -0700491/*
492 * Reclaim flags for mem_cgroup_hierarchical_reclaim
493 */
494#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
495#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
496#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
497#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
498
Glauber Costa09998212013-02-22 16:34:55 -0800499/*
500 * The memcg_create_mutex will be held whenever a new cgroup is created.
501 * As a consequence, any change that needs to protect against new child cgroups
502 * appearing has to hold it as well.
503 */
504static DEFINE_MUTEX(memcg_create_mutex);
505
Wanpeng Lib2145142012-07-31 16:46:01 -0700506struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
507{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400508 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700509}
510
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700511/* Some nice accessors for the vmpressure. */
512struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
513{
514 if (!memcg)
515 memcg = root_mem_cgroup;
516 return &memcg->vmpressure;
517}
518
519struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
520{
521 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
522}
523
524struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
525{
526 return &mem_cgroup_from_css(css)->vmpressure;
527}
528
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700529static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
530{
531 return (memcg == root_mem_cgroup);
532}
533
Glauber Costae1aab162011-12-11 21:47:03 +0000534/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700535#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000536
Glauber Costae1aab162011-12-11 21:47:03 +0000537void sock_update_memcg(struct sock *sk)
538{
Glauber Costa376be5f2012-01-20 04:57:14 +0000539 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000540 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700541 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000542
543 BUG_ON(!sk->sk_prot->proto_cgroup);
544
Glauber Costaf3f511e2012-01-05 20:16:39 +0000545 /* Socket cloning can throw us here with sk_cgrp already
546 * filled. It won't however, necessarily happen from
547 * process context. So the test for root memcg given
548 * the current task's memcg won't help us in this case.
549 *
550 * Respecting the original socket's memcg is a better
551 * decision in this case.
552 */
553 if (sk->sk_cgrp) {
554 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700555 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000556 return;
557 }
558
Glauber Costae1aab162011-12-11 21:47:03 +0000559 rcu_read_lock();
560 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700561 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700562 if (!mem_cgroup_is_root(memcg) &&
563 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700564 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000565 }
566 rcu_read_unlock();
567 }
568}
569EXPORT_SYMBOL(sock_update_memcg);
570
571void sock_release_memcg(struct sock *sk)
572{
Glauber Costa376be5f2012-01-20 04:57:14 +0000573 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000574 struct mem_cgroup *memcg;
575 WARN_ON(!sk->sk_cgrp->memcg);
576 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700577 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000578 }
579}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000580
581struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
582{
583 if (!memcg || mem_cgroup_is_root(memcg))
584 return NULL;
585
586 return &memcg->tcp_mem.cg_proto;
587}
588EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000589
Glauber Costa3f134612012-05-29 15:07:11 -0700590static void disarm_sock_keys(struct mem_cgroup *memcg)
591{
592 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
593 return;
594 static_key_slow_dec(&memcg_socket_limit_enabled);
595}
596#else
597static void disarm_sock_keys(struct mem_cgroup *memcg)
598{
599}
600#endif
601
Glauber Costaa8964b92012-12-18 14:22:09 -0800602#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800603/*
604 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
605 * There are two main reasons for not using the css_id for this:
606 * 1) this works better in sparse environments, where we have a lot of memcgs,
607 * but only a few kmem-limited. Or also, if we have, for instance, 200
608 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
609 * 200 entry array for that.
610 *
611 * 2) In order not to violate the cgroup API, we would like to do all memory
612 * allocation in ->create(). At that point, we haven't yet allocated the
613 * css_id. Having a separate index prevents us from messing with the cgroup
614 * core for this
615 *
616 * The current size of the caches array is stored in
617 * memcg_limited_groups_array_size. It will double each time we have to
618 * increase it.
619 */
620static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800621int memcg_limited_groups_array_size;
622
Glauber Costa55007d82012-12-18 14:22:38 -0800623/*
624 * MIN_SIZE is different than 1, because we would like to avoid going through
625 * the alloc/free process all the time. In a small machine, 4 kmem-limited
626 * cgroups is a reasonable guess. In the future, it could be a parameter or
627 * tunable, but that is strictly not necessary.
628 *
629 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
630 * this constant directly from cgroup, but it is understandable that this is
631 * better kept as an internal representation in cgroup.c. In any case, the
632 * css_id space is not getting any smaller, and we don't have to necessarily
633 * increase ours as well if it increases.
634 */
635#define MEMCG_CACHES_MIN_SIZE 4
636#define MEMCG_CACHES_MAX_SIZE 65535
637
Glauber Costad7f25f82012-12-18 14:22:40 -0800638/*
639 * A lot of the calls to the cache allocation functions are expected to be
640 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
641 * conditional to this static branch, we'll have to allow modules that does
642 * kmem_cache_alloc and the such to see this symbol as well
643 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800644struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800645EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800646
647static void disarm_kmem_keys(struct mem_cgroup *memcg)
648{
Glauber Costa55007d82012-12-18 14:22:38 -0800649 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800650 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800651 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
652 }
Glauber Costabea207c2012-12-18 14:22:11 -0800653 /*
654 * This check can't live in kmem destruction function,
655 * since the charges will outlive the cgroup
656 */
657 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800658}
659#else
660static void disarm_kmem_keys(struct mem_cgroup *memcg)
661{
662}
663#endif /* CONFIG_MEMCG_KMEM */
664
665static void disarm_static_keys(struct mem_cgroup *memcg)
666{
667 disarm_sock_keys(memcg);
668 disarm_kmem_keys(memcg);
669}
670
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700671static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800672
Balbir Singhf64c3f52009-09-23 15:56:37 -0700673static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700674mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700675{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800676 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700677 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700678}
679
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700680struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100681{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700682 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100683}
684
Balbir Singhf64c3f52009-09-23 15:56:37 -0700685static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700686page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700687{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700688 int nid = page_to_nid(page);
689 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700690
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700691 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700692}
693
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700694static struct mem_cgroup_tree_per_zone *
695soft_limit_tree_node_zone(int nid, int zid)
696{
697 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
698}
699
700static struct mem_cgroup_tree_per_zone *
701soft_limit_tree_from_page(struct page *page)
702{
703 int nid = page_to_nid(page);
704 int zid = page_zonenum(page);
705
706 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
707}
708
709static void
710__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
711 struct mem_cgroup_per_zone *mz,
712 struct mem_cgroup_tree_per_zone *mctz,
713 unsigned long long new_usage_in_excess)
714{
715 struct rb_node **p = &mctz->rb_root.rb_node;
716 struct rb_node *parent = NULL;
717 struct mem_cgroup_per_zone *mz_node;
718
719 if (mz->on_tree)
720 return;
721
722 mz->usage_in_excess = new_usage_in_excess;
723 if (!mz->usage_in_excess)
724 return;
725 while (*p) {
726 parent = *p;
727 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
728 tree_node);
729 if (mz->usage_in_excess < mz_node->usage_in_excess)
730 p = &(*p)->rb_left;
731 /*
732 * We can't avoid mem cgroups that are over their soft
733 * limit by the same amount
734 */
735 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
736 p = &(*p)->rb_right;
737 }
738 rb_link_node(&mz->tree_node, parent, p);
739 rb_insert_color(&mz->tree_node, &mctz->rb_root);
740 mz->on_tree = true;
741}
742
743static void
744__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
745 struct mem_cgroup_per_zone *mz,
746 struct mem_cgroup_tree_per_zone *mctz)
747{
748 if (!mz->on_tree)
749 return;
750 rb_erase(&mz->tree_node, &mctz->rb_root);
751 mz->on_tree = false;
752}
753
754static void
755mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
756 struct mem_cgroup_per_zone *mz,
757 struct mem_cgroup_tree_per_zone *mctz)
758{
759 spin_lock(&mctz->lock);
760 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
761 spin_unlock(&mctz->lock);
762}
763
764
765static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
766{
767 unsigned long long excess;
768 struct mem_cgroup_per_zone *mz;
769 struct mem_cgroup_tree_per_zone *mctz;
770 int nid = page_to_nid(page);
771 int zid = page_zonenum(page);
772 mctz = soft_limit_tree_from_page(page);
773
774 /*
775 * Necessary to update all ancestors when hierarchy is used.
776 * because their event counter is not touched.
777 */
778 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
779 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
780 excess = res_counter_soft_limit_excess(&memcg->res);
781 /*
782 * We have to update the tree if mz is on RB-tree or
783 * mem is over its softlimit.
784 */
785 if (excess || mz->on_tree) {
786 spin_lock(&mctz->lock);
787 /* if on-tree, remove it */
788 if (mz->on_tree)
789 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
790 /*
791 * Insert again. mz->usage_in_excess will be updated.
792 * If excess is 0, no tree ops.
793 */
794 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
795 spin_unlock(&mctz->lock);
796 }
797 }
798}
799
800static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
801{
802 int node, zone;
803 struct mem_cgroup_per_zone *mz;
804 struct mem_cgroup_tree_per_zone *mctz;
805
806 for_each_node(node) {
807 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
808 mz = mem_cgroup_zoneinfo(memcg, node, zone);
809 mctz = soft_limit_tree_node_zone(node, zone);
810 mem_cgroup_remove_exceeded(memcg, mz, mctz);
811 }
812 }
813}
814
815static struct mem_cgroup_per_zone *
816__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
817{
818 struct rb_node *rightmost = NULL;
819 struct mem_cgroup_per_zone *mz;
820
821retry:
822 mz = NULL;
823 rightmost = rb_last(&mctz->rb_root);
824 if (!rightmost)
825 goto done; /* Nothing to reclaim from */
826
827 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
828 /*
829 * Remove the node now but someone else can add it back,
830 * we will to add it back at the end of reclaim to its correct
831 * position in the tree.
832 */
833 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
834 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
835 !css_tryget(&mz->memcg->css))
836 goto retry;
837done:
838 return mz;
839}
840
841static struct mem_cgroup_per_zone *
842mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
843{
844 struct mem_cgroup_per_zone *mz;
845
846 spin_lock(&mctz->lock);
847 mz = __mem_cgroup_largest_soft_limit_node(mctz);
848 spin_unlock(&mctz->lock);
849 return mz;
850}
851
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700852/*
853 * Implementation Note: reading percpu statistics for memcg.
854 *
855 * Both of vmstat[] and percpu_counter has threshold and do periodic
856 * synchronization to implement "quick" read. There are trade-off between
857 * reading cost and precision of value. Then, we may have a chance to implement
858 * a periodic synchronizion of counter in memcg's counter.
859 *
860 * But this _read() function is used for user interface now. The user accounts
861 * memory usage by memory cgroup and he _always_ requires exact value because
862 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
863 * have to visit all online cpus and make sum. So, for now, unnecessary
864 * synchronization is not implemented. (just implemented for cpu hotplug)
865 *
866 * If there are kernel internal actions which can make use of some not-exact
867 * value, and reading all cpu value can be performance bottleneck in some
868 * common workload, threashold and synchonization as vmstat[] should be
869 * implemented.
870 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700871static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700872 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800873{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700874 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800875 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800876
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700877 get_online_cpus();
878 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700879 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700880#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700881 spin_lock(&memcg->pcp_counter_lock);
882 val += memcg->nocpu_base.count[idx];
883 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700884#endif
885 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800886 return val;
887}
888
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700889static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700890 bool charge)
891{
892 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700893 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700894}
895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700896static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700897 enum mem_cgroup_events_index idx)
898{
899 unsigned long val = 0;
900 int cpu;
901
David Rientjes9c567512013-10-16 13:46:43 -0700902 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700903 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700904 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700905#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700906 spin_lock(&memcg->pcp_counter_lock);
907 val += memcg->nocpu_base.events[idx];
908 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700909#endif
David Rientjes9c567512013-10-16 13:46:43 -0700910 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700911 return val;
912}
913
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700914static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700915 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700916 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800917{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800918 preempt_disable();
919
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700920 /*
921 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
922 * counted as CACHE even if it's on ANON LRU.
923 */
924 if (anon)
925 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700926 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800927 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700928 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700930
David Rientjesb070e652013-05-07 16:18:09 -0700931 if (PageTransHuge(page))
932 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
933 nr_pages);
934
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800935 /* pagein of a big page is an event. So, ignore page size */
936 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700937 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800938 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700939 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800940 nr_pages = -nr_pages; /* for event */
941 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800942
Johannes Weiner13114712012-05-29 15:07:07 -0700943 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800944
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800945 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800946}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800947
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700948unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700949mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700950{
951 struct mem_cgroup_per_zone *mz;
952
953 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
954 return mz->lru_size[lru];
955}
956
957static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700958mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700959 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700960{
961 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700962 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700963 unsigned long ret = 0;
964
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700965 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700966
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700967 for_each_lru(lru) {
968 if (BIT(lru) & lru_mask)
969 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700970 }
971 return ret;
972}
973
974static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700975mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700976 int nid, unsigned int lru_mask)
977{
Ying Han889976d2011-05-26 16:25:33 -0700978 u64 total = 0;
979 int zid;
980
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700981 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700982 total += mem_cgroup_zone_nr_lru_pages(memcg,
983 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700984
Ying Han889976d2011-05-26 16:25:33 -0700985 return total;
986}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700987
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700988static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700989 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800990{
Ying Han889976d2011-05-26 16:25:33 -0700991 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800992 u64 total = 0;
993
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800994 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700995 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800996 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800997}
998
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800999static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
1000 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001001{
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001002 unsigned long val, next;
1003
Johannes Weiner13114712012-05-29 15:07:07 -07001004 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -07001005 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001006 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001007 if ((long)next - (long)val < 0) {
1008 switch (target) {
1009 case MEM_CGROUP_TARGET_THRESH:
1010 next = val + THRESHOLDS_EVENTS_TARGET;
1011 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001012 case MEM_CGROUP_TARGET_SOFTLIMIT:
1013 next = val + SOFTLIMIT_EVENTS_TARGET;
1014 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001015 case MEM_CGROUP_TARGET_NUMAINFO:
1016 next = val + NUMAINFO_EVENTS_TARGET;
1017 break;
1018 default:
1019 break;
1020 }
1021 __this_cpu_write(memcg->stat->targets[target], next);
1022 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001023 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001024 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001025}
1026
1027/*
1028 * Check events in order.
1029 *
1030 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001031static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001032{
Steven Rostedt47994012011-11-02 13:38:33 -07001033 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001034 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001035 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1036 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001037 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001038 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001039
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001040 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1041 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001042#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001043 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1044 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001045#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001046 preempt_enable();
1047
1048 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001049 if (unlikely(do_softlimit))
1050 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001051#if MAX_NUMNODES > 1
1052 if (unlikely(do_numainfo))
1053 atomic_inc(&memcg->numainfo_events);
1054#endif
1055 } else
1056 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001057}
1058
Balbir Singhcf475ad2008-04-29 01:00:16 -07001059struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001060{
Balbir Singh31a78f22008-09-28 23:09:31 +01001061 /*
1062 * mm_update_next_owner() may clear mm->owner to NULL
1063 * if it races with swapoff, page migration, etc.
1064 * So this can be called with p == NULL.
1065 */
1066 if (unlikely(!p))
1067 return NULL;
1068
Tejun Heo8af01f52013-08-08 20:11:22 -04001069 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001070}
1071
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001072struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001073{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001074 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001075
1076 if (!mm)
1077 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001078 /*
1079 * Because we have no locks, mm->owner's may be being moved to other
1080 * cgroup. We use css_tryget() here even if this looks
1081 * pessimistic (rather than adding locks here).
1082 */
1083 rcu_read_lock();
1084 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001085 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1086 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001087 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001088 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001089 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001090 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001091}
1092
Michal Hocko16248d82013-04-29 15:07:19 -07001093/*
1094 * Returns a next (in a pre-order walk) alive memcg (with elevated css
1095 * ref. count) or NULL if the whole root's subtree has been visited.
1096 *
1097 * helper function to be used by mem_cgroup_iter
1098 */
1099static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Andrew Morton694fbc02013-09-24 15:27:37 -07001100 struct mem_cgroup *last_visited)
Michal Hocko16248d82013-04-29 15:07:19 -07001101{
Tejun Heo492eb212013-08-08 20:11:25 -04001102 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -07001103
Tejun Heobd8815a2013-08-08 20:11:27 -04001104 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -07001105skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -04001106 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -07001107
1108 /*
1109 * Even if we found a group we have to make sure it is
1110 * alive. css && !memcg means that the groups should be
1111 * skipped and we should continue the tree walk.
1112 * last_visited css is safe to use because it is
1113 * protected by css_get and the tree walk is rcu safe.
1114 */
Tejun Heo492eb212013-08-08 20:11:25 -04001115 if (next_css) {
1116 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
1117
Andrew Morton694fbc02013-09-24 15:27:37 -07001118 if (css_tryget(&mem->css))
1119 return mem;
1120 else {
Tejun Heo492eb212013-08-08 20:11:25 -04001121 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -07001122 goto skip_node;
1123 }
1124 }
1125
1126 return NULL;
1127}
1128
Johannes Weiner519ebea2013-07-03 15:04:51 -07001129static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1130{
1131 /*
1132 * When a group in the hierarchy below root is destroyed, the
1133 * hierarchy iterator can no longer be trusted since it might
1134 * have pointed to the destroyed group. Invalidate it.
1135 */
1136 atomic_inc(&root->dead_count);
1137}
1138
1139static struct mem_cgroup *
1140mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1141 struct mem_cgroup *root,
1142 int *sequence)
1143{
1144 struct mem_cgroup *position = NULL;
1145 /*
1146 * A cgroup destruction happens in two stages: offlining and
1147 * release. They are separated by a RCU grace period.
1148 *
1149 * If the iterator is valid, we may still race with an
1150 * offlining. The RCU lock ensures the object won't be
1151 * released, tryget will fail if we lost the race.
1152 */
1153 *sequence = atomic_read(&root->dead_count);
1154 if (iter->last_dead_count == *sequence) {
1155 smp_rmb();
1156 position = iter->last_visited;
1157 if (position && !css_tryget(&position->css))
1158 position = NULL;
1159 }
1160 return position;
1161}
1162
1163static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1164 struct mem_cgroup *last_visited,
1165 struct mem_cgroup *new_position,
1166 int sequence)
1167{
1168 if (last_visited)
1169 css_put(&last_visited->css);
1170 /*
1171 * We store the sequence count from the time @last_visited was
1172 * loaded successfully instead of rereading it here so that we
1173 * don't lose destruction events in between. We could have
1174 * raced with the destruction of @new_position after all.
1175 */
1176 iter->last_visited = new_position;
1177 smp_wmb();
1178 iter->last_dead_count = sequence;
1179}
1180
Johannes Weiner56600482012-01-12 17:17:59 -08001181/**
1182 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1183 * @root: hierarchy root
1184 * @prev: previously returned memcg, NULL on first invocation
1185 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1186 *
1187 * Returns references to children of the hierarchy below @root, or
1188 * @root itself, or %NULL after a full round-trip.
1189 *
1190 * Caller must pass the return value in @prev on subsequent
1191 * invocations for reference counting, or use mem_cgroup_iter_break()
1192 * to cancel a hierarchy walk before the round-trip is complete.
1193 *
1194 * Reclaimers can specify a zone and a priority level in @reclaim to
1195 * divide up the memcgs in the hierarchy among all concurrent
1196 * reclaimers operating on the same zone and priority.
1197 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001198struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001199 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001200 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001201{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001202 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001203 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001204
Andrew Morton694fbc02013-09-24 15:27:37 -07001205 if (mem_cgroup_disabled())
1206 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001207
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001208 if (!root)
1209 root = root_mem_cgroup;
1210
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001211 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001212 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001213
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001214 if (!root->use_hierarchy && root != root_mem_cgroup) {
1215 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001216 goto out_css_put;
Andrew Morton694fbc02013-09-24 15:27:37 -07001217 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001218 }
1219
Michal Hocko542f85f2013-04-29 15:07:15 -07001220 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001221 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001222 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001223 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001224
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001225 if (reclaim) {
1226 int nid = zone_to_nid(reclaim->zone);
1227 int zid = zone_idx(reclaim->zone);
1228 struct mem_cgroup_per_zone *mz;
1229
1230 mz = mem_cgroup_zoneinfo(root, nid, zid);
1231 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001232 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001233 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001234 goto out_unlock;
1235 }
Michal Hocko5f578162013-04-29 15:07:17 -07001236
Johannes Weiner519ebea2013-07-03 15:04:51 -07001237 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001238 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001239
Andrew Morton694fbc02013-09-24 15:27:37 -07001240 memcg = __mem_cgroup_iter_next(root, last_visited);
Michal Hocko542f85f2013-04-29 15:07:15 -07001241
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001242 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001243 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001244
Michal Hocko19f39402013-04-29 15:07:18 -07001245 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001246 iter->generation++;
1247 else if (!prev && memcg)
1248 reclaim->generation = iter->generation;
1249 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001250
Andrew Morton694fbc02013-09-24 15:27:37 -07001251 if (prev && !memcg)
Michal Hocko542f85f2013-04-29 15:07:15 -07001252 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001253 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001254out_unlock:
1255 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001256out_css_put:
1257 if (prev && prev != root)
1258 css_put(&prev->css);
1259
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001260 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001261}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001262
Johannes Weiner56600482012-01-12 17:17:59 -08001263/**
1264 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1265 * @root: hierarchy root
1266 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1267 */
1268void mem_cgroup_iter_break(struct mem_cgroup *root,
1269 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001270{
1271 if (!root)
1272 root = root_mem_cgroup;
1273 if (prev && prev != root)
1274 css_put(&prev->css);
1275}
1276
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001277/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001278 * Iteration constructs for visiting all cgroups (under a tree). If
1279 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1280 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001281 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001282#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001283 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001284 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001285 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001286
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001287#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001288 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001289 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001290 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001291
David Rientjes68ae5642012-12-12 13:51:57 -08001292void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001293{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001294 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001295
Ying Han456f9982011-05-26 16:25:38 -07001296 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001297 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1298 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001299 goto out;
1300
1301 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001302 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001303 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1304 break;
1305 case PGMAJFAULT:
1306 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001307 break;
1308 default:
1309 BUG();
1310 }
1311out:
1312 rcu_read_unlock();
1313}
David Rientjes68ae5642012-12-12 13:51:57 -08001314EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001315
Johannes Weiner925b7672012-01-12 17:18:15 -08001316/**
1317 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1318 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001319 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001320 *
1321 * Returns the lru list vector holding pages for the given @zone and
1322 * @mem. This can be the global zone lruvec, if the memory controller
1323 * is disabled.
1324 */
1325struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1326 struct mem_cgroup *memcg)
1327{
1328 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001329 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001330
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001331 if (mem_cgroup_disabled()) {
1332 lruvec = &zone->lruvec;
1333 goto out;
1334 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001335
1336 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001337 lruvec = &mz->lruvec;
1338out:
1339 /*
1340 * Since a node can be onlined after the mem_cgroup was created,
1341 * we have to be prepared to initialize lruvec->zone here;
1342 * and if offlined then reonlined, we need to reinitialize it.
1343 */
1344 if (unlikely(lruvec->zone != zone))
1345 lruvec->zone = zone;
1346 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001347}
1348
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001349/*
1350 * Following LRU functions are allowed to be used without PCG_LOCK.
1351 * Operations are called by routine of global LRU independently from memcg.
1352 * What we have to take care of here is validness of pc->mem_cgroup.
1353 *
1354 * Changes to pc->mem_cgroup happens when
1355 * 1. charge
1356 * 2. moving account
1357 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1358 * It is added to LRU before charge.
1359 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1360 * When moving account, the page is not on LRU. It's isolated.
1361 */
1362
Johannes Weiner925b7672012-01-12 17:18:15 -08001363/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001364 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001365 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001366 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001367 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001368struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001369{
1370 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001371 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001372 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001373 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001374
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001375 if (mem_cgroup_disabled()) {
1376 lruvec = &zone->lruvec;
1377 goto out;
1378 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001379
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001380 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001381 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001382
1383 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001384 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001385 * an uncharged page off lru does nothing to secure
1386 * its former mem_cgroup from sudden removal.
1387 *
1388 * Our caller holds lru_lock, and PageCgroupUsed is updated
1389 * under page_cgroup lock: between them, they make all uses
1390 * of pc->mem_cgroup safe.
1391 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001392 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001393 pc->mem_cgroup = memcg = root_mem_cgroup;
1394
Johannes Weiner925b7672012-01-12 17:18:15 -08001395 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001396 lruvec = &mz->lruvec;
1397out:
1398 /*
1399 * Since a node can be onlined after the mem_cgroup was created,
1400 * we have to be prepared to initialize lruvec->zone here;
1401 * and if offlined then reonlined, we need to reinitialize it.
1402 */
1403 if (unlikely(lruvec->zone != zone))
1404 lruvec->zone = zone;
1405 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001406}
1407
1408/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001409 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1410 * @lruvec: mem_cgroup per zone lru vector
1411 * @lru: index of lru list the page is sitting on
1412 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001413 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001414 * This function must be called when a page is added to or removed from an
1415 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001416 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001417void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1418 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001419{
1420 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001421 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001422
1423 if (mem_cgroup_disabled())
1424 return;
1425
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001426 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1427 lru_size = mz->lru_size + lru;
1428 *lru_size += nr_pages;
1429 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001430}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001431
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001432/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001433 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001434 * hierarchy subtree
1435 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001436bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1437 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001438{
Johannes Weiner91c637342012-05-29 15:06:24 -07001439 if (root_memcg == memcg)
1440 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001441 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001442 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001443 return css_is_ancestor(&memcg->css, &root_memcg->css);
1444}
1445
1446static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1447 struct mem_cgroup *memcg)
1448{
1449 bool ret;
1450
Johannes Weiner91c637342012-05-29 15:06:24 -07001451 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001452 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001453 rcu_read_unlock();
1454 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001455}
1456
David Rientjesffbdccf2013-07-03 15:01:23 -07001457bool task_in_mem_cgroup(struct task_struct *task,
1458 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001459{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001460 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001461 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001462 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001463
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001464 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001465 if (p) {
1466 curr = try_get_mem_cgroup_from_mm(p->mm);
1467 task_unlock(p);
1468 } else {
1469 /*
1470 * All threads may have already detached their mm's, but the oom
1471 * killer still needs to detect if they have already been oom
1472 * killed to prevent needlessly killing additional tasks.
1473 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001474 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001475 curr = mem_cgroup_from_task(task);
1476 if (curr)
1477 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001478 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001479 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001480 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001481 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001482 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001483 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001484 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001485 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1486 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001487 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001488 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001489 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001490 return ret;
1491}
1492
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001493int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001494{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001495 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001496 unsigned long inactive;
1497 unsigned long active;
1498 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001499
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001500 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1501 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001502
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001503 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1504 if (gb)
1505 inactive_ratio = int_sqrt(10 * gb);
1506 else
1507 inactive_ratio = 1;
1508
Johannes Weiner9b272972011-11-02 13:38:23 -07001509 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001510}
1511
Balbir Singh6d61ef42009-01-07 18:08:06 -08001512#define mem_cgroup_from_res_counter(counter, member) \
1513 container_of(counter, struct mem_cgroup, member)
1514
Johannes Weiner19942822011-02-01 15:52:43 -08001515/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001516 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001517 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001518 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001519 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001520 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001521 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001522static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001523{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001524 unsigned long long margin;
1525
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001526 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001527 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001528 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001529 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001530}
1531
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001532int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001533{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001534 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001535 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001536 return vm_swappiness;
1537
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001538 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001539}
1540
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001541/*
1542 * memcg->moving_account is used for checking possibility that some thread is
1543 * calling move_account(). When a thread on CPU-A starts moving pages under
1544 * a memcg, other threads should check memcg->moving_account under
1545 * rcu_read_lock(), like this:
1546 *
1547 * CPU-A CPU-B
1548 * rcu_read_lock()
1549 * memcg->moving_account+1 if (memcg->mocing_account)
1550 * take heavy locks.
1551 * synchronize_rcu() update something.
1552 * rcu_read_unlock()
1553 * start move here.
1554 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001555
1556/* for quick checking without looking up memcg */
1557atomic_t memcg_moving __read_mostly;
1558
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001559static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001560{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001561 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001562 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001563 synchronize_rcu();
1564}
1565
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001566static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001567{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001568 /*
1569 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1570 * We check NULL in callee rather than caller.
1571 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001572 if (memcg) {
1573 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001574 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001575 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001576}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001577
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001578/*
1579 * 2 routines for checking "mem" is under move_account() or not.
1580 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001581 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1582 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001583 * pc->mem_cgroup may be overwritten.
1584 *
1585 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1586 * under hierarchy of moving cgroups. This is for
1587 * waiting at hith-memory prressure caused by "move".
1588 */
1589
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001590static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001591{
1592 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001593 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001594}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001595
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001596static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001597{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001598 struct mem_cgroup *from;
1599 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001600 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001601 /*
1602 * Unlike task_move routines, we access mc.to, mc.from not under
1603 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1604 */
1605 spin_lock(&mc.lock);
1606 from = mc.from;
1607 to = mc.to;
1608 if (!from)
1609 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001610
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001611 ret = mem_cgroup_same_or_subtree(memcg, from)
1612 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001613unlock:
1614 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001615 return ret;
1616}
1617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001618static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001619{
1620 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001621 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001622 DEFINE_WAIT(wait);
1623 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1624 /* moving charge context might have finished. */
1625 if (mc.moving_task)
1626 schedule();
1627 finish_wait(&mc.waitq, &wait);
1628 return true;
1629 }
1630 }
1631 return false;
1632}
1633
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001634/*
1635 * Take this lock when
1636 * - a code tries to modify page's memcg while it's USED.
1637 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001638 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001639 */
1640static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1641 unsigned long *flags)
1642{
1643 spin_lock_irqsave(&memcg->move_lock, *flags);
1644}
1645
1646static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1647 unsigned long *flags)
1648{
1649 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1650}
1651
Sha Zhengju58cf1882013-02-22 16:32:05 -08001652#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001653/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001654 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001655 * @memcg: The memory cgroup that went over limit
1656 * @p: Task that is going to be killed
1657 *
1658 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1659 * enabled
1660 */
1661void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1662{
1663 struct cgroup *task_cgrp;
1664 struct cgroup *mem_cgrp;
1665 /*
1666 * Need a buffer in BSS, can't rely on allocations. The code relies
1667 * on the assumption that OOM is serialized for memory controller.
1668 * If this assumption is broken, revisit this code.
1669 */
1670 static char memcg_name[PATH_MAX];
1671 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001672 struct mem_cgroup *iter;
1673 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001674
Sha Zhengju58cf1882013-02-22 16:32:05 -08001675 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001676 return;
1677
Balbir Singhe2224322009-04-02 16:57:39 -07001678 rcu_read_lock();
1679
1680 mem_cgrp = memcg->css.cgroup;
1681 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1682
1683 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1684 if (ret < 0) {
1685 /*
1686 * Unfortunately, we are unable to convert to a useful name
1687 * But we'll still print out the usage information
1688 */
1689 rcu_read_unlock();
1690 goto done;
1691 }
1692 rcu_read_unlock();
1693
Andrew Mortond0451972013-02-22 16:32:06 -08001694 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001695
1696 rcu_read_lock();
1697 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1698 if (ret < 0) {
1699 rcu_read_unlock();
1700 goto done;
1701 }
1702 rcu_read_unlock();
1703
1704 /*
1705 * Continues from above, so we don't need an KERN_ level
1706 */
Andrew Mortond0451972013-02-22 16:32:06 -08001707 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001708done:
1709
Andrew Mortond0451972013-02-22 16:32:06 -08001710 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001711 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1712 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1713 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001714 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001715 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1716 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1717 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001718 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001719 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1720 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1721 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001722
1723 for_each_mem_cgroup_tree(iter, memcg) {
1724 pr_info("Memory cgroup stats");
1725
1726 rcu_read_lock();
1727 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1728 if (!ret)
1729 pr_cont(" for %s", memcg_name);
1730 rcu_read_unlock();
1731 pr_cont(":");
1732
1733 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1734 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1735 continue;
1736 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1737 K(mem_cgroup_read_stat(iter, i)));
1738 }
1739
1740 for (i = 0; i < NR_LRU_LISTS; i++)
1741 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1742 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1743
1744 pr_cont("\n");
1745 }
Balbir Singhe2224322009-04-02 16:57:39 -07001746}
1747
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001748/*
1749 * This function returns the number of memcg under hierarchy tree. Returns
1750 * 1(self count) if no children.
1751 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001752static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001753{
1754 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001755 struct mem_cgroup *iter;
1756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001757 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001758 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001759 return num;
1760}
1761
Balbir Singh6d61ef42009-01-07 18:08:06 -08001762/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001763 * Return the memory (and swap, if configured) limit for a memcg.
1764 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001765static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001766{
1767 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001768
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001769 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001770
David Rientjesa63d83f2010-08-09 17:19:46 -07001771 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001772 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001773 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001774 if (mem_cgroup_swappiness(memcg)) {
1775 u64 memsw;
1776
1777 limit += total_swap_pages << PAGE_SHIFT;
1778 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1779
1780 /*
1781 * If memsw is finite and limits the amount of swap space
1782 * available to this memcg, return that limit.
1783 */
1784 limit = min(limit, memsw);
1785 }
1786
1787 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001788}
1789
David Rientjes19965462012-12-11 16:00:26 -08001790static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1791 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001792{
1793 struct mem_cgroup *iter;
1794 unsigned long chosen_points = 0;
1795 unsigned long totalpages;
1796 unsigned int points = 0;
1797 struct task_struct *chosen = NULL;
1798
David Rientjes876aafb2012-07-31 16:43:48 -07001799 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001800 * If current has a pending SIGKILL or is exiting, then automatically
1801 * select it. The goal is to allow it to allocate so that it may
1802 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001803 */
David Rientjes465adcf2013-04-29 15:08:45 -07001804 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001805 set_thread_flag(TIF_MEMDIE);
1806 return;
1807 }
1808
1809 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001810 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1811 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001812 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001813 struct task_struct *task;
1814
Tejun Heo72ec7022013-08-08 20:11:26 -04001815 css_task_iter_start(&iter->css, &it);
1816 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001817 switch (oom_scan_process_thread(task, totalpages, NULL,
1818 false)) {
1819 case OOM_SCAN_SELECT:
1820 if (chosen)
1821 put_task_struct(chosen);
1822 chosen = task;
1823 chosen_points = ULONG_MAX;
1824 get_task_struct(chosen);
1825 /* fall through */
1826 case OOM_SCAN_CONTINUE:
1827 continue;
1828 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001829 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001830 mem_cgroup_iter_break(memcg, iter);
1831 if (chosen)
1832 put_task_struct(chosen);
1833 return;
1834 case OOM_SCAN_OK:
1835 break;
1836 };
1837 points = oom_badness(task, memcg, NULL, totalpages);
1838 if (points > chosen_points) {
1839 if (chosen)
1840 put_task_struct(chosen);
1841 chosen = task;
1842 chosen_points = points;
1843 get_task_struct(chosen);
1844 }
1845 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001846 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001847 }
1848
1849 if (!chosen)
1850 return;
1851 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001852 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1853 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001854}
1855
Johannes Weiner56600482012-01-12 17:17:59 -08001856static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1857 gfp_t gfp_mask,
1858 unsigned long flags)
1859{
1860 unsigned long total = 0;
1861 bool noswap = false;
1862 int loop;
1863
1864 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1865 noswap = true;
1866 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1867 noswap = true;
1868
1869 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1870 if (loop)
1871 drain_all_stock_async(memcg);
1872 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1873 /*
1874 * Allow limit shrinkers, which are triggered directly
1875 * by userspace, to catch signals and stop reclaim
1876 * after minimal progress, regardless of the margin.
1877 */
1878 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1879 break;
1880 if (mem_cgroup_margin(memcg))
1881 break;
1882 /*
1883 * If nothing was reclaimed after two attempts, there
1884 * may be no reclaimable pages in this hierarchy.
1885 */
1886 if (loop && !total)
1887 break;
1888 }
1889 return total;
1890}
1891
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001892/**
1893 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001894 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001895 * @nid: the node ID to be checked.
1896 * @noswap : specify true here if the user wants flle only information.
1897 *
1898 * This function returns whether the specified memcg contains any
1899 * reclaimable pages on a node. Returns true if there are any reclaimable
1900 * pages in the node.
1901 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001903 int nid, bool noswap)
1904{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001905 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001906 return true;
1907 if (noswap || !total_swap_pages)
1908 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001909 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001910 return true;
1911 return false;
1912
1913}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001914#if MAX_NUMNODES > 1
Ying Han889976d2011-05-26 16:25:33 -07001915
1916/*
1917 * Always updating the nodemask is not very good - even if we have an empty
1918 * list or the wrong list here, we can start from some node and traverse all
1919 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1920 *
1921 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001922static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001923{
1924 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001925 /*
1926 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1927 * pagein/pageout changes since the last update.
1928 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001929 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001930 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001931 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001932 return;
1933
Ying Han889976d2011-05-26 16:25:33 -07001934 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001935 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001936
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001937 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001939 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1940 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001941 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001942
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001943 atomic_set(&memcg->numainfo_events, 0);
1944 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001945}
1946
1947/*
1948 * Selecting a node where we start reclaim from. Because what we need is just
1949 * reducing usage counter, start from anywhere is O,K. Considering
1950 * memory reclaim from current node, there are pros. and cons.
1951 *
1952 * Freeing memory from current node means freeing memory from a node which
1953 * we'll use or we've used. So, it may make LRU bad. And if several threads
1954 * hit limits, it will see a contention on a node. But freeing from remote
1955 * node means more costs for memory reclaim because of memory latency.
1956 *
1957 * Now, we use round-robin. Better algorithm is welcomed.
1958 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001959int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001960{
1961 int node;
1962
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001963 mem_cgroup_may_update_nodemask(memcg);
1964 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001965
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001966 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001967 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001968 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001969 /*
1970 * We call this when we hit limit, not when pages are added to LRU.
1971 * No LRU may hold pages because all pages are UNEVICTABLE or
1972 * memcg is too small and all pages are not on LRU. In that case,
1973 * we use curret node.
1974 */
1975 if (unlikely(node == MAX_NUMNODES))
1976 node = numa_node_id();
1977
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001978 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001979 return node;
1980}
1981
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001982/*
1983 * Check all nodes whether it contains reclaimable pages or not.
1984 * For quick scan, we make use of scan_nodes. This will allow us to skip
1985 * unused nodes. But scan_nodes is lazily updated and may not cotain
1986 * enough new information. We need to do double check.
1987 */
1988static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1989{
1990 int nid;
1991
1992 /*
1993 * quick check...making use of scan_node.
1994 * We can skip unused nodes.
1995 */
1996 if (!nodes_empty(memcg->scan_nodes)) {
1997 for (nid = first_node(memcg->scan_nodes);
1998 nid < MAX_NUMNODES;
1999 nid = next_node(nid, memcg->scan_nodes)) {
2000
2001 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2002 return true;
2003 }
2004 }
2005 /*
2006 * Check rest of nodes.
2007 */
2008 for_each_node_state(nid, N_MEMORY) {
2009 if (node_isset(nid, memcg->scan_nodes))
2010 continue;
2011 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2012 return true;
2013 }
2014 return false;
2015}
2016
Ying Han889976d2011-05-26 16:25:33 -07002017#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07002019{
2020 return 0;
2021}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002022
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07002023static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
2024{
2025 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
2026}
Ying Han889976d2011-05-26 16:25:33 -07002027#endif
2028
Andrew Morton0608f432013-09-24 15:27:41 -07002029static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
2030 struct zone *zone,
2031 gfp_t gfp_mask,
2032 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08002033{
Andrew Morton0608f432013-09-24 15:27:41 -07002034 struct mem_cgroup *victim = NULL;
2035 int total = 0;
2036 int loop = 0;
2037 unsigned long excess;
2038 unsigned long nr_scanned;
2039 struct mem_cgroup_reclaim_cookie reclaim = {
2040 .zone = zone,
2041 .priority = 0,
2042 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07002043
Andrew Morton0608f432013-09-24 15:27:41 -07002044 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002045
Andrew Morton0608f432013-09-24 15:27:41 -07002046 while (1) {
2047 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
2048 if (!victim) {
2049 loop++;
2050 if (loop >= 2) {
2051 /*
2052 * If we have not been able to reclaim
2053 * anything, it might because there are
2054 * no reclaimable pages under this hierarchy
2055 */
2056 if (!total)
2057 break;
2058 /*
2059 * We want to do more targeted reclaim.
2060 * excess >> 2 is not to excessive so as to
2061 * reclaim too much, nor too less that we keep
2062 * coming back to reclaim from this cgroup
2063 */
2064 if (total >= (excess >> 2) ||
2065 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
2066 break;
2067 }
2068 continue;
2069 }
2070 if (!mem_cgroup_reclaimable(victim, false))
2071 continue;
2072 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2073 zone, &nr_scanned);
2074 *total_scanned += nr_scanned;
2075 if (!res_counter_soft_limit_excess(&root_memcg->res))
2076 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002077 }
Andrew Morton0608f432013-09-24 15:27:41 -07002078 mem_cgroup_iter_break(root_memcg, victim);
2079 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002080}
2081
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002082#ifdef CONFIG_LOCKDEP
2083static struct lockdep_map memcg_oom_lock_dep_map = {
2084 .name = "memcg_oom_lock",
2085};
2086#endif
2087
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002088static DEFINE_SPINLOCK(memcg_oom_lock);
2089
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002090/*
2091 * Check OOM-Killer is already running under our hierarchy.
2092 * If someone is running, return false.
2093 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002094static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002095{
Michal Hocko79dfdac2011-07-26 16:08:23 -07002096 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002097
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002098 spin_lock(&memcg_oom_lock);
2099
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002100 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07002101 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002102 /*
2103 * this subtree of our hierarchy is already locked
2104 * so we cannot give a lock.
2105 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07002106 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002107 mem_cgroup_iter_break(memcg, iter);
2108 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07002109 } else
2110 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002111 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002112
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002113 if (failed) {
2114 /*
2115 * OK, we failed to lock the whole subtree so we have
2116 * to clean up what we set up to the failing subtree
2117 */
2118 for_each_mem_cgroup_tree(iter, memcg) {
2119 if (iter == failed) {
2120 mem_cgroup_iter_break(memcg, iter);
2121 break;
2122 }
2123 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002124 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002125 } else
2126 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002127
2128 spin_unlock(&memcg_oom_lock);
2129
2130 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002131}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002132
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002133static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002134{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002135 struct mem_cgroup *iter;
2136
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002137 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002138 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002139 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002140 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002141 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002142}
2143
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002144static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002145{
2146 struct mem_cgroup *iter;
2147
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002148 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002149 atomic_inc(&iter->under_oom);
2150}
2151
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002152static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002153{
2154 struct mem_cgroup *iter;
2155
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002156 /*
2157 * When a new child is created while the hierarchy is under oom,
2158 * mem_cgroup_oom_lock() may not be called. We have to use
2159 * atomic_add_unless() here.
2160 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002162 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002163}
2164
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002165static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2166
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002167struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002168 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002169 wait_queue_t wait;
2170};
2171
2172static int memcg_oom_wake_function(wait_queue_t *wait,
2173 unsigned mode, int sync, void *arg)
2174{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002175 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2176 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002177 struct oom_wait_info *oom_wait_info;
2178
2179 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002180 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002181
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002182 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002183 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002184 * Then we can use css_is_ancestor without taking care of RCU.
2185 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002186 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2187 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002188 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002189 return autoremove_wake_function(wait, mode, sync, arg);
2190}
2191
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002192static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002193{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002194 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002195 /* for filtering, pass "memcg" as argument. */
2196 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002197}
2198
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002199static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002200{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002201 if (memcg && atomic_read(&memcg->under_oom))
2202 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002203}
2204
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002205static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002206{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002207 if (!current->memcg_oom.may_oom)
2208 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002209 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002210 * We are in the middle of the charge context here, so we
2211 * don't want to block when potentially sitting on a callstack
2212 * that holds all kinds of filesystem and mm locks.
2213 *
2214 * Also, the caller may handle a failed allocation gracefully
2215 * (like optional page cache readahead) and so an OOM killer
2216 * invocation might not even be necessary.
2217 *
2218 * That's why we don't do anything here except remember the
2219 * OOM context and then deal with it at the end of the page
2220 * fault when the stack is unwound, the locks are released,
2221 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002222 */
Johannes Weiner49426422013-10-16 13:46:59 -07002223 css_get(&memcg->css);
2224 current->memcg_oom.memcg = memcg;
2225 current->memcg_oom.gfp_mask = mask;
2226 current->memcg_oom.order = order;
2227}
2228
2229/**
2230 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2231 * @handle: actually kill/wait or just clean up the OOM state
2232 *
2233 * This has to be called at the end of a page fault if the memcg OOM
2234 * handler was enabled.
2235 *
2236 * Memcg supports userspace OOM handling where failed allocations must
2237 * sleep on a waitqueue until the userspace task resolves the
2238 * situation. Sleeping directly in the charge context with all kinds
2239 * of locks held is not a good idea, instead we remember an OOM state
2240 * in the task and mem_cgroup_oom_synchronize() has to be called at
2241 * the end of the page fault to complete the OOM handling.
2242 *
2243 * Returns %true if an ongoing memcg OOM situation was detected and
2244 * completed, %false otherwise.
2245 */
2246bool mem_cgroup_oom_synchronize(bool handle)
2247{
2248 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2249 struct oom_wait_info owait;
2250 bool locked;
2251
2252 /* OOM is global, do not handle */
2253 if (!memcg)
2254 return false;
2255
2256 if (!handle)
2257 goto cleanup;
2258
2259 owait.memcg = memcg;
2260 owait.wait.flags = 0;
2261 owait.wait.func = memcg_oom_wake_function;
2262 owait.wait.private = current;
2263 INIT_LIST_HEAD(&owait.wait.task_list);
2264
2265 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002266 mem_cgroup_mark_under_oom(memcg);
2267
2268 locked = mem_cgroup_oom_trylock(memcg);
2269
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002270 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002271 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002272
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002273 if (locked && !memcg->oom_kill_disable) {
2274 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07002275 finish_wait(&memcg_oom_waitq, &owait.wait);
2276 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2277 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002278 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002279 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07002280 mem_cgroup_unmark_under_oom(memcg);
2281 finish_wait(&memcg_oom_waitq, &owait.wait);
2282 }
2283
2284 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002285 mem_cgroup_oom_unlock(memcg);
2286 /*
2287 * There is no guarantee that an OOM-lock contender
2288 * sees the wakeups triggered by the OOM kill
2289 * uncharges. Wake any sleepers explicitely.
2290 */
2291 memcg_oom_recover(memcg);
2292 }
Johannes Weiner49426422013-10-16 13:46:59 -07002293cleanup:
2294 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002295 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002296 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002297}
2298
Balbir Singhd69b0422009-06-17 16:26:34 -07002299/*
2300 * Currently used to update mapped file statistics, but the routine can be
2301 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002302 *
2303 * Notes: Race condition
2304 *
2305 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2306 * it tends to be costly. But considering some conditions, we doesn't need
2307 * to do so _always_.
2308 *
2309 * Considering "charge", lock_page_cgroup() is not required because all
2310 * file-stat operations happen after a page is attached to radix-tree. There
2311 * are no race with "charge".
2312 *
2313 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2314 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2315 * if there are race with "uncharge". Statistics itself is properly handled
2316 * by flags.
2317 *
2318 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002319 * small, we check mm->moving_account and detect there are possibility of race
2320 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002321 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002322
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002323void __mem_cgroup_begin_update_page_stat(struct page *page,
2324 bool *locked, unsigned long *flags)
2325{
2326 struct mem_cgroup *memcg;
2327 struct page_cgroup *pc;
2328
2329 pc = lookup_page_cgroup(page);
2330again:
2331 memcg = pc->mem_cgroup;
2332 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2333 return;
2334 /*
2335 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002336 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002337 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002338 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002339 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002340 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002341 return;
2342
2343 move_lock_mem_cgroup(memcg, flags);
2344 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2345 move_unlock_mem_cgroup(memcg, flags);
2346 goto again;
2347 }
2348 *locked = true;
2349}
2350
2351void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2352{
2353 struct page_cgroup *pc = lookup_page_cgroup(page);
2354
2355 /*
2356 * It's guaranteed that pc->mem_cgroup never changes while
2357 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002358 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002359 */
2360 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2361}
2362
Greg Thelen2a7106f2011-01-13 15:47:37 -08002363void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002364 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002365{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002366 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002367 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002368 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002369
Johannes Weinercfa44942012-01-12 17:18:38 -08002370 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002371 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002372
Sha Zhengju658b72c2013-09-12 15:13:52 -07002373 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 memcg = pc->mem_cgroup;
2375 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002376 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002377
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002378 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002379}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002380
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002381/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002382 * size of first charge trial. "32" comes from vmscan.c's magic value.
2383 * TODO: maybe necessary to use big numbers in big irons.
2384 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002385#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002386struct memcg_stock_pcp {
2387 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002388 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002389 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002390 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002391#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002392};
2393static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002394static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002395
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002396/**
2397 * consume_stock: Try to consume stocked charge on this cpu.
2398 * @memcg: memcg to consume from.
2399 * @nr_pages: how many pages to charge.
2400 *
2401 * The charges will only happen if @memcg matches the current cpu's memcg
2402 * stock, and at least @nr_pages are available in that stock. Failure to
2403 * service an allocation will refill the stock.
2404 *
2405 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002406 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002407static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002408{
2409 struct memcg_stock_pcp *stock;
2410 bool ret = true;
2411
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002412 if (nr_pages > CHARGE_BATCH)
2413 return false;
2414
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002415 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002416 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2417 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002418 else /* need to call res_counter_charge */
2419 ret = false;
2420 put_cpu_var(memcg_stock);
2421 return ret;
2422}
2423
2424/*
2425 * Returns stocks cached in percpu to res_counter and reset cached information.
2426 */
2427static void drain_stock(struct memcg_stock_pcp *stock)
2428{
2429 struct mem_cgroup *old = stock->cached;
2430
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002431 if (stock->nr_pages) {
2432 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2433
2434 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002435 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002436 res_counter_uncharge(&old->memsw, bytes);
2437 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002438 }
2439 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002440}
2441
2442/*
2443 * This must be called under preempt disabled or must be called by
2444 * a thread which is pinned to local cpu.
2445 */
2446static void drain_local_stock(struct work_struct *dummy)
2447{
2448 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2449 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002450 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002451}
2452
Michal Hockoe4777492013-02-22 16:35:40 -08002453static void __init memcg_stock_init(void)
2454{
2455 int cpu;
2456
2457 for_each_possible_cpu(cpu) {
2458 struct memcg_stock_pcp *stock =
2459 &per_cpu(memcg_stock, cpu);
2460 INIT_WORK(&stock->work, drain_local_stock);
2461 }
2462}
2463
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002464/*
2465 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002466 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002467 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002468static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002469{
2470 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2471
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002472 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002473 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002474 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002475 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002476 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002477 put_cpu_var(memcg_stock);
2478}
2479
2480/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002481 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002482 * of the hierarchy under it. sync flag says whether we should block
2483 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002484 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002486{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002487 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002488
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002489 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002490 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002491 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002492 for_each_online_cpu(cpu) {
2493 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002494 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002496 memcg = stock->cached;
2497 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002498 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002499 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002500 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002501 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2502 if (cpu == curcpu)
2503 drain_local_stock(&stock->work);
2504 else
2505 schedule_work_on(cpu, &stock->work);
2506 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002507 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002508 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002509
2510 if (!sync)
2511 goto out;
2512
2513 for_each_online_cpu(cpu) {
2514 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002515 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002516 flush_work(&stock->work);
2517 }
2518out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002519 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002520}
2521
2522/*
2523 * Tries to drain stocked charges in other cpus. This function is asynchronous
2524 * and just put a work per cpu for draining localy on each cpu. Caller can
2525 * expects some charges will be back to res_counter later but cannot wait for
2526 * it.
2527 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002528static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002529{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002530 /*
2531 * If someone calls draining, avoid adding more kworker runs.
2532 */
2533 if (!mutex_trylock(&percpu_charge_mutex))
2534 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002535 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002536 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002537}
2538
2539/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002540static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002541{
2542 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002543 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002544 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002545 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002546}
2547
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002548/*
2549 * This function drains percpu counter value from DEAD cpu and
2550 * move it to local cpu. Note that this function can be preempted.
2551 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002552static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002553{
2554 int i;
2555
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002556 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002557 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002558 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002559
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002560 per_cpu(memcg->stat->count[i], cpu) = 0;
2561 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002562 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002563 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002564 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002565
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566 per_cpu(memcg->stat->events[i], cpu) = 0;
2567 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002568 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002569 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002570}
2571
Paul Gortmaker0db06282013-06-19 14:53:51 -04002572static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002573 unsigned long action,
2574 void *hcpu)
2575{
2576 int cpu = (unsigned long)hcpu;
2577 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002578 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002579
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002580 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002581 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002582
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002583 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002584 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002585
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002586 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002587 mem_cgroup_drain_pcp_counter(iter, cpu);
2588
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002589 stock = &per_cpu(memcg_stock, cpu);
2590 drain_stock(stock);
2591 return NOTIFY_OK;
2592}
2593
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002594
2595/* See __mem_cgroup_try_charge() for details */
2596enum {
2597 CHARGE_OK, /* success */
2598 CHARGE_RETRY, /* need to retry but retry is not bad */
2599 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2600 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002601};
2602
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002603static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002604 unsigned int nr_pages, unsigned int min_pages,
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002605 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002606{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002607 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002608 struct mem_cgroup *mem_over_limit;
2609 struct res_counter *fail_res;
2610 unsigned long flags = 0;
2611 int ret;
2612
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002613 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002614
2615 if (likely(!ret)) {
2616 if (!do_swap_account)
2617 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002618 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002619 if (likely(!ret))
2620 return CHARGE_OK;
2621
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002622 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002623 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2624 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2625 } else
2626 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002627 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002628 * Never reclaim on behalf of optional batching, retry with a
2629 * single page instead.
2630 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002631 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002632 return CHARGE_RETRY;
2633
2634 if (!(gfp_mask & __GFP_WAIT))
2635 return CHARGE_WOULDBLOCK;
2636
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002637 if (gfp_mask & __GFP_NORETRY)
2638 return CHARGE_NOMEM;
2639
Johannes Weiner56600482012-01-12 17:17:59 -08002640 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002641 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002642 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002643 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002644 * Even though the limit is exceeded at this point, reclaim
2645 * may have been able to free some pages. Retry the charge
2646 * before killing the task.
2647 *
2648 * Only for regular pages, though: huge pages are rather
2649 * unlikely to succeed so close to the limit, and we fall back
2650 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002651 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002652 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002653 return CHARGE_RETRY;
2654
2655 /*
2656 * At task move, charge accounts can be doubly counted. So, it's
2657 * better to wait until the end of task_move if something is going on.
2658 */
2659 if (mem_cgroup_wait_acct_move(mem_over_limit))
2660 return CHARGE_RETRY;
2661
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002662 if (invoke_oom)
2663 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002664
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002665 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002666}
2667
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002668/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002669 * __mem_cgroup_try_charge() does
2670 * 1. detect memcg to be charged against from passed *mm and *ptr,
2671 * 2. update res_counter
2672 * 3. call memory reclaim if necessary.
2673 *
2674 * In some special case, if the task is fatal, fatal_signal_pending() or
2675 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2676 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2677 * as possible without any hazards. 2: all pages should have a valid
2678 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2679 * pointer, that is treated as a charge to root_mem_cgroup.
2680 *
2681 * So __mem_cgroup_try_charge() will return
2682 * 0 ... on success, filling *ptr with a valid memcg pointer.
2683 * -ENOMEM ... charge failure because of resource limits.
2684 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2685 *
2686 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2687 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002688 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002689static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002690 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002691 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002692 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002693 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002694{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002695 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002696 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002697 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002698 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002699
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002700 /*
2701 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2702 * in system level. So, allow to go ahead dying process in addition to
2703 * MEMDIE process.
2704 */
2705 if (unlikely(test_thread_flag(TIF_MEMDIE)
2706 || fatal_signal_pending(current)))
2707 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002708
Johannes Weiner49426422013-10-16 13:46:59 -07002709 if (unlikely(task_in_memcg_oom(current)))
2710 goto bypass;
2711
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002712 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002713 * We always charge the cgroup the mm_struct belongs to.
2714 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002715 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002716 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002717 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002718 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002719 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002720again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002721 if (*ptr) { /* css should be a valid one */
2722 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002723 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002724 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002725 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002726 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002727 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002728 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002729 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002730
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002731 rcu_read_lock();
2732 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002733 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002734 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002735 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002736 * race with swapoff. Then, we have small risk of mis-accouning.
2737 * But such kind of mis-account by race always happens because
2738 * we don't have cgroup_mutex(). It's overkill and we allo that
2739 * small race, here.
2740 * (*) swapoff at el will charge against mm-struct not against
2741 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002742 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002743 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002744 if (!memcg)
2745 memcg = root_mem_cgroup;
2746 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002747 rcu_read_unlock();
2748 goto done;
2749 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002750 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002751 /*
2752 * It seems dagerous to access memcg without css_get().
2753 * But considering how consume_stok works, it's not
2754 * necessary. If consume_stock success, some charges
2755 * from this memcg are cached on this cpu. So, we
2756 * don't need to call css_get()/css_tryget() before
2757 * calling consume_stock().
2758 */
2759 rcu_read_unlock();
2760 goto done;
2761 }
2762 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002763 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002764 rcu_read_unlock();
2765 goto again;
2766 }
2767 rcu_read_unlock();
2768 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002769
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002770 do {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002771 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002772
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002773 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002774 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002775 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002776 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002777 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002778
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002779 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2780 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002781 switch (ret) {
2782 case CHARGE_OK:
2783 break;
2784 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002785 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002786 css_put(&memcg->css);
2787 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002788 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002789 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002790 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002791 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002792 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002793 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002794 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002795 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002796 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002797 nr_oom_retries--;
2798 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002799 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002800 } while (ret != CHARGE_OK);
2801
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002802 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002803 refill_stock(memcg, batch - nr_pages);
2804 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002805done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002806 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002807 return 0;
2808nomem:
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002809 if (!(gfp_mask & __GFP_NOFAIL)) {
2810 *ptr = NULL;
2811 return -ENOMEM;
2812 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002813bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002814 *ptr = root_mem_cgroup;
2815 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002816}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002817
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002818/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002819 * Somemtimes we have to undo a charge we got by try_charge().
2820 * This function is for that and do uncharge, put css's refcnt.
2821 * gotten by try_charge().
2822 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002823static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002824 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002825{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002826 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002827 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002828
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002829 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002830 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002831 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002832 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002833}
2834
2835/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002836 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2837 * This is useful when moving usage to parent cgroup.
2838 */
2839static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2840 unsigned int nr_pages)
2841{
2842 unsigned long bytes = nr_pages * PAGE_SIZE;
2843
2844 if (mem_cgroup_is_root(memcg))
2845 return;
2846
2847 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2848 if (do_swap_account)
2849 res_counter_uncharge_until(&memcg->memsw,
2850 memcg->memsw.parent, bytes);
2851}
2852
2853/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002854 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002855 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2856 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2857 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002858 */
2859static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2860{
2861 struct cgroup_subsys_state *css;
2862
2863 /* ID 0 is unused ID */
2864 if (!id)
2865 return NULL;
2866 css = css_lookup(&mem_cgroup_subsys, id);
2867 if (!css)
2868 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002869 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002870}
2871
Wu Fengguange42d9d52009-12-16 12:19:59 +01002872struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002873{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002874 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002875 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002876 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002877 swp_entry_t ent;
2878
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002879 VM_BUG_ON(!PageLocked(page));
2880
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002881 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002882 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002883 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002884 memcg = pc->mem_cgroup;
2885 if (memcg && !css_tryget(&memcg->css))
2886 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002887 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002888 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002889 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002890 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002891 memcg = mem_cgroup_lookup(id);
2892 if (memcg && !css_tryget(&memcg->css))
2893 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002894 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002895 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002896 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002897 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002898}
2899
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002900static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002901 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002902 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002903 enum charge_type ctype,
2904 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002905{
Johannes Weinerce587e62012-04-24 20:22:33 +02002906 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002907 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002908 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002909 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002910 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002911
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002912 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002913 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002914 /*
2915 * we don't need page_cgroup_lock about tail pages, becase they are not
2916 * accessed by any other context at this point.
2917 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002918
2919 /*
2920 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2921 * may already be on some other mem_cgroup's LRU. Take care of it.
2922 */
2923 if (lrucare) {
2924 zone = page_zone(page);
2925 spin_lock_irq(&zone->lru_lock);
2926 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002927 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002928 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002929 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002930 was_on_lru = true;
2931 }
2932 }
2933
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002934 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002935 /*
2936 * We access a page_cgroup asynchronously without lock_page_cgroup().
2937 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2938 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2939 * before USED bit, we need memory barrier here.
2940 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002941 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002942 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002943 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002944
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002945 if (lrucare) {
2946 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002947 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002948 VM_BUG_ON(PageLRU(page));
2949 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002950 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002951 }
2952 spin_unlock_irq(&zone->lru_lock);
2953 }
2954
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002955 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002956 anon = true;
2957 else
2958 anon = false;
2959
David Rientjesb070e652013-05-07 16:18:09 -07002960 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002961 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002962
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002963 /*
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07002964 * "charge_statistics" updated event counter. Then, check it.
2965 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2966 * if they exceeds softlimit.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002967 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002968 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002969}
2970
Glauber Costa7cf27982012-12-18 14:22:55 -08002971static DEFINE_MUTEX(set_limit_mutex);
2972
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002973#ifdef CONFIG_MEMCG_KMEM
2974static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2975{
2976 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2977 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2978}
2979
Glauber Costa1f458cb2012-12-18 14:22:50 -08002980/*
2981 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2982 * in the memcg_cache_params struct.
2983 */
2984static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2985{
2986 struct kmem_cache *cachep;
2987
2988 VM_BUG_ON(p->is_root_cache);
2989 cachep = p->root_cache;
2990 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2991}
2992
Glauber Costa749c5412012-12-18 14:23:01 -08002993#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002994static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2995 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002996{
Tejun Heo182446d2013-08-08 20:11:24 -04002997 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002998 struct memcg_cache_params *params;
2999
3000 if (!memcg_can_account_kmem(memcg))
3001 return -EIO;
3002
3003 print_slabinfo_header(m);
3004
3005 mutex_lock(&memcg->slab_caches_mutex);
3006 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
3007 cache_show(memcg_params_to_cache(params), m);
3008 mutex_unlock(&memcg->slab_caches_mutex);
3009
3010 return 0;
3011}
3012#endif
3013
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003014static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
3015{
3016 struct res_counter *fail_res;
3017 struct mem_cgroup *_memcg;
3018 int ret = 0;
3019 bool may_oom;
3020
3021 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
3022 if (ret)
3023 return ret;
3024
3025 /*
3026 * Conditions under which we can wait for the oom_killer. Those are
3027 * the same conditions tested by the core page allocator
3028 */
3029 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
3030
3031 _memcg = memcg;
3032 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
3033 &_memcg, may_oom);
3034
3035 if (ret == -EINTR) {
3036 /*
3037 * __mem_cgroup_try_charge() chosed to bypass to root due to
3038 * OOM kill or fatal signal. Since our only options are to
3039 * either fail the allocation or charge it to this cgroup, do
3040 * it as a temporary condition. But we can't fail. From a
3041 * kmem/slab perspective, the cache has already been selected,
3042 * by mem_cgroup_kmem_get_cache(), so it is too late to change
3043 * our minds.
3044 *
3045 * This condition will only trigger if the task entered
3046 * memcg_charge_kmem in a sane state, but was OOM-killed during
3047 * __mem_cgroup_try_charge() above. Tasks that were already
3048 * dying when the allocation triggers should have been already
3049 * directed to the root cgroup in memcontrol.h
3050 */
3051 res_counter_charge_nofail(&memcg->res, size, &fail_res);
3052 if (do_swap_account)
3053 res_counter_charge_nofail(&memcg->memsw, size,
3054 &fail_res);
3055 ret = 0;
3056 } else if (ret)
3057 res_counter_uncharge(&memcg->kmem, size);
3058
3059 return ret;
3060}
3061
3062static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
3063{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003064 res_counter_uncharge(&memcg->res, size);
3065 if (do_swap_account)
3066 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08003067
3068 /* Not down to 0 */
3069 if (res_counter_uncharge(&memcg->kmem, size))
3070 return;
3071
Li Zefan10d5ebf2013-07-08 16:00:33 -07003072 /*
3073 * Releases a reference taken in kmem_cgroup_css_offline in case
3074 * this last uncharge is racing with the offlining code or it is
3075 * outliving the memcg existence.
3076 *
3077 * The memory barrier imposed by test&clear is paired with the
3078 * explicit one in memcg_kmem_mark_dead().
3079 */
Glauber Costa7de37682012-12-18 14:22:07 -08003080 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07003081 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003082}
3083
Glauber Costa2633d7a2012-12-18 14:22:34 -08003084void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
3085{
3086 if (!memcg)
3087 return;
3088
3089 mutex_lock(&memcg->slab_caches_mutex);
3090 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
3091 mutex_unlock(&memcg->slab_caches_mutex);
3092}
3093
3094/*
3095 * helper for acessing a memcg's index. It will be used as an index in the
3096 * child cache array in kmem_cache, and also to derive its name. This function
3097 * will return -1 when this is not a kmem-limited memcg.
3098 */
3099int memcg_cache_id(struct mem_cgroup *memcg)
3100{
3101 return memcg ? memcg->kmemcg_id : -1;
3102}
3103
Glauber Costa55007d82012-12-18 14:22:38 -08003104/*
3105 * This ends up being protected by the set_limit mutex, during normal
3106 * operation, because that is its main call site.
3107 *
3108 * But when we create a new cache, we can call this as well if its parent
3109 * is kmem-limited. That will have to hold set_limit_mutex as well.
3110 */
3111int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3112{
3113 int num, ret;
3114
3115 num = ida_simple_get(&kmem_limited_groups,
3116 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3117 if (num < 0)
3118 return num;
3119 /*
3120 * After this point, kmem_accounted (that we test atomically in
3121 * the beginning of this conditional), is no longer 0. This
3122 * guarantees only one process will set the following boolean
3123 * to true. We don't need test_and_set because we're protected
3124 * by the set_limit_mutex anyway.
3125 */
3126 memcg_kmem_set_activated(memcg);
3127
3128 ret = memcg_update_all_caches(num+1);
3129 if (ret) {
3130 ida_simple_remove(&kmem_limited_groups, num);
3131 memcg_kmem_clear_activated(memcg);
3132 return ret;
3133 }
3134
3135 memcg->kmemcg_id = num;
3136 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3137 mutex_init(&memcg->slab_caches_mutex);
3138 return 0;
3139}
3140
3141static size_t memcg_caches_array_size(int num_groups)
3142{
3143 ssize_t size;
3144 if (num_groups <= 0)
3145 return 0;
3146
3147 size = 2 * num_groups;
3148 if (size < MEMCG_CACHES_MIN_SIZE)
3149 size = MEMCG_CACHES_MIN_SIZE;
3150 else if (size > MEMCG_CACHES_MAX_SIZE)
3151 size = MEMCG_CACHES_MAX_SIZE;
3152
3153 return size;
3154}
3155
3156/*
3157 * We should update the current array size iff all caches updates succeed. This
3158 * can only be done from the slab side. The slab mutex needs to be held when
3159 * calling this.
3160 */
3161void memcg_update_array_size(int num)
3162{
3163 if (num > memcg_limited_groups_array_size)
3164 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3165}
3166
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003167static void kmem_cache_destroy_work_func(struct work_struct *w);
3168
Glauber Costa55007d82012-12-18 14:22:38 -08003169int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3170{
3171 struct memcg_cache_params *cur_params = s->memcg_params;
3172
3173 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3174
3175 if (num_groups > memcg_limited_groups_array_size) {
3176 int i;
3177 ssize_t size = memcg_caches_array_size(num_groups);
3178
3179 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003180 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003181
3182 s->memcg_params = kzalloc(size, GFP_KERNEL);
3183 if (!s->memcg_params) {
3184 s->memcg_params = cur_params;
3185 return -ENOMEM;
3186 }
3187
3188 s->memcg_params->is_root_cache = true;
3189
3190 /*
3191 * There is the chance it will be bigger than
3192 * memcg_limited_groups_array_size, if we failed an allocation
3193 * in a cache, in which case all caches updated before it, will
3194 * have a bigger array.
3195 *
3196 * But if that is the case, the data after
3197 * memcg_limited_groups_array_size is certainly unused
3198 */
3199 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3200 if (!cur_params->memcg_caches[i])
3201 continue;
3202 s->memcg_params->memcg_caches[i] =
3203 cur_params->memcg_caches[i];
3204 }
3205
3206 /*
3207 * Ideally, we would wait until all caches succeed, and only
3208 * then free the old one. But this is not worth the extra
3209 * pointer per-cache we'd have to have for this.
3210 *
3211 * It is not a big deal if some caches are left with a size
3212 * bigger than the others. And all updates will reset this
3213 * anyway.
3214 */
3215 kfree(cur_params);
3216 }
3217 return 0;
3218}
3219
Glauber Costa943a4512012-12-18 14:23:03 -08003220int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3221 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003222{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003223 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003224
3225 if (!memcg_kmem_enabled())
3226 return 0;
3227
Andrey Vagin90c7a792013-09-11 14:22:18 -07003228 if (!memcg) {
3229 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003230 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003231 } else
3232 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003233
Glauber Costa2633d7a2012-12-18 14:22:34 -08003234 s->memcg_params = kzalloc(size, GFP_KERNEL);
3235 if (!s->memcg_params)
3236 return -ENOMEM;
3237
Glauber Costa943a4512012-12-18 14:23:03 -08003238 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003239 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003240 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003241 INIT_WORK(&s->memcg_params->destroy,
3242 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003243 } else
3244 s->memcg_params->is_root_cache = true;
3245
Glauber Costa2633d7a2012-12-18 14:22:34 -08003246 return 0;
3247}
3248
3249void memcg_release_cache(struct kmem_cache *s)
3250{
Glauber Costad7f25f82012-12-18 14:22:40 -08003251 struct kmem_cache *root;
3252 struct mem_cgroup *memcg;
3253 int id;
3254
3255 /*
3256 * This happens, for instance, when a root cache goes away before we
3257 * add any memcg.
3258 */
3259 if (!s->memcg_params)
3260 return;
3261
3262 if (s->memcg_params->is_root_cache)
3263 goto out;
3264
3265 memcg = s->memcg_params->memcg;
3266 id = memcg_cache_id(memcg);
3267
3268 root = s->memcg_params->root_cache;
3269 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003270
3271 mutex_lock(&memcg->slab_caches_mutex);
3272 list_del(&s->memcg_params->list);
3273 mutex_unlock(&memcg->slab_caches_mutex);
3274
Li Zefan20f05312013-07-08 16:00:31 -07003275 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003276out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003277 kfree(s->memcg_params);
3278}
3279
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003280/*
3281 * During the creation a new cache, we need to disable our accounting mechanism
3282 * altogether. This is true even if we are not creating, but rather just
3283 * enqueing new caches to be created.
3284 *
3285 * This is because that process will trigger allocations; some visible, like
3286 * explicit kmallocs to auxiliary data structures, name strings and internal
3287 * cache structures; some well concealed, like INIT_WORK() that can allocate
3288 * objects during debug.
3289 *
3290 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3291 * to it. This may not be a bounded recursion: since the first cache creation
3292 * failed to complete (waiting on the allocation), we'll just try to create the
3293 * cache again, failing at the same point.
3294 *
3295 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3296 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3297 * inside the following two functions.
3298 */
3299static inline void memcg_stop_kmem_account(void)
3300{
3301 VM_BUG_ON(!current->mm);
3302 current->memcg_kmem_skip_account++;
3303}
3304
3305static inline void memcg_resume_kmem_account(void)
3306{
3307 VM_BUG_ON(!current->mm);
3308 current->memcg_kmem_skip_account--;
3309}
3310
Glauber Costa1f458cb2012-12-18 14:22:50 -08003311static void kmem_cache_destroy_work_func(struct work_struct *w)
3312{
3313 struct kmem_cache *cachep;
3314 struct memcg_cache_params *p;
3315
3316 p = container_of(w, struct memcg_cache_params, destroy);
3317
3318 cachep = memcg_params_to_cache(p);
3319
Glauber Costa22933152012-12-18 14:22:59 -08003320 /*
3321 * If we get down to 0 after shrink, we could delete right away.
3322 * However, memcg_release_pages() already puts us back in the workqueue
3323 * in that case. If we proceed deleting, we'll get a dangling
3324 * reference, and removing the object from the workqueue in that case
3325 * is unnecessary complication. We are not a fast path.
3326 *
3327 * Note that this case is fundamentally different from racing with
3328 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3329 * kmem_cache_shrink, not only we would be reinserting a dead cache
3330 * into the queue, but doing so from inside the worker racing to
3331 * destroy it.
3332 *
3333 * So if we aren't down to zero, we'll just schedule a worker and try
3334 * again
3335 */
3336 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3337 kmem_cache_shrink(cachep);
3338 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3339 return;
3340 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003341 kmem_cache_destroy(cachep);
3342}
3343
3344void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3345{
3346 if (!cachep->memcg_params->dead)
3347 return;
3348
3349 /*
Glauber Costa22933152012-12-18 14:22:59 -08003350 * There are many ways in which we can get here.
3351 *
3352 * We can get to a memory-pressure situation while the delayed work is
3353 * still pending to run. The vmscan shrinkers can then release all
3354 * cache memory and get us to destruction. If this is the case, we'll
3355 * be executed twice, which is a bug (the second time will execute over
3356 * bogus data). In this case, cancelling the work should be fine.
3357 *
3358 * But we can also get here from the worker itself, if
3359 * kmem_cache_shrink is enough to shake all the remaining objects and
3360 * get the page count to 0. In this case, we'll deadlock if we try to
3361 * cancel the work (the worker runs with an internal lock held, which
3362 * is the same lock we would hold for cancel_work_sync().)
3363 *
3364 * Since we can't possibly know who got us here, just refrain from
3365 * running if there is already work pending
3366 */
3367 if (work_pending(&cachep->memcg_params->destroy))
3368 return;
3369 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003370 * We have to defer the actual destroying to a workqueue, because
3371 * we might currently be in a context that cannot sleep.
3372 */
3373 schedule_work(&cachep->memcg_params->destroy);
3374}
3375
Glauber Costad7f25f82012-12-18 14:22:40 -08003376/*
3377 * This lock protects updaters, not readers. We want readers to be as fast as
3378 * they can, and they will either see NULL or a valid cache value. Our model
3379 * allow them to see NULL, in which case the root memcg will be selected.
3380 *
3381 * We need this lock because multiple allocations to the same cache from a non
3382 * will span more than one worker. Only one of them can create the cache.
3383 */
3384static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003385
3386/*
3387 * Called with memcg_cache_mutex held
3388 */
3389static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3390 struct kmem_cache *s)
3391{
3392 struct kmem_cache *new;
3393 static char *tmp_name = NULL;
3394
3395 lockdep_assert_held(&memcg_cache_mutex);
3396
3397 /*
3398 * kmem_cache_create_memcg duplicates the given name and
3399 * cgroup_name for this name requires RCU context.
3400 * This static temporary buffer is used to prevent from
3401 * pointless shortliving allocation.
3402 */
3403 if (!tmp_name) {
3404 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3405 if (!tmp_name)
3406 return NULL;
3407 }
3408
3409 rcu_read_lock();
3410 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3411 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3412 rcu_read_unlock();
3413
3414 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3415 (s->flags & ~SLAB_PANIC), s->ctor, s);
3416
3417 if (new)
3418 new->allocflags |= __GFP_KMEMCG;
3419
3420 return new;
3421}
3422
Glauber Costad7f25f82012-12-18 14:22:40 -08003423static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3424 struct kmem_cache *cachep)
3425{
3426 struct kmem_cache *new_cachep;
3427 int idx;
3428
3429 BUG_ON(!memcg_can_account_kmem(memcg));
3430
3431 idx = memcg_cache_id(memcg);
3432
3433 mutex_lock(&memcg_cache_mutex);
3434 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003435 if (new_cachep) {
3436 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003437 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003438 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003439
3440 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003441 if (new_cachep == NULL) {
3442 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003443 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003444 goto out;
3445 }
3446
Glauber Costa1f458cb2012-12-18 14:22:50 -08003447 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003448
3449 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3450 /*
3451 * the readers won't lock, make sure everybody sees the updated value,
3452 * so they won't put stuff in the queue again for no reason
3453 */
3454 wmb();
3455out:
3456 mutex_unlock(&memcg_cache_mutex);
3457 return new_cachep;
3458}
3459
Glauber Costa7cf27982012-12-18 14:22:55 -08003460void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3461{
3462 struct kmem_cache *c;
3463 int i;
3464
3465 if (!s->memcg_params)
3466 return;
3467 if (!s->memcg_params->is_root_cache)
3468 return;
3469
3470 /*
3471 * If the cache is being destroyed, we trust that there is no one else
3472 * requesting objects from it. Even if there are, the sanity checks in
3473 * kmem_cache_destroy should caught this ill-case.
3474 *
3475 * Still, we don't want anyone else freeing memcg_caches under our
3476 * noses, which can happen if a new memcg comes to life. As usual,
3477 * we'll take the set_limit_mutex to protect ourselves against this.
3478 */
3479 mutex_lock(&set_limit_mutex);
3480 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3481 c = s->memcg_params->memcg_caches[i];
3482 if (!c)
3483 continue;
3484
3485 /*
3486 * We will now manually delete the caches, so to avoid races
3487 * we need to cancel all pending destruction workers and
3488 * proceed with destruction ourselves.
3489 *
3490 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3491 * and that could spawn the workers again: it is likely that
3492 * the cache still have active pages until this very moment.
3493 * This would lead us back to mem_cgroup_destroy_cache.
3494 *
3495 * But that will not execute at all if the "dead" flag is not
3496 * set, so flip it down to guarantee we are in control.
3497 */
3498 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003499 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003500 kmem_cache_destroy(c);
3501 }
3502 mutex_unlock(&set_limit_mutex);
3503}
3504
Glauber Costad7f25f82012-12-18 14:22:40 -08003505struct create_work {
3506 struct mem_cgroup *memcg;
3507 struct kmem_cache *cachep;
3508 struct work_struct work;
3509};
3510
Glauber Costa1f458cb2012-12-18 14:22:50 -08003511static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3512{
3513 struct kmem_cache *cachep;
3514 struct memcg_cache_params *params;
3515
3516 if (!memcg_kmem_is_active(memcg))
3517 return;
3518
3519 mutex_lock(&memcg->slab_caches_mutex);
3520 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3521 cachep = memcg_params_to_cache(params);
3522 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003523 schedule_work(&cachep->memcg_params->destroy);
3524 }
3525 mutex_unlock(&memcg->slab_caches_mutex);
3526}
3527
Glauber Costad7f25f82012-12-18 14:22:40 -08003528static void memcg_create_cache_work_func(struct work_struct *w)
3529{
3530 struct create_work *cw;
3531
3532 cw = container_of(w, struct create_work, work);
3533 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003534 kfree(cw);
3535}
3536
3537/*
3538 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003539 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003540static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3541 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003542{
3543 struct create_work *cw;
3544
3545 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003546 if (cw == NULL) {
3547 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003548 return;
3549 }
3550
3551 cw->memcg = memcg;
3552 cw->cachep = cachep;
3553
3554 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3555 schedule_work(&cw->work);
3556}
3557
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003558static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3559 struct kmem_cache *cachep)
3560{
3561 /*
3562 * We need to stop accounting when we kmalloc, because if the
3563 * corresponding kmalloc cache is not yet created, the first allocation
3564 * in __memcg_create_cache_enqueue will recurse.
3565 *
3566 * However, it is better to enclose the whole function. Depending on
3567 * the debugging options enabled, INIT_WORK(), for instance, can
3568 * trigger an allocation. This too, will make us recurse. Because at
3569 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3570 * the safest choice is to do it like this, wrapping the whole function.
3571 */
3572 memcg_stop_kmem_account();
3573 __memcg_create_cache_enqueue(memcg, cachep);
3574 memcg_resume_kmem_account();
3575}
Glauber Costad7f25f82012-12-18 14:22:40 -08003576/*
3577 * Return the kmem_cache we're supposed to use for a slab allocation.
3578 * We try to use the current memcg's version of the cache.
3579 *
3580 * If the cache does not exist yet, if we are the first user of it,
3581 * we either create it immediately, if possible, or create it asynchronously
3582 * in a workqueue.
3583 * In the latter case, we will let the current allocation go through with
3584 * the original cache.
3585 *
3586 * Can't be called in interrupt context or from kernel threads.
3587 * This function needs to be called with rcu_read_lock() held.
3588 */
3589struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3590 gfp_t gfp)
3591{
3592 struct mem_cgroup *memcg;
3593 int idx;
3594
3595 VM_BUG_ON(!cachep->memcg_params);
3596 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3597
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003598 if (!current->mm || current->memcg_kmem_skip_account)
3599 return cachep;
3600
Glauber Costad7f25f82012-12-18 14:22:40 -08003601 rcu_read_lock();
3602 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003603
3604 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003605 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003606
3607 idx = memcg_cache_id(memcg);
3608
3609 /*
3610 * barrier to mare sure we're always seeing the up to date value. The
3611 * code updating memcg_caches will issue a write barrier to match this.
3612 */
3613 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003614 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3615 cachep = cachep->memcg_params->memcg_caches[idx];
3616 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003617 }
3618
Li Zefanca0dde92013-04-29 15:08:57 -07003619 /* The corresponding put will be done in the workqueue. */
3620 if (!css_tryget(&memcg->css))
3621 goto out;
3622 rcu_read_unlock();
3623
3624 /*
3625 * If we are in a safe context (can wait, and not in interrupt
3626 * context), we could be be predictable and return right away.
3627 * This would guarantee that the allocation being performed
3628 * already belongs in the new cache.
3629 *
3630 * However, there are some clashes that can arrive from locking.
3631 * For instance, because we acquire the slab_mutex while doing
3632 * kmem_cache_dup, this means no further allocation could happen
3633 * with the slab_mutex held.
3634 *
3635 * Also, because cache creation issue get_online_cpus(), this
3636 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3637 * that ends up reversed during cpu hotplug. (cpuset allocates
3638 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3639 * better to defer everything.
3640 */
3641 memcg_create_cache_enqueue(memcg, cachep);
3642 return cachep;
3643out:
3644 rcu_read_unlock();
3645 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003646}
3647EXPORT_SYMBOL(__memcg_kmem_get_cache);
3648
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003649/*
3650 * We need to verify if the allocation against current->mm->owner's memcg is
3651 * possible for the given order. But the page is not allocated yet, so we'll
3652 * need a further commit step to do the final arrangements.
3653 *
3654 * It is possible for the task to switch cgroups in this mean time, so at
3655 * commit time, we can't rely on task conversion any longer. We'll then use
3656 * the handle argument to return to the caller which cgroup we should commit
3657 * against. We could also return the memcg directly and avoid the pointer
3658 * passing, but a boolean return value gives better semantics considering
3659 * the compiled-out case as well.
3660 *
3661 * Returning true means the allocation is possible.
3662 */
3663bool
3664__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3665{
3666 struct mem_cgroup *memcg;
3667 int ret;
3668
3669 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003670
3671 /*
3672 * Disabling accounting is only relevant for some specific memcg
3673 * internal allocations. Therefore we would initially not have such
3674 * check here, since direct calls to the page allocator that are marked
3675 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3676 * concerned with cache allocations, and by having this test at
3677 * memcg_kmem_get_cache, we are already able to relay the allocation to
3678 * the root cache and bypass the memcg cache altogether.
3679 *
3680 * There is one exception, though: the SLUB allocator does not create
3681 * large order caches, but rather service large kmallocs directly from
3682 * the page allocator. Therefore, the following sequence when backed by
3683 * the SLUB allocator:
3684 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003685 * memcg_stop_kmem_account();
3686 * kmalloc(<large_number>)
3687 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003688 *
3689 * would effectively ignore the fact that we should skip accounting,
3690 * since it will drive us directly to this function without passing
3691 * through the cache selector memcg_kmem_get_cache. Such large
3692 * allocations are extremely rare but can happen, for instance, for the
3693 * cache arrays. We bring this test here.
3694 */
3695 if (!current->mm || current->memcg_kmem_skip_account)
3696 return true;
3697
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003698 memcg = try_get_mem_cgroup_from_mm(current->mm);
3699
3700 /*
3701 * very rare case described in mem_cgroup_from_task. Unfortunately there
3702 * isn't much we can do without complicating this too much, and it would
3703 * be gfp-dependent anyway. Just let it go
3704 */
3705 if (unlikely(!memcg))
3706 return true;
3707
3708 if (!memcg_can_account_kmem(memcg)) {
3709 css_put(&memcg->css);
3710 return true;
3711 }
3712
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003713 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3714 if (!ret)
3715 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003716
3717 css_put(&memcg->css);
3718 return (ret == 0);
3719}
3720
3721void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3722 int order)
3723{
3724 struct page_cgroup *pc;
3725
3726 VM_BUG_ON(mem_cgroup_is_root(memcg));
3727
3728 /* The page allocation failed. Revert */
3729 if (!page) {
3730 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003731 return;
3732 }
3733
3734 pc = lookup_page_cgroup(page);
3735 lock_page_cgroup(pc);
3736 pc->mem_cgroup = memcg;
3737 SetPageCgroupUsed(pc);
3738 unlock_page_cgroup(pc);
3739}
3740
3741void __memcg_kmem_uncharge_pages(struct page *page, int order)
3742{
3743 struct mem_cgroup *memcg = NULL;
3744 struct page_cgroup *pc;
3745
3746
3747 pc = lookup_page_cgroup(page);
3748 /*
3749 * Fast unlocked return. Theoretically might have changed, have to
3750 * check again after locking.
3751 */
3752 if (!PageCgroupUsed(pc))
3753 return;
3754
3755 lock_page_cgroup(pc);
3756 if (PageCgroupUsed(pc)) {
3757 memcg = pc->mem_cgroup;
3758 ClearPageCgroupUsed(pc);
3759 }
3760 unlock_page_cgroup(pc);
3761
3762 /*
3763 * We trust that only if there is a memcg associated with the page, it
3764 * is a valid allocation
3765 */
3766 if (!memcg)
3767 return;
3768
3769 VM_BUG_ON(mem_cgroup_is_root(memcg));
3770 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003771}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003772#else
3773static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3774{
3775}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003776#endif /* CONFIG_MEMCG_KMEM */
3777
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003778#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3779
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003780#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003781/*
3782 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003783 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3784 * charge/uncharge will be never happen and move_account() is done under
3785 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003786 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003787void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003788{
3789 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003790 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003791 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003792 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003793
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003794 if (mem_cgroup_disabled())
3795 return;
David Rientjesb070e652013-05-07 16:18:09 -07003796
3797 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003798 for (i = 1; i < HPAGE_PMD_NR; i++) {
3799 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003800 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003801 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003802 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3803 }
David Rientjesb070e652013-05-07 16:18:09 -07003804 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3805 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003806}
Hugh Dickins12d27102012-01-12 17:19:52 -08003807#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003808
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003809static inline
3810void mem_cgroup_move_account_page_stat(struct mem_cgroup *from,
3811 struct mem_cgroup *to,
3812 unsigned int nr_pages,
3813 enum mem_cgroup_stat_index idx)
3814{
3815 /* Update stat data for mem_cgroup */
3816 preempt_disable();
Greg Thelen5e8cfc32013-10-30 13:56:21 -07003817 __this_cpu_sub(from->stat->count[idx], nr_pages);
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003818 __this_cpu_add(to->stat->count[idx], nr_pages);
3819 preempt_enable();
3820}
3821
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003822/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003823 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003824 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003825 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003826 * @pc: page_cgroup of the page.
3827 * @from: mem_cgroup which the page is moved from.
3828 * @to: mem_cgroup which the page is moved to. @from != @to.
3829 *
3830 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003831 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003832 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003833 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003834 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3835 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003836 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003837static int mem_cgroup_move_account(struct page *page,
3838 unsigned int nr_pages,
3839 struct page_cgroup *pc,
3840 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003841 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003842{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003843 unsigned long flags;
3844 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003845 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003846
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003847 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003848 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003849 /*
3850 * The page is isolated from LRU. So, collapse function
3851 * will not handle this page. But page splitting can happen.
3852 * Do this check under compound_page_lock(). The caller should
3853 * hold it.
3854 */
3855 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003856 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003857 goto out;
3858
3859 lock_page_cgroup(pc);
3860
3861 ret = -EINVAL;
3862 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3863 goto unlock;
3864
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003865 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003866
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003867 if (!anon && page_mapped(page))
3868 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3869 MEM_CGROUP_STAT_FILE_MAPPED);
3870
3871 if (PageWriteback(page))
3872 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3873 MEM_CGROUP_STAT_WRITEBACK);
3874
David Rientjesb070e652013-05-07 16:18:09 -07003875 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003876
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003877 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003878 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003879 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003880 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003881 ret = 0;
3882unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003883 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003884 /*
3885 * check events
3886 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003887 memcg_check_events(to, page);
3888 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003889out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003890 return ret;
3891}
3892
Michal Hocko2ef37d32012-10-26 13:37:30 +02003893/**
3894 * mem_cgroup_move_parent - moves page to the parent group
3895 * @page: the page to move
3896 * @pc: page_cgroup of the page
3897 * @child: page's cgroup
3898 *
3899 * move charges to its parent or the root cgroup if the group has no
3900 * parent (aka use_hierarchy==0).
3901 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3902 * mem_cgroup_move_account fails) the failure is always temporary and
3903 * it signals a race with a page removal/uncharge or migration. In the
3904 * first case the page is on the way out and it will vanish from the LRU
3905 * on the next attempt and the call should be retried later.
3906 * Isolation from the LRU fails only if page has been isolated from
3907 * the LRU since we looked at it and that usually means either global
3908 * reclaim or migration going on. The page will either get back to the
3909 * LRU or vanish.
3910 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3911 * (!PageCgroupUsed) or moved to a different group. The page will
3912 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003913 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003914static int mem_cgroup_move_parent(struct page *page,
3915 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003916 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003917{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003918 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003919 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003920 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003921 int ret;
3922
Michal Hockod8423012012-10-26 13:37:29 +02003923 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003924
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003925 ret = -EBUSY;
3926 if (!get_page_unless_zero(page))
3927 goto out;
3928 if (isolate_lru_page(page))
3929 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003930
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003931 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003932
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003933 parent = parent_mem_cgroup(child);
3934 /*
3935 * If no parent, move charges to root cgroup.
3936 */
3937 if (!parent)
3938 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003939
Michal Hocko2ef37d32012-10-26 13:37:30 +02003940 if (nr_pages > 1) {
3941 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003942 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003943 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003944
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003945 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003946 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003947 if (!ret)
3948 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003949
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003950 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003951 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003952 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003953put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003954 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003955out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003956 return ret;
3957}
3958
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003959/*
3960 * Charge the memory controller for page usage.
3961 * Return
3962 * 0 if the charge was successful
3963 * < 0 if the cgroup is over its limit
3964 */
3965static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003966 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003967{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003968 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003969 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003970 bool oom = true;
3971 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003972
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003973 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003974 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003975 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003976 /*
3977 * Never OOM-kill a process for a huge page. The
3978 * fault handler will fall back to regular pages.
3979 */
3980 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003981 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003982
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003983 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003984 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003985 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003986 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003987 return 0;
3988}
3989
3990int mem_cgroup_newpage_charge(struct page *page,
3991 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003992{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003993 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003994 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003995 VM_BUG_ON(page_mapped(page));
3996 VM_BUG_ON(page->mapping && !PageAnon(page));
3997 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003998 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003999 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08004000}
4001
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004002/*
4003 * While swap-in, try_charge -> commit or cancel, the page is locked.
4004 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02004005 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004006 * "commit()" or removed by "cancel()"
4007 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004008static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
4009 struct page *page,
4010 gfp_t mask,
4011 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004012{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004013 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07004014 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004015 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004016
Johannes Weiner90deb782012-07-31 16:45:47 -07004017 pc = lookup_page_cgroup(page);
4018 /*
4019 * Every swap fault against a single page tries to charge the
4020 * page, bail as early as possible. shmem_unuse() encounters
4021 * already charged pages, too. The USED bit is protected by
4022 * the page lock, which serializes swap cache removal, which
4023 * in turn serializes uncharging.
4024 */
4025 if (PageCgroupUsed(pc))
4026 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004027 if (!do_swap_account)
4028 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004029 memcg = try_get_mem_cgroup_from_page(page);
4030 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004031 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08004032 *memcgp = memcg;
4033 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004034 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004035 if (ret == -EINTR)
4036 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004037 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004038charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004039 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
4040 if (ret == -EINTR)
4041 ret = 0;
4042 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004043}
4044
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004045int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
4046 gfp_t gfp_mask, struct mem_cgroup **memcgp)
4047{
4048 *memcgp = NULL;
4049 if (mem_cgroup_disabled())
4050 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07004051 /*
4052 * A racing thread's fault, or swapoff, may have already
4053 * updated the pte, and even removed page from swap cache: in
4054 * those cases unuse_pte()'s pte_same() test will fail; but
4055 * there's also a KSM case which does need to charge the page.
4056 */
4057 if (!PageSwapCache(page)) {
4058 int ret;
4059
4060 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
4061 if (ret == -EINTR)
4062 ret = 0;
4063 return ret;
4064 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004065 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
4066}
4067
Johannes Weiner827a03d2012-07-31 16:45:36 -07004068void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
4069{
4070 if (mem_cgroup_disabled())
4071 return;
4072 if (!memcg)
4073 return;
4074 __mem_cgroup_cancel_charge(memcg, 1);
4075}
4076
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004077static void
Johannes Weiner72835c82012-01-12 17:18:32 -08004078__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004079 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004080{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004081 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004082 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08004083 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004084 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07004085
Johannes Weinerce587e62012-04-24 20:22:33 +02004086 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004087 /*
4088 * Now swap is on-memory. This means this page may be
4089 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004090 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
4091 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
4092 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004093 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004094 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004095 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07004096 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004097 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004098}
4099
Johannes Weiner72835c82012-01-12 17:18:32 -08004100void mem_cgroup_commit_charge_swapin(struct page *page,
4101 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004102{
Johannes Weiner72835c82012-01-12 17:18:32 -08004103 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004104 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004105}
4106
Johannes Weiner827a03d2012-07-31 16:45:36 -07004107int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
4108 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004109{
Johannes Weiner827a03d2012-07-31 16:45:36 -07004110 struct mem_cgroup *memcg = NULL;
4111 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4112 int ret;
4113
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004114 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07004115 return 0;
4116 if (PageCompound(page))
4117 return 0;
4118
Johannes Weiner827a03d2012-07-31 16:45:36 -07004119 if (!PageSwapCache(page))
4120 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
4121 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004122 ret = __mem_cgroup_try_charge_swapin(mm, page,
4123 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07004124 if (!ret)
4125 __mem_cgroup_commit_charge_swapin(page, memcg, type);
4126 }
4127 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004128}
4129
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004130static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004131 unsigned int nr_pages,
4132 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004133{
4134 struct memcg_batch_info *batch = NULL;
4135 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004136
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004137 /* If swapout, usage of swap doesn't decrease */
4138 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4139 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004140
4141 batch = &current->memcg_batch;
4142 /*
4143 * In usual, we do css_get() when we remember memcg pointer.
4144 * But in this case, we keep res->usage until end of a series of
4145 * uncharges. Then, it's ok to ignore memcg's refcnt.
4146 */
4147 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004148 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004149 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004150 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004151 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004152 * the same cgroup and we have chance to coalesce uncharges.
4153 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4154 * because we want to do uncharge as soon as possible.
4155 */
4156
4157 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4158 goto direct_uncharge;
4159
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004160 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004161 goto direct_uncharge;
4162
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004163 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004164 * In typical case, batch->memcg == mem. This means we can
4165 * merge a series of uncharges to an uncharge of res_counter.
4166 * If not, we uncharge res_counter ony by one.
4167 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004168 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004169 goto direct_uncharge;
4170 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004171 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004172 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004173 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004174 return;
4175direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004176 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004177 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004178 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4179 if (unlikely(batch->memcg != memcg))
4180 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004181}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004182
Balbir Singh8697d332008-02-07 00:13:59 -08004183/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004184 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004185 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004186static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004187__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4188 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004189{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004190 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004191 unsigned int nr_pages = 1;
4192 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004193 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004194
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004195 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004196 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004197
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004198 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004199 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004200 VM_BUG_ON(!PageTransHuge(page));
4201 }
Balbir Singh8697d332008-02-07 00:13:59 -08004202 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004203 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004204 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004205 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004206 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004207 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004208
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004209 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004210
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004211 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004212
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004213 if (!PageCgroupUsed(pc))
4214 goto unlock_out;
4215
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004216 anon = PageAnon(page);
4217
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004218 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004219 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004220 /*
4221 * Generally PageAnon tells if it's the anon statistics to be
4222 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4223 * used before page reached the stage of being marked PageAnon.
4224 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004225 anon = true;
4226 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004227 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004228 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004229 if (page_mapped(page))
4230 goto unlock_out;
4231 /*
4232 * Pages under migration may not be uncharged. But
4233 * end_migration() /must/ be the one uncharging the
4234 * unused post-migration page and so it has to call
4235 * here with the migration bit still set. See the
4236 * res_counter handling below.
4237 */
4238 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004239 goto unlock_out;
4240 break;
4241 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4242 if (!PageAnon(page)) { /* Shared memory */
4243 if (page->mapping && !page_is_file_cache(page))
4244 goto unlock_out;
4245 } else if (page_mapped(page)) /* Anon */
4246 goto unlock_out;
4247 break;
4248 default:
4249 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004250 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004251
David Rientjesb070e652013-05-07 16:18:09 -07004252 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004253
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004254 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004255 /*
4256 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4257 * freed from LRU. This is safe because uncharged page is expected not
4258 * to be reused (freed soon). Exception is SwapCache, it's handled by
4259 * special functions.
4260 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004261
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004262 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004263 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004264 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004265 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004266 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004267 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004268 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004269 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004270 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004271 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004272 /*
4273 * Migration does not charge the res_counter for the
4274 * replacement page, so leave it alone when phasing out the
4275 * page that is unused after the migration.
4276 */
4277 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004278 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004279
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004280 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004281
4282unlock_out:
4283 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004284 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004285}
4286
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004287void mem_cgroup_uncharge_page(struct page *page)
4288{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004289 /* early check. */
4290 if (page_mapped(page))
4291 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004292 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004293 /*
4294 * If the page is in swap cache, uncharge should be deferred
4295 * to the swap path, which also properly accounts swap usage
4296 * and handles memcg lifetime.
4297 *
4298 * Note that this check is not stable and reclaim may add the
4299 * page to swap cache at any time after this. However, if the
4300 * page is not in swap cache by the time page->mapcount hits
4301 * 0, there won't be any page table references to the swap
4302 * slot, and reclaim will free it and not actually write the
4303 * page to disk.
4304 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004305 if (PageSwapCache(page))
4306 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004307 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004308}
4309
4310void mem_cgroup_uncharge_cache_page(struct page *page)
4311{
4312 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004313 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004314 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004315}
4316
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004317/*
4318 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4319 * In that cases, pages are freed continuously and we can expect pages
4320 * are in the same memcg. All these calls itself limits the number of
4321 * pages freed at once, then uncharge_start/end() is called properly.
4322 * This may be called prural(2) times in a context,
4323 */
4324
4325void mem_cgroup_uncharge_start(void)
4326{
4327 current->memcg_batch.do_batch++;
4328 /* We can do nest. */
4329 if (current->memcg_batch.do_batch == 1) {
4330 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004331 current->memcg_batch.nr_pages = 0;
4332 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004333 }
4334}
4335
4336void mem_cgroup_uncharge_end(void)
4337{
4338 struct memcg_batch_info *batch = &current->memcg_batch;
4339
4340 if (!batch->do_batch)
4341 return;
4342
4343 batch->do_batch--;
4344 if (batch->do_batch) /* If stacked, do nothing. */
4345 return;
4346
4347 if (!batch->memcg)
4348 return;
4349 /*
4350 * This "batch->memcg" is valid without any css_get/put etc...
4351 * bacause we hide charges behind us.
4352 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004353 if (batch->nr_pages)
4354 res_counter_uncharge(&batch->memcg->res,
4355 batch->nr_pages * PAGE_SIZE);
4356 if (batch->memsw_nr_pages)
4357 res_counter_uncharge(&batch->memcg->memsw,
4358 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004359 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004360 /* forget this pointer (for sanity check) */
4361 batch->memcg = NULL;
4362}
4363
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004364#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004365/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004366 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004367 * memcg information is recorded to swap_cgroup of "ent"
4368 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004369void
4370mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004371{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004372 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004373 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004374
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004375 if (!swapout) /* this was a swap cache but the swap is unused ! */
4376 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4377
Johannes Weiner0030f532012-07-31 16:45:25 -07004378 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004379
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004380 /*
4381 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004382 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004383 */
4384 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004385 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004386}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004387#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004388
Andrew Mortonc255a452012-07-31 16:43:02 -07004389#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004390/*
4391 * called from swap_entry_free(). remove record in swap_cgroup and
4392 * uncharge "memsw" account.
4393 */
4394void mem_cgroup_uncharge_swap(swp_entry_t ent)
4395{
4396 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004397 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004398
4399 if (!do_swap_account)
4400 return;
4401
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004402 id = swap_cgroup_record(ent, 0);
4403 rcu_read_lock();
4404 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004405 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004406 /*
4407 * We uncharge this because swap is freed.
4408 * This memcg can be obsolete one. We avoid calling css_tryget
4409 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004410 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004411 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004412 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004413 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004414 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004415 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004416}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004417
4418/**
4419 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4420 * @entry: swap entry to be moved
4421 * @from: mem_cgroup which the entry is moved from
4422 * @to: mem_cgroup which the entry is moved to
4423 *
4424 * It succeeds only when the swap_cgroup's record for this entry is the same
4425 * as the mem_cgroup's id of @from.
4426 *
4427 * Returns 0 on success, -EINVAL on failure.
4428 *
4429 * The caller must have charged to @to, IOW, called res_counter_charge() about
4430 * both res and memsw, and called css_get().
4431 */
4432static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004433 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004434{
4435 unsigned short old_id, new_id;
4436
4437 old_id = css_id(&from->css);
4438 new_id = css_id(&to->css);
4439
4440 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004441 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004442 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004443 /*
4444 * This function is only called from task migration context now.
4445 * It postpones res_counter and refcount handling till the end
4446 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004447 * improvement. But we cannot postpone css_get(to) because if
4448 * the process that has been moved to @to does swap-in, the
4449 * refcount of @to might be decreased to 0.
4450 *
4451 * We are in attach() phase, so the cgroup is guaranteed to be
4452 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004453 */
Li Zefan40503772013-07-08 16:00:34 -07004454 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004455 return 0;
4456 }
4457 return -EINVAL;
4458}
4459#else
4460static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004461 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004462{
4463 return -EINVAL;
4464}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004465#endif
4466
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004467/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004468 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4469 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004470 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004471void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4472 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004473{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004474 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004475 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004476 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004477 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004478
Johannes Weiner72835c82012-01-12 17:18:32 -08004479 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004480
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004481 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004482 return;
Balbir Singh40779602008-04-04 14:29:59 -07004483
Mel Gormanb32967f2012-11-19 12:35:47 +00004484 if (PageTransHuge(page))
4485 nr_pages <<= compound_order(page);
4486
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004487 pc = lookup_page_cgroup(page);
4488 lock_page_cgroup(pc);
4489 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004490 memcg = pc->mem_cgroup;
4491 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004492 /*
4493 * At migrating an anonymous page, its mapcount goes down
4494 * to 0 and uncharge() will be called. But, even if it's fully
4495 * unmapped, migration may fail and this page has to be
4496 * charged again. We set MIGRATION flag here and delay uncharge
4497 * until end_migration() is called
4498 *
4499 * Corner Case Thinking
4500 * A)
4501 * When the old page was mapped as Anon and it's unmap-and-freed
4502 * while migration was ongoing.
4503 * If unmap finds the old page, uncharge() of it will be delayed
4504 * until end_migration(). If unmap finds a new page, it's
4505 * uncharged when it make mapcount to be 1->0. If unmap code
4506 * finds swap_migration_entry, the new page will not be mapped
4507 * and end_migration() will find it(mapcount==0).
4508 *
4509 * B)
4510 * When the old page was mapped but migraion fails, the kernel
4511 * remaps it. A charge for it is kept by MIGRATION flag even
4512 * if mapcount goes down to 0. We can do remap successfully
4513 * without charging it again.
4514 *
4515 * C)
4516 * The "old" page is under lock_page() until the end of
4517 * migration, so, the old page itself will not be swapped-out.
4518 * If the new page is swapped out before end_migraton, our
4519 * hook to usual swap-out path will catch the event.
4520 */
4521 if (PageAnon(page))
4522 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004523 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004524 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004525 /*
4526 * If the page is not charged at this point,
4527 * we return here.
4528 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004529 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004530 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004531
Johannes Weiner72835c82012-01-12 17:18:32 -08004532 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004533 /*
4534 * We charge new page before it's used/mapped. So, even if unlock_page()
4535 * is called before end_migration, we can catch all events on this new
4536 * page. In the case new page is migrated but not remapped, new page's
4537 * mapcount will be finally 0 and we call uncharge in end_migration().
4538 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004539 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004540 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004541 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004542 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004543 /*
4544 * The page is committed to the memcg, but it's not actually
4545 * charged to the res_counter since we plan on replacing the
4546 * old one and only one page is going to be left afterwards.
4547 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004548 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004549}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004550
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004551/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004552void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004553 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004554{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004555 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004556 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004557 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004558
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004559 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004560 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004561
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004562 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004563 used = oldpage;
4564 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004565 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004566 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004567 unused = oldpage;
4568 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004569 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004570 __mem_cgroup_uncharge_common(unused,
4571 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4572 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4573 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004574 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004575 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004576 * We disallowed uncharge of pages under migration because mapcount
4577 * of the page goes down to zero, temporarly.
4578 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004579 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004580 pc = lookup_page_cgroup(oldpage);
4581 lock_page_cgroup(pc);
4582 ClearPageCgroupMigration(pc);
4583 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004584
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004585 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004586 * If a page is a file cache, radix-tree replacement is very atomic
4587 * and we can skip this check. When it was an Anon page, its mapcount
4588 * goes down to 0. But because we added MIGRATION flage, it's not
4589 * uncharged yet. There are several case but page->mapcount check
4590 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4591 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004592 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004593 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004594 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004595}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004596
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004597/*
4598 * At replace page cache, newpage is not under any memcg but it's on
4599 * LRU. So, this function doesn't touch res_counter but handles LRU
4600 * in correct way. Both pages are locked so we cannot race with uncharge.
4601 */
4602void mem_cgroup_replace_page_cache(struct page *oldpage,
4603 struct page *newpage)
4604{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004605 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004606 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004607 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004608
4609 if (mem_cgroup_disabled())
4610 return;
4611
4612 pc = lookup_page_cgroup(oldpage);
4613 /* fix accounting on old pages */
4614 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004615 if (PageCgroupUsed(pc)) {
4616 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004617 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004618 ClearPageCgroupUsed(pc);
4619 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004620 unlock_page_cgroup(pc);
4621
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004622 /*
4623 * When called from shmem_replace_page(), in some cases the
4624 * oldpage has already been charged, and in some cases not.
4625 */
4626 if (!memcg)
4627 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004628 /*
4629 * Even if newpage->mapping was NULL before starting replacement,
4630 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4631 * LRU while we overwrite pc->mem_cgroup.
4632 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004633 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004634}
4635
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004636#ifdef CONFIG_DEBUG_VM
4637static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4638{
4639 struct page_cgroup *pc;
4640
4641 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004642 /*
4643 * Can be NULL while feeding pages into the page allocator for
4644 * the first time, i.e. during boot or memory hotplug;
4645 * or when mem_cgroup_disabled().
4646 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004647 if (likely(pc) && PageCgroupUsed(pc))
4648 return pc;
4649 return NULL;
4650}
4651
4652bool mem_cgroup_bad_page_check(struct page *page)
4653{
4654 if (mem_cgroup_disabled())
4655 return false;
4656
4657 return lookup_page_cgroup_used(page) != NULL;
4658}
4659
4660void mem_cgroup_print_bad_page(struct page *page)
4661{
4662 struct page_cgroup *pc;
4663
4664 pc = lookup_page_cgroup_used(page);
4665 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004666 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4667 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004668 }
4669}
4670#endif
4671
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004672static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004673 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004674{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004675 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004676 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004677 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004678 int children = mem_cgroup_count_children(memcg);
4679 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004680 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004681
4682 /*
4683 * For keeping hierarchical_reclaim simple, how long we should retry
4684 * is depends on callers. We set our retry-count to be function
4685 * of # of children which we should visit in this loop.
4686 */
4687 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4688
4689 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004690
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004691 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004692 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004693 if (signal_pending(current)) {
4694 ret = -EINTR;
4695 break;
4696 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004697 /*
4698 * Rather than hide all in some function, I do this in
4699 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004700 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004701 */
4702 mutex_lock(&set_limit_mutex);
4703 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4704 if (memswlimit < val) {
4705 ret = -EINVAL;
4706 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004707 break;
4708 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004709
4710 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4711 if (memlimit < val)
4712 enlarge = 1;
4713
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004714 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004715 if (!ret) {
4716 if (memswlimit == val)
4717 memcg->memsw_is_minimum = true;
4718 else
4719 memcg->memsw_is_minimum = false;
4720 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004721 mutex_unlock(&set_limit_mutex);
4722
4723 if (!ret)
4724 break;
4725
Johannes Weiner56600482012-01-12 17:17:59 -08004726 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4727 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004728 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4729 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004730 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004731 retry_count--;
4732 else
4733 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004734 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004735 if (!ret && enlarge)
4736 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004737
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004738 return ret;
4739}
4740
Li Zefan338c8432009-06-17 16:27:15 -07004741static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4742 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004743{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004744 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004745 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004746 int children = mem_cgroup_count_children(memcg);
4747 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004748 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004749
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004750 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004751 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004752 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004753 while (retry_count) {
4754 if (signal_pending(current)) {
4755 ret = -EINTR;
4756 break;
4757 }
4758 /*
4759 * Rather than hide all in some function, I do this in
4760 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004761 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004762 */
4763 mutex_lock(&set_limit_mutex);
4764 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4765 if (memlimit > val) {
4766 ret = -EINVAL;
4767 mutex_unlock(&set_limit_mutex);
4768 break;
4769 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004770 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4771 if (memswlimit < val)
4772 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004773 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004774 if (!ret) {
4775 if (memlimit == val)
4776 memcg->memsw_is_minimum = true;
4777 else
4778 memcg->memsw_is_minimum = false;
4779 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004780 mutex_unlock(&set_limit_mutex);
4781
4782 if (!ret)
4783 break;
4784
Johannes Weiner56600482012-01-12 17:17:59 -08004785 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4786 MEM_CGROUP_RECLAIM_NOSWAP |
4787 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004788 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004789 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004790 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004791 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004792 else
4793 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004794 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004795 if (!ret && enlarge)
4796 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004797 return ret;
4798}
4799
Andrew Morton0608f432013-09-24 15:27:41 -07004800unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
4801 gfp_t gfp_mask,
4802 unsigned long *total_scanned)
4803{
4804 unsigned long nr_reclaimed = 0;
4805 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4806 unsigned long reclaimed;
4807 int loop = 0;
4808 struct mem_cgroup_tree_per_zone *mctz;
4809 unsigned long long excess;
4810 unsigned long nr_scanned;
4811
4812 if (order > 0)
4813 return 0;
4814
4815 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
4816 /*
4817 * This loop can run a while, specially if mem_cgroup's continuously
4818 * keep exceeding their soft limit and putting the system under
4819 * pressure
4820 */
4821 do {
4822 if (next_mz)
4823 mz = next_mz;
4824 else
4825 mz = mem_cgroup_largest_soft_limit_node(mctz);
4826 if (!mz)
4827 break;
4828
4829 nr_scanned = 0;
4830 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
4831 gfp_mask, &nr_scanned);
4832 nr_reclaimed += reclaimed;
4833 *total_scanned += nr_scanned;
4834 spin_lock(&mctz->lock);
4835
4836 /*
4837 * If we failed to reclaim anything from this memory cgroup
4838 * it is time to move on to the next cgroup
4839 */
4840 next_mz = NULL;
4841 if (!reclaimed) {
4842 do {
4843 /*
4844 * Loop until we find yet another one.
4845 *
4846 * By the time we get the soft_limit lock
4847 * again, someone might have aded the
4848 * group back on the RB tree. Iterate to
4849 * make sure we get a different mem.
4850 * mem_cgroup_largest_soft_limit_node returns
4851 * NULL if no other cgroup is present on
4852 * the tree
4853 */
4854 next_mz =
4855 __mem_cgroup_largest_soft_limit_node(mctz);
4856 if (next_mz == mz)
4857 css_put(&next_mz->memcg->css);
4858 else /* next_mz == NULL or other memcg */
4859 break;
4860 } while (1);
4861 }
4862 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4863 excess = res_counter_soft_limit_excess(&mz->memcg->res);
4864 /*
4865 * One school of thought says that we should not add
4866 * back the node to the tree if reclaim returns 0.
4867 * But our reclaim could return 0, simply because due
4868 * to priority we are exposing a smaller subset of
4869 * memory to reclaim from. Consider this as a longer
4870 * term TODO.
4871 */
4872 /* If excess == 0, no tree ops */
4873 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
4874 spin_unlock(&mctz->lock);
4875 css_put(&mz->memcg->css);
4876 loop++;
4877 /*
4878 * Could not reclaim anything and there are no more
4879 * mem cgroups to try or we seem to be looping without
4880 * reclaiming anything.
4881 */
4882 if (!nr_reclaimed &&
4883 (next_mz == NULL ||
4884 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4885 break;
4886 } while (!nr_reclaimed);
4887 if (next_mz)
4888 css_put(&next_mz->memcg->css);
4889 return nr_reclaimed;
4890}
4891
Michal Hocko2ef37d32012-10-26 13:37:30 +02004892/**
4893 * mem_cgroup_force_empty_list - clears LRU of a group
4894 * @memcg: group to clear
4895 * @node: NUMA node
4896 * @zid: zone id
4897 * @lru: lru to to clear
4898 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004899 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004900 * reclaim the pages page themselves - pages are moved to the parent (or root)
4901 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004902 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004903static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004904 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004905{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004906 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004907 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004908 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004909 struct page *busy;
4910 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004911
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004912 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004913 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4914 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004915
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004916 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004917 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004918 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004919 struct page *page;
4920
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004921 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004922 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004923 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004924 break;
4925 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004926 page = list_entry(list->prev, struct page, lru);
4927 if (busy == page) {
4928 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004929 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004930 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004931 continue;
4932 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004933 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004934
Johannes Weiner925b7672012-01-12 17:18:15 -08004935 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004936
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004937 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004938 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004939 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004940 cond_resched();
4941 } else
4942 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004943 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004944}
4945
4946/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004947 * make mem_cgroup's charge to be 0 if there is no task by moving
4948 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004949 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004950 *
4951 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004952 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004953static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004954{
Michal Hockoc26251f2012-10-26 13:37:28 +02004955 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004956 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004957
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004958 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004959 /* This is for making all *used* pages to be on LRU. */
4960 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004961 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004962 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004963 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004964 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004965 enum lru_list lru;
4966 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004967 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004968 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004969 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004970 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004971 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004972 mem_cgroup_end_move(memcg);
4973 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004974 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004975
Michal Hocko2ef37d32012-10-26 13:37:30 +02004976 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004977 * Kernel memory may not necessarily be trackable to a specific
4978 * process. So they are not migrated, and therefore we can't
4979 * expect their value to drop to 0 here.
4980 * Having res filled up with kmem only is enough.
4981 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004982 * This is a safety check because mem_cgroup_force_empty_list
4983 * could have raced with mem_cgroup_replace_page_cache callers
4984 * so the lru seemed empty but the page could have been added
4985 * right after the check. RES_USAGE should be safe as we always
4986 * charge before adding to the LRU.
4987 */
Glauber Costabea207c2012-12-18 14:22:11 -08004988 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4989 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4990 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004991}
4992
Glauber Costab5f99b52013-02-22 16:34:53 -08004993static inline bool memcg_has_children(struct mem_cgroup *memcg)
4994{
Johannes Weiner696ac172013-10-31 16:34:15 -07004995 lockdep_assert_held(&memcg_create_mutex);
4996 /*
4997 * The lock does not prevent addition or deletion to the list
4998 * of children, but it prevents a new child from being
4999 * initialized based on this parent in css_online(), so it's
5000 * enough to decide whether hierarchically inherited
5001 * attributes can still be changed or not.
5002 */
5003 return memcg->use_hierarchy &&
5004 !list_empty(&memcg->css.cgroup->children);
Glauber Costab5f99b52013-02-22 16:34:53 -08005005}
5006
5007/*
Michal Hockoc26251f2012-10-26 13:37:28 +02005008 * Reclaims as many pages from the given memcg as possible and moves
5009 * the rest to the parent.
5010 *
5011 * Caller is responsible for holding css reference for memcg.
5012 */
5013static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
5014{
5015 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
5016 struct cgroup *cgrp = memcg->css.cgroup;
5017
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005018 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02005019 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
5020 return -EBUSY;
5021
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005022 /* we call try-to-free pages for make this cgroup empty */
5023 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005024 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07005025 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005026 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005027
Michal Hockoc26251f2012-10-26 13:37:28 +02005028 if (signal_pending(current))
5029 return -EINTR;
5030
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005031 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07005032 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005033 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005034 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005035 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02005036 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005037 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005038
5039 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005040 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02005041 mem_cgroup_reparent_charges(memcg);
5042
5043 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08005044}
5045
Tejun Heo182446d2013-08-08 20:11:24 -04005046static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
5047 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005048{
Tejun Heo182446d2013-08-08 20:11:24 -04005049 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02005050
Michal Hockod8423012012-10-26 13:37:29 +02005051 if (mem_cgroup_is_root(memcg))
5052 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07005053 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005054}
5055
Tejun Heo182446d2013-08-08 20:11:24 -04005056static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
5057 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08005058{
Tejun Heo182446d2013-08-08 20:11:24 -04005059 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005060}
5061
Tejun Heo182446d2013-08-08 20:11:24 -04005062static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
5063 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08005064{
5065 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04005066 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005067 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08005068
Glauber Costa09998212013-02-22 16:34:55 -08005069 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07005070
5071 if (memcg->use_hierarchy == val)
5072 goto out;
5073
Balbir Singh18f59ea2009-01-07 18:08:07 -08005074 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005075 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08005076 * in the child subtrees. If it is unset, then the change can
5077 * occur, provided the current cgroup has no children.
5078 *
5079 * For the root cgroup, parent_mem is NULL, we allow value to be
5080 * set if there are no children.
5081 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005082 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08005083 (val == 1 || val == 0)) {
Johannes Weiner696ac172013-10-31 16:34:15 -07005084 if (list_empty(&memcg->css.cgroup->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005085 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005086 else
5087 retval = -EBUSY;
5088 } else
5089 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07005090
5091out:
Glauber Costa09998212013-02-22 16:34:55 -08005092 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005093
5094 return retval;
5095}
5096
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005097
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005098static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005099 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005100{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005101 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005102 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005103
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005104 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005105 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005106 val += mem_cgroup_read_stat(iter, idx);
5107
5108 if (val < 0) /* race ? */
5109 val = 0;
5110 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005111}
5112
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005113static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005114{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005115 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005116
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005117 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005118 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00005119 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005120 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00005121 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005122 }
5123
David Rientjesb070e652013-05-07 16:18:09 -07005124 /*
5125 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
5126 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
5127 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005128 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
5129 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005130
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005131 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005132 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005133
5134 return val << PAGE_SHIFT;
5135}
5136
Tejun Heo182446d2013-08-08 20:11:24 -04005137static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
5138 struct cftype *cft, struct file *file,
5139 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005140{
Tejun Heo182446d2013-08-08 20:11:24 -04005141 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07005142 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005143 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005144 int name, len;
5145 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005146
5147 type = MEMFILE_TYPE(cft->private);
5148 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005149
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005150 switch (type) {
5151 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005152 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005153 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005154 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005155 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005156 break;
5157 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005158 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005159 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005160 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005161 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005162 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005163 case _KMEM:
5164 val = res_counter_read_u64(&memcg->kmem, name);
5165 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005166 default:
5167 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005168 }
Tejun Heoaf36f902012-04-01 12:09:55 -07005169
5170 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5171 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005172}
Glauber Costa510fc4e2012-12-18 14:21:47 -08005173
Tejun Heo182446d2013-08-08 20:11:24 -04005174static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005175{
5176 int ret = -EINVAL;
5177#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04005178 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005179 /*
5180 * For simplicity, we won't allow this to be disabled. It also can't
5181 * be changed if the cgroup has children already, or if tasks had
5182 * already joined.
5183 *
5184 * If tasks join before we set the limit, a person looking at
5185 * kmem.usage_in_bytes will have no way to determine when it took
5186 * place, which makes the value quite meaningless.
5187 *
5188 * After it first became limited, changes in the value of the limit are
5189 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08005190 */
Glauber Costa09998212013-02-22 16:34:55 -08005191 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005192 mutex_lock(&set_limit_mutex);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07005193 if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04005194 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08005195 ret = -EBUSY;
5196 goto out;
5197 }
5198 ret = res_counter_set_limit(&memcg->kmem, val);
5199 VM_BUG_ON(ret);
5200
Glauber Costa55007d82012-12-18 14:22:38 -08005201 ret = memcg_update_cache_sizes(memcg);
5202 if (ret) {
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07005203 res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
Glauber Costa55007d82012-12-18 14:22:38 -08005204 goto out;
5205 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005206 static_key_slow_inc(&memcg_kmem_enabled_key);
5207 /*
5208 * setting the active bit after the inc will guarantee no one
5209 * starts accounting before all call sites are patched
5210 */
5211 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005212 } else
5213 ret = res_counter_set_limit(&memcg->kmem, val);
5214out:
5215 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005216 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005217#endif
5218 return ret;
5219}
5220
Hugh Dickins6d0439902013-02-22 16:35:50 -08005221#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005222static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005223{
Glauber Costa55007d82012-12-18 14:22:38 -08005224 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005225 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5226 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005227 goto out;
5228
Glauber Costa510fc4e2012-12-18 14:21:47 -08005229 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005230 /*
5231 * When that happen, we need to disable the static branch only on those
5232 * memcgs that enabled it. To achieve this, we would be forced to
5233 * complicate the code by keeping track of which memcgs were the ones
5234 * that actually enabled limits, and which ones got it from its
5235 * parents.
5236 *
5237 * It is a lot simpler just to do static_key_slow_inc() on every child
5238 * that is accounted.
5239 */
Glauber Costa55007d82012-12-18 14:22:38 -08005240 if (!memcg_kmem_is_active(memcg))
5241 goto out;
5242
5243 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07005244 * __mem_cgroup_free() will issue static_key_slow_dec() because this
5245 * memcg is active already. If the later initialization fails then the
5246 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08005247 */
Glauber Costa55007d82012-12-18 14:22:38 -08005248 static_key_slow_inc(&memcg_kmem_enabled_key);
5249
5250 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07005251 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005252 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07005253 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005254 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005255out:
5256 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005257}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005258#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005259
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005260/*
5261 * The user of this function is...
5262 * RES_LIMIT.
5263 */
Tejun Heo182446d2013-08-08 20:11:24 -04005264static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07005265 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005266{
Tejun Heo182446d2013-08-08 20:11:24 -04005267 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005268 enum res_type type;
5269 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005270 unsigned long long val;
5271 int ret;
5272
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005273 type = MEMFILE_TYPE(cft->private);
5274 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005275
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005276 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005277 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005278 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5279 ret = -EINVAL;
5280 break;
5281 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005282 /* This function does all necessary parse...reuse it */
5283 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005284 if (ret)
5285 break;
5286 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005287 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005288 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005289 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005290 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04005291 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005292 else
5293 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005294 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005295 case RES_SOFT_LIMIT:
5296 ret = res_counter_memparse_write_strategy(buffer, &val);
5297 if (ret)
5298 break;
5299 /*
5300 * For memsw, soft limits are hard to implement in terms
5301 * of semantics, for now, we support soft limits for
5302 * control without swap
5303 */
5304 if (type == _MEM)
5305 ret = res_counter_set_soft_limit(&memcg->res, val);
5306 else
5307 ret = -EINVAL;
5308 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005309 default:
5310 ret = -EINVAL; /* should be BUG() ? */
5311 break;
5312 }
5313 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005314}
5315
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005316static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5317 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5318{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005319 unsigned long long min_limit, min_memsw_limit, tmp;
5320
5321 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5322 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005323 if (!memcg->use_hierarchy)
5324 goto out;
5325
Tejun Heo63876982013-08-08 20:11:23 -04005326 while (css_parent(&memcg->css)) {
5327 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005328 if (!memcg->use_hierarchy)
5329 break;
5330 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5331 min_limit = min(min_limit, tmp);
5332 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5333 min_memsw_limit = min(min_memsw_limit, tmp);
5334 }
5335out:
5336 *mem_limit = min_limit;
5337 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005338}
5339
Tejun Heo182446d2013-08-08 20:11:24 -04005340static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005341{
Tejun Heo182446d2013-08-08 20:11:24 -04005342 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005343 int name;
5344 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005345
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005346 type = MEMFILE_TYPE(event);
5347 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005348
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005349 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005350 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005351 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005352 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005353 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005354 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005355 else if (type == _KMEM)
5356 res_counter_reset_max(&memcg->kmem);
5357 else
5358 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005359 break;
5360 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005361 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005362 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005363 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005364 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005365 else if (type == _KMEM)
5366 res_counter_reset_failcnt(&memcg->kmem);
5367 else
5368 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005369 break;
5370 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005371
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005372 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005373}
5374
Tejun Heo182446d2013-08-08 20:11:24 -04005375static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005376 struct cftype *cft)
5377{
Tejun Heo182446d2013-08-08 20:11:24 -04005378 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005379}
5380
Daisuke Nishimura02491442010-03-10 15:22:17 -08005381#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005382static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005383 struct cftype *cft, u64 val)
5384{
Tejun Heo182446d2013-08-08 20:11:24 -04005385 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005386
5387 if (val >= (1 << NR_MOVE_TYPE))
5388 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005389
Glauber Costaee5e8472013-02-22 16:34:50 -08005390 /*
5391 * No kind of locking is needed in here, because ->can_attach() will
5392 * check this value once in the beginning of the process, and then carry
5393 * on with stale data. This means that changes to this value will only
5394 * affect task migrations starting after the change.
5395 */
5396 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005397 return 0;
5398}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005399#else
Tejun Heo182446d2013-08-08 20:11:24 -04005400static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005401 struct cftype *cft, u64 val)
5402{
5403 return -ENOSYS;
5404}
5405#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005406
Ying Han406eb0c2011-05-26 16:25:37 -07005407#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005408static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5409 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005410{
5411 int nid;
5412 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5413 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005414 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005415
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005416 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005417 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005418 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005419 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005420 seq_printf(m, " N%d=%lu", nid, node_nr);
5421 }
5422 seq_putc(m, '\n');
5423
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005424 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005425 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005426 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005427 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005428 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005429 seq_printf(m, " N%d=%lu", nid, node_nr);
5430 }
5431 seq_putc(m, '\n');
5432
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005433 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005434 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005435 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005436 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005437 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005438 seq_printf(m, " N%d=%lu", nid, node_nr);
5439 }
5440 seq_putc(m, '\n');
5441
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005442 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005443 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005444 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005445 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005446 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005447 seq_printf(m, " N%d=%lu", nid, node_nr);
5448 }
5449 seq_putc(m, '\n');
5450 return 0;
5451}
5452#endif /* CONFIG_NUMA */
5453
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005454static inline void mem_cgroup_lru_names_not_uptodate(void)
5455{
5456 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5457}
5458
Tejun Heo182446d2013-08-08 20:11:24 -04005459static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005460 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005461{
Tejun Heo182446d2013-08-08 20:11:24 -04005462 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005463 struct mem_cgroup *mi;
5464 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005465
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005466 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005467 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005468 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005469 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5470 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005471 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005472
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005473 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5474 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5475 mem_cgroup_read_events(memcg, i));
5476
5477 for (i = 0; i < NR_LRU_LISTS; i++)
5478 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5479 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5480
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005481 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005482 {
5483 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005484 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005485 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005486 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005487 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5488 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005489 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005490
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005491 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5492 long long val = 0;
5493
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005494 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005495 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005496 for_each_mem_cgroup_tree(mi, memcg)
5497 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5498 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5499 }
5500
5501 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5502 unsigned long long val = 0;
5503
5504 for_each_mem_cgroup_tree(mi, memcg)
5505 val += mem_cgroup_read_events(mi, i);
5506 seq_printf(m, "total_%s %llu\n",
5507 mem_cgroup_events_names[i], val);
5508 }
5509
5510 for (i = 0; i < NR_LRU_LISTS; i++) {
5511 unsigned long long val = 0;
5512
5513 for_each_mem_cgroup_tree(mi, memcg)
5514 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5515 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005516 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005517
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005518#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005519 {
5520 int nid, zid;
5521 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005522 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005523 unsigned long recent_rotated[2] = {0, 0};
5524 unsigned long recent_scanned[2] = {0, 0};
5525
5526 for_each_online_node(nid)
5527 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005528 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005529 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005530
Hugh Dickins89abfab2012-05-29 15:06:53 -07005531 recent_rotated[0] += rstat->recent_rotated[0];
5532 recent_rotated[1] += rstat->recent_rotated[1];
5533 recent_scanned[0] += rstat->recent_scanned[0];
5534 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005535 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005536 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5537 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5538 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5539 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005540 }
5541#endif
5542
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005543 return 0;
5544}
5545
Tejun Heo182446d2013-08-08 20:11:24 -04005546static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5547 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005548{
Tejun Heo182446d2013-08-08 20:11:24 -04005549 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005550
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005551 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005552}
5553
Tejun Heo182446d2013-08-08 20:11:24 -04005554static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5555 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005556{
Tejun Heo182446d2013-08-08 20:11:24 -04005557 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005558 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005559
Tejun Heo63876982013-08-08 20:11:23 -04005560 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005561 return -EINVAL;
5562
Glauber Costa09998212013-02-22 16:34:55 -08005563 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005564
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005565 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005566 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005567 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005568 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005569 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005570
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005571 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005572
Glauber Costa09998212013-02-22 16:34:55 -08005573 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005574
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005575 return 0;
5576}
5577
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005578static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5579{
5580 struct mem_cgroup_threshold_ary *t;
5581 u64 usage;
5582 int i;
5583
5584 rcu_read_lock();
5585 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005586 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005587 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005588 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005589
5590 if (!t)
5591 goto unlock;
5592
5593 usage = mem_cgroup_usage(memcg, swap);
5594
5595 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005596 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005597 * If it's not true, a threshold was crossed after last
5598 * call of __mem_cgroup_threshold().
5599 */
Phil Carmody5407a562010-05-26 14:42:42 -07005600 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005601
5602 /*
5603 * Iterate backward over array of thresholds starting from
5604 * current_threshold and check if a threshold is crossed.
5605 * If none of thresholds below usage is crossed, we read
5606 * only one element of the array here.
5607 */
5608 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5609 eventfd_signal(t->entries[i].eventfd, 1);
5610
5611 /* i = current_threshold + 1 */
5612 i++;
5613
5614 /*
5615 * Iterate forward over array of thresholds starting from
5616 * current_threshold+1 and check if a threshold is crossed.
5617 * If none of thresholds above usage is crossed, we read
5618 * only one element of the array here.
5619 */
5620 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5621 eventfd_signal(t->entries[i].eventfd, 1);
5622
5623 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005624 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005625unlock:
5626 rcu_read_unlock();
5627}
5628
5629static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5630{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005631 while (memcg) {
5632 __mem_cgroup_threshold(memcg, false);
5633 if (do_swap_account)
5634 __mem_cgroup_threshold(memcg, true);
5635
5636 memcg = parent_mem_cgroup(memcg);
5637 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005638}
5639
5640static int compare_thresholds(const void *a, const void *b)
5641{
5642 const struct mem_cgroup_threshold *_a = a;
5643 const struct mem_cgroup_threshold *_b = b;
5644
Greg Thelen2bff24a2013-09-11 14:23:08 -07005645 if (_a->threshold > _b->threshold)
5646 return 1;
5647
5648 if (_a->threshold < _b->threshold)
5649 return -1;
5650
5651 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005652}
5653
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005654static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005655{
5656 struct mem_cgroup_eventfd_list *ev;
5657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005658 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005659 eventfd_signal(ev->eventfd, 1);
5660 return 0;
5661}
5662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005663static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005664{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005665 struct mem_cgroup *iter;
5666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005667 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005668 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005669}
5670
Tejun Heo81eeaf02013-08-08 20:11:26 -04005671static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005672 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005673{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005674 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005675 struct mem_cgroup_thresholds *thresholds;
5676 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005677 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005678 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005679 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005680
5681 ret = res_counter_memparse_write_strategy(args, &threshold);
5682 if (ret)
5683 return ret;
5684
5685 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005686
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005687 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005688 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005689 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005690 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005691 else
5692 BUG();
5693
5694 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5695
5696 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005697 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005698 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5699
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005700 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005701
5702 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005703 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005704 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005705 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005706 ret = -ENOMEM;
5707 goto unlock;
5708 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005709 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005710
5711 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005712 if (thresholds->primary) {
5713 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005714 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005715 }
5716
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005717 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005718 new->entries[size - 1].eventfd = eventfd;
5719 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005720
5721 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005722 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005723 compare_thresholds, NULL);
5724
5725 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005726 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005727 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005728 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005729 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005730 * new->current_threshold will not be used until
5731 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005732 * it here.
5733 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005734 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005735 } else
5736 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005737 }
5738
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005739 /* Free old spare buffer and save old primary buffer as spare */
5740 kfree(thresholds->spare);
5741 thresholds->spare = thresholds->primary;
5742
5743 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005744
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005745 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005746 synchronize_rcu();
5747
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005748unlock:
5749 mutex_unlock(&memcg->thresholds_lock);
5750
5751 return ret;
5752}
5753
Tejun Heo81eeaf02013-08-08 20:11:26 -04005754static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005755 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005756{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005757 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005758 struct mem_cgroup_thresholds *thresholds;
5759 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005760 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005761 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005762 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005763
5764 mutex_lock(&memcg->thresholds_lock);
5765 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005766 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005767 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005768 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005769 else
5770 BUG();
5771
Anton Vorontsov371528c2012-02-24 05:14:46 +04005772 if (!thresholds->primary)
5773 goto unlock;
5774
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005775 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5776
5777 /* Check if a threshold crossed before removing */
5778 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5779
5780 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005781 size = 0;
5782 for (i = 0; i < thresholds->primary->size; i++) {
5783 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005784 size++;
5785 }
5786
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005787 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005788
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005789 /* Set thresholds array to NULL if we don't have thresholds */
5790 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005791 kfree(new);
5792 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005793 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005794 }
5795
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005796 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005797
5798 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005799 new->current_threshold = -1;
5800 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5801 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005802 continue;
5803
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005804 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005805 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005806 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005807 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005808 * until rcu_assign_pointer(), so it's safe to increment
5809 * it here.
5810 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005811 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005812 }
5813 j++;
5814 }
5815
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005816swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005817 /* Swap primary and spare array */
5818 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005819 /* If all events are unregistered, free the spare array */
5820 if (!new) {
5821 kfree(thresholds->spare);
5822 thresholds->spare = NULL;
5823 }
5824
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005825 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005826
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005827 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005828 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005829unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005830 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005831}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005832
Tejun Heo81eeaf02013-08-08 20:11:26 -04005833static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005834 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5835{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005836 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005837 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005838 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005839
5840 BUG_ON(type != _OOM_TYPE);
5841 event = kmalloc(sizeof(*event), GFP_KERNEL);
5842 if (!event)
5843 return -ENOMEM;
5844
Michal Hocko1af8efe2011-07-26 16:08:24 -07005845 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005846
5847 event->eventfd = eventfd;
5848 list_add(&event->list, &memcg->oom_notify);
5849
5850 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005851 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005852 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005853 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005854
5855 return 0;
5856}
5857
Tejun Heo81eeaf02013-08-08 20:11:26 -04005858static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005859 struct cftype *cft, struct eventfd_ctx *eventfd)
5860{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005861 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005862 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005863 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005864
5865 BUG_ON(type != _OOM_TYPE);
5866
Michal Hocko1af8efe2011-07-26 16:08:24 -07005867 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005869 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005870 if (ev->eventfd == eventfd) {
5871 list_del(&ev->list);
5872 kfree(ev);
5873 }
5874 }
5875
Michal Hocko1af8efe2011-07-26 16:08:24 -07005876 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005877}
5878
Tejun Heo182446d2013-08-08 20:11:24 -04005879static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005880 struct cftype *cft, struct cgroup_map_cb *cb)
5881{
Tejun Heo182446d2013-08-08 20:11:24 -04005882 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005883
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005884 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005885
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005886 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005887 cb->fill(cb, "under_oom", 1);
5888 else
5889 cb->fill(cb, "under_oom", 0);
5890 return 0;
5891}
5892
Tejun Heo182446d2013-08-08 20:11:24 -04005893static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005894 struct cftype *cft, u64 val)
5895{
Tejun Heo182446d2013-08-08 20:11:24 -04005896 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005897 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005898
5899 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005900 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005901 return -EINVAL;
5902
Glauber Costa09998212013-02-22 16:34:55 -08005903 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005904 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005905 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005906 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005907 return -EINVAL;
5908 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005909 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005910 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005911 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005912 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005913 return 0;
5914}
5915
Andrew Mortonc255a452012-07-31 16:43:02 -07005916#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005917static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005918{
Glauber Costa55007d82012-12-18 14:22:38 -08005919 int ret;
5920
Glauber Costa2633d7a2012-12-18 14:22:34 -08005921 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005922 ret = memcg_propagate_kmem(memcg);
5923 if (ret)
5924 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005925
Glauber Costa1d62e432012-04-09 19:36:33 -03005926 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005927}
Glauber Costae5671df2011-12-11 21:47:01 +00005928
Li Zefan10d5ebf2013-07-08 16:00:33 -07005929static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005930{
Glauber Costa1d62e432012-04-09 19:36:33 -03005931 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005932}
5933
5934static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5935{
5936 if (!memcg_kmem_is_active(memcg))
5937 return;
5938
5939 /*
5940 * kmem charges can outlive the cgroup. In the case of slab
5941 * pages, for instance, a page contain objects from various
5942 * processes. As we prevent from taking a reference for every
5943 * such allocation we have to be careful when doing uncharge
5944 * (see memcg_uncharge_kmem) and here during offlining.
5945 *
5946 * The idea is that that only the _last_ uncharge which sees
5947 * the dead memcg will drop the last reference. An additional
5948 * reference is taken here before the group is marked dead
5949 * which is then paired with css_put during uncharge resp. here.
5950 *
5951 * Although this might sound strange as this path is called from
5952 * css_offline() when the referencemight have dropped down to 0
5953 * and shouldn't be incremented anymore (css_tryget would fail)
5954 * we do not have other options because of the kmem allocations
5955 * lifetime.
5956 */
5957 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005958
5959 memcg_kmem_mark_dead(memcg);
5960
5961 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5962 return;
5963
Glauber Costa7de37682012-12-18 14:22:07 -08005964 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005965 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005966}
Glauber Costae5671df2011-12-11 21:47:01 +00005967#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005968static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005969{
5970 return 0;
5971}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005972
Li Zefan10d5ebf2013-07-08 16:00:33 -07005973static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5974{
5975}
5976
5977static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005978{
5979}
Glauber Costae5671df2011-12-11 21:47:01 +00005980#endif
5981
Tejun Heo79bd9812013-11-22 18:20:42 -05005982/*
5983 * Unregister event and free resources.
5984 *
5985 * Gets called from workqueue.
5986 */
5987static void cgroup_event_remove(struct work_struct *work)
5988{
5989 struct cgroup_event *event = container_of(work, struct cgroup_event,
5990 remove);
5991 struct cgroup_subsys_state *css = event->css;
5992
5993 remove_wait_queue(event->wqh, &event->wait);
5994
5995 event->cft->unregister_event(css, event->cft, event->eventfd);
5996
5997 /* Notify userspace the event is going away. */
5998 eventfd_signal(event->eventfd, 1);
5999
6000 eventfd_ctx_put(event->eventfd);
6001 kfree(event);
6002 css_put(css);
6003}
6004
6005/*
6006 * Gets called on POLLHUP on eventfd when user closes it.
6007 *
6008 * Called with wqh->lock held and interrupts disabled.
6009 */
6010static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
6011 int sync, void *key)
6012{
6013 struct cgroup_event *event = container_of(wait,
6014 struct cgroup_event, wait);
6015 struct cgroup *cgrp = event->css->cgroup;
6016 unsigned long flags = (unsigned long)key;
6017
6018 if (flags & POLLHUP) {
6019 /*
6020 * If the event has been detached at cgroup removal, we
6021 * can simply return knowing the other side will cleanup
6022 * for us.
6023 *
6024 * We can't race against event freeing since the other
6025 * side will require wqh->lock via remove_wait_queue(),
6026 * which we hold.
6027 */
6028 spin_lock(&cgrp->event_list_lock);
6029 if (!list_empty(&event->list)) {
6030 list_del_init(&event->list);
6031 /*
6032 * We are in atomic context, but cgroup_event_remove()
6033 * may sleep, so we have to call it in workqueue.
6034 */
6035 schedule_work(&event->remove);
6036 }
6037 spin_unlock(&cgrp->event_list_lock);
6038 }
6039
6040 return 0;
6041}
6042
6043static void cgroup_event_ptable_queue_proc(struct file *file,
6044 wait_queue_head_t *wqh, poll_table *pt)
6045{
6046 struct cgroup_event *event = container_of(pt,
6047 struct cgroup_event, pt);
6048
6049 event->wqh = wqh;
6050 add_wait_queue(wqh, &event->wait);
6051}
6052
6053/*
6054 * Parse input and register new cgroup event handler.
6055 *
6056 * Input must be in format '<event_fd> <control_fd> <args>'.
6057 * Interpretation of args is defined by control file implementation.
6058 */
Tejun Heob5557c42013-11-22 18:20:42 -05006059static int cgroup_write_event_control(struct cgroup_subsys_state *css,
Tejun Heo79bd9812013-11-22 18:20:42 -05006060 struct cftype *cft, const char *buffer)
6061{
Tejun Heob5557c42013-11-22 18:20:42 -05006062 struct cgroup *cgrp = css->cgroup;
Tejun Heo79bd9812013-11-22 18:20:42 -05006063 struct cgroup_event *event;
6064 struct cgroup_subsys_state *cfile_css;
6065 unsigned int efd, cfd;
6066 struct fd efile;
6067 struct fd cfile;
6068 char *endp;
6069 int ret;
6070
6071 efd = simple_strtoul(buffer, &endp, 10);
6072 if (*endp != ' ')
6073 return -EINVAL;
6074 buffer = endp + 1;
6075
6076 cfd = simple_strtoul(buffer, &endp, 10);
6077 if ((*endp != ' ') && (*endp != '\0'))
6078 return -EINVAL;
6079 buffer = endp + 1;
6080
6081 event = kzalloc(sizeof(*event), GFP_KERNEL);
6082 if (!event)
6083 return -ENOMEM;
6084
Tejun Heob5557c42013-11-22 18:20:42 -05006085 event->css = css;
Tejun Heo79bd9812013-11-22 18:20:42 -05006086 INIT_LIST_HEAD(&event->list);
6087 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
6088 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
6089 INIT_WORK(&event->remove, cgroup_event_remove);
6090
6091 efile = fdget(efd);
6092 if (!efile.file) {
6093 ret = -EBADF;
6094 goto out_kfree;
6095 }
6096
6097 event->eventfd = eventfd_ctx_fileget(efile.file);
6098 if (IS_ERR(event->eventfd)) {
6099 ret = PTR_ERR(event->eventfd);
6100 goto out_put_efile;
6101 }
6102
6103 cfile = fdget(cfd);
6104 if (!cfile.file) {
6105 ret = -EBADF;
6106 goto out_put_eventfd;
6107 }
6108
6109 /* the process need read permission on control file */
6110 /* AV: shouldn't we check that it's been opened for read instead? */
6111 ret = inode_permission(file_inode(cfile.file), MAY_READ);
6112 if (ret < 0)
6113 goto out_put_cfile;
6114
6115 event->cft = __file_cft(cfile.file);
6116 if (IS_ERR(event->cft)) {
6117 ret = PTR_ERR(event->cft);
6118 goto out_put_cfile;
6119 }
6120
Tejun Heo79bd9812013-11-22 18:20:42 -05006121 /*
Tejun Heob5557c42013-11-22 18:20:42 -05006122 * Verify @cfile should belong to @css. Also, remaining events are
6123 * automatically removed on cgroup destruction but the removal is
6124 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05006125 */
6126 rcu_read_lock();
6127
6128 ret = -EINVAL;
Tejun Heob5557c42013-11-22 18:20:42 -05006129 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent,
6130 &mem_cgroup_subsys);
6131 if (cfile_css == css && css_tryget(css))
Tejun Heo79bd9812013-11-22 18:20:42 -05006132 ret = 0;
6133
6134 rcu_read_unlock();
6135 if (ret)
6136 goto out_put_cfile;
6137
6138 if (!event->cft->register_event || !event->cft->unregister_event) {
6139 ret = -EINVAL;
6140 goto out_put_css;
6141 }
6142
Tejun Heob5557c42013-11-22 18:20:42 -05006143 ret = event->cft->register_event(css, event->cft,
Tejun Heo79bd9812013-11-22 18:20:42 -05006144 event->eventfd, buffer);
6145 if (ret)
6146 goto out_put_css;
6147
6148 efile.file->f_op->poll(efile.file, &event->pt);
6149
6150 spin_lock(&cgrp->event_list_lock);
6151 list_add(&event->list, &cgrp->event_list);
6152 spin_unlock(&cgrp->event_list_lock);
6153
6154 fdput(cfile);
6155 fdput(efile);
6156
6157 return 0;
6158
6159out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05006160 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006161out_put_cfile:
6162 fdput(cfile);
6163out_put_eventfd:
6164 eventfd_ctx_put(event->eventfd);
6165out_put_efile:
6166 fdput(efile);
6167out_kfree:
6168 kfree(event);
6169
6170 return ret;
6171}
6172
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006173static struct cftype mem_cgroup_files[] = {
6174 {
Balbir Singh0eea1032008-02-07 00:13:57 -08006175 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006176 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07006177 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006178 .register_event = mem_cgroup_usage_register_event,
6179 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006180 },
6181 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07006182 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006183 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07006184 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07006185 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07006186 },
6187 {
Balbir Singh0eea1032008-02-07 00:13:57 -08006188 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006189 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07006190 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07006191 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006192 },
6193 {
Balbir Singh296c81d2009-09-23 15:56:36 -07006194 .name = "soft_limit_in_bytes",
6195 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
6196 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07006197 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07006198 },
6199 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006200 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006201 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07006202 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07006203 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006204 },
Balbir Singh8697d332008-02-07 00:13:59 -08006205 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006206 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006207 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006208 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08006209 {
6210 .name = "force_empty",
6211 .trigger = mem_cgroup_force_empty_write,
6212 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08006213 {
6214 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07006215 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08006216 .write_u64 = mem_cgroup_hierarchy_write,
6217 .read_u64 = mem_cgroup_hierarchy_read,
6218 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006219 {
Tejun Heo79bd9812013-11-22 18:20:42 -05006220 .name = "cgroup.event_control",
6221 .write_string = cgroup_write_event_control,
6222 .flags = CFTYPE_NO_PREFIX,
6223 .mode = S_IWUGO,
6224 },
6225 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006226 .name = "swappiness",
6227 .read_u64 = mem_cgroup_swappiness_read,
6228 .write_u64 = mem_cgroup_swappiness_write,
6229 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006230 {
6231 .name = "move_charge_at_immigrate",
6232 .read_u64 = mem_cgroup_move_charge_read,
6233 .write_u64 = mem_cgroup_move_charge_write,
6234 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006235 {
6236 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07006237 .read_map = mem_cgroup_oom_control_read,
6238 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006239 .register_event = mem_cgroup_oom_register_event,
6240 .unregister_event = mem_cgroup_oom_unregister_event,
6241 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
6242 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006243 {
6244 .name = "pressure_level",
6245 .register_event = vmpressure_register_event,
6246 .unregister_event = vmpressure_unregister_event,
6247 },
Ying Han406eb0c2011-05-26 16:25:37 -07006248#ifdef CONFIG_NUMA
6249 {
6250 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006251 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07006252 },
6253#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006254#ifdef CONFIG_MEMCG_KMEM
6255 {
6256 .name = "kmem.limit_in_bytes",
6257 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
6258 .write_string = mem_cgroup_write,
6259 .read = mem_cgroup_read,
6260 },
6261 {
6262 .name = "kmem.usage_in_bytes",
6263 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
6264 .read = mem_cgroup_read,
6265 },
6266 {
6267 .name = "kmem.failcnt",
6268 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
6269 .trigger = mem_cgroup_reset,
6270 .read = mem_cgroup_read,
6271 },
6272 {
6273 .name = "kmem.max_usage_in_bytes",
6274 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6275 .trigger = mem_cgroup_reset,
6276 .read = mem_cgroup_read,
6277 },
Glauber Costa749c5412012-12-18 14:23:01 -08006278#ifdef CONFIG_SLABINFO
6279 {
6280 .name = "kmem.slabinfo",
6281 .read_seq_string = mem_cgroup_slabinfo_read,
6282 },
6283#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006284#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07006285 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07006286};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006287
Michal Hocko2d110852013-02-22 16:34:43 -08006288#ifdef CONFIG_MEMCG_SWAP
6289static struct cftype memsw_cgroup_files[] = {
6290 {
6291 .name = "memsw.usage_in_bytes",
6292 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6293 .read = mem_cgroup_read,
6294 .register_event = mem_cgroup_usage_register_event,
6295 .unregister_event = mem_cgroup_usage_unregister_event,
6296 },
6297 {
6298 .name = "memsw.max_usage_in_bytes",
6299 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6300 .trigger = mem_cgroup_reset,
6301 .read = mem_cgroup_read,
6302 },
6303 {
6304 .name = "memsw.limit_in_bytes",
6305 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6306 .write_string = mem_cgroup_write,
6307 .read = mem_cgroup_read,
6308 },
6309 {
6310 .name = "memsw.failcnt",
6311 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6312 .trigger = mem_cgroup_reset,
6313 .read = mem_cgroup_read,
6314 },
6315 { }, /* terminate */
6316};
6317#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006318static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006319{
6320 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006321 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006322 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006323 /*
6324 * This routine is called against possible nodes.
6325 * But it's BUG to call kmalloc() against offline node.
6326 *
6327 * TODO: this routine can waste much memory for nodes which will
6328 * never be onlined. It's better to use memory hotplug callback
6329 * function.
6330 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006331 if (!node_state(node, N_NORMAL_MEMORY))
6332 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08006333 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006334 if (!pn)
6335 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006336
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006337 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6338 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08006339 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006340 mz->usage_in_excess = 0;
6341 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006342 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006343 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07006344 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006345 return 0;
6346}
6347
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006348static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006349{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07006350 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006351}
6352
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006353static struct mem_cgroup *mem_cgroup_alloc(void)
6354{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006355 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006356 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006357
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006358 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08006359 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006360 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006361 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006362 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006363
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006364 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07006365 return NULL;
6366
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006367 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6368 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006369 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006370 spin_lock_init(&memcg->pcp_counter_lock);
6371 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006372
6373out_free:
6374 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006375 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006376 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006377 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006378 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006379}
6380
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006381/*
Glauber Costac8b2a362012-12-18 14:22:13 -08006382 * At destroying mem_cgroup, references from swap_cgroup can remain.
6383 * (scanning all at force_empty is too costly...)
6384 *
6385 * Instead of clearing all references at force_empty, we remember
6386 * the number of reference from swap_cgroup and free mem_cgroup when
6387 * it goes down to 0.
6388 *
6389 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07006390 */
Glauber Costac8b2a362012-12-18 14:22:13 -08006391
6392static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006393{
Glauber Costac8b2a362012-12-18 14:22:13 -08006394 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006395 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006396
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006397 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006398 free_css_id(&mem_cgroup_subsys, &memcg->css);
6399
6400 for_each_node(node)
6401 free_mem_cgroup_per_zone_info(memcg, node);
6402
6403 free_percpu(memcg->stat);
6404
Glauber Costa3f134612012-05-29 15:07:11 -07006405 /*
6406 * We need to make sure that (at least for now), the jump label
6407 * destruction code runs outside of the cgroup lock. This is because
6408 * get_online_cpus(), which is called from the static_branch update,
6409 * can't be called inside the cgroup_lock. cpusets are the ones
6410 * enforcing this dependency, so if they ever change, we might as well.
6411 *
6412 * schedule_work() will guarantee this happens. Be careful if you need
6413 * to move this code around, and make sure it is outside
6414 * the cgroup_lock.
6415 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006416 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006417 if (size < PAGE_SIZE)
6418 kfree(memcg);
6419 else
6420 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006421}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006422
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006423/*
6424 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6425 */
Glauber Costae1aab162011-12-11 21:47:03 +00006426struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006427{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006428 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006429 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006430 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006431}
Glauber Costae1aab162011-12-11 21:47:03 +00006432EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006433
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006434static void __init mem_cgroup_soft_limit_tree_init(void)
6435{
6436 struct mem_cgroup_tree_per_node *rtpn;
6437 struct mem_cgroup_tree_per_zone *rtpz;
6438 int tmp, node, zone;
6439
6440 for_each_node(node) {
6441 tmp = node;
6442 if (!node_state(node, N_NORMAL_MEMORY))
6443 tmp = -1;
6444 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6445 BUG_ON(!rtpn);
6446
6447 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6448
6449 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6450 rtpz = &rtpn->rb_tree_per_zone[zone];
6451 rtpz->rb_root = RB_ROOT;
6452 spin_lock_init(&rtpz->lock);
6453 }
6454 }
6455}
6456
Li Zefan0eb253e2009-01-15 13:51:25 -08006457static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04006458mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006459{
Glauber Costad142e3e2013-02-22 16:34:52 -08006460 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006461 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006462 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006463
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006464 memcg = mem_cgroup_alloc();
6465 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006466 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006467
Bob Liu3ed28fa2012-01-12 17:19:04 -08006468 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006469 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006470 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006471
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006472 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04006473 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006474 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006475 res_counter_init(&memcg->res, NULL);
6476 res_counter_init(&memcg->memsw, NULL);
6477 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006478 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006479
Glauber Costad142e3e2013-02-22 16:34:52 -08006480 memcg->last_scanned_node = MAX_NUMNODES;
6481 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08006482 memcg->move_charge_at_immigrate = 0;
6483 mutex_init(&memcg->thresholds_lock);
6484 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006485 vmpressure_init(&memcg->vmpressure);
Glauber Costad142e3e2013-02-22 16:34:52 -08006486
6487 return &memcg->css;
6488
6489free_out:
6490 __mem_cgroup_free(memcg);
6491 return ERR_PTR(error);
6492}
6493
6494static int
Tejun Heoeb954192013-08-08 20:11:23 -04006495mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08006496{
Tejun Heoeb954192013-08-08 20:11:23 -04006497 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6498 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08006499 int error = 0;
6500
Tejun Heo63876982013-08-08 20:11:23 -04006501 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08006502 return 0;
6503
Glauber Costa09998212013-02-22 16:34:55 -08006504 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006505
6506 memcg->use_hierarchy = parent->use_hierarchy;
6507 memcg->oom_kill_disable = parent->oom_kill_disable;
6508 memcg->swappiness = mem_cgroup_swappiness(parent);
6509
6510 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006511 res_counter_init(&memcg->res, &parent->res);
6512 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006513 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006514
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006515 /*
Li Zefan8d76a972013-07-08 16:00:36 -07006516 * No need to take a reference to the parent because cgroup
6517 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006518 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08006519 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006520 res_counter_init(&memcg->res, NULL);
6521 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006522 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006523 /*
6524 * Deeper hierachy with use_hierarchy == false doesn't make
6525 * much sense so let cgroup subsystem know about this
6526 * unfortunate state in our controller.
6527 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006528 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006529 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006530 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006531
6532 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006533 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006534 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006535}
6536
Michal Hocko5f578162013-04-29 15:07:17 -07006537/*
6538 * Announce all parents that a group from their hierarchy is gone.
6539 */
6540static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6541{
6542 struct mem_cgroup *parent = memcg;
6543
6544 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006545 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006546
6547 /*
6548 * if the root memcg is not hierarchical we have to check it
6549 * explicitely.
6550 */
6551 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006552 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006553}
6554
Tejun Heoeb954192013-08-08 20:11:23 -04006555static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006556{
Tejun Heoeb954192013-08-08 20:11:23 -04006557 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006558 struct cgroup *cgrp = css->cgroup;
6559 struct cgroup_event *event, *tmp;
6560
6561 /*
6562 * Unregister events and notify userspace.
6563 * Notify userspace about cgroup removing only after rmdir of cgroup
6564 * directory to avoid race between userspace and kernelspace.
6565 */
6566 spin_lock(&cgrp->event_list_lock);
6567 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
6568 list_del_init(&event->list);
6569 schedule_work(&event->remove);
6570 }
6571 spin_unlock(&cgrp->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006572
Li Zefan10d5ebf2013-07-08 16:00:33 -07006573 kmem_cgroup_css_offline(memcg);
6574
Michal Hocko5f578162013-04-29 15:07:17 -07006575 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006576 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006577 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006578 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006579}
6580
Tejun Heoeb954192013-08-08 20:11:23 -04006581static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006582{
Tejun Heoeb954192013-08-08 20:11:23 -04006583 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006584
Li Zefan10d5ebf2013-07-08 16:00:33 -07006585 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006586 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006587}
6588
Daisuke Nishimura02491442010-03-10 15:22:17 -08006589#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006590/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006591#define PRECHARGE_COUNT_AT_ONCE 256
6592static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006593{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006594 int ret = 0;
6595 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006596 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006597
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006598 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006599 mc.precharge += count;
6600 /* we don't need css_get for root */
6601 return ret;
6602 }
6603 /* try to charge at once */
6604 if (count > 1) {
6605 struct res_counter *dummy;
6606 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006607 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006608 * by cgroup_lock_live_cgroup() that it is not removed and we
6609 * are still under the same cgroup_mutex. So we can postpone
6610 * css_get().
6611 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006612 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006613 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006614 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006615 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006616 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006617 goto one_by_one;
6618 }
6619 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006620 return ret;
6621 }
6622one_by_one:
6623 /* fall back to one by one charge */
6624 while (count--) {
6625 if (signal_pending(current)) {
6626 ret = -EINTR;
6627 break;
6628 }
6629 if (!batch_count--) {
6630 batch_count = PRECHARGE_COUNT_AT_ONCE;
6631 cond_resched();
6632 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006633 ret = __mem_cgroup_try_charge(NULL,
6634 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006635 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006636 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006637 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006638 mc.precharge++;
6639 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006640 return ret;
6641}
6642
6643/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006644 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006645 * @vma: the vma the pte to be checked belongs
6646 * @addr: the address corresponding to the pte to be checked
6647 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006648 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006649 *
6650 * Returns
6651 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6652 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6653 * move charge. if @target is not NULL, the page is stored in target->page
6654 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006655 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6656 * target for charge migration. if @target is not NULL, the entry is stored
6657 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006658 *
6659 * Called with pte lock held.
6660 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006661union mc_target {
6662 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006663 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006664};
6665
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006666enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006667 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006668 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006669 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006670};
6671
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006672static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6673 unsigned long addr, pte_t ptent)
6674{
6675 struct page *page = vm_normal_page(vma, addr, ptent);
6676
6677 if (!page || !page_mapped(page))
6678 return NULL;
6679 if (PageAnon(page)) {
6680 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006681 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006682 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006683 } else if (!move_file())
6684 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006685 return NULL;
6686 if (!get_page_unless_zero(page))
6687 return NULL;
6688
6689 return page;
6690}
6691
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006692#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006693static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6694 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6695{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006696 struct page *page = NULL;
6697 swp_entry_t ent = pte_to_swp_entry(ptent);
6698
6699 if (!move_anon() || non_swap_entry(ent))
6700 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006701 /*
6702 * Because lookup_swap_cache() updates some statistics counter,
6703 * we call find_get_page() with swapper_space directly.
6704 */
Shaohua Li33806f02013-02-22 16:34:37 -08006705 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006706 if (do_swap_account)
6707 entry->val = ent.val;
6708
6709 return page;
6710}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006711#else
6712static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6713 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6714{
6715 return NULL;
6716}
6717#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006718
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006719static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6720 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6721{
6722 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006723 struct address_space *mapping;
6724 pgoff_t pgoff;
6725
6726 if (!vma->vm_file) /* anonymous vma */
6727 return NULL;
6728 if (!move_file())
6729 return NULL;
6730
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006731 mapping = vma->vm_file->f_mapping;
6732 if (pte_none(ptent))
6733 pgoff = linear_page_index(vma, addr);
6734 else /* pte_file(ptent) is true */
6735 pgoff = pte_to_pgoff(ptent);
6736
6737 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006738 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006739
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006740#ifdef CONFIG_SWAP
6741 /* shmem/tmpfs may report page out on swap: account for that too. */
6742 if (radix_tree_exceptional_entry(page)) {
6743 swp_entry_t swap = radix_to_swp_entry(page);
6744 if (do_swap_account)
6745 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006746 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006747 }
6748#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006749 return page;
6750}
6751
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006752static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006753 unsigned long addr, pte_t ptent, union mc_target *target)
6754{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006755 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006756 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006757 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006758 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006759
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006760 if (pte_present(ptent))
6761 page = mc_handle_present_pte(vma, addr, ptent);
6762 else if (is_swap_pte(ptent))
6763 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006764 else if (pte_none(ptent) || pte_file(ptent))
6765 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006766
6767 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006768 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006769 if (page) {
6770 pc = lookup_page_cgroup(page);
6771 /*
6772 * Do only loose check w/o page_cgroup lock.
6773 * mem_cgroup_move_account() checks the pc is valid or not under
6774 * the lock.
6775 */
6776 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6777 ret = MC_TARGET_PAGE;
6778 if (target)
6779 target->page = page;
6780 }
6781 if (!ret || !target)
6782 put_page(page);
6783 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006784 /* There is a swap entry and a page doesn't exist or isn't charged */
6785 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006786 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006787 ret = MC_TARGET_SWAP;
6788 if (target)
6789 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006790 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006791 return ret;
6792}
6793
Naoya Horiguchi12724852012-03-21 16:34:28 -07006794#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6795/*
6796 * We don't consider swapping or file mapped pages because THP does not
6797 * support them for now.
6798 * Caller should make sure that pmd_trans_huge(pmd) is true.
6799 */
6800static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6801 unsigned long addr, pmd_t pmd, union mc_target *target)
6802{
6803 struct page *page = NULL;
6804 struct page_cgroup *pc;
6805 enum mc_target_type ret = MC_TARGET_NONE;
6806
6807 page = pmd_page(pmd);
6808 VM_BUG_ON(!page || !PageHead(page));
6809 if (!move_anon())
6810 return ret;
6811 pc = lookup_page_cgroup(page);
6812 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6813 ret = MC_TARGET_PAGE;
6814 if (target) {
6815 get_page(page);
6816 target->page = page;
6817 }
6818 }
6819 return ret;
6820}
6821#else
6822static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6823 unsigned long addr, pmd_t pmd, union mc_target *target)
6824{
6825 return MC_TARGET_NONE;
6826}
6827#endif
6828
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006829static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6830 unsigned long addr, unsigned long end,
6831 struct mm_walk *walk)
6832{
6833 struct vm_area_struct *vma = walk->private;
6834 pte_t *pte;
6835 spinlock_t *ptl;
6836
Naoya Horiguchi12724852012-03-21 16:34:28 -07006837 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6838 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6839 mc.precharge += HPAGE_PMD_NR;
6840 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006841 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006842 }
Dave Hansen03319322011-03-22 16:32:56 -07006843
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006844 if (pmd_trans_unstable(pmd))
6845 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006846 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6847 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006848 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006849 mc.precharge++; /* increment precharge temporarily */
6850 pte_unmap_unlock(pte - 1, ptl);
6851 cond_resched();
6852
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006853 return 0;
6854}
6855
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006856static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6857{
6858 unsigned long precharge;
6859 struct vm_area_struct *vma;
6860
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006861 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006862 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6863 struct mm_walk mem_cgroup_count_precharge_walk = {
6864 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6865 .mm = mm,
6866 .private = vma,
6867 };
6868 if (is_vm_hugetlb_page(vma))
6869 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006870 walk_page_range(vma->vm_start, vma->vm_end,
6871 &mem_cgroup_count_precharge_walk);
6872 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006873 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006874
6875 precharge = mc.precharge;
6876 mc.precharge = 0;
6877
6878 return precharge;
6879}
6880
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006881static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6882{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006883 unsigned long precharge = mem_cgroup_count_precharge(mm);
6884
6885 VM_BUG_ON(mc.moving_task);
6886 mc.moving_task = current;
6887 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006888}
6889
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006890/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6891static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006892{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006893 struct mem_cgroup *from = mc.from;
6894 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006895 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006896
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006897 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006898 if (mc.precharge) {
6899 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6900 mc.precharge = 0;
6901 }
6902 /*
6903 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6904 * we must uncharge here.
6905 */
6906 if (mc.moved_charge) {
6907 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6908 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006909 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006910 /* we must fixup refcnts and charges */
6911 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006912 /* uncharge swap account from the old cgroup */
6913 if (!mem_cgroup_is_root(mc.from))
6914 res_counter_uncharge(&mc.from->memsw,
6915 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006916
6917 for (i = 0; i < mc.moved_swap; i++)
6918 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006919
6920 if (!mem_cgroup_is_root(mc.to)) {
6921 /*
6922 * we charged both to->res and to->memsw, so we should
6923 * uncharge to->res.
6924 */
6925 res_counter_uncharge(&mc.to->res,
6926 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006927 }
Li Zefan40503772013-07-08 16:00:34 -07006928 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006929 mc.moved_swap = 0;
6930 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006931 memcg_oom_recover(from);
6932 memcg_oom_recover(to);
6933 wake_up_all(&mc.waitq);
6934}
6935
6936static void mem_cgroup_clear_mc(void)
6937{
6938 struct mem_cgroup *from = mc.from;
6939
6940 /*
6941 * we must clear moving_task before waking up waiters at the end of
6942 * task migration.
6943 */
6944 mc.moving_task = NULL;
6945 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006946 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006947 mc.from = NULL;
6948 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006949 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006950 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006951}
6952
Tejun Heoeb954192013-08-08 20:11:23 -04006953static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006954 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006955{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006956 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006957 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006958 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006959 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006960
Glauber Costaee5e8472013-02-22 16:34:50 -08006961 /*
6962 * We are now commited to this value whatever it is. Changes in this
6963 * tunable will only affect upcoming migrations, not the current one.
6964 * So we need to save it, and keep it going.
6965 */
6966 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6967 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006968 struct mm_struct *mm;
6969 struct mem_cgroup *from = mem_cgroup_from_task(p);
6970
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006971 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006972
6973 mm = get_task_mm(p);
6974 if (!mm)
6975 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006976 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006977 if (mm->owner == p) {
6978 VM_BUG_ON(mc.from);
6979 VM_BUG_ON(mc.to);
6980 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006981 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006982 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006983 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006984 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006985 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006986 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006987 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006988 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006989 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006990
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006991 ret = mem_cgroup_precharge_mc(mm);
6992 if (ret)
6993 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006994 }
6995 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006996 }
6997 return ret;
6998}
6999
Tejun Heoeb954192013-08-08 20:11:23 -04007000static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007001 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007002{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007003 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007004}
7005
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007006static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
7007 unsigned long addr, unsigned long end,
7008 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007009{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007010 int ret = 0;
7011 struct vm_area_struct *vma = walk->private;
7012 pte_t *pte;
7013 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07007014 enum mc_target_type target_type;
7015 union mc_target target;
7016 struct page *page;
7017 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007018
Naoya Horiguchi12724852012-03-21 16:34:28 -07007019 /*
7020 * We don't take compound_lock() here but no race with splitting thp
7021 * happens because:
7022 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
7023 * under splitting, which means there's no concurrent thp split,
7024 * - if another thread runs into split_huge_page() just after we
7025 * entered this if-block, the thread must wait for page table lock
7026 * to be unlocked in __split_huge_page_splitting(), where the main
7027 * part of thp split is not executed yet.
7028 */
7029 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07007030 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07007031 spin_unlock(&vma->vm_mm->page_table_lock);
7032 return 0;
7033 }
7034 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
7035 if (target_type == MC_TARGET_PAGE) {
7036 page = target.page;
7037 if (!isolate_lru_page(page)) {
7038 pc = lookup_page_cgroup(page);
7039 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07007040 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07007041 mc.precharge -= HPAGE_PMD_NR;
7042 mc.moved_charge += HPAGE_PMD_NR;
7043 }
7044 putback_lru_page(page);
7045 }
7046 put_page(page);
7047 }
7048 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07007049 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07007050 }
7051
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07007052 if (pmd_trans_unstable(pmd))
7053 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007054retry:
7055 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
7056 for (; addr != end; addr += PAGE_SIZE) {
7057 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08007058 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007059
7060 if (!mc.precharge)
7061 break;
7062
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07007063 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007064 case MC_TARGET_PAGE:
7065 page = target.page;
7066 if (isolate_lru_page(page))
7067 goto put;
7068 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07007069 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07007070 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007071 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08007072 /* we uncharge from mc.from later. */
7073 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007074 }
7075 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07007076put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007077 put_page(page);
7078 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08007079 case MC_TARGET_SWAP:
7080 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07007081 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08007082 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08007083 /* we fixup refcnts and charges later. */
7084 mc.moved_swap++;
7085 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08007086 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007087 default:
7088 break;
7089 }
7090 }
7091 pte_unmap_unlock(pte - 1, ptl);
7092 cond_resched();
7093
7094 if (addr != end) {
7095 /*
7096 * We have consumed all precharges we got in can_attach().
7097 * We try charge one by one, but don't do any additional
7098 * charges to mc.to if we have failed in charge once in attach()
7099 * phase.
7100 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08007101 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007102 if (!ret)
7103 goto retry;
7104 }
7105
7106 return ret;
7107}
7108
7109static void mem_cgroup_move_charge(struct mm_struct *mm)
7110{
7111 struct vm_area_struct *vma;
7112
7113 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007114retry:
7115 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
7116 /*
7117 * Someone who are holding the mmap_sem might be waiting in
7118 * waitq. So we cancel all extra charges, wake up all waiters,
7119 * and retry. Because we cancel precharges, we might not be able
7120 * to move enough charges, but moving charge is a best-effort
7121 * feature anyway, so it wouldn't be a big problem.
7122 */
7123 __mem_cgroup_clear_mc();
7124 cond_resched();
7125 goto retry;
7126 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007127 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7128 int ret;
7129 struct mm_walk mem_cgroup_move_charge_walk = {
7130 .pmd_entry = mem_cgroup_move_charge_pte_range,
7131 .mm = mm,
7132 .private = vma,
7133 };
7134 if (is_vm_hugetlb_page(vma))
7135 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007136 ret = walk_page_range(vma->vm_start, vma->vm_end,
7137 &mem_cgroup_move_charge_walk);
7138 if (ret)
7139 /*
7140 * means we have consumed all precharges and failed in
7141 * doing additional charge. Just abandon here.
7142 */
7143 break;
7144 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007145 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007146}
7147
Tejun Heoeb954192013-08-08 20:11:23 -04007148static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007149 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08007150{
Tejun Heo2f7ee562011-12-12 18:12:21 -08007151 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007152 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007153
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007154 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007155 if (mc.to)
7156 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007157 mmput(mm);
7158 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007159 if (mc.to)
7160 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08007161}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007162#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04007163static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007164 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007165{
7166 return 0;
7167}
Tejun Heoeb954192013-08-08 20:11:23 -04007168static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007169 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007170{
7171}
Tejun Heoeb954192013-08-08 20:11:23 -04007172static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007173 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007174{
7175}
7176#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08007177
Tejun Heof00baae2013-04-15 13:41:15 -07007178/*
7179 * Cgroup retains root cgroups across [un]mount cycles making it necessary
7180 * to verify sane_behavior flag on each mount attempt.
7181 */
Tejun Heoeb954192013-08-08 20:11:23 -04007182static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07007183{
7184 /*
7185 * use_hierarchy is forced with sane_behavior. cgroup core
7186 * guarantees that @root doesn't have any children, so turning it
7187 * on for the root memcg is enough.
7188 */
Tejun Heoeb954192013-08-08 20:11:23 -04007189 if (cgroup_sane_behavior(root_css->cgroup))
7190 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07007191}
7192
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007193struct cgroup_subsys mem_cgroup_subsys = {
7194 .name = "memory",
7195 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007196 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08007197 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08007198 .css_offline = mem_cgroup_css_offline,
7199 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007200 .can_attach = mem_cgroup_can_attach,
7201 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08007202 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07007203 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07007204 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08007205 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07007206 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007207};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007208
Andrew Mortonc255a452012-07-31 16:43:02 -07007209#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08007210static int __init enable_swap_account(char *s)
7211{
Michal Hockoa2c89902011-05-24 17:12:50 -07007212 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007213 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07007214 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007215 really_do_swap_account = 0;
7216 return 1;
7217}
Michal Hockoa2c89902011-05-24 17:12:50 -07007218__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007219
Michal Hocko2d110852013-02-22 16:34:43 -08007220static void __init memsw_file_init(void)
7221{
Michal Hocko6acc8b02013-02-22 16:34:45 -08007222 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08007223}
Michal Hocko6acc8b02013-02-22 16:34:45 -08007224
7225static void __init enable_swap_cgroup(void)
7226{
7227 if (!mem_cgroup_disabled() && really_do_swap_account) {
7228 do_swap_account = 1;
7229 memsw_file_init();
7230 }
7231}
7232
Michal Hocko2d110852013-02-22 16:34:43 -08007233#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08007234static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08007235{
7236}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007237#endif
Michal Hocko2d110852013-02-22 16:34:43 -08007238
7239/*
Michal Hocko10813122013-02-22 16:35:41 -08007240 * subsys_initcall() for memory controller.
7241 *
7242 * Some parts like hotcpu_notifier() have to be initialized from this context
7243 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
7244 * everything that doesn't depend on a specific mem_cgroup structure should
7245 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08007246 */
7247static int __init mem_cgroup_init(void)
7248{
7249 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08007250 enable_swap_cgroup();
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07007251 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08007252 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08007253 return 0;
7254}
7255subsys_initcall(mem_cgroup_init);