blob: 42f2843af1a7481c42f80c212fd07a46a489d394 [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 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500236 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500237 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500238 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500239 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500240 * eventfd to signal userspace about the event.
241 */
242 struct eventfd_ctx *eventfd;
243 /*
244 * Each of these stored in a list by the cgroup.
245 */
246 struct list_head list;
247 /*
Tejun Heofba94802013-11-22 18:20:43 -0500248 * register_event() callback will be used to add new userspace
249 * waiter for changes related to this event. Use eventfd_signal()
250 * on eventfd to send notification to userspace.
251 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500252 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500253 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500254 /*
255 * unregister_event() callback will be called when userspace closes
256 * the eventfd or on cgroup removing. This callback must be set,
257 * if you want provide notification functionality.
258 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500259 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500260 struct eventfd_ctx *eventfd);
261 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500262 * All fields below needed to unregister event when
263 * userspace closes eventfd.
264 */
265 poll_table pt;
266 wait_queue_head_t *wqh;
267 wait_queue_t wait;
268 struct work_struct remove;
269};
270
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700271static void mem_cgroup_threshold(struct mem_cgroup *memcg);
272static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800273
Balbir Singhf64c3f52009-09-23 15:56:37 -0700274/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800275 * The memory controller data structure. The memory controller controls both
276 * page cache and RSS per cgroup. We would eventually like to provide
277 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
278 * to help the administrator determine what knobs to tune.
279 *
280 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800281 * we hit the water mark. May be even add a low water mark, such that
282 * no reclaim occurs from a cgroup at it's low water mark, this is
283 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800284 */
285struct mem_cgroup {
286 struct cgroup_subsys_state css;
287 /*
288 * the counter to account for memory usage
289 */
290 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700291
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700292 /* vmpressure notifications */
293 struct vmpressure vmpressure;
294
Li Zefan465939a2013-07-08 16:00:38 -0700295 /*
296 * the counter to account for mem+swap usage.
297 */
298 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700299
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800300 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800301 * the counter to account for kernel memory usage.
302 */
303 struct res_counter kmem;
304 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800305 * Should the accounting and control be hierarchical, per subtree?
306 */
307 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800308 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700309
310 bool oom_lock;
311 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700312 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700313
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700314 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700315 /* OOM-Killer disable */
316 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800317
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700318 /* set when res.limit == memsw.limit */
319 bool memsw_is_minimum;
320
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800321 /* protect arrays of thresholds */
322 struct mutex thresholds_lock;
323
324 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700325 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700326
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800327 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700328 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700329
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700330 /* For oom notifier event fd */
331 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700332
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800333 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800334 * Should we move charges of a task when a task is moved into this
335 * mem_cgroup ? And what type of charges should we move ?
336 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700337 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800338 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700339 * set > 0 if pages under this cgroup are moving to other cgroup.
340 */
341 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700342 /* taken only while moving_account > 0 */
343 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700344 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800345 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800346 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700347 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700348 /*
349 * used when a cpu is offlined or other synchronizations
350 * See mem_cgroup_read_stat().
351 */
352 struct mem_cgroup_stat_cpu nocpu_base;
353 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000354
Michal Hocko5f578162013-04-29 15:07:17 -0700355 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700356#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000357 struct tcp_memcontrol tcp_mem;
358#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800359#if defined(CONFIG_MEMCG_KMEM)
360 /* analogous to slab_common's slab_caches list. per-memcg */
361 struct list_head memcg_slab_caches;
362 /* Not a spinlock, we can take a lot of time walking the list */
363 struct mutex slab_caches_mutex;
364 /* Index in the kmem_cache->memcg_params->memcg_caches array */
365 int kmemcg_id;
366#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800367
368 int last_scanned_node;
369#if MAX_NUMNODES > 1
370 nodemask_t scan_nodes;
371 atomic_t numainfo_events;
372 atomic_t numainfo_updating;
373#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700374
Tejun Heofba94802013-11-22 18:20:43 -0500375 /* List of events which userspace want to receive */
376 struct list_head event_list;
377 spinlock_t event_list_lock;
378
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700379 struct mem_cgroup_per_node *nodeinfo[0];
380 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800381};
382
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800383static size_t memcg_size(void)
384{
385 return sizeof(struct mem_cgroup) +
386 nr_node_ids * sizeof(struct mem_cgroup_per_node);
387}
388
Glauber Costa510fc4e2012-12-18 14:21:47 -0800389/* internal only representation about the status of kmem accounting. */
390enum {
391 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800392 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800393 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800394};
395
Glauber Costaa8964b92012-12-18 14:22:09 -0800396/* We account when limit is on, but only after call sites are patched */
397#define KMEM_ACCOUNTED_MASK \
398 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800399
400#ifdef CONFIG_MEMCG_KMEM
401static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
402{
403 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
404}
Glauber Costa7de37682012-12-18 14:22:07 -0800405
406static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
407{
408 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
409}
410
Glauber Costaa8964b92012-12-18 14:22:09 -0800411static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
412{
413 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
414}
415
Glauber Costa55007d82012-12-18 14:22:38 -0800416static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
417{
418 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
419}
420
Glauber Costa7de37682012-12-18 14:22:07 -0800421static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
422{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700423 /*
424 * Our caller must use css_get() first, because memcg_uncharge_kmem()
425 * will call css_put() if it sees the memcg is dead.
426 */
427 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800428 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
429 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
430}
431
432static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
433{
434 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
435 &memcg->kmem_account_flags);
436}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800437#endif
438
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800439/* Stuffs for move charges at task migration. */
440/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800441 * Types of charges to be moved. "move_charge_at_immitgrate" and
442 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800443 */
444enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800445 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700446 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800447 NR_MOVE_TYPE,
448};
449
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800450/* "mc" and its members are protected by cgroup_mutex */
451static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800452 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800453 struct mem_cgroup *from;
454 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800455 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800456 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800457 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800458 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800459 struct task_struct *moving_task; /* a task moving charges */
460 wait_queue_head_t waitq; /* a waitq for other context */
461} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700462 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800463 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
464};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800465
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700466static bool move_anon(void)
467{
Glauber Costaee5e8472013-02-22 16:34:50 -0800468 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700469}
470
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700471static bool move_file(void)
472{
Glauber Costaee5e8472013-02-22 16:34:50 -0800473 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700474}
475
Balbir Singh4e416952009-09-23 15:56:39 -0700476/*
477 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
478 * limit reclaim to prevent infinite loops, if they ever occur.
479 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700480#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700481#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700482
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800483enum charge_type {
484 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700485 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800486 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700487 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700488 NR_CHARGE_TYPE,
489};
490
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800491/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800492enum res_type {
493 _MEM,
494 _MEMSWAP,
495 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800496 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800497};
498
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700499#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
500#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800501#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700502/* Used for OOM nofiier */
503#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800504
Balbir Singh75822b42009-09-23 15:56:38 -0700505/*
506 * Reclaim flags for mem_cgroup_hierarchical_reclaim
507 */
508#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
509#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
510#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
511#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
512
Glauber Costa09998212013-02-22 16:34:55 -0800513/*
514 * The memcg_create_mutex will be held whenever a new cgroup is created.
515 * As a consequence, any change that needs to protect against new child cgroups
516 * appearing has to hold it as well.
517 */
518static DEFINE_MUTEX(memcg_create_mutex);
519
Wanpeng Lib2145142012-07-31 16:46:01 -0700520struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
521{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400522 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700523}
524
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700525/* Some nice accessors for the vmpressure. */
526struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
527{
528 if (!memcg)
529 memcg = root_mem_cgroup;
530 return &memcg->vmpressure;
531}
532
533struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
534{
535 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
536}
537
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700538static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
539{
540 return (memcg == root_mem_cgroup);
541}
542
Glauber Costae1aab162011-12-11 21:47:03 +0000543/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700544#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000545
Glauber Costae1aab162011-12-11 21:47:03 +0000546void sock_update_memcg(struct sock *sk)
547{
Glauber Costa376be5f2012-01-20 04:57:14 +0000548 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000549 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700550 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000551
552 BUG_ON(!sk->sk_prot->proto_cgroup);
553
Glauber Costaf3f511e2012-01-05 20:16:39 +0000554 /* Socket cloning can throw us here with sk_cgrp already
555 * filled. It won't however, necessarily happen from
556 * process context. So the test for root memcg given
557 * the current task's memcg won't help us in this case.
558 *
559 * Respecting the original socket's memcg is a better
560 * decision in this case.
561 */
562 if (sk->sk_cgrp) {
563 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700564 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000565 return;
566 }
567
Glauber Costae1aab162011-12-11 21:47:03 +0000568 rcu_read_lock();
569 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700570 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700571 if (!mem_cgroup_is_root(memcg) &&
572 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700573 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000574 }
575 rcu_read_unlock();
576 }
577}
578EXPORT_SYMBOL(sock_update_memcg);
579
580void sock_release_memcg(struct sock *sk)
581{
Glauber Costa376be5f2012-01-20 04:57:14 +0000582 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000583 struct mem_cgroup *memcg;
584 WARN_ON(!sk->sk_cgrp->memcg);
585 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700586 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000587 }
588}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000589
590struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
591{
592 if (!memcg || mem_cgroup_is_root(memcg))
593 return NULL;
594
595 return &memcg->tcp_mem.cg_proto;
596}
597EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000598
Glauber Costa3f134612012-05-29 15:07:11 -0700599static void disarm_sock_keys(struct mem_cgroup *memcg)
600{
601 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
602 return;
603 static_key_slow_dec(&memcg_socket_limit_enabled);
604}
605#else
606static void disarm_sock_keys(struct mem_cgroup *memcg)
607{
608}
609#endif
610
Glauber Costaa8964b92012-12-18 14:22:09 -0800611#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800612/*
613 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
614 * There are two main reasons for not using the css_id for this:
615 * 1) this works better in sparse environments, where we have a lot of memcgs,
616 * but only a few kmem-limited. Or also, if we have, for instance, 200
617 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
618 * 200 entry array for that.
619 *
620 * 2) In order not to violate the cgroup API, we would like to do all memory
621 * allocation in ->create(). At that point, we haven't yet allocated the
622 * css_id. Having a separate index prevents us from messing with the cgroup
623 * core for this
624 *
625 * The current size of the caches array is stored in
626 * memcg_limited_groups_array_size. It will double each time we have to
627 * increase it.
628 */
629static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800630int memcg_limited_groups_array_size;
631
Glauber Costa55007d82012-12-18 14:22:38 -0800632/*
633 * MIN_SIZE is different than 1, because we would like to avoid going through
634 * the alloc/free process all the time. In a small machine, 4 kmem-limited
635 * cgroups is a reasonable guess. In the future, it could be a parameter or
636 * tunable, but that is strictly not necessary.
637 *
638 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
639 * this constant directly from cgroup, but it is understandable that this is
640 * better kept as an internal representation in cgroup.c. In any case, the
641 * css_id space is not getting any smaller, and we don't have to necessarily
642 * increase ours as well if it increases.
643 */
644#define MEMCG_CACHES_MIN_SIZE 4
645#define MEMCG_CACHES_MAX_SIZE 65535
646
Glauber Costad7f25f82012-12-18 14:22:40 -0800647/*
648 * A lot of the calls to the cache allocation functions are expected to be
649 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
650 * conditional to this static branch, we'll have to allow modules that does
651 * kmem_cache_alloc and the such to see this symbol as well
652 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800653struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800654EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800655
656static void disarm_kmem_keys(struct mem_cgroup *memcg)
657{
Glauber Costa55007d82012-12-18 14:22:38 -0800658 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800659 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800660 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
661 }
Glauber Costabea207c2012-12-18 14:22:11 -0800662 /*
663 * This check can't live in kmem destruction function,
664 * since the charges will outlive the cgroup
665 */
666 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800667}
668#else
669static void disarm_kmem_keys(struct mem_cgroup *memcg)
670{
671}
672#endif /* CONFIG_MEMCG_KMEM */
673
674static void disarm_static_keys(struct mem_cgroup *memcg)
675{
676 disarm_sock_keys(memcg);
677 disarm_kmem_keys(memcg);
678}
679
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700680static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800681
Balbir Singhf64c3f52009-09-23 15:56:37 -0700682static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700683mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700684{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800685 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700686 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700687}
688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700689struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100690{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700691 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100692}
693
Balbir Singhf64c3f52009-09-23 15:56:37 -0700694static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700695page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700696{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700697 int nid = page_to_nid(page);
698 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700699
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700700 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700701}
702
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700703static struct mem_cgroup_tree_per_zone *
704soft_limit_tree_node_zone(int nid, int zid)
705{
706 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
707}
708
709static struct mem_cgroup_tree_per_zone *
710soft_limit_tree_from_page(struct page *page)
711{
712 int nid = page_to_nid(page);
713 int zid = page_zonenum(page);
714
715 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
716}
717
718static void
719__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
720 struct mem_cgroup_per_zone *mz,
721 struct mem_cgroup_tree_per_zone *mctz,
722 unsigned long long new_usage_in_excess)
723{
724 struct rb_node **p = &mctz->rb_root.rb_node;
725 struct rb_node *parent = NULL;
726 struct mem_cgroup_per_zone *mz_node;
727
728 if (mz->on_tree)
729 return;
730
731 mz->usage_in_excess = new_usage_in_excess;
732 if (!mz->usage_in_excess)
733 return;
734 while (*p) {
735 parent = *p;
736 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
737 tree_node);
738 if (mz->usage_in_excess < mz_node->usage_in_excess)
739 p = &(*p)->rb_left;
740 /*
741 * We can't avoid mem cgroups that are over their soft
742 * limit by the same amount
743 */
744 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
745 p = &(*p)->rb_right;
746 }
747 rb_link_node(&mz->tree_node, parent, p);
748 rb_insert_color(&mz->tree_node, &mctz->rb_root);
749 mz->on_tree = true;
750}
751
752static void
753__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
754 struct mem_cgroup_per_zone *mz,
755 struct mem_cgroup_tree_per_zone *mctz)
756{
757 if (!mz->on_tree)
758 return;
759 rb_erase(&mz->tree_node, &mctz->rb_root);
760 mz->on_tree = false;
761}
762
763static void
764mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
765 struct mem_cgroup_per_zone *mz,
766 struct mem_cgroup_tree_per_zone *mctz)
767{
768 spin_lock(&mctz->lock);
769 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
770 spin_unlock(&mctz->lock);
771}
772
773
774static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
775{
776 unsigned long long excess;
777 struct mem_cgroup_per_zone *mz;
778 struct mem_cgroup_tree_per_zone *mctz;
779 int nid = page_to_nid(page);
780 int zid = page_zonenum(page);
781 mctz = soft_limit_tree_from_page(page);
782
783 /*
784 * Necessary to update all ancestors when hierarchy is used.
785 * because their event counter is not touched.
786 */
787 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
788 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
789 excess = res_counter_soft_limit_excess(&memcg->res);
790 /*
791 * We have to update the tree if mz is on RB-tree or
792 * mem is over its softlimit.
793 */
794 if (excess || mz->on_tree) {
795 spin_lock(&mctz->lock);
796 /* if on-tree, remove it */
797 if (mz->on_tree)
798 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
799 /*
800 * Insert again. mz->usage_in_excess will be updated.
801 * If excess is 0, no tree ops.
802 */
803 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
804 spin_unlock(&mctz->lock);
805 }
806 }
807}
808
809static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
810{
811 int node, zone;
812 struct mem_cgroup_per_zone *mz;
813 struct mem_cgroup_tree_per_zone *mctz;
814
815 for_each_node(node) {
816 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
817 mz = mem_cgroup_zoneinfo(memcg, node, zone);
818 mctz = soft_limit_tree_node_zone(node, zone);
819 mem_cgroup_remove_exceeded(memcg, mz, mctz);
820 }
821 }
822}
823
824static struct mem_cgroup_per_zone *
825__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
826{
827 struct rb_node *rightmost = NULL;
828 struct mem_cgroup_per_zone *mz;
829
830retry:
831 mz = NULL;
832 rightmost = rb_last(&mctz->rb_root);
833 if (!rightmost)
834 goto done; /* Nothing to reclaim from */
835
836 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
837 /*
838 * Remove the node now but someone else can add it back,
839 * we will to add it back at the end of reclaim to its correct
840 * position in the tree.
841 */
842 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
843 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
844 !css_tryget(&mz->memcg->css))
845 goto retry;
846done:
847 return mz;
848}
849
850static struct mem_cgroup_per_zone *
851mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
852{
853 struct mem_cgroup_per_zone *mz;
854
855 spin_lock(&mctz->lock);
856 mz = __mem_cgroup_largest_soft_limit_node(mctz);
857 spin_unlock(&mctz->lock);
858 return mz;
859}
860
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700861/*
862 * Implementation Note: reading percpu statistics for memcg.
863 *
864 * Both of vmstat[] and percpu_counter has threshold and do periodic
865 * synchronization to implement "quick" read. There are trade-off between
866 * reading cost and precision of value. Then, we may have a chance to implement
867 * a periodic synchronizion of counter in memcg's counter.
868 *
869 * But this _read() function is used for user interface now. The user accounts
870 * memory usage by memory cgroup and he _always_ requires exact value because
871 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
872 * have to visit all online cpus and make sum. So, for now, unnecessary
873 * synchronization is not implemented. (just implemented for cpu hotplug)
874 *
875 * If there are kernel internal actions which can make use of some not-exact
876 * value, and reading all cpu value can be performance bottleneck in some
877 * common workload, threashold and synchonization as vmstat[] should be
878 * implemented.
879 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700880static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700881 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800882{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700883 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800884 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800885
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700886 get_online_cpus();
887 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700888 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700889#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700890 spin_lock(&memcg->pcp_counter_lock);
891 val += memcg->nocpu_base.count[idx];
892 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700893#endif
894 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800895 return val;
896}
897
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700898static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700899 bool charge)
900{
901 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700902 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700903}
904
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700905static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700906 enum mem_cgroup_events_index idx)
907{
908 unsigned long val = 0;
909 int cpu;
910
David Rientjes9c567512013-10-16 13:46:43 -0700911 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700912 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700913 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700914#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700915 spin_lock(&memcg->pcp_counter_lock);
916 val += memcg->nocpu_base.events[idx];
917 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700918#endif
David Rientjes9c567512013-10-16 13:46:43 -0700919 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700920 return val;
921}
922
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700923static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700924 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700925 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800926{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800927 preempt_disable();
928
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700929 /*
930 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
931 * counted as CACHE even if it's on ANON LRU.
932 */
933 if (anon)
934 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700935 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800936 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700937 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700938 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700939
David Rientjesb070e652013-05-07 16:18:09 -0700940 if (PageTransHuge(page))
941 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
942 nr_pages);
943
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800944 /* pagein of a big page is an event. So, ignore page size */
945 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700946 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800947 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700948 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800949 nr_pages = -nr_pages; /* for event */
950 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800951
Johannes Weiner13114712012-05-29 15:07:07 -0700952 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800953
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800954 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800955}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800956
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700957unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700958mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700959{
960 struct mem_cgroup_per_zone *mz;
961
962 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
963 return mz->lru_size[lru];
964}
965
966static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700967mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700968 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700969{
970 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700971 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700972 unsigned long ret = 0;
973
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700974 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700975
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700976 for_each_lru(lru) {
977 if (BIT(lru) & lru_mask)
978 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700979 }
980 return ret;
981}
982
983static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700984mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700985 int nid, unsigned int lru_mask)
986{
Ying Han889976d2011-05-26 16:25:33 -0700987 u64 total = 0;
988 int zid;
989
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700990 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700991 total += mem_cgroup_zone_nr_lru_pages(memcg,
992 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700993
Ying Han889976d2011-05-26 16:25:33 -0700994 return total;
995}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700996
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700997static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700998 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800999{
Ying Han889976d2011-05-26 16:25:33 -07001000 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001001 u64 total = 0;
1002
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001003 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001004 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001005 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -08001006}
1007
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001008static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
1009 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001010{
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001011 unsigned long val, next;
1012
Johannes Weiner13114712012-05-29 15:07:07 -07001013 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -07001014 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001015 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001016 if ((long)next - (long)val < 0) {
1017 switch (target) {
1018 case MEM_CGROUP_TARGET_THRESH:
1019 next = val + THRESHOLDS_EVENTS_TARGET;
1020 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001021 case MEM_CGROUP_TARGET_SOFTLIMIT:
1022 next = val + SOFTLIMIT_EVENTS_TARGET;
1023 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001024 case MEM_CGROUP_TARGET_NUMAINFO:
1025 next = val + NUMAINFO_EVENTS_TARGET;
1026 break;
1027 default:
1028 break;
1029 }
1030 __this_cpu_write(memcg->stat->targets[target], next);
1031 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001032 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001033 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001034}
1035
1036/*
1037 * Check events in order.
1038 *
1039 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001040static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001041{
Steven Rostedt47994012011-11-02 13:38:33 -07001042 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001043 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001044 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1045 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001046 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001047 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001048
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001049 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1050 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001051#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001052 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1053 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001054#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001055 preempt_enable();
1056
1057 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001058 if (unlikely(do_softlimit))
1059 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001060#if MAX_NUMNODES > 1
1061 if (unlikely(do_numainfo))
1062 atomic_inc(&memcg->numainfo_events);
1063#endif
1064 } else
1065 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001066}
1067
Balbir Singhcf475ad2008-04-29 01:00:16 -07001068struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001069{
Balbir Singh31a78f22008-09-28 23:09:31 +01001070 /*
1071 * mm_update_next_owner() may clear mm->owner to NULL
1072 * if it races with swapoff, page migration, etc.
1073 * So this can be called with p == NULL.
1074 */
1075 if (unlikely(!p))
1076 return NULL;
1077
Tejun Heo8af01f52013-08-08 20:11:22 -04001078 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001079}
1080
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001081struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001082{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001083 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001084
1085 if (!mm)
1086 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001087 /*
1088 * Because we have no locks, mm->owner's may be being moved to other
1089 * cgroup. We use css_tryget() here even if this looks
1090 * pessimistic (rather than adding locks here).
1091 */
1092 rcu_read_lock();
1093 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001094 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1095 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001096 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001097 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001098 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001099 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001100}
1101
Michal Hocko16248d82013-04-29 15:07:19 -07001102/*
1103 * Returns a next (in a pre-order walk) alive memcg (with elevated css
1104 * ref. count) or NULL if the whole root's subtree has been visited.
1105 *
1106 * helper function to be used by mem_cgroup_iter
1107 */
1108static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Andrew Morton694fbc02013-09-24 15:27:37 -07001109 struct mem_cgroup *last_visited)
Michal Hocko16248d82013-04-29 15:07:19 -07001110{
Tejun Heo492eb212013-08-08 20:11:25 -04001111 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -07001112
Tejun Heobd8815a2013-08-08 20:11:27 -04001113 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -07001114skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -04001115 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -07001116
1117 /*
1118 * Even if we found a group we have to make sure it is
1119 * alive. css && !memcg means that the groups should be
1120 * skipped and we should continue the tree walk.
1121 * last_visited css is safe to use because it is
1122 * protected by css_get and the tree walk is rcu safe.
1123 */
Tejun Heo492eb212013-08-08 20:11:25 -04001124 if (next_css) {
1125 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
1126
Andrew Morton694fbc02013-09-24 15:27:37 -07001127 if (css_tryget(&mem->css))
1128 return mem;
1129 else {
Tejun Heo492eb212013-08-08 20:11:25 -04001130 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -07001131 goto skip_node;
1132 }
1133 }
1134
1135 return NULL;
1136}
1137
Johannes Weiner519ebea2013-07-03 15:04:51 -07001138static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1139{
1140 /*
1141 * When a group in the hierarchy below root is destroyed, the
1142 * hierarchy iterator can no longer be trusted since it might
1143 * have pointed to the destroyed group. Invalidate it.
1144 */
1145 atomic_inc(&root->dead_count);
1146}
1147
1148static struct mem_cgroup *
1149mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1150 struct mem_cgroup *root,
1151 int *sequence)
1152{
1153 struct mem_cgroup *position = NULL;
1154 /*
1155 * A cgroup destruction happens in two stages: offlining and
1156 * release. They are separated by a RCU grace period.
1157 *
1158 * If the iterator is valid, we may still race with an
1159 * offlining. The RCU lock ensures the object won't be
1160 * released, tryget will fail if we lost the race.
1161 */
1162 *sequence = atomic_read(&root->dead_count);
1163 if (iter->last_dead_count == *sequence) {
1164 smp_rmb();
1165 position = iter->last_visited;
1166 if (position && !css_tryget(&position->css))
1167 position = NULL;
1168 }
1169 return position;
1170}
1171
1172static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1173 struct mem_cgroup *last_visited,
1174 struct mem_cgroup *new_position,
1175 int sequence)
1176{
1177 if (last_visited)
1178 css_put(&last_visited->css);
1179 /*
1180 * We store the sequence count from the time @last_visited was
1181 * loaded successfully instead of rereading it here so that we
1182 * don't lose destruction events in between. We could have
1183 * raced with the destruction of @new_position after all.
1184 */
1185 iter->last_visited = new_position;
1186 smp_wmb();
1187 iter->last_dead_count = sequence;
1188}
1189
Johannes Weiner56600482012-01-12 17:17:59 -08001190/**
1191 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1192 * @root: hierarchy root
1193 * @prev: previously returned memcg, NULL on first invocation
1194 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1195 *
1196 * Returns references to children of the hierarchy below @root, or
1197 * @root itself, or %NULL after a full round-trip.
1198 *
1199 * Caller must pass the return value in @prev on subsequent
1200 * invocations for reference counting, or use mem_cgroup_iter_break()
1201 * to cancel a hierarchy walk before the round-trip is complete.
1202 *
1203 * Reclaimers can specify a zone and a priority level in @reclaim to
1204 * divide up the memcgs in the hierarchy among all concurrent
1205 * reclaimers operating on the same zone and priority.
1206 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001207struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001208 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001209 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001210{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001211 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001212 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001213
Andrew Morton694fbc02013-09-24 15:27:37 -07001214 if (mem_cgroup_disabled())
1215 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001216
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001217 if (!root)
1218 root = root_mem_cgroup;
1219
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001220 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001221 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001222
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001223 if (!root->use_hierarchy && root != root_mem_cgroup) {
1224 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001225 goto out_css_put;
Andrew Morton694fbc02013-09-24 15:27:37 -07001226 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001227 }
1228
Michal Hocko542f85f2013-04-29 15:07:15 -07001229 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001230 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001231 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001232 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001233
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001234 if (reclaim) {
1235 int nid = zone_to_nid(reclaim->zone);
1236 int zid = zone_idx(reclaim->zone);
1237 struct mem_cgroup_per_zone *mz;
1238
1239 mz = mem_cgroup_zoneinfo(root, nid, zid);
1240 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001241 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001242 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001243 goto out_unlock;
1244 }
Michal Hocko5f578162013-04-29 15:07:17 -07001245
Johannes Weiner519ebea2013-07-03 15:04:51 -07001246 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001247 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001248
Andrew Morton694fbc02013-09-24 15:27:37 -07001249 memcg = __mem_cgroup_iter_next(root, last_visited);
Michal Hocko542f85f2013-04-29 15:07:15 -07001250
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001251 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001252 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001253
Michal Hocko19f39402013-04-29 15:07:18 -07001254 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001255 iter->generation++;
1256 else if (!prev && memcg)
1257 reclaim->generation = iter->generation;
1258 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001259
Andrew Morton694fbc02013-09-24 15:27:37 -07001260 if (prev && !memcg)
Michal Hocko542f85f2013-04-29 15:07:15 -07001261 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001262 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001263out_unlock:
1264 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001265out_css_put:
1266 if (prev && prev != root)
1267 css_put(&prev->css);
1268
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001269 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001270}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001271
Johannes Weiner56600482012-01-12 17:17:59 -08001272/**
1273 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1274 * @root: hierarchy root
1275 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1276 */
1277void mem_cgroup_iter_break(struct mem_cgroup *root,
1278 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001279{
1280 if (!root)
1281 root = root_mem_cgroup;
1282 if (prev && prev != root)
1283 css_put(&prev->css);
1284}
1285
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001286/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001287 * Iteration constructs for visiting all cgroups (under a tree). If
1288 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1289 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001290 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001291#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001292 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001293 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001294 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001295
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001296#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001297 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001298 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001299 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001300
David Rientjes68ae5642012-12-12 13:51:57 -08001301void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001302{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001303 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001304
Ying Han456f9982011-05-26 16:25:38 -07001305 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001306 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1307 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001308 goto out;
1309
1310 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001311 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001312 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1313 break;
1314 case PGMAJFAULT:
1315 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001316 break;
1317 default:
1318 BUG();
1319 }
1320out:
1321 rcu_read_unlock();
1322}
David Rientjes68ae5642012-12-12 13:51:57 -08001323EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001324
Johannes Weiner925b7672012-01-12 17:18:15 -08001325/**
1326 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1327 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001328 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001329 *
1330 * Returns the lru list vector holding pages for the given @zone and
1331 * @mem. This can be the global zone lruvec, if the memory controller
1332 * is disabled.
1333 */
1334struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1335 struct mem_cgroup *memcg)
1336{
1337 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001338 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001339
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001340 if (mem_cgroup_disabled()) {
1341 lruvec = &zone->lruvec;
1342 goto out;
1343 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001344
1345 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001346 lruvec = &mz->lruvec;
1347out:
1348 /*
1349 * Since a node can be onlined after the mem_cgroup was created,
1350 * we have to be prepared to initialize lruvec->zone here;
1351 * and if offlined then reonlined, we need to reinitialize it.
1352 */
1353 if (unlikely(lruvec->zone != zone))
1354 lruvec->zone = zone;
1355 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001356}
1357
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001358/*
1359 * Following LRU functions are allowed to be used without PCG_LOCK.
1360 * Operations are called by routine of global LRU independently from memcg.
1361 * What we have to take care of here is validness of pc->mem_cgroup.
1362 *
1363 * Changes to pc->mem_cgroup happens when
1364 * 1. charge
1365 * 2. moving account
1366 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1367 * It is added to LRU before charge.
1368 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1369 * When moving account, the page is not on LRU. It's isolated.
1370 */
1371
Johannes Weiner925b7672012-01-12 17:18:15 -08001372/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001373 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001374 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001375 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001376 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001377struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001378{
1379 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001380 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001381 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001382 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001383
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001384 if (mem_cgroup_disabled()) {
1385 lruvec = &zone->lruvec;
1386 goto out;
1387 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001388
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001389 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001390 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001391
1392 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001393 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001394 * an uncharged page off lru does nothing to secure
1395 * its former mem_cgroup from sudden removal.
1396 *
1397 * Our caller holds lru_lock, and PageCgroupUsed is updated
1398 * under page_cgroup lock: between them, they make all uses
1399 * of pc->mem_cgroup safe.
1400 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001401 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001402 pc->mem_cgroup = memcg = root_mem_cgroup;
1403
Johannes Weiner925b7672012-01-12 17:18:15 -08001404 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001405 lruvec = &mz->lruvec;
1406out:
1407 /*
1408 * Since a node can be onlined after the mem_cgroup was created,
1409 * we have to be prepared to initialize lruvec->zone here;
1410 * and if offlined then reonlined, we need to reinitialize it.
1411 */
1412 if (unlikely(lruvec->zone != zone))
1413 lruvec->zone = zone;
1414 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001415}
1416
1417/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001418 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1419 * @lruvec: mem_cgroup per zone lru vector
1420 * @lru: index of lru list the page is sitting on
1421 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001422 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001423 * This function must be called when a page is added to or removed from an
1424 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001425 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001426void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1427 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001428{
1429 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001430 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001431
1432 if (mem_cgroup_disabled())
1433 return;
1434
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001435 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1436 lru_size = mz->lru_size + lru;
1437 *lru_size += nr_pages;
1438 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001439}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001440
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001441/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001442 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001443 * hierarchy subtree
1444 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001445bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1446 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001447{
Johannes Weiner91c637342012-05-29 15:06:24 -07001448 if (root_memcg == memcg)
1449 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001450 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001451 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001452 return css_is_ancestor(&memcg->css, &root_memcg->css);
1453}
1454
1455static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1456 struct mem_cgroup *memcg)
1457{
1458 bool ret;
1459
Johannes Weiner91c637342012-05-29 15:06:24 -07001460 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001461 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001462 rcu_read_unlock();
1463 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001464}
1465
David Rientjesffbdccf2013-07-03 15:01:23 -07001466bool task_in_mem_cgroup(struct task_struct *task,
1467 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001468{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001469 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001470 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001471 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001472
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001473 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001474 if (p) {
1475 curr = try_get_mem_cgroup_from_mm(p->mm);
1476 task_unlock(p);
1477 } else {
1478 /*
1479 * All threads may have already detached their mm's, but the oom
1480 * killer still needs to detect if they have already been oom
1481 * killed to prevent needlessly killing additional tasks.
1482 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001483 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001484 curr = mem_cgroup_from_task(task);
1485 if (curr)
1486 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001487 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001488 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001489 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001490 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001491 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001492 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001493 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001494 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1495 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001496 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001497 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001498 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001499 return ret;
1500}
1501
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001502int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001503{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001504 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001505 unsigned long inactive;
1506 unsigned long active;
1507 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001508
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001509 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1510 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001511
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001512 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1513 if (gb)
1514 inactive_ratio = int_sqrt(10 * gb);
1515 else
1516 inactive_ratio = 1;
1517
Johannes Weiner9b272972011-11-02 13:38:23 -07001518 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001519}
1520
Balbir Singh6d61ef42009-01-07 18:08:06 -08001521#define mem_cgroup_from_res_counter(counter, member) \
1522 container_of(counter, struct mem_cgroup, member)
1523
Johannes Weiner19942822011-02-01 15:52:43 -08001524/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001525 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001526 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001527 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001528 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001529 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001530 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001531static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001532{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001533 unsigned long long margin;
1534
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001535 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001536 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001537 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001538 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001539}
1540
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001541int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001542{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001543 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001544 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001545 return vm_swappiness;
1546
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001547 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001548}
1549
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001550/*
1551 * memcg->moving_account is used for checking possibility that some thread is
1552 * calling move_account(). When a thread on CPU-A starts moving pages under
1553 * a memcg, other threads should check memcg->moving_account under
1554 * rcu_read_lock(), like this:
1555 *
1556 * CPU-A CPU-B
1557 * rcu_read_lock()
1558 * memcg->moving_account+1 if (memcg->mocing_account)
1559 * take heavy locks.
1560 * synchronize_rcu() update something.
1561 * rcu_read_unlock()
1562 * start move here.
1563 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001564
1565/* for quick checking without looking up memcg */
1566atomic_t memcg_moving __read_mostly;
1567
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001568static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001569{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001570 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001571 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001572 synchronize_rcu();
1573}
1574
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001575static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001576{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001577 /*
1578 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1579 * We check NULL in callee rather than caller.
1580 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001581 if (memcg) {
1582 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001583 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001584 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001585}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001586
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001587/*
1588 * 2 routines for checking "mem" is under move_account() or not.
1589 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001590 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1591 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001592 * pc->mem_cgroup may be overwritten.
1593 *
1594 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1595 * under hierarchy of moving cgroups. This is for
1596 * waiting at hith-memory prressure caused by "move".
1597 */
1598
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001599static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001600{
1601 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001602 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001603}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001604
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001605static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001606{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001607 struct mem_cgroup *from;
1608 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001609 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001610 /*
1611 * Unlike task_move routines, we access mc.to, mc.from not under
1612 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1613 */
1614 spin_lock(&mc.lock);
1615 from = mc.from;
1616 to = mc.to;
1617 if (!from)
1618 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001619
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001620 ret = mem_cgroup_same_or_subtree(memcg, from)
1621 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001622unlock:
1623 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001624 return ret;
1625}
1626
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001627static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001628{
1629 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001630 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001631 DEFINE_WAIT(wait);
1632 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1633 /* moving charge context might have finished. */
1634 if (mc.moving_task)
1635 schedule();
1636 finish_wait(&mc.waitq, &wait);
1637 return true;
1638 }
1639 }
1640 return false;
1641}
1642
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001643/*
1644 * Take this lock when
1645 * - a code tries to modify page's memcg while it's USED.
1646 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001647 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001648 */
1649static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1650 unsigned long *flags)
1651{
1652 spin_lock_irqsave(&memcg->move_lock, *flags);
1653}
1654
1655static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1656 unsigned long *flags)
1657{
1658 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1659}
1660
Sha Zhengju58cf1882013-02-22 16:32:05 -08001661#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001662/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001663 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001664 * @memcg: The memory cgroup that went over limit
1665 * @p: Task that is going to be killed
1666 *
1667 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1668 * enabled
1669 */
1670void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1671{
1672 struct cgroup *task_cgrp;
1673 struct cgroup *mem_cgrp;
1674 /*
1675 * Need a buffer in BSS, can't rely on allocations. The code relies
1676 * on the assumption that OOM is serialized for memory controller.
1677 * If this assumption is broken, revisit this code.
1678 */
1679 static char memcg_name[PATH_MAX];
1680 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001681 struct mem_cgroup *iter;
1682 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001683
Sha Zhengju58cf1882013-02-22 16:32:05 -08001684 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001685 return;
1686
Balbir Singhe2224322009-04-02 16:57:39 -07001687 rcu_read_lock();
1688
1689 mem_cgrp = memcg->css.cgroup;
1690 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1691
1692 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1693 if (ret < 0) {
1694 /*
1695 * Unfortunately, we are unable to convert to a useful name
1696 * But we'll still print out the usage information
1697 */
1698 rcu_read_unlock();
1699 goto done;
1700 }
1701 rcu_read_unlock();
1702
Andrew Mortond0451972013-02-22 16:32:06 -08001703 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001704
1705 rcu_read_lock();
1706 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1707 if (ret < 0) {
1708 rcu_read_unlock();
1709 goto done;
1710 }
1711 rcu_read_unlock();
1712
1713 /*
1714 * Continues from above, so we don't need an KERN_ level
1715 */
Andrew Mortond0451972013-02-22 16:32:06 -08001716 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001717done:
1718
Andrew Mortond0451972013-02-22 16:32:06 -08001719 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001720 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1721 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1722 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001723 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001724 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1725 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1726 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001727 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001728 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1729 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1730 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001731
1732 for_each_mem_cgroup_tree(iter, memcg) {
1733 pr_info("Memory cgroup stats");
1734
1735 rcu_read_lock();
1736 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1737 if (!ret)
1738 pr_cont(" for %s", memcg_name);
1739 rcu_read_unlock();
1740 pr_cont(":");
1741
1742 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1743 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1744 continue;
1745 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1746 K(mem_cgroup_read_stat(iter, i)));
1747 }
1748
1749 for (i = 0; i < NR_LRU_LISTS; i++)
1750 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1751 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1752
1753 pr_cont("\n");
1754 }
Balbir Singhe2224322009-04-02 16:57:39 -07001755}
1756
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001757/*
1758 * This function returns the number of memcg under hierarchy tree. Returns
1759 * 1(self count) if no children.
1760 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001761static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001762{
1763 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001764 struct mem_cgroup *iter;
1765
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001766 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001767 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001768 return num;
1769}
1770
Balbir Singh6d61ef42009-01-07 18:08:06 -08001771/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001772 * Return the memory (and swap, if configured) limit for a memcg.
1773 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001774static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001775{
1776 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001777
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001778 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001779
David Rientjesa63d83f2010-08-09 17:19:46 -07001780 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001781 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001782 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001783 if (mem_cgroup_swappiness(memcg)) {
1784 u64 memsw;
1785
1786 limit += total_swap_pages << PAGE_SHIFT;
1787 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1788
1789 /*
1790 * If memsw is finite and limits the amount of swap space
1791 * available to this memcg, return that limit.
1792 */
1793 limit = min(limit, memsw);
1794 }
1795
1796 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001797}
1798
David Rientjes19965462012-12-11 16:00:26 -08001799static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1800 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001801{
1802 struct mem_cgroup *iter;
1803 unsigned long chosen_points = 0;
1804 unsigned long totalpages;
1805 unsigned int points = 0;
1806 struct task_struct *chosen = NULL;
1807
David Rientjes876aafb2012-07-31 16:43:48 -07001808 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001809 * If current has a pending SIGKILL or is exiting, then automatically
1810 * select it. The goal is to allow it to allocate so that it may
1811 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001812 */
David Rientjes465adcf2013-04-29 15:08:45 -07001813 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001814 set_thread_flag(TIF_MEMDIE);
1815 return;
1816 }
1817
1818 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001819 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1820 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001821 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001822 struct task_struct *task;
1823
Tejun Heo72ec7022013-08-08 20:11:26 -04001824 css_task_iter_start(&iter->css, &it);
1825 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001826 switch (oom_scan_process_thread(task, totalpages, NULL,
1827 false)) {
1828 case OOM_SCAN_SELECT:
1829 if (chosen)
1830 put_task_struct(chosen);
1831 chosen = task;
1832 chosen_points = ULONG_MAX;
1833 get_task_struct(chosen);
1834 /* fall through */
1835 case OOM_SCAN_CONTINUE:
1836 continue;
1837 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001838 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001839 mem_cgroup_iter_break(memcg, iter);
1840 if (chosen)
1841 put_task_struct(chosen);
1842 return;
1843 case OOM_SCAN_OK:
1844 break;
1845 };
1846 points = oom_badness(task, memcg, NULL, totalpages);
1847 if (points > chosen_points) {
1848 if (chosen)
1849 put_task_struct(chosen);
1850 chosen = task;
1851 chosen_points = points;
1852 get_task_struct(chosen);
1853 }
1854 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001855 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001856 }
1857
1858 if (!chosen)
1859 return;
1860 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001861 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1862 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001863}
1864
Johannes Weiner56600482012-01-12 17:17:59 -08001865static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1866 gfp_t gfp_mask,
1867 unsigned long flags)
1868{
1869 unsigned long total = 0;
1870 bool noswap = false;
1871 int loop;
1872
1873 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1874 noswap = true;
1875 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1876 noswap = true;
1877
1878 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1879 if (loop)
1880 drain_all_stock_async(memcg);
1881 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1882 /*
1883 * Allow limit shrinkers, which are triggered directly
1884 * by userspace, to catch signals and stop reclaim
1885 * after minimal progress, regardless of the margin.
1886 */
1887 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1888 break;
1889 if (mem_cgroup_margin(memcg))
1890 break;
1891 /*
1892 * If nothing was reclaimed after two attempts, there
1893 * may be no reclaimable pages in this hierarchy.
1894 */
1895 if (loop && !total)
1896 break;
1897 }
1898 return total;
1899}
1900
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001901/**
1902 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001903 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001904 * @nid: the node ID to be checked.
1905 * @noswap : specify true here if the user wants flle only information.
1906 *
1907 * This function returns whether the specified memcg contains any
1908 * reclaimable pages on a node. Returns true if there are any reclaimable
1909 * pages in the node.
1910 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001911static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001912 int nid, bool noswap)
1913{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001914 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001915 return true;
1916 if (noswap || !total_swap_pages)
1917 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001918 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001919 return true;
1920 return false;
1921
1922}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001923#if MAX_NUMNODES > 1
Ying Han889976d2011-05-26 16:25:33 -07001924
1925/*
1926 * Always updating the nodemask is not very good - even if we have an empty
1927 * list or the wrong list here, we can start from some node and traverse all
1928 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1929 *
1930 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001931static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001932{
1933 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001934 /*
1935 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1936 * pagein/pageout changes since the last update.
1937 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001938 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001939 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001940 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001941 return;
1942
Ying Han889976d2011-05-26 16:25:33 -07001943 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001944 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001945
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001946 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001947
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001948 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1949 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001950 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001951
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001952 atomic_set(&memcg->numainfo_events, 0);
1953 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001954}
1955
1956/*
1957 * Selecting a node where we start reclaim from. Because what we need is just
1958 * reducing usage counter, start from anywhere is O,K. Considering
1959 * memory reclaim from current node, there are pros. and cons.
1960 *
1961 * Freeing memory from current node means freeing memory from a node which
1962 * we'll use or we've used. So, it may make LRU bad. And if several threads
1963 * hit limits, it will see a contention on a node. But freeing from remote
1964 * node means more costs for memory reclaim because of memory latency.
1965 *
1966 * Now, we use round-robin. Better algorithm is welcomed.
1967 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001968int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001969{
1970 int node;
1971
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001972 mem_cgroup_may_update_nodemask(memcg);
1973 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001974
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001975 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001976 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001978 /*
1979 * We call this when we hit limit, not when pages are added to LRU.
1980 * No LRU may hold pages because all pages are UNEVICTABLE or
1981 * memcg is too small and all pages are not on LRU. In that case,
1982 * we use curret node.
1983 */
1984 if (unlikely(node == MAX_NUMNODES))
1985 node = numa_node_id();
1986
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001987 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001988 return node;
1989}
1990
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001991/*
1992 * Check all nodes whether it contains reclaimable pages or not.
1993 * For quick scan, we make use of scan_nodes. This will allow us to skip
1994 * unused nodes. But scan_nodes is lazily updated and may not cotain
1995 * enough new information. We need to do double check.
1996 */
1997static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1998{
1999 int nid;
2000
2001 /*
2002 * quick check...making use of scan_node.
2003 * We can skip unused nodes.
2004 */
2005 if (!nodes_empty(memcg->scan_nodes)) {
2006 for (nid = first_node(memcg->scan_nodes);
2007 nid < MAX_NUMNODES;
2008 nid = next_node(nid, memcg->scan_nodes)) {
2009
2010 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2011 return true;
2012 }
2013 }
2014 /*
2015 * Check rest of nodes.
2016 */
2017 for_each_node_state(nid, N_MEMORY) {
2018 if (node_isset(nid, memcg->scan_nodes))
2019 continue;
2020 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2021 return true;
2022 }
2023 return false;
2024}
2025
Ying Han889976d2011-05-26 16:25:33 -07002026#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002027int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07002028{
2029 return 0;
2030}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07002031
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07002032static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
2033{
2034 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
2035}
Ying Han889976d2011-05-26 16:25:33 -07002036#endif
2037
Andrew Morton0608f432013-09-24 15:27:41 -07002038static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
2039 struct zone *zone,
2040 gfp_t gfp_mask,
2041 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08002042{
Andrew Morton0608f432013-09-24 15:27:41 -07002043 struct mem_cgroup *victim = NULL;
2044 int total = 0;
2045 int loop = 0;
2046 unsigned long excess;
2047 unsigned long nr_scanned;
2048 struct mem_cgroup_reclaim_cookie reclaim = {
2049 .zone = zone,
2050 .priority = 0,
2051 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07002052
Andrew Morton0608f432013-09-24 15:27:41 -07002053 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002054
Andrew Morton0608f432013-09-24 15:27:41 -07002055 while (1) {
2056 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
2057 if (!victim) {
2058 loop++;
2059 if (loop >= 2) {
2060 /*
2061 * If we have not been able to reclaim
2062 * anything, it might because there are
2063 * no reclaimable pages under this hierarchy
2064 */
2065 if (!total)
2066 break;
2067 /*
2068 * We want to do more targeted reclaim.
2069 * excess >> 2 is not to excessive so as to
2070 * reclaim too much, nor too less that we keep
2071 * coming back to reclaim from this cgroup
2072 */
2073 if (total >= (excess >> 2) ||
2074 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
2075 break;
2076 }
2077 continue;
2078 }
2079 if (!mem_cgroup_reclaimable(victim, false))
2080 continue;
2081 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2082 zone, &nr_scanned);
2083 *total_scanned += nr_scanned;
2084 if (!res_counter_soft_limit_excess(&root_memcg->res))
2085 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002086 }
Andrew Morton0608f432013-09-24 15:27:41 -07002087 mem_cgroup_iter_break(root_memcg, victim);
2088 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002089}
2090
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002091#ifdef CONFIG_LOCKDEP
2092static struct lockdep_map memcg_oom_lock_dep_map = {
2093 .name = "memcg_oom_lock",
2094};
2095#endif
2096
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002097static DEFINE_SPINLOCK(memcg_oom_lock);
2098
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002099/*
2100 * Check OOM-Killer is already running under our hierarchy.
2101 * If someone is running, return false.
2102 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002103static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002104{
Michal Hocko79dfdac2011-07-26 16:08:23 -07002105 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002106
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002107 spin_lock(&memcg_oom_lock);
2108
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002109 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07002110 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002111 /*
2112 * this subtree of our hierarchy is already locked
2113 * so we cannot give a lock.
2114 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07002115 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002116 mem_cgroup_iter_break(memcg, iter);
2117 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07002118 } else
2119 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002120 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002121
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002122 if (failed) {
2123 /*
2124 * OK, we failed to lock the whole subtree so we have
2125 * to clean up what we set up to the failing subtree
2126 */
2127 for_each_mem_cgroup_tree(iter, memcg) {
2128 if (iter == failed) {
2129 mem_cgroup_iter_break(memcg, iter);
2130 break;
2131 }
2132 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002133 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002134 } else
2135 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002136
2137 spin_unlock(&memcg_oom_lock);
2138
2139 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002140}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002141
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002142static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002143{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002144 struct mem_cgroup *iter;
2145
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002146 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002147 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002148 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002149 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002150 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002151}
2152
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002153static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002154{
2155 struct mem_cgroup *iter;
2156
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002157 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002158 atomic_inc(&iter->under_oom);
2159}
2160
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002162{
2163 struct mem_cgroup *iter;
2164
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002165 /*
2166 * When a new child is created while the hierarchy is under oom,
2167 * mem_cgroup_oom_lock() may not be called. We have to use
2168 * atomic_add_unless() here.
2169 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002170 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002171 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002172}
2173
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002174static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2175
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002176struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002177 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002178 wait_queue_t wait;
2179};
2180
2181static int memcg_oom_wake_function(wait_queue_t *wait,
2182 unsigned mode, int sync, void *arg)
2183{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002184 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2185 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002186 struct oom_wait_info *oom_wait_info;
2187
2188 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002189 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002190
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002191 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002192 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002193 * Then we can use css_is_ancestor without taking care of RCU.
2194 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002195 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2196 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002197 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002198 return autoremove_wake_function(wait, mode, sync, arg);
2199}
2200
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002201static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002202{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002203 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002204 /* for filtering, pass "memcg" as argument. */
2205 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002206}
2207
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002208static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002209{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002210 if (memcg && atomic_read(&memcg->under_oom))
2211 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002212}
2213
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002214static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002215{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002216 if (!current->memcg_oom.may_oom)
2217 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002218 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002219 * We are in the middle of the charge context here, so we
2220 * don't want to block when potentially sitting on a callstack
2221 * that holds all kinds of filesystem and mm locks.
2222 *
2223 * Also, the caller may handle a failed allocation gracefully
2224 * (like optional page cache readahead) and so an OOM killer
2225 * invocation might not even be necessary.
2226 *
2227 * That's why we don't do anything here except remember the
2228 * OOM context and then deal with it at the end of the page
2229 * fault when the stack is unwound, the locks are released,
2230 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002231 */
Johannes Weiner49426422013-10-16 13:46:59 -07002232 css_get(&memcg->css);
2233 current->memcg_oom.memcg = memcg;
2234 current->memcg_oom.gfp_mask = mask;
2235 current->memcg_oom.order = order;
2236}
2237
2238/**
2239 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2240 * @handle: actually kill/wait or just clean up the OOM state
2241 *
2242 * This has to be called at the end of a page fault if the memcg OOM
2243 * handler was enabled.
2244 *
2245 * Memcg supports userspace OOM handling where failed allocations must
2246 * sleep on a waitqueue until the userspace task resolves the
2247 * situation. Sleeping directly in the charge context with all kinds
2248 * of locks held is not a good idea, instead we remember an OOM state
2249 * in the task and mem_cgroup_oom_synchronize() has to be called at
2250 * the end of the page fault to complete the OOM handling.
2251 *
2252 * Returns %true if an ongoing memcg OOM situation was detected and
2253 * completed, %false otherwise.
2254 */
2255bool mem_cgroup_oom_synchronize(bool handle)
2256{
2257 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2258 struct oom_wait_info owait;
2259 bool locked;
2260
2261 /* OOM is global, do not handle */
2262 if (!memcg)
2263 return false;
2264
2265 if (!handle)
2266 goto cleanup;
2267
2268 owait.memcg = memcg;
2269 owait.wait.flags = 0;
2270 owait.wait.func = memcg_oom_wake_function;
2271 owait.wait.private = current;
2272 INIT_LIST_HEAD(&owait.wait.task_list);
2273
2274 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002275 mem_cgroup_mark_under_oom(memcg);
2276
2277 locked = mem_cgroup_oom_trylock(memcg);
2278
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002279 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002280 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002281
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002282 if (locked && !memcg->oom_kill_disable) {
2283 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07002284 finish_wait(&memcg_oom_waitq, &owait.wait);
2285 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2286 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002287 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002288 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07002289 mem_cgroup_unmark_under_oom(memcg);
2290 finish_wait(&memcg_oom_waitq, &owait.wait);
2291 }
2292
2293 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002294 mem_cgroup_oom_unlock(memcg);
2295 /*
2296 * There is no guarantee that an OOM-lock contender
2297 * sees the wakeups triggered by the OOM kill
2298 * uncharges. Wake any sleepers explicitely.
2299 */
2300 memcg_oom_recover(memcg);
2301 }
Johannes Weiner49426422013-10-16 13:46:59 -07002302cleanup:
2303 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002304 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002305 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002306}
2307
Balbir Singhd69b0422009-06-17 16:26:34 -07002308/*
2309 * Currently used to update mapped file statistics, but the routine can be
2310 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002311 *
2312 * Notes: Race condition
2313 *
2314 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2315 * it tends to be costly. But considering some conditions, we doesn't need
2316 * to do so _always_.
2317 *
2318 * Considering "charge", lock_page_cgroup() is not required because all
2319 * file-stat operations happen after a page is attached to radix-tree. There
2320 * are no race with "charge".
2321 *
2322 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2323 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2324 * if there are race with "uncharge". Statistics itself is properly handled
2325 * by flags.
2326 *
2327 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002328 * small, we check mm->moving_account and detect there are possibility of race
2329 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002330 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002331
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002332void __mem_cgroup_begin_update_page_stat(struct page *page,
2333 bool *locked, unsigned long *flags)
2334{
2335 struct mem_cgroup *memcg;
2336 struct page_cgroup *pc;
2337
2338 pc = lookup_page_cgroup(page);
2339again:
2340 memcg = pc->mem_cgroup;
2341 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2342 return;
2343 /*
2344 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002345 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002346 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002347 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002348 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002349 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002350 return;
2351
2352 move_lock_mem_cgroup(memcg, flags);
2353 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2354 move_unlock_mem_cgroup(memcg, flags);
2355 goto again;
2356 }
2357 *locked = true;
2358}
2359
2360void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2361{
2362 struct page_cgroup *pc = lookup_page_cgroup(page);
2363
2364 /*
2365 * It's guaranteed that pc->mem_cgroup never changes while
2366 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002367 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002368 */
2369 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2370}
2371
Greg Thelen2a7106f2011-01-13 15:47:37 -08002372void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002373 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002374{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002375 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002376 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002377 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002378
Johannes Weinercfa44942012-01-12 17:18:38 -08002379 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002380 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002381
Sha Zhengju658b72c2013-09-12 15:13:52 -07002382 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002383 memcg = pc->mem_cgroup;
2384 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002385 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002388}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002389
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002390/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002391 * size of first charge trial. "32" comes from vmscan.c's magic value.
2392 * TODO: maybe necessary to use big numbers in big irons.
2393 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002394#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002395struct memcg_stock_pcp {
2396 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002397 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002398 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002399 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002400#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002401};
2402static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002403static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002404
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002405/**
2406 * consume_stock: Try to consume stocked charge on this cpu.
2407 * @memcg: memcg to consume from.
2408 * @nr_pages: how many pages to charge.
2409 *
2410 * The charges will only happen if @memcg matches the current cpu's memcg
2411 * stock, and at least @nr_pages are available in that stock. Failure to
2412 * service an allocation will refill the stock.
2413 *
2414 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002415 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002416static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002417{
2418 struct memcg_stock_pcp *stock;
2419 bool ret = true;
2420
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002421 if (nr_pages > CHARGE_BATCH)
2422 return false;
2423
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002424 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002425 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2426 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002427 else /* need to call res_counter_charge */
2428 ret = false;
2429 put_cpu_var(memcg_stock);
2430 return ret;
2431}
2432
2433/*
2434 * Returns stocks cached in percpu to res_counter and reset cached information.
2435 */
2436static void drain_stock(struct memcg_stock_pcp *stock)
2437{
2438 struct mem_cgroup *old = stock->cached;
2439
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002440 if (stock->nr_pages) {
2441 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2442
2443 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002444 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002445 res_counter_uncharge(&old->memsw, bytes);
2446 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002447 }
2448 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002449}
2450
2451/*
2452 * This must be called under preempt disabled or must be called by
2453 * a thread which is pinned to local cpu.
2454 */
2455static void drain_local_stock(struct work_struct *dummy)
2456{
2457 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2458 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002459 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002460}
2461
Michal Hockoe4777492013-02-22 16:35:40 -08002462static void __init memcg_stock_init(void)
2463{
2464 int cpu;
2465
2466 for_each_possible_cpu(cpu) {
2467 struct memcg_stock_pcp *stock =
2468 &per_cpu(memcg_stock, cpu);
2469 INIT_WORK(&stock->work, drain_local_stock);
2470 }
2471}
2472
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002473/*
2474 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002475 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002476 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002478{
2479 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2480
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002481 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002482 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002483 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002484 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002485 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002486 put_cpu_var(memcg_stock);
2487}
2488
2489/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002490 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002491 * of the hierarchy under it. sync flag says whether we should block
2492 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002493 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002494static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002495{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002496 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002497
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002498 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002499 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002500 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002501 for_each_online_cpu(cpu) {
2502 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002503 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002504
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002505 memcg = stock->cached;
2506 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002507 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002509 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002510 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2511 if (cpu == curcpu)
2512 drain_local_stock(&stock->work);
2513 else
2514 schedule_work_on(cpu, &stock->work);
2515 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002516 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002517 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002518
2519 if (!sync)
2520 goto out;
2521
2522 for_each_online_cpu(cpu) {
2523 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002524 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002525 flush_work(&stock->work);
2526 }
2527out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002528 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002529}
2530
2531/*
2532 * Tries to drain stocked charges in other cpus. This function is asynchronous
2533 * and just put a work per cpu for draining localy on each cpu. Caller can
2534 * expects some charges will be back to res_counter later but cannot wait for
2535 * it.
2536 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002537static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002538{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002539 /*
2540 * If someone calls draining, avoid adding more kworker runs.
2541 */
2542 if (!mutex_trylock(&percpu_charge_mutex))
2543 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002544 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002545 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002546}
2547
2548/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002549static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002550{
2551 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002552 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002553 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002554 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002555}
2556
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002557/*
2558 * This function drains percpu counter value from DEAD cpu and
2559 * move it to local cpu. Note that this function can be preempted.
2560 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002561static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002562{
2563 int i;
2564
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002565 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002566 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002567 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002568
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002569 per_cpu(memcg->stat->count[i], cpu) = 0;
2570 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002571 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002572 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002573 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002574
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002575 per_cpu(memcg->stat->events[i], cpu) = 0;
2576 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002577 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002578 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002579}
2580
Paul Gortmaker0db06282013-06-19 14:53:51 -04002581static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002582 unsigned long action,
2583 void *hcpu)
2584{
2585 int cpu = (unsigned long)hcpu;
2586 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002587 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002588
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002589 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002590 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002591
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002592 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002593 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002594
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002595 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002596 mem_cgroup_drain_pcp_counter(iter, cpu);
2597
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002598 stock = &per_cpu(memcg_stock, cpu);
2599 drain_stock(stock);
2600 return NOTIFY_OK;
2601}
2602
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002603
2604/* See __mem_cgroup_try_charge() for details */
2605enum {
2606 CHARGE_OK, /* success */
2607 CHARGE_RETRY, /* need to retry but retry is not bad */
2608 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2609 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002610};
2611
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002612static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002613 unsigned int nr_pages, unsigned int min_pages,
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002614 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002615{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002616 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002617 struct mem_cgroup *mem_over_limit;
2618 struct res_counter *fail_res;
2619 unsigned long flags = 0;
2620 int ret;
2621
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002622 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002623
2624 if (likely(!ret)) {
2625 if (!do_swap_account)
2626 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002627 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002628 if (likely(!ret))
2629 return CHARGE_OK;
2630
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002631 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002632 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2633 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2634 } else
2635 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002636 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002637 * Never reclaim on behalf of optional batching, retry with a
2638 * single page instead.
2639 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002640 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002641 return CHARGE_RETRY;
2642
2643 if (!(gfp_mask & __GFP_WAIT))
2644 return CHARGE_WOULDBLOCK;
2645
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002646 if (gfp_mask & __GFP_NORETRY)
2647 return CHARGE_NOMEM;
2648
Johannes Weiner56600482012-01-12 17:17:59 -08002649 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002650 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002651 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002652 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002653 * Even though the limit is exceeded at this point, reclaim
2654 * may have been able to free some pages. Retry the charge
2655 * before killing the task.
2656 *
2657 * Only for regular pages, though: huge pages are rather
2658 * unlikely to succeed so close to the limit, and we fall back
2659 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002660 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002661 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002662 return CHARGE_RETRY;
2663
2664 /*
2665 * At task move, charge accounts can be doubly counted. So, it's
2666 * better to wait until the end of task_move if something is going on.
2667 */
2668 if (mem_cgroup_wait_acct_move(mem_over_limit))
2669 return CHARGE_RETRY;
2670
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002671 if (invoke_oom)
2672 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002673
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002674 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002675}
2676
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002677/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002678 * __mem_cgroup_try_charge() does
2679 * 1. detect memcg to be charged against from passed *mm and *ptr,
2680 * 2. update res_counter
2681 * 3. call memory reclaim if necessary.
2682 *
2683 * In some special case, if the task is fatal, fatal_signal_pending() or
2684 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2685 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2686 * as possible without any hazards. 2: all pages should have a valid
2687 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2688 * pointer, that is treated as a charge to root_mem_cgroup.
2689 *
2690 * So __mem_cgroup_try_charge() will return
2691 * 0 ... on success, filling *ptr with a valid memcg pointer.
2692 * -ENOMEM ... charge failure because of resource limits.
2693 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2694 *
2695 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2696 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002697 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002698static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002699 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002700 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002701 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002702 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002703{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002704 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002705 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002706 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002707 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002708
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002709 /*
2710 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2711 * in system level. So, allow to go ahead dying process in addition to
2712 * MEMDIE process.
2713 */
2714 if (unlikely(test_thread_flag(TIF_MEMDIE)
2715 || fatal_signal_pending(current)))
2716 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002717
Johannes Weiner49426422013-10-16 13:46:59 -07002718 if (unlikely(task_in_memcg_oom(current)))
2719 goto bypass;
2720
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002721 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002722 * We always charge the cgroup the mm_struct belongs to.
2723 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002724 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002725 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002726 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002727 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002728 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002729again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002730 if (*ptr) { /* css should be a valid one */
2731 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002732 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002733 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002734 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002735 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002736 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002737 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002738 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002739
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002740 rcu_read_lock();
2741 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002742 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002743 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002744 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002745 * race with swapoff. Then, we have small risk of mis-accouning.
2746 * But such kind of mis-account by race always happens because
2747 * we don't have cgroup_mutex(). It's overkill and we allo that
2748 * small race, here.
2749 * (*) swapoff at el will charge against mm-struct not against
2750 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002751 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002752 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002753 if (!memcg)
2754 memcg = root_mem_cgroup;
2755 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002756 rcu_read_unlock();
2757 goto done;
2758 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002759 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002760 /*
2761 * It seems dagerous to access memcg without css_get().
2762 * But considering how consume_stok works, it's not
2763 * necessary. If consume_stock success, some charges
2764 * from this memcg are cached on this cpu. So, we
2765 * don't need to call css_get()/css_tryget() before
2766 * calling consume_stock().
2767 */
2768 rcu_read_unlock();
2769 goto done;
2770 }
2771 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002772 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002773 rcu_read_unlock();
2774 goto again;
2775 }
2776 rcu_read_unlock();
2777 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002778
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002779 do {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002780 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002781
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002782 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002783 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002784 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002785 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002786 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002787
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002788 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2789 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002790 switch (ret) {
2791 case CHARGE_OK:
2792 break;
2793 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002794 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002795 css_put(&memcg->css);
2796 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002797 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002798 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002799 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002800 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002801 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002802 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002803 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002804 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002805 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002806 nr_oom_retries--;
2807 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002808 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002809 } while (ret != CHARGE_OK);
2810
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002811 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002812 refill_stock(memcg, batch - nr_pages);
2813 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002814done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002815 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002816 return 0;
2817nomem:
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002818 if (!(gfp_mask & __GFP_NOFAIL)) {
2819 *ptr = NULL;
2820 return -ENOMEM;
2821 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002822bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002823 *ptr = root_mem_cgroup;
2824 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002825}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002826
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002827/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002828 * Somemtimes we have to undo a charge we got by try_charge().
2829 * This function is for that and do uncharge, put css's refcnt.
2830 * gotten by try_charge().
2831 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002832static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002833 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002834{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002835 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002836 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002837
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002838 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002839 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002840 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002841 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002842}
2843
2844/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002845 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2846 * This is useful when moving usage to parent cgroup.
2847 */
2848static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2849 unsigned int nr_pages)
2850{
2851 unsigned long bytes = nr_pages * PAGE_SIZE;
2852
2853 if (mem_cgroup_is_root(memcg))
2854 return;
2855
2856 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2857 if (do_swap_account)
2858 res_counter_uncharge_until(&memcg->memsw,
2859 memcg->memsw.parent, bytes);
2860}
2861
2862/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002863 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002864 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2865 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2866 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002867 */
2868static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2869{
2870 struct cgroup_subsys_state *css;
2871
2872 /* ID 0 is unused ID */
2873 if (!id)
2874 return NULL;
2875 css = css_lookup(&mem_cgroup_subsys, id);
2876 if (!css)
2877 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002878 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002879}
2880
Wu Fengguange42d9d52009-12-16 12:19:59 +01002881struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002882{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002883 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002884 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002885 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002886 swp_entry_t ent;
2887
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002888 VM_BUG_ON(!PageLocked(page));
2889
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002890 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002891 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002892 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002893 memcg = pc->mem_cgroup;
2894 if (memcg && !css_tryget(&memcg->css))
2895 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002896 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002897 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002898 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002899 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002900 memcg = mem_cgroup_lookup(id);
2901 if (memcg && !css_tryget(&memcg->css))
2902 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002903 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002904 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002905 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002906 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002907}
2908
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002909static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002910 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002911 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002912 enum charge_type ctype,
2913 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002914{
Johannes Weinerce587e62012-04-24 20:22:33 +02002915 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002916 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002917 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002918 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002919 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002920
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002921 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002922 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002923 /*
2924 * we don't need page_cgroup_lock about tail pages, becase they are not
2925 * accessed by any other context at this point.
2926 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002927
2928 /*
2929 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2930 * may already be on some other mem_cgroup's LRU. Take care of it.
2931 */
2932 if (lrucare) {
2933 zone = page_zone(page);
2934 spin_lock_irq(&zone->lru_lock);
2935 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002936 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002937 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002938 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002939 was_on_lru = true;
2940 }
2941 }
2942
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002943 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002944 /*
2945 * We access a page_cgroup asynchronously without lock_page_cgroup().
2946 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2947 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2948 * before USED bit, we need memory barrier here.
2949 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002950 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002951 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002952 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002953
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002954 if (lrucare) {
2955 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002956 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002957 VM_BUG_ON(PageLRU(page));
2958 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002959 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002960 }
2961 spin_unlock_irq(&zone->lru_lock);
2962 }
2963
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002964 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002965 anon = true;
2966 else
2967 anon = false;
2968
David Rientjesb070e652013-05-07 16:18:09 -07002969 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002970 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002971
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002972 /*
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07002973 * "charge_statistics" updated event counter. Then, check it.
2974 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2975 * if they exceeds softlimit.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002976 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002977 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002978}
2979
Glauber Costa7cf27982012-12-18 14:22:55 -08002980static DEFINE_MUTEX(set_limit_mutex);
2981
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002982#ifdef CONFIG_MEMCG_KMEM
2983static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2984{
2985 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2986 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2987}
2988
Glauber Costa1f458cb2012-12-18 14:22:50 -08002989/*
2990 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2991 * in the memcg_cache_params struct.
2992 */
2993static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2994{
2995 struct kmem_cache *cachep;
2996
2997 VM_BUG_ON(p->is_root_cache);
2998 cachep = p->root_cache;
2999 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
3000}
3001
Glauber Costa749c5412012-12-18 14:23:01 -08003002#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04003003static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
3004 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08003005{
Tejun Heo182446d2013-08-08 20:11:24 -04003006 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08003007 struct memcg_cache_params *params;
3008
3009 if (!memcg_can_account_kmem(memcg))
3010 return -EIO;
3011
3012 print_slabinfo_header(m);
3013
3014 mutex_lock(&memcg->slab_caches_mutex);
3015 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
3016 cache_show(memcg_params_to_cache(params), m);
3017 mutex_unlock(&memcg->slab_caches_mutex);
3018
3019 return 0;
3020}
3021#endif
3022
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003023static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
3024{
3025 struct res_counter *fail_res;
3026 struct mem_cgroup *_memcg;
3027 int ret = 0;
3028 bool may_oom;
3029
3030 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
3031 if (ret)
3032 return ret;
3033
3034 /*
3035 * Conditions under which we can wait for the oom_killer. Those are
3036 * the same conditions tested by the core page allocator
3037 */
3038 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
3039
3040 _memcg = memcg;
3041 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
3042 &_memcg, may_oom);
3043
3044 if (ret == -EINTR) {
3045 /*
3046 * __mem_cgroup_try_charge() chosed to bypass to root due to
3047 * OOM kill or fatal signal. Since our only options are to
3048 * either fail the allocation or charge it to this cgroup, do
3049 * it as a temporary condition. But we can't fail. From a
3050 * kmem/slab perspective, the cache has already been selected,
3051 * by mem_cgroup_kmem_get_cache(), so it is too late to change
3052 * our minds.
3053 *
3054 * This condition will only trigger if the task entered
3055 * memcg_charge_kmem in a sane state, but was OOM-killed during
3056 * __mem_cgroup_try_charge() above. Tasks that were already
3057 * dying when the allocation triggers should have been already
3058 * directed to the root cgroup in memcontrol.h
3059 */
3060 res_counter_charge_nofail(&memcg->res, size, &fail_res);
3061 if (do_swap_account)
3062 res_counter_charge_nofail(&memcg->memsw, size,
3063 &fail_res);
3064 ret = 0;
3065 } else if (ret)
3066 res_counter_uncharge(&memcg->kmem, size);
3067
3068 return ret;
3069}
3070
3071static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
3072{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003073 res_counter_uncharge(&memcg->res, size);
3074 if (do_swap_account)
3075 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08003076
3077 /* Not down to 0 */
3078 if (res_counter_uncharge(&memcg->kmem, size))
3079 return;
3080
Li Zefan10d5ebf2013-07-08 16:00:33 -07003081 /*
3082 * Releases a reference taken in kmem_cgroup_css_offline in case
3083 * this last uncharge is racing with the offlining code or it is
3084 * outliving the memcg existence.
3085 *
3086 * The memory barrier imposed by test&clear is paired with the
3087 * explicit one in memcg_kmem_mark_dead().
3088 */
Glauber Costa7de37682012-12-18 14:22:07 -08003089 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07003090 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003091}
3092
Glauber Costa2633d7a2012-12-18 14:22:34 -08003093void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
3094{
3095 if (!memcg)
3096 return;
3097
3098 mutex_lock(&memcg->slab_caches_mutex);
3099 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
3100 mutex_unlock(&memcg->slab_caches_mutex);
3101}
3102
3103/*
3104 * helper for acessing a memcg's index. It will be used as an index in the
3105 * child cache array in kmem_cache, and also to derive its name. This function
3106 * will return -1 when this is not a kmem-limited memcg.
3107 */
3108int memcg_cache_id(struct mem_cgroup *memcg)
3109{
3110 return memcg ? memcg->kmemcg_id : -1;
3111}
3112
Glauber Costa55007d82012-12-18 14:22:38 -08003113/*
3114 * This ends up being protected by the set_limit mutex, during normal
3115 * operation, because that is its main call site.
3116 *
3117 * But when we create a new cache, we can call this as well if its parent
3118 * is kmem-limited. That will have to hold set_limit_mutex as well.
3119 */
3120int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3121{
3122 int num, ret;
3123
3124 num = ida_simple_get(&kmem_limited_groups,
3125 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3126 if (num < 0)
3127 return num;
3128 /*
3129 * After this point, kmem_accounted (that we test atomically in
3130 * the beginning of this conditional), is no longer 0. This
3131 * guarantees only one process will set the following boolean
3132 * to true. We don't need test_and_set because we're protected
3133 * by the set_limit_mutex anyway.
3134 */
3135 memcg_kmem_set_activated(memcg);
3136
3137 ret = memcg_update_all_caches(num+1);
3138 if (ret) {
3139 ida_simple_remove(&kmem_limited_groups, num);
3140 memcg_kmem_clear_activated(memcg);
3141 return ret;
3142 }
3143
3144 memcg->kmemcg_id = num;
3145 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3146 mutex_init(&memcg->slab_caches_mutex);
3147 return 0;
3148}
3149
3150static size_t memcg_caches_array_size(int num_groups)
3151{
3152 ssize_t size;
3153 if (num_groups <= 0)
3154 return 0;
3155
3156 size = 2 * num_groups;
3157 if (size < MEMCG_CACHES_MIN_SIZE)
3158 size = MEMCG_CACHES_MIN_SIZE;
3159 else if (size > MEMCG_CACHES_MAX_SIZE)
3160 size = MEMCG_CACHES_MAX_SIZE;
3161
3162 return size;
3163}
3164
3165/*
3166 * We should update the current array size iff all caches updates succeed. This
3167 * can only be done from the slab side. The slab mutex needs to be held when
3168 * calling this.
3169 */
3170void memcg_update_array_size(int num)
3171{
3172 if (num > memcg_limited_groups_array_size)
3173 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3174}
3175
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003176static void kmem_cache_destroy_work_func(struct work_struct *w);
3177
Glauber Costa55007d82012-12-18 14:22:38 -08003178int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3179{
3180 struct memcg_cache_params *cur_params = s->memcg_params;
3181
3182 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3183
3184 if (num_groups > memcg_limited_groups_array_size) {
3185 int i;
3186 ssize_t size = memcg_caches_array_size(num_groups);
3187
3188 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003189 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003190
3191 s->memcg_params = kzalloc(size, GFP_KERNEL);
3192 if (!s->memcg_params) {
3193 s->memcg_params = cur_params;
3194 return -ENOMEM;
3195 }
3196
3197 s->memcg_params->is_root_cache = true;
3198
3199 /*
3200 * There is the chance it will be bigger than
3201 * memcg_limited_groups_array_size, if we failed an allocation
3202 * in a cache, in which case all caches updated before it, will
3203 * have a bigger array.
3204 *
3205 * But if that is the case, the data after
3206 * memcg_limited_groups_array_size is certainly unused
3207 */
3208 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3209 if (!cur_params->memcg_caches[i])
3210 continue;
3211 s->memcg_params->memcg_caches[i] =
3212 cur_params->memcg_caches[i];
3213 }
3214
3215 /*
3216 * Ideally, we would wait until all caches succeed, and only
3217 * then free the old one. But this is not worth the extra
3218 * pointer per-cache we'd have to have for this.
3219 *
3220 * It is not a big deal if some caches are left with a size
3221 * bigger than the others. And all updates will reset this
3222 * anyway.
3223 */
3224 kfree(cur_params);
3225 }
3226 return 0;
3227}
3228
Glauber Costa943a4512012-12-18 14:23:03 -08003229int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3230 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003231{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003232 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003233
3234 if (!memcg_kmem_enabled())
3235 return 0;
3236
Andrey Vagin90c7a792013-09-11 14:22:18 -07003237 if (!memcg) {
3238 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003239 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003240 } else
3241 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003242
Glauber Costa2633d7a2012-12-18 14:22:34 -08003243 s->memcg_params = kzalloc(size, GFP_KERNEL);
3244 if (!s->memcg_params)
3245 return -ENOMEM;
3246
Glauber Costa943a4512012-12-18 14:23:03 -08003247 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003248 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003249 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003250 INIT_WORK(&s->memcg_params->destroy,
3251 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003252 } else
3253 s->memcg_params->is_root_cache = true;
3254
Glauber Costa2633d7a2012-12-18 14:22:34 -08003255 return 0;
3256}
3257
3258void memcg_release_cache(struct kmem_cache *s)
3259{
Glauber Costad7f25f82012-12-18 14:22:40 -08003260 struct kmem_cache *root;
3261 struct mem_cgroup *memcg;
3262 int id;
3263
3264 /*
3265 * This happens, for instance, when a root cache goes away before we
3266 * add any memcg.
3267 */
3268 if (!s->memcg_params)
3269 return;
3270
3271 if (s->memcg_params->is_root_cache)
3272 goto out;
3273
3274 memcg = s->memcg_params->memcg;
3275 id = memcg_cache_id(memcg);
3276
3277 root = s->memcg_params->root_cache;
3278 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003279
3280 mutex_lock(&memcg->slab_caches_mutex);
3281 list_del(&s->memcg_params->list);
3282 mutex_unlock(&memcg->slab_caches_mutex);
3283
Li Zefan20f05312013-07-08 16:00:31 -07003284 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003285out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003286 kfree(s->memcg_params);
3287}
3288
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003289/*
3290 * During the creation a new cache, we need to disable our accounting mechanism
3291 * altogether. This is true even if we are not creating, but rather just
3292 * enqueing new caches to be created.
3293 *
3294 * This is because that process will trigger allocations; some visible, like
3295 * explicit kmallocs to auxiliary data structures, name strings and internal
3296 * cache structures; some well concealed, like INIT_WORK() that can allocate
3297 * objects during debug.
3298 *
3299 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3300 * to it. This may not be a bounded recursion: since the first cache creation
3301 * failed to complete (waiting on the allocation), we'll just try to create the
3302 * cache again, failing at the same point.
3303 *
3304 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3305 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3306 * inside the following two functions.
3307 */
3308static inline void memcg_stop_kmem_account(void)
3309{
3310 VM_BUG_ON(!current->mm);
3311 current->memcg_kmem_skip_account++;
3312}
3313
3314static inline void memcg_resume_kmem_account(void)
3315{
3316 VM_BUG_ON(!current->mm);
3317 current->memcg_kmem_skip_account--;
3318}
3319
Glauber Costa1f458cb2012-12-18 14:22:50 -08003320static void kmem_cache_destroy_work_func(struct work_struct *w)
3321{
3322 struct kmem_cache *cachep;
3323 struct memcg_cache_params *p;
3324
3325 p = container_of(w, struct memcg_cache_params, destroy);
3326
3327 cachep = memcg_params_to_cache(p);
3328
Glauber Costa22933152012-12-18 14:22:59 -08003329 /*
3330 * If we get down to 0 after shrink, we could delete right away.
3331 * However, memcg_release_pages() already puts us back in the workqueue
3332 * in that case. If we proceed deleting, we'll get a dangling
3333 * reference, and removing the object from the workqueue in that case
3334 * is unnecessary complication. We are not a fast path.
3335 *
3336 * Note that this case is fundamentally different from racing with
3337 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3338 * kmem_cache_shrink, not only we would be reinserting a dead cache
3339 * into the queue, but doing so from inside the worker racing to
3340 * destroy it.
3341 *
3342 * So if we aren't down to zero, we'll just schedule a worker and try
3343 * again
3344 */
3345 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3346 kmem_cache_shrink(cachep);
3347 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3348 return;
3349 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003350 kmem_cache_destroy(cachep);
3351}
3352
3353void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3354{
3355 if (!cachep->memcg_params->dead)
3356 return;
3357
3358 /*
Glauber Costa22933152012-12-18 14:22:59 -08003359 * There are many ways in which we can get here.
3360 *
3361 * We can get to a memory-pressure situation while the delayed work is
3362 * still pending to run. The vmscan shrinkers can then release all
3363 * cache memory and get us to destruction. If this is the case, we'll
3364 * be executed twice, which is a bug (the second time will execute over
3365 * bogus data). In this case, cancelling the work should be fine.
3366 *
3367 * But we can also get here from the worker itself, if
3368 * kmem_cache_shrink is enough to shake all the remaining objects and
3369 * get the page count to 0. In this case, we'll deadlock if we try to
3370 * cancel the work (the worker runs with an internal lock held, which
3371 * is the same lock we would hold for cancel_work_sync().)
3372 *
3373 * Since we can't possibly know who got us here, just refrain from
3374 * running if there is already work pending
3375 */
3376 if (work_pending(&cachep->memcg_params->destroy))
3377 return;
3378 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003379 * We have to defer the actual destroying to a workqueue, because
3380 * we might currently be in a context that cannot sleep.
3381 */
3382 schedule_work(&cachep->memcg_params->destroy);
3383}
3384
Glauber Costad7f25f82012-12-18 14:22:40 -08003385/*
3386 * This lock protects updaters, not readers. We want readers to be as fast as
3387 * they can, and they will either see NULL or a valid cache value. Our model
3388 * allow them to see NULL, in which case the root memcg will be selected.
3389 *
3390 * We need this lock because multiple allocations to the same cache from a non
3391 * will span more than one worker. Only one of them can create the cache.
3392 */
3393static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003394
3395/*
3396 * Called with memcg_cache_mutex held
3397 */
3398static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3399 struct kmem_cache *s)
3400{
3401 struct kmem_cache *new;
3402 static char *tmp_name = NULL;
3403
3404 lockdep_assert_held(&memcg_cache_mutex);
3405
3406 /*
3407 * kmem_cache_create_memcg duplicates the given name and
3408 * cgroup_name for this name requires RCU context.
3409 * This static temporary buffer is used to prevent from
3410 * pointless shortliving allocation.
3411 */
3412 if (!tmp_name) {
3413 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3414 if (!tmp_name)
3415 return NULL;
3416 }
3417
3418 rcu_read_lock();
3419 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3420 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3421 rcu_read_unlock();
3422
3423 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3424 (s->flags & ~SLAB_PANIC), s->ctor, s);
3425
3426 if (new)
3427 new->allocflags |= __GFP_KMEMCG;
3428
3429 return new;
3430}
3431
Glauber Costad7f25f82012-12-18 14:22:40 -08003432static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3433 struct kmem_cache *cachep)
3434{
3435 struct kmem_cache *new_cachep;
3436 int idx;
3437
3438 BUG_ON(!memcg_can_account_kmem(memcg));
3439
3440 idx = memcg_cache_id(memcg);
3441
3442 mutex_lock(&memcg_cache_mutex);
3443 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003444 if (new_cachep) {
3445 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003446 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003447 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003448
3449 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003450 if (new_cachep == NULL) {
3451 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003452 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003453 goto out;
3454 }
3455
Glauber Costa1f458cb2012-12-18 14:22:50 -08003456 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003457
3458 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3459 /*
3460 * the readers won't lock, make sure everybody sees the updated value,
3461 * so they won't put stuff in the queue again for no reason
3462 */
3463 wmb();
3464out:
3465 mutex_unlock(&memcg_cache_mutex);
3466 return new_cachep;
3467}
3468
Glauber Costa7cf27982012-12-18 14:22:55 -08003469void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3470{
3471 struct kmem_cache *c;
3472 int i;
3473
3474 if (!s->memcg_params)
3475 return;
3476 if (!s->memcg_params->is_root_cache)
3477 return;
3478
3479 /*
3480 * If the cache is being destroyed, we trust that there is no one else
3481 * requesting objects from it. Even if there are, the sanity checks in
3482 * kmem_cache_destroy should caught this ill-case.
3483 *
3484 * Still, we don't want anyone else freeing memcg_caches under our
3485 * noses, which can happen if a new memcg comes to life. As usual,
3486 * we'll take the set_limit_mutex to protect ourselves against this.
3487 */
3488 mutex_lock(&set_limit_mutex);
3489 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3490 c = s->memcg_params->memcg_caches[i];
3491 if (!c)
3492 continue;
3493
3494 /*
3495 * We will now manually delete the caches, so to avoid races
3496 * we need to cancel all pending destruction workers and
3497 * proceed with destruction ourselves.
3498 *
3499 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3500 * and that could spawn the workers again: it is likely that
3501 * the cache still have active pages until this very moment.
3502 * This would lead us back to mem_cgroup_destroy_cache.
3503 *
3504 * But that will not execute at all if the "dead" flag is not
3505 * set, so flip it down to guarantee we are in control.
3506 */
3507 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003508 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003509 kmem_cache_destroy(c);
3510 }
3511 mutex_unlock(&set_limit_mutex);
3512}
3513
Glauber Costad7f25f82012-12-18 14:22:40 -08003514struct create_work {
3515 struct mem_cgroup *memcg;
3516 struct kmem_cache *cachep;
3517 struct work_struct work;
3518};
3519
Glauber Costa1f458cb2012-12-18 14:22:50 -08003520static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3521{
3522 struct kmem_cache *cachep;
3523 struct memcg_cache_params *params;
3524
3525 if (!memcg_kmem_is_active(memcg))
3526 return;
3527
3528 mutex_lock(&memcg->slab_caches_mutex);
3529 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3530 cachep = memcg_params_to_cache(params);
3531 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003532 schedule_work(&cachep->memcg_params->destroy);
3533 }
3534 mutex_unlock(&memcg->slab_caches_mutex);
3535}
3536
Glauber Costad7f25f82012-12-18 14:22:40 -08003537static void memcg_create_cache_work_func(struct work_struct *w)
3538{
3539 struct create_work *cw;
3540
3541 cw = container_of(w, struct create_work, work);
3542 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003543 kfree(cw);
3544}
3545
3546/*
3547 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003548 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003549static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3550 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003551{
3552 struct create_work *cw;
3553
3554 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003555 if (cw == NULL) {
3556 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003557 return;
3558 }
3559
3560 cw->memcg = memcg;
3561 cw->cachep = cachep;
3562
3563 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3564 schedule_work(&cw->work);
3565}
3566
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003567static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3568 struct kmem_cache *cachep)
3569{
3570 /*
3571 * We need to stop accounting when we kmalloc, because if the
3572 * corresponding kmalloc cache is not yet created, the first allocation
3573 * in __memcg_create_cache_enqueue will recurse.
3574 *
3575 * However, it is better to enclose the whole function. Depending on
3576 * the debugging options enabled, INIT_WORK(), for instance, can
3577 * trigger an allocation. This too, will make us recurse. Because at
3578 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3579 * the safest choice is to do it like this, wrapping the whole function.
3580 */
3581 memcg_stop_kmem_account();
3582 __memcg_create_cache_enqueue(memcg, cachep);
3583 memcg_resume_kmem_account();
3584}
Glauber Costad7f25f82012-12-18 14:22:40 -08003585/*
3586 * Return the kmem_cache we're supposed to use for a slab allocation.
3587 * We try to use the current memcg's version of the cache.
3588 *
3589 * If the cache does not exist yet, if we are the first user of it,
3590 * we either create it immediately, if possible, or create it asynchronously
3591 * in a workqueue.
3592 * In the latter case, we will let the current allocation go through with
3593 * the original cache.
3594 *
3595 * Can't be called in interrupt context or from kernel threads.
3596 * This function needs to be called with rcu_read_lock() held.
3597 */
3598struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3599 gfp_t gfp)
3600{
3601 struct mem_cgroup *memcg;
3602 int idx;
3603
3604 VM_BUG_ON(!cachep->memcg_params);
3605 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3606
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003607 if (!current->mm || current->memcg_kmem_skip_account)
3608 return cachep;
3609
Glauber Costad7f25f82012-12-18 14:22:40 -08003610 rcu_read_lock();
3611 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003612
3613 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003614 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003615
3616 idx = memcg_cache_id(memcg);
3617
3618 /*
3619 * barrier to mare sure we're always seeing the up to date value. The
3620 * code updating memcg_caches will issue a write barrier to match this.
3621 */
3622 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003623 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3624 cachep = cachep->memcg_params->memcg_caches[idx];
3625 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003626 }
3627
Li Zefanca0dde92013-04-29 15:08:57 -07003628 /* The corresponding put will be done in the workqueue. */
3629 if (!css_tryget(&memcg->css))
3630 goto out;
3631 rcu_read_unlock();
3632
3633 /*
3634 * If we are in a safe context (can wait, and not in interrupt
3635 * context), we could be be predictable and return right away.
3636 * This would guarantee that the allocation being performed
3637 * already belongs in the new cache.
3638 *
3639 * However, there are some clashes that can arrive from locking.
3640 * For instance, because we acquire the slab_mutex while doing
3641 * kmem_cache_dup, this means no further allocation could happen
3642 * with the slab_mutex held.
3643 *
3644 * Also, because cache creation issue get_online_cpus(), this
3645 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3646 * that ends up reversed during cpu hotplug. (cpuset allocates
3647 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3648 * better to defer everything.
3649 */
3650 memcg_create_cache_enqueue(memcg, cachep);
3651 return cachep;
3652out:
3653 rcu_read_unlock();
3654 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003655}
3656EXPORT_SYMBOL(__memcg_kmem_get_cache);
3657
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003658/*
3659 * We need to verify if the allocation against current->mm->owner's memcg is
3660 * possible for the given order. But the page is not allocated yet, so we'll
3661 * need a further commit step to do the final arrangements.
3662 *
3663 * It is possible for the task to switch cgroups in this mean time, so at
3664 * commit time, we can't rely on task conversion any longer. We'll then use
3665 * the handle argument to return to the caller which cgroup we should commit
3666 * against. We could also return the memcg directly and avoid the pointer
3667 * passing, but a boolean return value gives better semantics considering
3668 * the compiled-out case as well.
3669 *
3670 * Returning true means the allocation is possible.
3671 */
3672bool
3673__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3674{
3675 struct mem_cgroup *memcg;
3676 int ret;
3677
3678 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003679
3680 /*
3681 * Disabling accounting is only relevant for some specific memcg
3682 * internal allocations. Therefore we would initially not have such
3683 * check here, since direct calls to the page allocator that are marked
3684 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3685 * concerned with cache allocations, and by having this test at
3686 * memcg_kmem_get_cache, we are already able to relay the allocation to
3687 * the root cache and bypass the memcg cache altogether.
3688 *
3689 * There is one exception, though: the SLUB allocator does not create
3690 * large order caches, but rather service large kmallocs directly from
3691 * the page allocator. Therefore, the following sequence when backed by
3692 * the SLUB allocator:
3693 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003694 * memcg_stop_kmem_account();
3695 * kmalloc(<large_number>)
3696 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003697 *
3698 * would effectively ignore the fact that we should skip accounting,
3699 * since it will drive us directly to this function without passing
3700 * through the cache selector memcg_kmem_get_cache. Such large
3701 * allocations are extremely rare but can happen, for instance, for the
3702 * cache arrays. We bring this test here.
3703 */
3704 if (!current->mm || current->memcg_kmem_skip_account)
3705 return true;
3706
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003707 memcg = try_get_mem_cgroup_from_mm(current->mm);
3708
3709 /*
3710 * very rare case described in mem_cgroup_from_task. Unfortunately there
3711 * isn't much we can do without complicating this too much, and it would
3712 * be gfp-dependent anyway. Just let it go
3713 */
3714 if (unlikely(!memcg))
3715 return true;
3716
3717 if (!memcg_can_account_kmem(memcg)) {
3718 css_put(&memcg->css);
3719 return true;
3720 }
3721
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003722 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3723 if (!ret)
3724 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003725
3726 css_put(&memcg->css);
3727 return (ret == 0);
3728}
3729
3730void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3731 int order)
3732{
3733 struct page_cgroup *pc;
3734
3735 VM_BUG_ON(mem_cgroup_is_root(memcg));
3736
3737 /* The page allocation failed. Revert */
3738 if (!page) {
3739 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003740 return;
3741 }
3742
3743 pc = lookup_page_cgroup(page);
3744 lock_page_cgroup(pc);
3745 pc->mem_cgroup = memcg;
3746 SetPageCgroupUsed(pc);
3747 unlock_page_cgroup(pc);
3748}
3749
3750void __memcg_kmem_uncharge_pages(struct page *page, int order)
3751{
3752 struct mem_cgroup *memcg = NULL;
3753 struct page_cgroup *pc;
3754
3755
3756 pc = lookup_page_cgroup(page);
3757 /*
3758 * Fast unlocked return. Theoretically might have changed, have to
3759 * check again after locking.
3760 */
3761 if (!PageCgroupUsed(pc))
3762 return;
3763
3764 lock_page_cgroup(pc);
3765 if (PageCgroupUsed(pc)) {
3766 memcg = pc->mem_cgroup;
3767 ClearPageCgroupUsed(pc);
3768 }
3769 unlock_page_cgroup(pc);
3770
3771 /*
3772 * We trust that only if there is a memcg associated with the page, it
3773 * is a valid allocation
3774 */
3775 if (!memcg)
3776 return;
3777
3778 VM_BUG_ON(mem_cgroup_is_root(memcg));
3779 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003780}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003781#else
3782static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3783{
3784}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003785#endif /* CONFIG_MEMCG_KMEM */
3786
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003787#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3788
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003789#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003790/*
3791 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003792 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3793 * charge/uncharge will be never happen and move_account() is done under
3794 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003795 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003796void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003797{
3798 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003799 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003800 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003801 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003802
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003803 if (mem_cgroup_disabled())
3804 return;
David Rientjesb070e652013-05-07 16:18:09 -07003805
3806 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003807 for (i = 1; i < HPAGE_PMD_NR; i++) {
3808 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003809 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003810 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003811 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3812 }
David Rientjesb070e652013-05-07 16:18:09 -07003813 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3814 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003815}
Hugh Dickins12d27102012-01-12 17:19:52 -08003816#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003817
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003818static inline
3819void mem_cgroup_move_account_page_stat(struct mem_cgroup *from,
3820 struct mem_cgroup *to,
3821 unsigned int nr_pages,
3822 enum mem_cgroup_stat_index idx)
3823{
3824 /* Update stat data for mem_cgroup */
3825 preempt_disable();
Greg Thelen5e8cfc32013-10-30 13:56:21 -07003826 __this_cpu_sub(from->stat->count[idx], nr_pages);
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003827 __this_cpu_add(to->stat->count[idx], nr_pages);
3828 preempt_enable();
3829}
3830
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003832 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003833 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003834 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003835 * @pc: page_cgroup of the page.
3836 * @from: mem_cgroup which the page is moved from.
3837 * @to: mem_cgroup which the page is moved to. @from != @to.
3838 *
3839 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003840 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003841 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003842 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003843 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3844 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003845 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003846static int mem_cgroup_move_account(struct page *page,
3847 unsigned int nr_pages,
3848 struct page_cgroup *pc,
3849 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003850 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003851{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003852 unsigned long flags;
3853 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003854 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003855
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003856 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003857 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003858 /*
3859 * The page is isolated from LRU. So, collapse function
3860 * will not handle this page. But page splitting can happen.
3861 * Do this check under compound_page_lock(). The caller should
3862 * hold it.
3863 */
3864 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003865 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003866 goto out;
3867
3868 lock_page_cgroup(pc);
3869
3870 ret = -EINVAL;
3871 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3872 goto unlock;
3873
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003874 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003875
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003876 if (!anon && page_mapped(page))
3877 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3878 MEM_CGROUP_STAT_FILE_MAPPED);
3879
3880 if (PageWriteback(page))
3881 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3882 MEM_CGROUP_STAT_WRITEBACK);
3883
David Rientjesb070e652013-05-07 16:18:09 -07003884 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003885
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003886 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003887 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003888 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003889 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003890 ret = 0;
3891unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003892 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003893 /*
3894 * check events
3895 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003896 memcg_check_events(to, page);
3897 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003898out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003899 return ret;
3900}
3901
Michal Hocko2ef37d32012-10-26 13:37:30 +02003902/**
3903 * mem_cgroup_move_parent - moves page to the parent group
3904 * @page: the page to move
3905 * @pc: page_cgroup of the page
3906 * @child: page's cgroup
3907 *
3908 * move charges to its parent or the root cgroup if the group has no
3909 * parent (aka use_hierarchy==0).
3910 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3911 * mem_cgroup_move_account fails) the failure is always temporary and
3912 * it signals a race with a page removal/uncharge or migration. In the
3913 * first case the page is on the way out and it will vanish from the LRU
3914 * on the next attempt and the call should be retried later.
3915 * Isolation from the LRU fails only if page has been isolated from
3916 * the LRU since we looked at it and that usually means either global
3917 * reclaim or migration going on. The page will either get back to the
3918 * LRU or vanish.
3919 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3920 * (!PageCgroupUsed) or moved to a different group. The page will
3921 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003922 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003923static int mem_cgroup_move_parent(struct page *page,
3924 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003925 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003926{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003927 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003928 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003929 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003930 int ret;
3931
Michal Hockod8423012012-10-26 13:37:29 +02003932 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003933
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003934 ret = -EBUSY;
3935 if (!get_page_unless_zero(page))
3936 goto out;
3937 if (isolate_lru_page(page))
3938 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003939
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003940 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003941
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003942 parent = parent_mem_cgroup(child);
3943 /*
3944 * If no parent, move charges to root cgroup.
3945 */
3946 if (!parent)
3947 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003948
Michal Hocko2ef37d32012-10-26 13:37:30 +02003949 if (nr_pages > 1) {
3950 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003951 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003952 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003953
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003954 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003955 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003956 if (!ret)
3957 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003958
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003959 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003960 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003961 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003962put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003963 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003964out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003965 return ret;
3966}
3967
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003968/*
3969 * Charge the memory controller for page usage.
3970 * Return
3971 * 0 if the charge was successful
3972 * < 0 if the cgroup is over its limit
3973 */
3974static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003975 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003976{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003977 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003978 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003979 bool oom = true;
3980 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003981
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003982 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003983 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003984 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003985 /*
3986 * Never OOM-kill a process for a huge page. The
3987 * fault handler will fall back to regular pages.
3988 */
3989 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003990 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003991
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003992 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003993 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003994 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003995 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003996 return 0;
3997}
3998
3999int mem_cgroup_newpage_charge(struct page *page,
4000 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08004001{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004002 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07004003 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08004004 VM_BUG_ON(page_mapped(page));
4005 VM_BUG_ON(page->mapping && !PageAnon(page));
4006 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08004007 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004008 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08004009}
4010
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004011/*
4012 * While swap-in, try_charge -> commit or cancel, the page is locked.
4013 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02004014 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004015 * "commit()" or removed by "cancel()"
4016 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004017static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
4018 struct page *page,
4019 gfp_t mask,
4020 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004021{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004022 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07004023 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004024 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004025
Johannes Weiner90deb782012-07-31 16:45:47 -07004026 pc = lookup_page_cgroup(page);
4027 /*
4028 * Every swap fault against a single page tries to charge the
4029 * page, bail as early as possible. shmem_unuse() encounters
4030 * already charged pages, too. The USED bit is protected by
4031 * the page lock, which serializes swap cache removal, which
4032 * in turn serializes uncharging.
4033 */
4034 if (PageCgroupUsed(pc))
4035 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004036 if (!do_swap_account)
4037 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004038 memcg = try_get_mem_cgroup_from_page(page);
4039 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004040 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08004041 *memcgp = memcg;
4042 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004044 if (ret == -EINTR)
4045 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08004046 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004047charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004048 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
4049 if (ret == -EINTR)
4050 ret = 0;
4051 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004052}
4053
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004054int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
4055 gfp_t gfp_mask, struct mem_cgroup **memcgp)
4056{
4057 *memcgp = NULL;
4058 if (mem_cgroup_disabled())
4059 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07004060 /*
4061 * A racing thread's fault, or swapoff, may have already
4062 * updated the pte, and even removed page from swap cache: in
4063 * those cases unuse_pte()'s pte_same() test will fail; but
4064 * there's also a KSM case which does need to charge the page.
4065 */
4066 if (!PageSwapCache(page)) {
4067 int ret;
4068
4069 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
4070 if (ret == -EINTR)
4071 ret = 0;
4072 return ret;
4073 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004074 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
4075}
4076
Johannes Weiner827a03d2012-07-31 16:45:36 -07004077void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
4078{
4079 if (mem_cgroup_disabled())
4080 return;
4081 if (!memcg)
4082 return;
4083 __mem_cgroup_cancel_charge(memcg, 1);
4084}
4085
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004086static void
Johannes Weiner72835c82012-01-12 17:18:32 -08004087__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004088 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004089{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004090 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004091 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08004092 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004093 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07004094
Johannes Weinerce587e62012-04-24 20:22:33 +02004095 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004096 /*
4097 * Now swap is on-memory. This means this page may be
4098 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004099 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
4100 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
4101 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004102 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08004103 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004104 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07004105 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004106 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004107}
4108
Johannes Weiner72835c82012-01-12 17:18:32 -08004109void mem_cgroup_commit_charge_swapin(struct page *page,
4110 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004111{
Johannes Weiner72835c82012-01-12 17:18:32 -08004112 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004113 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07004114}
4115
Johannes Weiner827a03d2012-07-31 16:45:36 -07004116int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
4117 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004118{
Johannes Weiner827a03d2012-07-31 16:45:36 -07004119 struct mem_cgroup *memcg = NULL;
4120 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4121 int ret;
4122
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004123 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07004124 return 0;
4125 if (PageCompound(page))
4126 return 0;
4127
Johannes Weiner827a03d2012-07-31 16:45:36 -07004128 if (!PageSwapCache(page))
4129 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
4130 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07004131 ret = __mem_cgroup_try_charge_swapin(mm, page,
4132 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07004133 if (!ret)
4134 __mem_cgroup_commit_charge_swapin(page, memcg, type);
4135 }
4136 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004137}
4138
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004139static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004140 unsigned int nr_pages,
4141 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004142{
4143 struct memcg_batch_info *batch = NULL;
4144 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004145
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004146 /* If swapout, usage of swap doesn't decrease */
4147 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4148 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004149
4150 batch = &current->memcg_batch;
4151 /*
4152 * In usual, we do css_get() when we remember memcg pointer.
4153 * But in this case, we keep res->usage until end of a series of
4154 * uncharges. Then, it's ok to ignore memcg's refcnt.
4155 */
4156 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004157 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004158 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004159 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004160 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004161 * the same cgroup and we have chance to coalesce uncharges.
4162 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4163 * because we want to do uncharge as soon as possible.
4164 */
4165
4166 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4167 goto direct_uncharge;
4168
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004169 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004170 goto direct_uncharge;
4171
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004172 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004173 * In typical case, batch->memcg == mem. This means we can
4174 * merge a series of uncharges to an uncharge of res_counter.
4175 * If not, we uncharge res_counter ony by one.
4176 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004177 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004178 goto direct_uncharge;
4179 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004180 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004181 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004182 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004183 return;
4184direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004185 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004186 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004187 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4188 if (unlikely(batch->memcg != memcg))
4189 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004190}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004191
Balbir Singh8697d332008-02-07 00:13:59 -08004192/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004193 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004194 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004195static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004196__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4197 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004198{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004199 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004200 unsigned int nr_pages = 1;
4201 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004202 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004203
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004204 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004205 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004206
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004207 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004208 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004209 VM_BUG_ON(!PageTransHuge(page));
4210 }
Balbir Singh8697d332008-02-07 00:13:59 -08004211 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004212 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004213 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004214 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004215 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004216 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004217
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004218 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004219
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004220 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004221
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004222 if (!PageCgroupUsed(pc))
4223 goto unlock_out;
4224
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004225 anon = PageAnon(page);
4226
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004227 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004228 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004229 /*
4230 * Generally PageAnon tells if it's the anon statistics to be
4231 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4232 * used before page reached the stage of being marked PageAnon.
4233 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004234 anon = true;
4235 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004236 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004237 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004238 if (page_mapped(page))
4239 goto unlock_out;
4240 /*
4241 * Pages under migration may not be uncharged. But
4242 * end_migration() /must/ be the one uncharging the
4243 * unused post-migration page and so it has to call
4244 * here with the migration bit still set. See the
4245 * res_counter handling below.
4246 */
4247 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004248 goto unlock_out;
4249 break;
4250 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4251 if (!PageAnon(page)) { /* Shared memory */
4252 if (page->mapping && !page_is_file_cache(page))
4253 goto unlock_out;
4254 } else if (page_mapped(page)) /* Anon */
4255 goto unlock_out;
4256 break;
4257 default:
4258 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004259 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004260
David Rientjesb070e652013-05-07 16:18:09 -07004261 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004262
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004263 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004264 /*
4265 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4266 * freed from LRU. This is safe because uncharged page is expected not
4267 * to be reused (freed soon). Exception is SwapCache, it's handled by
4268 * special functions.
4269 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004270
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004271 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004272 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004273 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004274 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004275 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004276 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004277 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004278 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004279 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004280 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004281 /*
4282 * Migration does not charge the res_counter for the
4283 * replacement page, so leave it alone when phasing out the
4284 * page that is unused after the migration.
4285 */
4286 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004287 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004288
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004289 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004290
4291unlock_out:
4292 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004293 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004294}
4295
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004296void mem_cgroup_uncharge_page(struct page *page)
4297{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004298 /* early check. */
4299 if (page_mapped(page))
4300 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004301 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004302 /*
4303 * If the page is in swap cache, uncharge should be deferred
4304 * to the swap path, which also properly accounts swap usage
4305 * and handles memcg lifetime.
4306 *
4307 * Note that this check is not stable and reclaim may add the
4308 * page to swap cache at any time after this. However, if the
4309 * page is not in swap cache by the time page->mapcount hits
4310 * 0, there won't be any page table references to the swap
4311 * slot, and reclaim will free it and not actually write the
4312 * page to disk.
4313 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004314 if (PageSwapCache(page))
4315 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004316 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004317}
4318
4319void mem_cgroup_uncharge_cache_page(struct page *page)
4320{
4321 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004322 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004323 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004324}
4325
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004326/*
4327 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4328 * In that cases, pages are freed continuously and we can expect pages
4329 * are in the same memcg. All these calls itself limits the number of
4330 * pages freed at once, then uncharge_start/end() is called properly.
4331 * This may be called prural(2) times in a context,
4332 */
4333
4334void mem_cgroup_uncharge_start(void)
4335{
4336 current->memcg_batch.do_batch++;
4337 /* We can do nest. */
4338 if (current->memcg_batch.do_batch == 1) {
4339 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004340 current->memcg_batch.nr_pages = 0;
4341 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004342 }
4343}
4344
4345void mem_cgroup_uncharge_end(void)
4346{
4347 struct memcg_batch_info *batch = &current->memcg_batch;
4348
4349 if (!batch->do_batch)
4350 return;
4351
4352 batch->do_batch--;
4353 if (batch->do_batch) /* If stacked, do nothing. */
4354 return;
4355
4356 if (!batch->memcg)
4357 return;
4358 /*
4359 * This "batch->memcg" is valid without any css_get/put etc...
4360 * bacause we hide charges behind us.
4361 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004362 if (batch->nr_pages)
4363 res_counter_uncharge(&batch->memcg->res,
4364 batch->nr_pages * PAGE_SIZE);
4365 if (batch->memsw_nr_pages)
4366 res_counter_uncharge(&batch->memcg->memsw,
4367 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004368 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004369 /* forget this pointer (for sanity check) */
4370 batch->memcg = NULL;
4371}
4372
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004373#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004374/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004375 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004376 * memcg information is recorded to swap_cgroup of "ent"
4377 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004378void
4379mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004380{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004381 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004382 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004383
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004384 if (!swapout) /* this was a swap cache but the swap is unused ! */
4385 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4386
Johannes Weiner0030f532012-07-31 16:45:25 -07004387 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004388
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004389 /*
4390 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004391 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004392 */
4393 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004394 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004395}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004396#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004397
Andrew Mortonc255a452012-07-31 16:43:02 -07004398#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004399/*
4400 * called from swap_entry_free(). remove record in swap_cgroup and
4401 * uncharge "memsw" account.
4402 */
4403void mem_cgroup_uncharge_swap(swp_entry_t ent)
4404{
4405 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004406 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004407
4408 if (!do_swap_account)
4409 return;
4410
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004411 id = swap_cgroup_record(ent, 0);
4412 rcu_read_lock();
4413 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004414 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004415 /*
4416 * We uncharge this because swap is freed.
4417 * This memcg can be obsolete one. We avoid calling css_tryget
4418 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004419 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004420 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004421 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004422 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004423 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004424 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004425}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004426
4427/**
4428 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4429 * @entry: swap entry to be moved
4430 * @from: mem_cgroup which the entry is moved from
4431 * @to: mem_cgroup which the entry is moved to
4432 *
4433 * It succeeds only when the swap_cgroup's record for this entry is the same
4434 * as the mem_cgroup's id of @from.
4435 *
4436 * Returns 0 on success, -EINVAL on failure.
4437 *
4438 * The caller must have charged to @to, IOW, called res_counter_charge() about
4439 * both res and memsw, and called css_get().
4440 */
4441static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004442 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004443{
4444 unsigned short old_id, new_id;
4445
4446 old_id = css_id(&from->css);
4447 new_id = css_id(&to->css);
4448
4449 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004450 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004451 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004452 /*
4453 * This function is only called from task migration context now.
4454 * It postpones res_counter and refcount handling till the end
4455 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004456 * improvement. But we cannot postpone css_get(to) because if
4457 * the process that has been moved to @to does swap-in, the
4458 * refcount of @to might be decreased to 0.
4459 *
4460 * We are in attach() phase, so the cgroup is guaranteed to be
4461 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004462 */
Li Zefan40503772013-07-08 16:00:34 -07004463 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004464 return 0;
4465 }
4466 return -EINVAL;
4467}
4468#else
4469static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004470 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004471{
4472 return -EINVAL;
4473}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004474#endif
4475
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004476/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004477 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4478 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004479 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004480void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4481 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004482{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004483 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004484 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004485 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004486 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004487
Johannes Weiner72835c82012-01-12 17:18:32 -08004488 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004489
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004490 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004491 return;
Balbir Singh40779602008-04-04 14:29:59 -07004492
Mel Gormanb32967f2012-11-19 12:35:47 +00004493 if (PageTransHuge(page))
4494 nr_pages <<= compound_order(page);
4495
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004496 pc = lookup_page_cgroup(page);
4497 lock_page_cgroup(pc);
4498 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004499 memcg = pc->mem_cgroup;
4500 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004501 /*
4502 * At migrating an anonymous page, its mapcount goes down
4503 * to 0 and uncharge() will be called. But, even if it's fully
4504 * unmapped, migration may fail and this page has to be
4505 * charged again. We set MIGRATION flag here and delay uncharge
4506 * until end_migration() is called
4507 *
4508 * Corner Case Thinking
4509 * A)
4510 * When the old page was mapped as Anon and it's unmap-and-freed
4511 * while migration was ongoing.
4512 * If unmap finds the old page, uncharge() of it will be delayed
4513 * until end_migration(). If unmap finds a new page, it's
4514 * uncharged when it make mapcount to be 1->0. If unmap code
4515 * finds swap_migration_entry, the new page will not be mapped
4516 * and end_migration() will find it(mapcount==0).
4517 *
4518 * B)
4519 * When the old page was mapped but migraion fails, the kernel
4520 * remaps it. A charge for it is kept by MIGRATION flag even
4521 * if mapcount goes down to 0. We can do remap successfully
4522 * without charging it again.
4523 *
4524 * C)
4525 * The "old" page is under lock_page() until the end of
4526 * migration, so, the old page itself will not be swapped-out.
4527 * If the new page is swapped out before end_migraton, our
4528 * hook to usual swap-out path will catch the event.
4529 */
4530 if (PageAnon(page))
4531 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004532 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004533 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004534 /*
4535 * If the page is not charged at this point,
4536 * we return here.
4537 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004538 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004539 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004540
Johannes Weiner72835c82012-01-12 17:18:32 -08004541 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004542 /*
4543 * We charge new page before it's used/mapped. So, even if unlock_page()
4544 * is called before end_migration, we can catch all events on this new
4545 * page. In the case new page is migrated but not remapped, new page's
4546 * mapcount will be finally 0 and we call uncharge in end_migration().
4547 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004548 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004549 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004550 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004551 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004552 /*
4553 * The page is committed to the memcg, but it's not actually
4554 * charged to the res_counter since we plan on replacing the
4555 * old one and only one page is going to be left afterwards.
4556 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004557 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004558}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004559
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004560/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004561void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004562 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004563{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004564 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004565 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004566 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004567
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004568 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004569 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004570
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004571 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004572 used = oldpage;
4573 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004574 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004575 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004576 unused = oldpage;
4577 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004578 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004579 __mem_cgroup_uncharge_common(unused,
4580 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4581 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4582 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004583 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004584 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004585 * We disallowed uncharge of pages under migration because mapcount
4586 * of the page goes down to zero, temporarly.
4587 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004588 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004589 pc = lookup_page_cgroup(oldpage);
4590 lock_page_cgroup(pc);
4591 ClearPageCgroupMigration(pc);
4592 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004593
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004594 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004595 * If a page is a file cache, radix-tree replacement is very atomic
4596 * and we can skip this check. When it was an Anon page, its mapcount
4597 * goes down to 0. But because we added MIGRATION flage, it's not
4598 * uncharged yet. There are several case but page->mapcount check
4599 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4600 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004601 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004602 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004603 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004604}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004605
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004606/*
4607 * At replace page cache, newpage is not under any memcg but it's on
4608 * LRU. So, this function doesn't touch res_counter but handles LRU
4609 * in correct way. Both pages are locked so we cannot race with uncharge.
4610 */
4611void mem_cgroup_replace_page_cache(struct page *oldpage,
4612 struct page *newpage)
4613{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004614 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004615 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004616 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004617
4618 if (mem_cgroup_disabled())
4619 return;
4620
4621 pc = lookup_page_cgroup(oldpage);
4622 /* fix accounting on old pages */
4623 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004624 if (PageCgroupUsed(pc)) {
4625 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004626 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004627 ClearPageCgroupUsed(pc);
4628 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004629 unlock_page_cgroup(pc);
4630
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004631 /*
4632 * When called from shmem_replace_page(), in some cases the
4633 * oldpage has already been charged, and in some cases not.
4634 */
4635 if (!memcg)
4636 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004637 /*
4638 * Even if newpage->mapping was NULL before starting replacement,
4639 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4640 * LRU while we overwrite pc->mem_cgroup.
4641 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004642 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004643}
4644
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004645#ifdef CONFIG_DEBUG_VM
4646static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4647{
4648 struct page_cgroup *pc;
4649
4650 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004651 /*
4652 * Can be NULL while feeding pages into the page allocator for
4653 * the first time, i.e. during boot or memory hotplug;
4654 * or when mem_cgroup_disabled().
4655 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004656 if (likely(pc) && PageCgroupUsed(pc))
4657 return pc;
4658 return NULL;
4659}
4660
4661bool mem_cgroup_bad_page_check(struct page *page)
4662{
4663 if (mem_cgroup_disabled())
4664 return false;
4665
4666 return lookup_page_cgroup_used(page) != NULL;
4667}
4668
4669void mem_cgroup_print_bad_page(struct page *page)
4670{
4671 struct page_cgroup *pc;
4672
4673 pc = lookup_page_cgroup_used(page);
4674 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004675 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4676 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004677 }
4678}
4679#endif
4680
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004681static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004682 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004683{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004684 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004685 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004686 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004687 int children = mem_cgroup_count_children(memcg);
4688 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004689 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004690
4691 /*
4692 * For keeping hierarchical_reclaim simple, how long we should retry
4693 * is depends on callers. We set our retry-count to be function
4694 * of # of children which we should visit in this loop.
4695 */
4696 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4697
4698 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004699
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004700 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004701 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004702 if (signal_pending(current)) {
4703 ret = -EINTR;
4704 break;
4705 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004706 /*
4707 * Rather than hide all in some function, I do this in
4708 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004709 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004710 */
4711 mutex_lock(&set_limit_mutex);
4712 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4713 if (memswlimit < val) {
4714 ret = -EINVAL;
4715 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004716 break;
4717 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004718
4719 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4720 if (memlimit < val)
4721 enlarge = 1;
4722
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004723 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004724 if (!ret) {
4725 if (memswlimit == val)
4726 memcg->memsw_is_minimum = true;
4727 else
4728 memcg->memsw_is_minimum = false;
4729 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004730 mutex_unlock(&set_limit_mutex);
4731
4732 if (!ret)
4733 break;
4734
Johannes Weiner56600482012-01-12 17:17:59 -08004735 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4736 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004737 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4738 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004739 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004740 retry_count--;
4741 else
4742 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004743 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004744 if (!ret && enlarge)
4745 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004746
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004747 return ret;
4748}
4749
Li Zefan338c8432009-06-17 16:27:15 -07004750static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4751 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004752{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004753 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004754 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004755 int children = mem_cgroup_count_children(memcg);
4756 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004757 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004758
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004759 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004760 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004761 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004762 while (retry_count) {
4763 if (signal_pending(current)) {
4764 ret = -EINTR;
4765 break;
4766 }
4767 /*
4768 * Rather than hide all in some function, I do this in
4769 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004770 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004771 */
4772 mutex_lock(&set_limit_mutex);
4773 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4774 if (memlimit > val) {
4775 ret = -EINVAL;
4776 mutex_unlock(&set_limit_mutex);
4777 break;
4778 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004779 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4780 if (memswlimit < val)
4781 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004782 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004783 if (!ret) {
4784 if (memlimit == val)
4785 memcg->memsw_is_minimum = true;
4786 else
4787 memcg->memsw_is_minimum = false;
4788 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004789 mutex_unlock(&set_limit_mutex);
4790
4791 if (!ret)
4792 break;
4793
Johannes Weiner56600482012-01-12 17:17:59 -08004794 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4795 MEM_CGROUP_RECLAIM_NOSWAP |
4796 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004797 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004798 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004799 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004800 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004801 else
4802 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004803 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004804 if (!ret && enlarge)
4805 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004806 return ret;
4807}
4808
Andrew Morton0608f432013-09-24 15:27:41 -07004809unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
4810 gfp_t gfp_mask,
4811 unsigned long *total_scanned)
4812{
4813 unsigned long nr_reclaimed = 0;
4814 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4815 unsigned long reclaimed;
4816 int loop = 0;
4817 struct mem_cgroup_tree_per_zone *mctz;
4818 unsigned long long excess;
4819 unsigned long nr_scanned;
4820
4821 if (order > 0)
4822 return 0;
4823
4824 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
4825 /*
4826 * This loop can run a while, specially if mem_cgroup's continuously
4827 * keep exceeding their soft limit and putting the system under
4828 * pressure
4829 */
4830 do {
4831 if (next_mz)
4832 mz = next_mz;
4833 else
4834 mz = mem_cgroup_largest_soft_limit_node(mctz);
4835 if (!mz)
4836 break;
4837
4838 nr_scanned = 0;
4839 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
4840 gfp_mask, &nr_scanned);
4841 nr_reclaimed += reclaimed;
4842 *total_scanned += nr_scanned;
4843 spin_lock(&mctz->lock);
4844
4845 /*
4846 * If we failed to reclaim anything from this memory cgroup
4847 * it is time to move on to the next cgroup
4848 */
4849 next_mz = NULL;
4850 if (!reclaimed) {
4851 do {
4852 /*
4853 * Loop until we find yet another one.
4854 *
4855 * By the time we get the soft_limit lock
4856 * again, someone might have aded the
4857 * group back on the RB tree. Iterate to
4858 * make sure we get a different mem.
4859 * mem_cgroup_largest_soft_limit_node returns
4860 * NULL if no other cgroup is present on
4861 * the tree
4862 */
4863 next_mz =
4864 __mem_cgroup_largest_soft_limit_node(mctz);
4865 if (next_mz == mz)
4866 css_put(&next_mz->memcg->css);
4867 else /* next_mz == NULL or other memcg */
4868 break;
4869 } while (1);
4870 }
4871 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4872 excess = res_counter_soft_limit_excess(&mz->memcg->res);
4873 /*
4874 * One school of thought says that we should not add
4875 * back the node to the tree if reclaim returns 0.
4876 * But our reclaim could return 0, simply because due
4877 * to priority we are exposing a smaller subset of
4878 * memory to reclaim from. Consider this as a longer
4879 * term TODO.
4880 */
4881 /* If excess == 0, no tree ops */
4882 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
4883 spin_unlock(&mctz->lock);
4884 css_put(&mz->memcg->css);
4885 loop++;
4886 /*
4887 * Could not reclaim anything and there are no more
4888 * mem cgroups to try or we seem to be looping without
4889 * reclaiming anything.
4890 */
4891 if (!nr_reclaimed &&
4892 (next_mz == NULL ||
4893 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4894 break;
4895 } while (!nr_reclaimed);
4896 if (next_mz)
4897 css_put(&next_mz->memcg->css);
4898 return nr_reclaimed;
4899}
4900
Michal Hocko2ef37d32012-10-26 13:37:30 +02004901/**
4902 * mem_cgroup_force_empty_list - clears LRU of a group
4903 * @memcg: group to clear
4904 * @node: NUMA node
4905 * @zid: zone id
4906 * @lru: lru to to clear
4907 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004908 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004909 * reclaim the pages page themselves - pages are moved to the parent (or root)
4910 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004911 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004912static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004913 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004914{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004915 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004916 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004917 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004918 struct page *busy;
4919 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004920
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004921 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004922 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4923 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004924
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004925 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004926 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004927 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004928 struct page *page;
4929
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004930 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004931 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004932 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004933 break;
4934 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004935 page = list_entry(list->prev, struct page, lru);
4936 if (busy == page) {
4937 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004938 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004939 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004940 continue;
4941 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004942 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004943
Johannes Weiner925b7672012-01-12 17:18:15 -08004944 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004945
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004946 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004947 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004948 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004949 cond_resched();
4950 } else
4951 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004952 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004953}
4954
4955/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004956 * make mem_cgroup's charge to be 0 if there is no task by moving
4957 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004958 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004959 *
4960 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004961 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004962static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004963{
Michal Hockoc26251f2012-10-26 13:37:28 +02004964 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004965 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004966
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004967 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004968 /* This is for making all *used* pages to be on LRU. */
4969 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004970 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004971 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004972 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004973 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004974 enum lru_list lru;
4975 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004976 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004977 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004978 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004979 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004980 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004981 mem_cgroup_end_move(memcg);
4982 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004983 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004984
Michal Hocko2ef37d32012-10-26 13:37:30 +02004985 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004986 * Kernel memory may not necessarily be trackable to a specific
4987 * process. So they are not migrated, and therefore we can't
4988 * expect their value to drop to 0 here.
4989 * Having res filled up with kmem only is enough.
4990 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004991 * This is a safety check because mem_cgroup_force_empty_list
4992 * could have raced with mem_cgroup_replace_page_cache callers
4993 * so the lru seemed empty but the page could have been added
4994 * right after the check. RES_USAGE should be safe as we always
4995 * charge before adding to the LRU.
4996 */
Glauber Costabea207c2012-12-18 14:22:11 -08004997 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4998 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4999 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02005000}
5001
Glauber Costab5f99b52013-02-22 16:34:53 -08005002static inline bool memcg_has_children(struct mem_cgroup *memcg)
5003{
Johannes Weiner696ac172013-10-31 16:34:15 -07005004 lockdep_assert_held(&memcg_create_mutex);
5005 /*
5006 * The lock does not prevent addition or deletion to the list
5007 * of children, but it prevents a new child from being
5008 * initialized based on this parent in css_online(), so it's
5009 * enough to decide whether hierarchically inherited
5010 * attributes can still be changed or not.
5011 */
5012 return memcg->use_hierarchy &&
5013 !list_empty(&memcg->css.cgroup->children);
Glauber Costab5f99b52013-02-22 16:34:53 -08005014}
5015
5016/*
Michal Hockoc26251f2012-10-26 13:37:28 +02005017 * Reclaims as many pages from the given memcg as possible and moves
5018 * the rest to the parent.
5019 *
5020 * Caller is responsible for holding css reference for memcg.
5021 */
5022static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
5023{
5024 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
5025 struct cgroup *cgrp = memcg->css.cgroup;
5026
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005027 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02005028 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
5029 return -EBUSY;
5030
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005031 /* we call try-to-free pages for make this cgroup empty */
5032 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005033 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07005034 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005035 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005036
Michal Hockoc26251f2012-10-26 13:37:28 +02005037 if (signal_pending(current))
5038 return -EINTR;
5039
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005040 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07005041 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005042 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005043 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005044 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02005045 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005046 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08005047
5048 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005049 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02005050 mem_cgroup_reparent_charges(memcg);
5051
5052 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08005053}
5054
Tejun Heo182446d2013-08-08 20:11:24 -04005055static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
5056 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005057{
Tejun Heo182446d2013-08-08 20:11:24 -04005058 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02005059
Michal Hockod8423012012-10-26 13:37:29 +02005060 if (mem_cgroup_is_root(memcg))
5061 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07005062 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005063}
5064
Tejun Heo182446d2013-08-08 20:11:24 -04005065static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
5066 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08005067{
Tejun Heo182446d2013-08-08 20:11:24 -04005068 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005069}
5070
Tejun Heo182446d2013-08-08 20:11:24 -04005071static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
5072 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08005073{
5074 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04005075 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005076 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08005077
Glauber Costa09998212013-02-22 16:34:55 -08005078 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07005079
5080 if (memcg->use_hierarchy == val)
5081 goto out;
5082
Balbir Singh18f59ea2009-01-07 18:08:07 -08005083 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005084 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08005085 * in the child subtrees. If it is unset, then the change can
5086 * occur, provided the current cgroup has no children.
5087 *
5088 * For the root cgroup, parent_mem is NULL, we allow value to be
5089 * set if there are no children.
5090 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005091 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08005092 (val == 1 || val == 0)) {
Johannes Weiner696ac172013-10-31 16:34:15 -07005093 if (list_empty(&memcg->css.cgroup->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005094 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005095 else
5096 retval = -EBUSY;
5097 } else
5098 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07005099
5100out:
Glauber Costa09998212013-02-22 16:34:55 -08005101 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005102
5103 return retval;
5104}
5105
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005106
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005107static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005108 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005109{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005110 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005111 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005112
Johannes Weiner7a159cc2011-03-23 16:42:38 -07005113 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005114 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005115 val += mem_cgroup_read_stat(iter, idx);
5116
5117 if (val < 0) /* race ? */
5118 val = 0;
5119 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07005120}
5121
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005122static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005123{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005124 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005125
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005126 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005127 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00005128 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005129 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00005130 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005131 }
5132
David Rientjesb070e652013-05-07 16:18:09 -07005133 /*
5134 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
5135 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
5136 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005137 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
5138 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005139
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005140 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005141 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005142
5143 return val << PAGE_SHIFT;
5144}
5145
Tejun Heo182446d2013-08-08 20:11:24 -04005146static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
5147 struct cftype *cft, struct file *file,
5148 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005149{
Tejun Heo182446d2013-08-08 20:11:24 -04005150 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07005151 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005152 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005153 int name, len;
5154 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005155
5156 type = MEMFILE_TYPE(cft->private);
5157 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005158
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005159 switch (type) {
5160 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005161 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005162 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005163 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005164 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005165 break;
5166 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005167 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005168 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08005169 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005170 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005171 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005172 case _KMEM:
5173 val = res_counter_read_u64(&memcg->kmem, name);
5174 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005175 default:
5176 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005177 }
Tejun Heoaf36f902012-04-01 12:09:55 -07005178
5179 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5180 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005181}
Glauber Costa510fc4e2012-12-18 14:21:47 -08005182
Tejun Heo182446d2013-08-08 20:11:24 -04005183static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005184{
5185 int ret = -EINVAL;
5186#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04005187 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005188 /*
5189 * For simplicity, we won't allow this to be disabled. It also can't
5190 * be changed if the cgroup has children already, or if tasks had
5191 * already joined.
5192 *
5193 * If tasks join before we set the limit, a person looking at
5194 * kmem.usage_in_bytes will have no way to determine when it took
5195 * place, which makes the value quite meaningless.
5196 *
5197 * After it first became limited, changes in the value of the limit are
5198 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08005199 */
Glauber Costa09998212013-02-22 16:34:55 -08005200 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005201 mutex_lock(&set_limit_mutex);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07005202 if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04005203 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08005204 ret = -EBUSY;
5205 goto out;
5206 }
5207 ret = res_counter_set_limit(&memcg->kmem, val);
5208 VM_BUG_ON(ret);
5209
Glauber Costa55007d82012-12-18 14:22:38 -08005210 ret = memcg_update_cache_sizes(memcg);
5211 if (ret) {
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07005212 res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
Glauber Costa55007d82012-12-18 14:22:38 -08005213 goto out;
5214 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005215 static_key_slow_inc(&memcg_kmem_enabled_key);
5216 /*
5217 * setting the active bit after the inc will guarantee no one
5218 * starts accounting before all call sites are patched
5219 */
5220 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005221 } else
5222 ret = res_counter_set_limit(&memcg->kmem, val);
5223out:
5224 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005225 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005226#endif
5227 return ret;
5228}
5229
Hugh Dickins6d0439902013-02-22 16:35:50 -08005230#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005231static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005232{
Glauber Costa55007d82012-12-18 14:22:38 -08005233 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005234 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5235 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005236 goto out;
5237
Glauber Costa510fc4e2012-12-18 14:21:47 -08005238 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005239 /*
5240 * When that happen, we need to disable the static branch only on those
5241 * memcgs that enabled it. To achieve this, we would be forced to
5242 * complicate the code by keeping track of which memcgs were the ones
5243 * that actually enabled limits, and which ones got it from its
5244 * parents.
5245 *
5246 * It is a lot simpler just to do static_key_slow_inc() on every child
5247 * that is accounted.
5248 */
Glauber Costa55007d82012-12-18 14:22:38 -08005249 if (!memcg_kmem_is_active(memcg))
5250 goto out;
5251
5252 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07005253 * __mem_cgroup_free() will issue static_key_slow_dec() because this
5254 * memcg is active already. If the later initialization fails then the
5255 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08005256 */
Glauber Costa55007d82012-12-18 14:22:38 -08005257 static_key_slow_inc(&memcg_kmem_enabled_key);
5258
5259 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07005260 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005261 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07005262 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005263 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005264out:
5265 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005266}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005267#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005268
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005269/*
5270 * The user of this function is...
5271 * RES_LIMIT.
5272 */
Tejun Heo182446d2013-08-08 20:11:24 -04005273static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07005274 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005275{
Tejun Heo182446d2013-08-08 20:11:24 -04005276 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005277 enum res_type type;
5278 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005279 unsigned long long val;
5280 int ret;
5281
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005282 type = MEMFILE_TYPE(cft->private);
5283 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005284
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005285 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005286 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005287 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5288 ret = -EINVAL;
5289 break;
5290 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005291 /* This function does all necessary parse...reuse it */
5292 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005293 if (ret)
5294 break;
5295 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005296 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005297 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005298 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005299 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04005300 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005301 else
5302 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005303 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005304 case RES_SOFT_LIMIT:
5305 ret = res_counter_memparse_write_strategy(buffer, &val);
5306 if (ret)
5307 break;
5308 /*
5309 * For memsw, soft limits are hard to implement in terms
5310 * of semantics, for now, we support soft limits for
5311 * control without swap
5312 */
5313 if (type == _MEM)
5314 ret = res_counter_set_soft_limit(&memcg->res, val);
5315 else
5316 ret = -EINVAL;
5317 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005318 default:
5319 ret = -EINVAL; /* should be BUG() ? */
5320 break;
5321 }
5322 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005323}
5324
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005325static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5326 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5327{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005328 unsigned long long min_limit, min_memsw_limit, tmp;
5329
5330 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5331 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005332 if (!memcg->use_hierarchy)
5333 goto out;
5334
Tejun Heo63876982013-08-08 20:11:23 -04005335 while (css_parent(&memcg->css)) {
5336 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005337 if (!memcg->use_hierarchy)
5338 break;
5339 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5340 min_limit = min(min_limit, tmp);
5341 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5342 min_memsw_limit = min(min_memsw_limit, tmp);
5343 }
5344out:
5345 *mem_limit = min_limit;
5346 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005347}
5348
Tejun Heo182446d2013-08-08 20:11:24 -04005349static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005350{
Tejun Heo182446d2013-08-08 20:11:24 -04005351 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005352 int name;
5353 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005354
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005355 type = MEMFILE_TYPE(event);
5356 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005357
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005358 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005359 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005360 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005361 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005362 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005363 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005364 else if (type == _KMEM)
5365 res_counter_reset_max(&memcg->kmem);
5366 else
5367 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005368 break;
5369 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005370 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005371 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005372 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005373 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005374 else if (type == _KMEM)
5375 res_counter_reset_failcnt(&memcg->kmem);
5376 else
5377 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005378 break;
5379 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005380
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005381 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005382}
5383
Tejun Heo182446d2013-08-08 20:11:24 -04005384static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005385 struct cftype *cft)
5386{
Tejun Heo182446d2013-08-08 20:11:24 -04005387 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005388}
5389
Daisuke Nishimura02491442010-03-10 15:22:17 -08005390#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005391static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005392 struct cftype *cft, u64 val)
5393{
Tejun Heo182446d2013-08-08 20:11:24 -04005394 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005395
5396 if (val >= (1 << NR_MOVE_TYPE))
5397 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005398
Glauber Costaee5e8472013-02-22 16:34:50 -08005399 /*
5400 * No kind of locking is needed in here, because ->can_attach() will
5401 * check this value once in the beginning of the process, and then carry
5402 * on with stale data. This means that changes to this value will only
5403 * affect task migrations starting after the change.
5404 */
5405 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005406 return 0;
5407}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005408#else
Tejun Heo182446d2013-08-08 20:11:24 -04005409static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005410 struct cftype *cft, u64 val)
5411{
5412 return -ENOSYS;
5413}
5414#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005415
Ying Han406eb0c2011-05-26 16:25:37 -07005416#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005417static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5418 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005419{
5420 int nid;
5421 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5422 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005423 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005424
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005425 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005426 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005427 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005428 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
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 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005434 seq_printf(m, "file=%lu", file_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_FILE);
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 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005443 seq_printf(m, "anon=%lu", anon_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 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005447 seq_printf(m, " N%d=%lu", nid, node_nr);
5448 }
5449 seq_putc(m, '\n');
5450
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005451 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005452 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005453 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005454 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005455 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005456 seq_printf(m, " N%d=%lu", nid, node_nr);
5457 }
5458 seq_putc(m, '\n');
5459 return 0;
5460}
5461#endif /* CONFIG_NUMA */
5462
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005463static inline void mem_cgroup_lru_names_not_uptodate(void)
5464{
5465 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5466}
5467
Tejun Heo182446d2013-08-08 20:11:24 -04005468static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005469 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005470{
Tejun Heo182446d2013-08-08 20:11:24 -04005471 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005472 struct mem_cgroup *mi;
5473 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005474
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005475 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005476 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005477 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005478 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5479 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005480 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005481
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005482 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5483 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5484 mem_cgroup_read_events(memcg, i));
5485
5486 for (i = 0; i < NR_LRU_LISTS; i++)
5487 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5488 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5489
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005490 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005491 {
5492 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005493 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005494 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005495 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005496 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5497 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005498 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005499
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005500 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5501 long long val = 0;
5502
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005503 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005504 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005505 for_each_mem_cgroup_tree(mi, memcg)
5506 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5507 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5508 }
5509
5510 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5511 unsigned long long val = 0;
5512
5513 for_each_mem_cgroup_tree(mi, memcg)
5514 val += mem_cgroup_read_events(mi, i);
5515 seq_printf(m, "total_%s %llu\n",
5516 mem_cgroup_events_names[i], val);
5517 }
5518
5519 for (i = 0; i < NR_LRU_LISTS; i++) {
5520 unsigned long long val = 0;
5521
5522 for_each_mem_cgroup_tree(mi, memcg)
5523 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5524 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005525 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005526
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005527#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005528 {
5529 int nid, zid;
5530 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005531 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005532 unsigned long recent_rotated[2] = {0, 0};
5533 unsigned long recent_scanned[2] = {0, 0};
5534
5535 for_each_online_node(nid)
5536 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005537 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005538 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005539
Hugh Dickins89abfab2012-05-29 15:06:53 -07005540 recent_rotated[0] += rstat->recent_rotated[0];
5541 recent_rotated[1] += rstat->recent_rotated[1];
5542 recent_scanned[0] += rstat->recent_scanned[0];
5543 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005544 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005545 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5546 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5547 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5548 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005549 }
5550#endif
5551
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005552 return 0;
5553}
5554
Tejun Heo182446d2013-08-08 20:11:24 -04005555static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5556 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005557{
Tejun Heo182446d2013-08-08 20:11:24 -04005558 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005559
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005560 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005561}
5562
Tejun Heo182446d2013-08-08 20:11:24 -04005563static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5564 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005565{
Tejun Heo182446d2013-08-08 20:11:24 -04005566 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005567 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005568
Tejun Heo63876982013-08-08 20:11:23 -04005569 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005570 return -EINVAL;
5571
Glauber Costa09998212013-02-22 16:34:55 -08005572 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005573
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005574 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005575 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005576 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005577 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005578 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005579
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005580 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005581
Glauber Costa09998212013-02-22 16:34:55 -08005582 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005583
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005584 return 0;
5585}
5586
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005587static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5588{
5589 struct mem_cgroup_threshold_ary *t;
5590 u64 usage;
5591 int i;
5592
5593 rcu_read_lock();
5594 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005595 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005596 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005597 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005598
5599 if (!t)
5600 goto unlock;
5601
5602 usage = mem_cgroup_usage(memcg, swap);
5603
5604 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005605 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005606 * If it's not true, a threshold was crossed after last
5607 * call of __mem_cgroup_threshold().
5608 */
Phil Carmody5407a562010-05-26 14:42:42 -07005609 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005610
5611 /*
5612 * Iterate backward over array of thresholds starting from
5613 * current_threshold and check if a threshold is crossed.
5614 * If none of thresholds below usage is crossed, we read
5615 * only one element of the array here.
5616 */
5617 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5618 eventfd_signal(t->entries[i].eventfd, 1);
5619
5620 /* i = current_threshold + 1 */
5621 i++;
5622
5623 /*
5624 * Iterate forward over array of thresholds starting from
5625 * current_threshold+1 and check if a threshold is crossed.
5626 * If none of thresholds above usage is crossed, we read
5627 * only one element of the array here.
5628 */
5629 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5630 eventfd_signal(t->entries[i].eventfd, 1);
5631
5632 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005633 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005634unlock:
5635 rcu_read_unlock();
5636}
5637
5638static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5639{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005640 while (memcg) {
5641 __mem_cgroup_threshold(memcg, false);
5642 if (do_swap_account)
5643 __mem_cgroup_threshold(memcg, true);
5644
5645 memcg = parent_mem_cgroup(memcg);
5646 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005647}
5648
5649static int compare_thresholds(const void *a, const void *b)
5650{
5651 const struct mem_cgroup_threshold *_a = a;
5652 const struct mem_cgroup_threshold *_b = b;
5653
Greg Thelen2bff24a2013-09-11 14:23:08 -07005654 if (_a->threshold > _b->threshold)
5655 return 1;
5656
5657 if (_a->threshold < _b->threshold)
5658 return -1;
5659
5660 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005661}
5662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005663static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005664{
5665 struct mem_cgroup_eventfd_list *ev;
5666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005667 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005668 eventfd_signal(ev->eventfd, 1);
5669 return 0;
5670}
5671
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005672static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005673{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005674 struct mem_cgroup *iter;
5675
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005676 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005677 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005678}
5679
Tejun Heo59b6f872013-11-22 18:20:43 -05005680static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005681 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005682{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005683 struct mem_cgroup_thresholds *thresholds;
5684 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005685 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005686 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005687
5688 ret = res_counter_memparse_write_strategy(args, &threshold);
5689 if (ret)
5690 return ret;
5691
5692 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005693
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005694 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005695 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005696 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005697 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005698 else
5699 BUG();
5700
5701 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5702
5703 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005704 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005705 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5706
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005707 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005708
5709 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005710 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005711 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005712 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005713 ret = -ENOMEM;
5714 goto unlock;
5715 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005716 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005717
5718 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005719 if (thresholds->primary) {
5720 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005721 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005722 }
5723
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005724 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005725 new->entries[size - 1].eventfd = eventfd;
5726 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005727
5728 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005729 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005730 compare_thresholds, NULL);
5731
5732 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005733 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005734 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005735 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005736 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005737 * new->current_threshold will not be used until
5738 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005739 * it here.
5740 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005741 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005742 } else
5743 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005744 }
5745
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005746 /* Free old spare buffer and save old primary buffer as spare */
5747 kfree(thresholds->spare);
5748 thresholds->spare = thresholds->primary;
5749
5750 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005751
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005752 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005753 synchronize_rcu();
5754
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005755unlock:
5756 mutex_unlock(&memcg->thresholds_lock);
5757
5758 return ret;
5759}
5760
Tejun Heo59b6f872013-11-22 18:20:43 -05005761static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005762 struct eventfd_ctx *eventfd, const char *args)
5763{
Tejun Heo59b6f872013-11-22 18:20:43 -05005764 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05005765}
5766
Tejun Heo59b6f872013-11-22 18:20:43 -05005767static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005768 struct eventfd_ctx *eventfd, const char *args)
5769{
Tejun Heo59b6f872013-11-22 18:20:43 -05005770 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05005771}
5772
Tejun Heo59b6f872013-11-22 18:20:43 -05005773static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005774 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005775{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005776 struct mem_cgroup_thresholds *thresholds;
5777 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005778 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005779 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005780
5781 mutex_lock(&memcg->thresholds_lock);
5782 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005783 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005784 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005785 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005786 else
5787 BUG();
5788
Anton Vorontsov371528c2012-02-24 05:14:46 +04005789 if (!thresholds->primary)
5790 goto unlock;
5791
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005792 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5793
5794 /* Check if a threshold crossed before removing */
5795 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5796
5797 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005798 size = 0;
5799 for (i = 0; i < thresholds->primary->size; i++) {
5800 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005801 size++;
5802 }
5803
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005804 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005805
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005806 /* Set thresholds array to NULL if we don't have thresholds */
5807 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005808 kfree(new);
5809 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005810 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005811 }
5812
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005813 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005814
5815 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005816 new->current_threshold = -1;
5817 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5818 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005819 continue;
5820
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005821 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005822 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005823 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005824 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005825 * until rcu_assign_pointer(), so it's safe to increment
5826 * it here.
5827 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005828 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005829 }
5830 j++;
5831 }
5832
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005833swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005834 /* Swap primary and spare array */
5835 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005836 /* If all events are unregistered, free the spare array */
5837 if (!new) {
5838 kfree(thresholds->spare);
5839 thresholds->spare = NULL;
5840 }
5841
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005842 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005843
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005844 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005845 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005846unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005847 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005848}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005849
Tejun Heo59b6f872013-11-22 18:20:43 -05005850static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005851 struct eventfd_ctx *eventfd)
5852{
Tejun Heo59b6f872013-11-22 18:20:43 -05005853 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05005854}
5855
Tejun Heo59b6f872013-11-22 18:20:43 -05005856static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005857 struct eventfd_ctx *eventfd)
5858{
Tejun Heo59b6f872013-11-22 18:20:43 -05005859 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05005860}
5861
Tejun Heo59b6f872013-11-22 18:20:43 -05005862static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005863 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005864{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005865 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005866
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005867 event = kmalloc(sizeof(*event), GFP_KERNEL);
5868 if (!event)
5869 return -ENOMEM;
5870
Michal Hocko1af8efe2011-07-26 16:08:24 -07005871 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005872
5873 event->eventfd = eventfd;
5874 list_add(&event->list, &memcg->oom_notify);
5875
5876 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005877 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005878 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005879 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005880
5881 return 0;
5882}
5883
Tejun Heo59b6f872013-11-22 18:20:43 -05005884static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05005885 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005886{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005887 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005888
Michal Hocko1af8efe2011-07-26 16:08:24 -07005889 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005891 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005892 if (ev->eventfd == eventfd) {
5893 list_del(&ev->list);
5894 kfree(ev);
5895 }
5896 }
5897
Michal Hocko1af8efe2011-07-26 16:08:24 -07005898 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005899}
5900
Tejun Heo182446d2013-08-08 20:11:24 -04005901static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005902 struct cftype *cft, struct cgroup_map_cb *cb)
5903{
Tejun Heo182446d2013-08-08 20:11:24 -04005904 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005906 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005907
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005908 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005909 cb->fill(cb, "under_oom", 1);
5910 else
5911 cb->fill(cb, "under_oom", 0);
5912 return 0;
5913}
5914
Tejun Heo182446d2013-08-08 20:11:24 -04005915static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005916 struct cftype *cft, u64 val)
5917{
Tejun Heo182446d2013-08-08 20:11:24 -04005918 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005919 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005920
5921 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005922 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005923 return -EINVAL;
5924
Glauber Costa09998212013-02-22 16:34:55 -08005925 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005926 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005927 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005928 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005929 return -EINVAL;
5930 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005931 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005932 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005933 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005934 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005935 return 0;
5936}
5937
Andrew Mortonc255a452012-07-31 16:43:02 -07005938#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005939static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005940{
Glauber Costa55007d82012-12-18 14:22:38 -08005941 int ret;
5942
Glauber Costa2633d7a2012-12-18 14:22:34 -08005943 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005944 ret = memcg_propagate_kmem(memcg);
5945 if (ret)
5946 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005947
Glauber Costa1d62e432012-04-09 19:36:33 -03005948 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005949}
Glauber Costae5671df2011-12-11 21:47:01 +00005950
Li Zefan10d5ebf2013-07-08 16:00:33 -07005951static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005952{
Glauber Costa1d62e432012-04-09 19:36:33 -03005953 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005954}
5955
5956static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5957{
5958 if (!memcg_kmem_is_active(memcg))
5959 return;
5960
5961 /*
5962 * kmem charges can outlive the cgroup. In the case of slab
5963 * pages, for instance, a page contain objects from various
5964 * processes. As we prevent from taking a reference for every
5965 * such allocation we have to be careful when doing uncharge
5966 * (see memcg_uncharge_kmem) and here during offlining.
5967 *
5968 * The idea is that that only the _last_ uncharge which sees
5969 * the dead memcg will drop the last reference. An additional
5970 * reference is taken here before the group is marked dead
5971 * which is then paired with css_put during uncharge resp. here.
5972 *
5973 * Although this might sound strange as this path is called from
5974 * css_offline() when the referencemight have dropped down to 0
5975 * and shouldn't be incremented anymore (css_tryget would fail)
5976 * we do not have other options because of the kmem allocations
5977 * lifetime.
5978 */
5979 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005980
5981 memcg_kmem_mark_dead(memcg);
5982
5983 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5984 return;
5985
Glauber Costa7de37682012-12-18 14:22:07 -08005986 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005987 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005988}
Glauber Costae5671df2011-12-11 21:47:01 +00005989#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005990static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005991{
5992 return 0;
5993}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005994
Li Zefan10d5ebf2013-07-08 16:00:33 -07005995static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5996{
5997}
5998
5999static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006000{
6001}
Glauber Costae5671df2011-12-11 21:47:01 +00006002#endif
6003
Tejun Heo79bd9812013-11-22 18:20:42 -05006004/*
6005 * Unregister event and free resources.
6006 *
6007 * Gets called from workqueue.
6008 */
6009static void cgroup_event_remove(struct work_struct *work)
6010{
6011 struct cgroup_event *event = container_of(work, struct cgroup_event,
6012 remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05006013 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05006014
6015 remove_wait_queue(event->wqh, &event->wait);
6016
Tejun Heo59b6f872013-11-22 18:20:43 -05006017 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05006018
6019 /* Notify userspace the event is going away. */
6020 eventfd_signal(event->eventfd, 1);
6021
6022 eventfd_ctx_put(event->eventfd);
6023 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05006024 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006025}
6026
6027/*
6028 * Gets called on POLLHUP on eventfd when user closes it.
6029 *
6030 * Called with wqh->lock held and interrupts disabled.
6031 */
6032static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
6033 int sync, void *key)
6034{
6035 struct cgroup_event *event = container_of(wait,
6036 struct cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05006037 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05006038 unsigned long flags = (unsigned long)key;
6039
6040 if (flags & POLLHUP) {
6041 /*
6042 * If the event has been detached at cgroup removal, we
6043 * can simply return knowing the other side will cleanup
6044 * for us.
6045 *
6046 * We can't race against event freeing since the other
6047 * side will require wqh->lock via remove_wait_queue(),
6048 * which we hold.
6049 */
Tejun Heofba94802013-11-22 18:20:43 -05006050 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05006051 if (!list_empty(&event->list)) {
6052 list_del_init(&event->list);
6053 /*
6054 * We are in atomic context, but cgroup_event_remove()
6055 * may sleep, so we have to call it in workqueue.
6056 */
6057 schedule_work(&event->remove);
6058 }
Tejun Heofba94802013-11-22 18:20:43 -05006059 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05006060 }
6061
6062 return 0;
6063}
6064
6065static void cgroup_event_ptable_queue_proc(struct file *file,
6066 wait_queue_head_t *wqh, poll_table *pt)
6067{
6068 struct cgroup_event *event = container_of(pt,
6069 struct cgroup_event, pt);
6070
6071 event->wqh = wqh;
6072 add_wait_queue(wqh, &event->wait);
6073}
6074
6075/*
6076 * Parse input and register new cgroup event handler.
6077 *
6078 * Input must be in format '<event_fd> <control_fd> <args>'.
6079 * Interpretation of args is defined by control file implementation.
6080 */
Tejun Heob5557c42013-11-22 18:20:42 -05006081static int cgroup_write_event_control(struct cgroup_subsys_state *css,
Tejun Heo79bd9812013-11-22 18:20:42 -05006082 struct cftype *cft, const char *buffer)
6083{
Tejun Heofba94802013-11-22 18:20:43 -05006084 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006085 struct cgroup_event *event;
6086 struct cgroup_subsys_state *cfile_css;
6087 unsigned int efd, cfd;
6088 struct fd efile;
6089 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05006090 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05006091 char *endp;
6092 int ret;
6093
6094 efd = simple_strtoul(buffer, &endp, 10);
6095 if (*endp != ' ')
6096 return -EINVAL;
6097 buffer = endp + 1;
6098
6099 cfd = simple_strtoul(buffer, &endp, 10);
6100 if ((*endp != ' ') && (*endp != '\0'))
6101 return -EINVAL;
6102 buffer = endp + 1;
6103
6104 event = kzalloc(sizeof(*event), GFP_KERNEL);
6105 if (!event)
6106 return -ENOMEM;
6107
Tejun Heo59b6f872013-11-22 18:20:43 -05006108 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05006109 INIT_LIST_HEAD(&event->list);
6110 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
6111 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
6112 INIT_WORK(&event->remove, cgroup_event_remove);
6113
6114 efile = fdget(efd);
6115 if (!efile.file) {
6116 ret = -EBADF;
6117 goto out_kfree;
6118 }
6119
6120 event->eventfd = eventfd_ctx_fileget(efile.file);
6121 if (IS_ERR(event->eventfd)) {
6122 ret = PTR_ERR(event->eventfd);
6123 goto out_put_efile;
6124 }
6125
6126 cfile = fdget(cfd);
6127 if (!cfile.file) {
6128 ret = -EBADF;
6129 goto out_put_eventfd;
6130 }
6131
6132 /* the process need read permission on control file */
6133 /* AV: shouldn't we check that it's been opened for read instead? */
6134 ret = inode_permission(file_inode(cfile.file), MAY_READ);
6135 if (ret < 0)
6136 goto out_put_cfile;
6137
Tejun Heo79bd9812013-11-22 18:20:42 -05006138 /*
Tejun Heofba94802013-11-22 18:20:43 -05006139 * Determine the event callbacks and set them in @event. This used
6140 * to be done via struct cftype but cgroup core no longer knows
6141 * about these events. The following is crude but the whole thing
6142 * is for compatibility anyway.
6143 */
6144 name = cfile.file->f_dentry->d_name.name;
6145
6146 if (!strcmp(name, "memory.usage_in_bytes")) {
6147 event->register_event = mem_cgroup_usage_register_event;
6148 event->unregister_event = mem_cgroup_usage_unregister_event;
6149 } else if (!strcmp(name, "memory.oom_control")) {
6150 event->register_event = mem_cgroup_oom_register_event;
6151 event->unregister_event = mem_cgroup_oom_unregister_event;
6152 } else if (!strcmp(name, "memory.pressure_level")) {
6153 event->register_event = vmpressure_register_event;
6154 event->unregister_event = vmpressure_unregister_event;
6155 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05006156 event->register_event = memsw_cgroup_usage_register_event;
6157 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05006158 } else {
6159 ret = -EINVAL;
6160 goto out_put_cfile;
6161 }
6162
6163 /*
Tejun Heob5557c42013-11-22 18:20:42 -05006164 * Verify @cfile should belong to @css. Also, remaining events are
6165 * automatically removed on cgroup destruction but the removal is
6166 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05006167 */
6168 rcu_read_lock();
6169
6170 ret = -EINVAL;
Tejun Heob5557c42013-11-22 18:20:42 -05006171 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent,
6172 &mem_cgroup_subsys);
6173 if (cfile_css == css && css_tryget(css))
Tejun Heo79bd9812013-11-22 18:20:42 -05006174 ret = 0;
6175
6176 rcu_read_unlock();
6177 if (ret)
6178 goto out_put_cfile;
6179
Tejun Heo59b6f872013-11-22 18:20:43 -05006180 ret = event->register_event(memcg, event->eventfd, buffer);
Tejun Heo79bd9812013-11-22 18:20:42 -05006181 if (ret)
6182 goto out_put_css;
6183
6184 efile.file->f_op->poll(efile.file, &event->pt);
6185
Tejun Heofba94802013-11-22 18:20:43 -05006186 spin_lock(&memcg->event_list_lock);
6187 list_add(&event->list, &memcg->event_list);
6188 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05006189
6190 fdput(cfile);
6191 fdput(efile);
6192
6193 return 0;
6194
6195out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05006196 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006197out_put_cfile:
6198 fdput(cfile);
6199out_put_eventfd:
6200 eventfd_ctx_put(event->eventfd);
6201out_put_efile:
6202 fdput(efile);
6203out_kfree:
6204 kfree(event);
6205
6206 return ret;
6207}
6208
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006209static struct cftype mem_cgroup_files[] = {
6210 {
Balbir Singh0eea1032008-02-07 00:13:57 -08006211 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006212 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07006213 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006214 },
6215 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07006216 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006217 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07006218 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07006219 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07006220 },
6221 {
Balbir Singh0eea1032008-02-07 00:13:57 -08006222 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006223 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07006224 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07006225 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006226 },
6227 {
Balbir Singh296c81d2009-09-23 15:56:36 -07006228 .name = "soft_limit_in_bytes",
6229 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
6230 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07006231 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07006232 },
6233 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006234 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006235 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07006236 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07006237 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006238 },
Balbir Singh8697d332008-02-07 00:13:59 -08006239 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006240 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006241 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08006242 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08006243 {
6244 .name = "force_empty",
6245 .trigger = mem_cgroup_force_empty_write,
6246 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08006247 {
6248 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07006249 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08006250 .write_u64 = mem_cgroup_hierarchy_write,
6251 .read_u64 = mem_cgroup_hierarchy_read,
6252 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006253 {
Tejun Heo79bd9812013-11-22 18:20:42 -05006254 .name = "cgroup.event_control",
6255 .write_string = cgroup_write_event_control,
6256 .flags = CFTYPE_NO_PREFIX,
6257 .mode = S_IWUGO,
6258 },
6259 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006260 .name = "swappiness",
6261 .read_u64 = mem_cgroup_swappiness_read,
6262 .write_u64 = mem_cgroup_swappiness_write,
6263 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006264 {
6265 .name = "move_charge_at_immigrate",
6266 .read_u64 = mem_cgroup_move_charge_read,
6267 .write_u64 = mem_cgroup_move_charge_write,
6268 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006269 {
6270 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07006271 .read_map = mem_cgroup_oom_control_read,
6272 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07006273 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
6274 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006275 {
6276 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006277 },
Ying Han406eb0c2011-05-26 16:25:37 -07006278#ifdef CONFIG_NUMA
6279 {
6280 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07006281 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07006282 },
6283#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006284#ifdef CONFIG_MEMCG_KMEM
6285 {
6286 .name = "kmem.limit_in_bytes",
6287 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
6288 .write_string = mem_cgroup_write,
6289 .read = mem_cgroup_read,
6290 },
6291 {
6292 .name = "kmem.usage_in_bytes",
6293 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
6294 .read = mem_cgroup_read,
6295 },
6296 {
6297 .name = "kmem.failcnt",
6298 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
6299 .trigger = mem_cgroup_reset,
6300 .read = mem_cgroup_read,
6301 },
6302 {
6303 .name = "kmem.max_usage_in_bytes",
6304 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6305 .trigger = mem_cgroup_reset,
6306 .read = mem_cgroup_read,
6307 },
Glauber Costa749c5412012-12-18 14:23:01 -08006308#ifdef CONFIG_SLABINFO
6309 {
6310 .name = "kmem.slabinfo",
6311 .read_seq_string = mem_cgroup_slabinfo_read,
6312 },
6313#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08006314#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07006315 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07006316};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006317
Michal Hocko2d110852013-02-22 16:34:43 -08006318#ifdef CONFIG_MEMCG_SWAP
6319static struct cftype memsw_cgroup_files[] = {
6320 {
6321 .name = "memsw.usage_in_bytes",
6322 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6323 .read = mem_cgroup_read,
Michal Hocko2d110852013-02-22 16:34:43 -08006324 },
6325 {
6326 .name = "memsw.max_usage_in_bytes",
6327 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6328 .trigger = mem_cgroup_reset,
6329 .read = mem_cgroup_read,
6330 },
6331 {
6332 .name = "memsw.limit_in_bytes",
6333 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6334 .write_string = mem_cgroup_write,
6335 .read = mem_cgroup_read,
6336 },
6337 {
6338 .name = "memsw.failcnt",
6339 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6340 .trigger = mem_cgroup_reset,
6341 .read = mem_cgroup_read,
6342 },
6343 { }, /* terminate */
6344};
6345#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006346static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006347{
6348 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006349 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006350 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006351 /*
6352 * This routine is called against possible nodes.
6353 * But it's BUG to call kmalloc() against offline node.
6354 *
6355 * TODO: this routine can waste much memory for nodes which will
6356 * never be onlined. It's better to use memory hotplug callback
6357 * function.
6358 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07006359 if (!node_state(node, N_NORMAL_MEMORY))
6360 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08006361 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006362 if (!pn)
6363 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006364
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006365 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6366 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08006367 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006368 mz->usage_in_excess = 0;
6369 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006370 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006371 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07006372 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006373 return 0;
6374}
6375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006376static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006377{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07006378 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08006379}
6380
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006381static struct mem_cgroup *mem_cgroup_alloc(void)
6382{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006383 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006384 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006385
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006386 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08006387 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006388 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006389 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006390 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006391
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006392 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07006393 return NULL;
6394
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006395 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6396 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006397 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006398 spin_lock_init(&memcg->pcp_counter_lock);
6399 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006400
6401out_free:
6402 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006403 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006404 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07006405 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08006406 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006407}
6408
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006409/*
Glauber Costac8b2a362012-12-18 14:22:13 -08006410 * At destroying mem_cgroup, references from swap_cgroup can remain.
6411 * (scanning all at force_empty is too costly...)
6412 *
6413 * Instead of clearing all references at force_empty, we remember
6414 * the number of reference from swap_cgroup and free mem_cgroup when
6415 * it goes down to 0.
6416 *
6417 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07006418 */
Glauber Costac8b2a362012-12-18 14:22:13 -08006419
6420static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006421{
Glauber Costac8b2a362012-12-18 14:22:13 -08006422 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006423 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006424
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006425 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006426 free_css_id(&mem_cgroup_subsys, &memcg->css);
6427
6428 for_each_node(node)
6429 free_mem_cgroup_per_zone_info(memcg, node);
6430
6431 free_percpu(memcg->stat);
6432
Glauber Costa3f134612012-05-29 15:07:11 -07006433 /*
6434 * We need to make sure that (at least for now), the jump label
6435 * destruction code runs outside of the cgroup lock. This is because
6436 * get_online_cpus(), which is called from the static_branch update,
6437 * can't be called inside the cgroup_lock. cpusets are the ones
6438 * enforcing this dependency, so if they ever change, we might as well.
6439 *
6440 * schedule_work() will guarantee this happens. Be careful if you need
6441 * to move this code around, and make sure it is outside
6442 * the cgroup_lock.
6443 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006444 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006445 if (size < PAGE_SIZE)
6446 kfree(memcg);
6447 else
6448 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006449}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006450
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006451/*
6452 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6453 */
Glauber Costae1aab162011-12-11 21:47:03 +00006454struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006455{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006456 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006457 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006458 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006459}
Glauber Costae1aab162011-12-11 21:47:03 +00006460EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006461
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006462static void __init mem_cgroup_soft_limit_tree_init(void)
6463{
6464 struct mem_cgroup_tree_per_node *rtpn;
6465 struct mem_cgroup_tree_per_zone *rtpz;
6466 int tmp, node, zone;
6467
6468 for_each_node(node) {
6469 tmp = node;
6470 if (!node_state(node, N_NORMAL_MEMORY))
6471 tmp = -1;
6472 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6473 BUG_ON(!rtpn);
6474
6475 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6476
6477 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6478 rtpz = &rtpn->rb_tree_per_zone[zone];
6479 rtpz->rb_root = RB_ROOT;
6480 spin_lock_init(&rtpz->lock);
6481 }
6482 }
6483}
6484
Li Zefan0eb253e2009-01-15 13:51:25 -08006485static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04006486mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006487{
Glauber Costad142e3e2013-02-22 16:34:52 -08006488 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006489 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006490 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006491
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006492 memcg = mem_cgroup_alloc();
6493 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006494 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006495
Bob Liu3ed28fa2012-01-12 17:19:04 -08006496 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006497 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006498 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006499
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006500 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04006501 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006502 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006503 res_counter_init(&memcg->res, NULL);
6504 res_counter_init(&memcg->memsw, NULL);
6505 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006506 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006507
Glauber Costad142e3e2013-02-22 16:34:52 -08006508 memcg->last_scanned_node = MAX_NUMNODES;
6509 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08006510 memcg->move_charge_at_immigrate = 0;
6511 mutex_init(&memcg->thresholds_lock);
6512 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006513 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05006514 INIT_LIST_HEAD(&memcg->event_list);
6515 spin_lock_init(&memcg->event_list_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08006516
6517 return &memcg->css;
6518
6519free_out:
6520 __mem_cgroup_free(memcg);
6521 return ERR_PTR(error);
6522}
6523
6524static int
Tejun Heoeb954192013-08-08 20:11:23 -04006525mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08006526{
Tejun Heoeb954192013-08-08 20:11:23 -04006527 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6528 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08006529 int error = 0;
6530
Tejun Heo63876982013-08-08 20:11:23 -04006531 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08006532 return 0;
6533
Glauber Costa09998212013-02-22 16:34:55 -08006534 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006535
6536 memcg->use_hierarchy = parent->use_hierarchy;
6537 memcg->oom_kill_disable = parent->oom_kill_disable;
6538 memcg->swappiness = mem_cgroup_swappiness(parent);
6539
6540 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006541 res_counter_init(&memcg->res, &parent->res);
6542 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006543 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006544
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006545 /*
Li Zefan8d76a972013-07-08 16:00:36 -07006546 * No need to take a reference to the parent because cgroup
6547 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006548 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08006549 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006550 res_counter_init(&memcg->res, NULL);
6551 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006552 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006553 /*
6554 * Deeper hierachy with use_hierarchy == false doesn't make
6555 * much sense so let cgroup subsystem know about this
6556 * unfortunate state in our controller.
6557 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006558 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006559 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006560 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006561
6562 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006563 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006564 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006565}
6566
Michal Hocko5f578162013-04-29 15:07:17 -07006567/*
6568 * Announce all parents that a group from their hierarchy is gone.
6569 */
6570static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6571{
6572 struct mem_cgroup *parent = memcg;
6573
6574 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006575 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006576
6577 /*
6578 * if the root memcg is not hierarchical we have to check it
6579 * explicitely.
6580 */
6581 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006582 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006583}
6584
Tejun Heoeb954192013-08-08 20:11:23 -04006585static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006586{
Tejun Heoeb954192013-08-08 20:11:23 -04006587 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05006588 struct cgroup_event *event, *tmp;
6589
6590 /*
6591 * Unregister events and notify userspace.
6592 * Notify userspace about cgroup removing only after rmdir of cgroup
6593 * directory to avoid race between userspace and kernelspace.
6594 */
Tejun Heofba94802013-11-22 18:20:43 -05006595 spin_lock(&memcg->event_list_lock);
6596 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05006597 list_del_init(&event->list);
6598 schedule_work(&event->remove);
6599 }
Tejun Heofba94802013-11-22 18:20:43 -05006600 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006601
Li Zefan10d5ebf2013-07-08 16:00:33 -07006602 kmem_cgroup_css_offline(memcg);
6603
Michal Hocko5f578162013-04-29 15:07:17 -07006604 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006605 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006606 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006607 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006608}
6609
Tejun Heoeb954192013-08-08 20:11:23 -04006610static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006611{
Tejun Heoeb954192013-08-08 20:11:23 -04006612 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006613
Li Zefan10d5ebf2013-07-08 16:00:33 -07006614 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006615 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006616}
6617
Daisuke Nishimura02491442010-03-10 15:22:17 -08006618#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006619/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006620#define PRECHARGE_COUNT_AT_ONCE 256
6621static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006622{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006623 int ret = 0;
6624 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006625 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006626
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006627 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006628 mc.precharge += count;
6629 /* we don't need css_get for root */
6630 return ret;
6631 }
6632 /* try to charge at once */
6633 if (count > 1) {
6634 struct res_counter *dummy;
6635 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006636 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006637 * by cgroup_lock_live_cgroup() that it is not removed and we
6638 * are still under the same cgroup_mutex. So we can postpone
6639 * css_get().
6640 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006641 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006642 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006643 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006644 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006645 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006646 goto one_by_one;
6647 }
6648 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006649 return ret;
6650 }
6651one_by_one:
6652 /* fall back to one by one charge */
6653 while (count--) {
6654 if (signal_pending(current)) {
6655 ret = -EINTR;
6656 break;
6657 }
6658 if (!batch_count--) {
6659 batch_count = PRECHARGE_COUNT_AT_ONCE;
6660 cond_resched();
6661 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006662 ret = __mem_cgroup_try_charge(NULL,
6663 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006664 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006665 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006666 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006667 mc.precharge++;
6668 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006669 return ret;
6670}
6671
6672/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006673 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006674 * @vma: the vma the pte to be checked belongs
6675 * @addr: the address corresponding to the pte to be checked
6676 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006677 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006678 *
6679 * Returns
6680 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6681 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6682 * move charge. if @target is not NULL, the page is stored in target->page
6683 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006684 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6685 * target for charge migration. if @target is not NULL, the entry is stored
6686 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006687 *
6688 * Called with pte lock held.
6689 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006690union mc_target {
6691 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006692 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006693};
6694
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006695enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006696 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006697 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006698 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006699};
6700
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006701static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6702 unsigned long addr, pte_t ptent)
6703{
6704 struct page *page = vm_normal_page(vma, addr, ptent);
6705
6706 if (!page || !page_mapped(page))
6707 return NULL;
6708 if (PageAnon(page)) {
6709 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006710 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006711 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006712 } else if (!move_file())
6713 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006714 return NULL;
6715 if (!get_page_unless_zero(page))
6716 return NULL;
6717
6718 return page;
6719}
6720
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006721#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006722static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6723 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6724{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006725 struct page *page = NULL;
6726 swp_entry_t ent = pte_to_swp_entry(ptent);
6727
6728 if (!move_anon() || non_swap_entry(ent))
6729 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006730 /*
6731 * Because lookup_swap_cache() updates some statistics counter,
6732 * we call find_get_page() with swapper_space directly.
6733 */
Shaohua Li33806f02013-02-22 16:34:37 -08006734 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006735 if (do_swap_account)
6736 entry->val = ent.val;
6737
6738 return page;
6739}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006740#else
6741static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6742 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6743{
6744 return NULL;
6745}
6746#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006747
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006748static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6749 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6750{
6751 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006752 struct address_space *mapping;
6753 pgoff_t pgoff;
6754
6755 if (!vma->vm_file) /* anonymous vma */
6756 return NULL;
6757 if (!move_file())
6758 return NULL;
6759
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006760 mapping = vma->vm_file->f_mapping;
6761 if (pte_none(ptent))
6762 pgoff = linear_page_index(vma, addr);
6763 else /* pte_file(ptent) is true */
6764 pgoff = pte_to_pgoff(ptent);
6765
6766 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006767 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006768
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006769#ifdef CONFIG_SWAP
6770 /* shmem/tmpfs may report page out on swap: account for that too. */
6771 if (radix_tree_exceptional_entry(page)) {
6772 swp_entry_t swap = radix_to_swp_entry(page);
6773 if (do_swap_account)
6774 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006775 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006776 }
6777#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006778 return page;
6779}
6780
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006781static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006782 unsigned long addr, pte_t ptent, union mc_target *target)
6783{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006784 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006785 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006786 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006787 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006788
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006789 if (pte_present(ptent))
6790 page = mc_handle_present_pte(vma, addr, ptent);
6791 else if (is_swap_pte(ptent))
6792 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006793 else if (pte_none(ptent) || pte_file(ptent))
6794 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006795
6796 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006797 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006798 if (page) {
6799 pc = lookup_page_cgroup(page);
6800 /*
6801 * Do only loose check w/o page_cgroup lock.
6802 * mem_cgroup_move_account() checks the pc is valid or not under
6803 * the lock.
6804 */
6805 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6806 ret = MC_TARGET_PAGE;
6807 if (target)
6808 target->page = page;
6809 }
6810 if (!ret || !target)
6811 put_page(page);
6812 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006813 /* There is a swap entry and a page doesn't exist or isn't charged */
6814 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006815 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006816 ret = MC_TARGET_SWAP;
6817 if (target)
6818 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006819 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006820 return ret;
6821}
6822
Naoya Horiguchi12724852012-03-21 16:34:28 -07006823#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6824/*
6825 * We don't consider swapping or file mapped pages because THP does not
6826 * support them for now.
6827 * Caller should make sure that pmd_trans_huge(pmd) is true.
6828 */
6829static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6830 unsigned long addr, pmd_t pmd, union mc_target *target)
6831{
6832 struct page *page = NULL;
6833 struct page_cgroup *pc;
6834 enum mc_target_type ret = MC_TARGET_NONE;
6835
6836 page = pmd_page(pmd);
6837 VM_BUG_ON(!page || !PageHead(page));
6838 if (!move_anon())
6839 return ret;
6840 pc = lookup_page_cgroup(page);
6841 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6842 ret = MC_TARGET_PAGE;
6843 if (target) {
6844 get_page(page);
6845 target->page = page;
6846 }
6847 }
6848 return ret;
6849}
6850#else
6851static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6852 unsigned long addr, pmd_t pmd, union mc_target *target)
6853{
6854 return MC_TARGET_NONE;
6855}
6856#endif
6857
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006858static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6859 unsigned long addr, unsigned long end,
6860 struct mm_walk *walk)
6861{
6862 struct vm_area_struct *vma = walk->private;
6863 pte_t *pte;
6864 spinlock_t *ptl;
6865
Naoya Horiguchi12724852012-03-21 16:34:28 -07006866 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6867 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6868 mc.precharge += HPAGE_PMD_NR;
6869 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006870 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006871 }
Dave Hansen03319322011-03-22 16:32:56 -07006872
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006873 if (pmd_trans_unstable(pmd))
6874 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006875 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6876 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006877 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006878 mc.precharge++; /* increment precharge temporarily */
6879 pte_unmap_unlock(pte - 1, ptl);
6880 cond_resched();
6881
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006882 return 0;
6883}
6884
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006885static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6886{
6887 unsigned long precharge;
6888 struct vm_area_struct *vma;
6889
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006890 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006891 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6892 struct mm_walk mem_cgroup_count_precharge_walk = {
6893 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6894 .mm = mm,
6895 .private = vma,
6896 };
6897 if (is_vm_hugetlb_page(vma))
6898 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006899 walk_page_range(vma->vm_start, vma->vm_end,
6900 &mem_cgroup_count_precharge_walk);
6901 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006902 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006903
6904 precharge = mc.precharge;
6905 mc.precharge = 0;
6906
6907 return precharge;
6908}
6909
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006910static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6911{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006912 unsigned long precharge = mem_cgroup_count_precharge(mm);
6913
6914 VM_BUG_ON(mc.moving_task);
6915 mc.moving_task = current;
6916 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006917}
6918
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006919/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6920static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006921{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006922 struct mem_cgroup *from = mc.from;
6923 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006924 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006925
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006926 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006927 if (mc.precharge) {
6928 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6929 mc.precharge = 0;
6930 }
6931 /*
6932 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6933 * we must uncharge here.
6934 */
6935 if (mc.moved_charge) {
6936 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6937 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006938 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006939 /* we must fixup refcnts and charges */
6940 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006941 /* uncharge swap account from the old cgroup */
6942 if (!mem_cgroup_is_root(mc.from))
6943 res_counter_uncharge(&mc.from->memsw,
6944 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006945
6946 for (i = 0; i < mc.moved_swap; i++)
6947 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006948
6949 if (!mem_cgroup_is_root(mc.to)) {
6950 /*
6951 * we charged both to->res and to->memsw, so we should
6952 * uncharge to->res.
6953 */
6954 res_counter_uncharge(&mc.to->res,
6955 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006956 }
Li Zefan40503772013-07-08 16:00:34 -07006957 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006958 mc.moved_swap = 0;
6959 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006960 memcg_oom_recover(from);
6961 memcg_oom_recover(to);
6962 wake_up_all(&mc.waitq);
6963}
6964
6965static void mem_cgroup_clear_mc(void)
6966{
6967 struct mem_cgroup *from = mc.from;
6968
6969 /*
6970 * we must clear moving_task before waking up waiters at the end of
6971 * task migration.
6972 */
6973 mc.moving_task = NULL;
6974 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006975 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006976 mc.from = NULL;
6977 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006978 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006979 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006980}
6981
Tejun Heoeb954192013-08-08 20:11:23 -04006982static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006983 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006984{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006985 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006986 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006987 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006988 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006989
Glauber Costaee5e8472013-02-22 16:34:50 -08006990 /*
6991 * We are now commited to this value whatever it is. Changes in this
6992 * tunable will only affect upcoming migrations, not the current one.
6993 * So we need to save it, and keep it going.
6994 */
6995 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6996 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006997 struct mm_struct *mm;
6998 struct mem_cgroup *from = mem_cgroup_from_task(p);
6999
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07007000 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007001
7002 mm = get_task_mm(p);
7003 if (!mm)
7004 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007005 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007006 if (mm->owner == p) {
7007 VM_BUG_ON(mc.from);
7008 VM_BUG_ON(mc.to);
7009 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08007010 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08007011 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07007012 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07007013 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007014 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07007015 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08007016 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07007017 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007018 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007019
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007020 ret = mem_cgroup_precharge_mc(mm);
7021 if (ret)
7022 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007023 }
7024 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007025 }
7026 return ret;
7027}
7028
Tejun Heoeb954192013-08-08 20:11:23 -04007029static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007030 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007031{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007032 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007033}
7034
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007035static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
7036 unsigned long addr, unsigned long end,
7037 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007038{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007039 int ret = 0;
7040 struct vm_area_struct *vma = walk->private;
7041 pte_t *pte;
7042 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07007043 enum mc_target_type target_type;
7044 union mc_target target;
7045 struct page *page;
7046 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007047
Naoya Horiguchi12724852012-03-21 16:34:28 -07007048 /*
7049 * We don't take compound_lock() here but no race with splitting thp
7050 * happens because:
7051 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
7052 * under splitting, which means there's no concurrent thp split,
7053 * - if another thread runs into split_huge_page() just after we
7054 * entered this if-block, the thread must wait for page table lock
7055 * to be unlocked in __split_huge_page_splitting(), where the main
7056 * part of thp split is not executed yet.
7057 */
7058 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07007059 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07007060 spin_unlock(&vma->vm_mm->page_table_lock);
7061 return 0;
7062 }
7063 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
7064 if (target_type == MC_TARGET_PAGE) {
7065 page = target.page;
7066 if (!isolate_lru_page(page)) {
7067 pc = lookup_page_cgroup(page);
7068 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07007069 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07007070 mc.precharge -= HPAGE_PMD_NR;
7071 mc.moved_charge += HPAGE_PMD_NR;
7072 }
7073 putback_lru_page(page);
7074 }
7075 put_page(page);
7076 }
7077 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07007078 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07007079 }
7080
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07007081 if (pmd_trans_unstable(pmd))
7082 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007083retry:
7084 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
7085 for (; addr != end; addr += PAGE_SIZE) {
7086 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08007087 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007088
7089 if (!mc.precharge)
7090 break;
7091
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07007092 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007093 case MC_TARGET_PAGE:
7094 page = target.page;
7095 if (isolate_lru_page(page))
7096 goto put;
7097 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07007098 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07007099 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007100 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08007101 /* we uncharge from mc.from later. */
7102 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007103 }
7104 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07007105put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007106 put_page(page);
7107 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08007108 case MC_TARGET_SWAP:
7109 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07007110 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08007111 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08007112 /* we fixup refcnts and charges later. */
7113 mc.moved_swap++;
7114 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08007115 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007116 default:
7117 break;
7118 }
7119 }
7120 pte_unmap_unlock(pte - 1, ptl);
7121 cond_resched();
7122
7123 if (addr != end) {
7124 /*
7125 * We have consumed all precharges we got in can_attach().
7126 * We try charge one by one, but don't do any additional
7127 * charges to mc.to if we have failed in charge once in attach()
7128 * phase.
7129 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08007130 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007131 if (!ret)
7132 goto retry;
7133 }
7134
7135 return ret;
7136}
7137
7138static void mem_cgroup_move_charge(struct mm_struct *mm)
7139{
7140 struct vm_area_struct *vma;
7141
7142 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007143retry:
7144 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
7145 /*
7146 * Someone who are holding the mmap_sem might be waiting in
7147 * waitq. So we cancel all extra charges, wake up all waiters,
7148 * and retry. Because we cancel precharges, we might not be able
7149 * to move enough charges, but moving charge is a best-effort
7150 * feature anyway, so it wouldn't be a big problem.
7151 */
7152 __mem_cgroup_clear_mc();
7153 cond_resched();
7154 goto retry;
7155 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007156 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7157 int ret;
7158 struct mm_walk mem_cgroup_move_charge_walk = {
7159 .pmd_entry = mem_cgroup_move_charge_pte_range,
7160 .mm = mm,
7161 .private = vma,
7162 };
7163 if (is_vm_hugetlb_page(vma))
7164 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08007165 ret = walk_page_range(vma->vm_start, vma->vm_end,
7166 &mem_cgroup_move_charge_walk);
7167 if (ret)
7168 /*
7169 * means we have consumed all precharges and failed in
7170 * doing additional charge. Just abandon here.
7171 */
7172 break;
7173 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007174 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007175}
7176
Tejun Heoeb954192013-08-08 20:11:23 -04007177static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007178 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08007179{
Tejun Heo2f7ee562011-12-12 18:12:21 -08007180 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007181 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007182
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007183 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007184 if (mc.to)
7185 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08007186 mmput(mm);
7187 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07007188 if (mc.to)
7189 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08007190}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007191#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04007192static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007193 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007194{
7195 return 0;
7196}
Tejun Heoeb954192013-08-08 20:11:23 -04007197static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007198 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007199{
7200}
Tejun Heoeb954192013-08-08 20:11:23 -04007201static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08007202 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07007203{
7204}
7205#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08007206
Tejun Heof00baae2013-04-15 13:41:15 -07007207/*
7208 * Cgroup retains root cgroups across [un]mount cycles making it necessary
7209 * to verify sane_behavior flag on each mount attempt.
7210 */
Tejun Heoeb954192013-08-08 20:11:23 -04007211static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07007212{
7213 /*
7214 * use_hierarchy is forced with sane_behavior. cgroup core
7215 * guarantees that @root doesn't have any children, so turning it
7216 * on for the root memcg is enough.
7217 */
Tejun Heoeb954192013-08-08 20:11:23 -04007218 if (cgroup_sane_behavior(root_css->cgroup))
7219 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07007220}
7221
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007222struct cgroup_subsys mem_cgroup_subsys = {
7223 .name = "memory",
7224 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007225 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08007226 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08007227 .css_offline = mem_cgroup_css_offline,
7228 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08007229 .can_attach = mem_cgroup_can_attach,
7230 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08007231 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07007232 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07007233 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08007234 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07007235 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08007236};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007237
Andrew Mortonc255a452012-07-31 16:43:02 -07007238#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08007239static int __init enable_swap_account(char *s)
7240{
Michal Hockoa2c89902011-05-24 17:12:50 -07007241 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007242 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07007243 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08007244 really_do_swap_account = 0;
7245 return 1;
7246}
Michal Hockoa2c89902011-05-24 17:12:50 -07007247__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007248
Michal Hocko2d110852013-02-22 16:34:43 -08007249static void __init memsw_file_init(void)
7250{
Michal Hocko6acc8b02013-02-22 16:34:45 -08007251 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08007252}
Michal Hocko6acc8b02013-02-22 16:34:45 -08007253
7254static void __init enable_swap_cgroup(void)
7255{
7256 if (!mem_cgroup_disabled() && really_do_swap_account) {
7257 do_swap_account = 1;
7258 memsw_file_init();
7259 }
7260}
7261
Michal Hocko2d110852013-02-22 16:34:43 -08007262#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08007263static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08007264{
7265}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08007266#endif
Michal Hocko2d110852013-02-22 16:34:43 -08007267
7268/*
Michal Hocko10813122013-02-22 16:35:41 -08007269 * subsys_initcall() for memory controller.
7270 *
7271 * Some parts like hotcpu_notifier() have to be initialized from this context
7272 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
7273 * everything that doesn't depend on a specific mem_cgroup structure should
7274 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08007275 */
7276static int __init mem_cgroup_init(void)
7277{
7278 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08007279 enable_swap_cgroup();
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07007280 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08007281 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08007282 return 0;
7283}
7284subsys_initcall(mem_cgroup_init);