blob: 9cda99dfac4f202a9bc69e4923245ee698ff842a [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>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070052#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070053#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070054#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080055#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070056#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070057#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050058#include <linux/file.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080059#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000060#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070061#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000062#include <net/tcp_memcontrol.h>
Qiang Huangf35c3a82013-11-12 15:08:22 -080063#include "slab.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
Tejun Heo073219e2014-02-08 10:36:58 -050069struct cgroup_subsys memory_cgrp_subsys __read_mostly;
70EXPORT_SYMBOL(memory_cgrp_subsys);
David Rientjes68ae5642012-12-12 13:51:57 -080071
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
Fabian Frederickada4ba52014-06-04 16:08:08 -070083static int really_do_swap_account __initdata;
Michal Hockoa42c3902010-11-24 12:57:08 -080084#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;
Hugh Dickinsd2ab70a2014-01-23 15:53:30 -0800152 int last_dead_count;
Michal Hocko5f578162013-04-29 15:07:17 -0700153
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 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500234struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500235 /*
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
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -0700295 /* css_online() has been completed */
296 int initialized;
297
Li Zefan465939a2013-07-08 16:00:38 -0700298 /*
299 * the counter to account for mem+swap usage.
300 */
301 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700302
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800303 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800304 * the counter to account for kernel memory usage.
305 */
306 struct res_counter kmem;
307 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800308 * Should the accounting and control be hierarchical, per subtree?
309 */
310 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800311 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700312
313 bool oom_lock;
314 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700315 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700316
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700317 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700318 /* OOM-Killer disable */
319 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800320
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)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700357 struct cg_proto tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000358#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800359#if defined(CONFIG_MEMCG_KMEM)
Vladimir Davydovbd673142014-06-04 16:07:40 -0700360 /* analogous to slab_common's slab_caches list, but per-memcg;
361 * protected by memcg_slab_mutex */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800362 struct list_head memcg_slab_caches;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800363 /* Index in the kmem_cache->memcg_params->memcg_caches array */
364 int kmemcg_id;
365#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800366
367 int last_scanned_node;
368#if MAX_NUMNODES > 1
369 nodemask_t scan_nodes;
370 atomic_t numainfo_events;
371 atomic_t numainfo_updating;
372#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700373
Tejun Heofba94802013-11-22 18:20:43 -0500374 /* List of events which userspace want to receive */
375 struct list_head event_list;
376 spinlock_t event_list_lock;
377
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700378 struct mem_cgroup_per_node *nodeinfo[0];
379 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800380};
381
Glauber Costa510fc4e2012-12-18 14:21:47 -0800382/* internal only representation about the status of kmem accounting. */
383enum {
Vladimir Davydov6de64be2014-01-23 15:53:08 -0800384 KMEM_ACCOUNTED_ACTIVE, /* accounted by this cgroup itself */
Glauber Costa7de37682012-12-18 14:22:07 -0800385 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800386};
387
Glauber Costa510fc4e2012-12-18 14:21:47 -0800388#ifdef CONFIG_MEMCG_KMEM
389static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
390{
391 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
392}
Glauber Costa7de37682012-12-18 14:22:07 -0800393
394static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
395{
396 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
397}
398
399static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
400{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700401 /*
402 * Our caller must use css_get() first, because memcg_uncharge_kmem()
403 * will call css_put() if it sees the memcg is dead.
404 */
405 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800406 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
407 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
408}
409
410static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
411{
412 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
413 &memcg->kmem_account_flags);
414}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800415#endif
416
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800417/* Stuffs for move charges at task migration. */
418/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800419 * Types of charges to be moved. "move_charge_at_immitgrate" and
420 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800421 */
422enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800423 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700424 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800425 NR_MOVE_TYPE,
426};
427
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800428/* "mc" and its members are protected by cgroup_mutex */
429static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800430 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800431 struct mem_cgroup *from;
432 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800433 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800434 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800435 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800436 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800437 struct task_struct *moving_task; /* a task moving charges */
438 wait_queue_head_t waitq; /* a waitq for other context */
439} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700440 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800441 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
442};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800443
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700444static bool move_anon(void)
445{
Glauber Costaee5e8472013-02-22 16:34:50 -0800446 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700447}
448
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700449static bool move_file(void)
450{
Glauber Costaee5e8472013-02-22 16:34:50 -0800451 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700452}
453
Balbir Singh4e416952009-09-23 15:56:39 -0700454/*
455 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
456 * limit reclaim to prevent infinite loops, if they ever occur.
457 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700458#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700459#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700460
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800461enum charge_type {
462 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700463 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800464 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700465 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700466 NR_CHARGE_TYPE,
467};
468
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800469/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800470enum res_type {
471 _MEM,
472 _MEMSWAP,
473 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800474 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800475};
476
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700477#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
478#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800479#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700480/* Used for OOM nofiier */
481#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800482
Balbir Singh75822b42009-09-23 15:56:38 -0700483/*
484 * Reclaim flags for mem_cgroup_hierarchical_reclaim
485 */
486#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
487#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
488#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
489#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
490
Glauber Costa09998212013-02-22 16:34:55 -0800491/*
492 * The memcg_create_mutex will be held whenever a new cgroup is created.
493 * As a consequence, any change that needs to protect against new child cgroups
494 * appearing has to hold it as well.
495 */
496static DEFINE_MUTEX(memcg_create_mutex);
497
Wanpeng Lib2145142012-07-31 16:46:01 -0700498struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
499{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400500 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700501}
502
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700503/* Some nice accessors for the vmpressure. */
504struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
505{
506 if (!memcg)
507 memcg = root_mem_cgroup;
508 return &memcg->vmpressure;
509}
510
511struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
512{
513 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
514}
515
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700516static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
517{
518 return (memcg == root_mem_cgroup);
519}
520
Li Zefan4219b2d2013-09-23 16:56:29 +0800521/*
522 * We restrict the id in the range of [1, 65535], so it can fit into
523 * an unsigned short.
524 */
525#define MEM_CGROUP_ID_MAX USHRT_MAX
526
Li Zefan34c00c32013-09-23 16:56:01 +0800527static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
528{
Tejun Heo15a4c832014-05-04 15:09:14 -0400529 return memcg->css.id;
Li Zefan34c00c32013-09-23 16:56:01 +0800530}
531
532static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
533{
534 struct cgroup_subsys_state *css;
535
Tejun Heo7d699dd2014-05-04 15:09:13 -0400536 css = css_from_id(id, &memory_cgrp_subsys);
Li Zefan34c00c32013-09-23 16:56:01 +0800537 return mem_cgroup_from_css(css);
538}
539
Glauber Costae1aab162011-12-11 21:47:03 +0000540/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700541#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000542
Glauber Costae1aab162011-12-11 21:47:03 +0000543void sock_update_memcg(struct sock *sk)
544{
Glauber Costa376be5f2012-01-20 04:57:14 +0000545 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000546 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700547 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000548
549 BUG_ON(!sk->sk_prot->proto_cgroup);
550
Glauber Costaf3f511e2012-01-05 20:16:39 +0000551 /* Socket cloning can throw us here with sk_cgrp already
552 * filled. It won't however, necessarily happen from
553 * process context. So the test for root memcg given
554 * the current task's memcg won't help us in this case.
555 *
556 * Respecting the original socket's memcg is a better
557 * decision in this case.
558 */
559 if (sk->sk_cgrp) {
560 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700561 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000562 return;
563 }
564
Glauber Costae1aab162011-12-11 21:47:03 +0000565 rcu_read_lock();
566 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700567 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700568 if (!mem_cgroup_is_root(memcg) &&
Tejun Heoec903c02014-05-13 12:11:01 -0400569 memcg_proto_active(cg_proto) &&
570 css_tryget_online(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700571 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000572 }
573 rcu_read_unlock();
574 }
575}
576EXPORT_SYMBOL(sock_update_memcg);
577
578void sock_release_memcg(struct sock *sk)
579{
Glauber Costa376be5f2012-01-20 04:57:14 +0000580 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000581 struct mem_cgroup *memcg;
582 WARN_ON(!sk->sk_cgrp->memcg);
583 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700584 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000585 }
586}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000587
588struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
589{
590 if (!memcg || mem_cgroup_is_root(memcg))
591 return NULL;
592
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700593 return &memcg->tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000594}
595EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000596
Glauber Costa3f134612012-05-29 15:07:11 -0700597static void disarm_sock_keys(struct mem_cgroup *memcg)
598{
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700599 if (!memcg_proto_activated(&memcg->tcp_mem))
Glauber Costa3f134612012-05-29 15:07:11 -0700600 return;
601 static_key_slow_dec(&memcg_socket_limit_enabled);
602}
603#else
604static void disarm_sock_keys(struct mem_cgroup *memcg)
605{
606}
607#endif
608
Glauber Costaa8964b92012-12-18 14:22:09 -0800609#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800610/*
611 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800612 * The main reason for not using cgroup id for this:
613 * this works better in sparse environments, where we have a lot of memcgs,
614 * but only a few kmem-limited. Or also, if we have, for instance, 200
615 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
616 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800617 *
618 * The current size of the caches array is stored in
619 * memcg_limited_groups_array_size. It will double each time we have to
620 * increase it.
621 */
622static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800623int memcg_limited_groups_array_size;
624
Glauber Costa55007d82012-12-18 14:22:38 -0800625/*
626 * MIN_SIZE is different than 1, because we would like to avoid going through
627 * the alloc/free process all the time. In a small machine, 4 kmem-limited
628 * cgroups is a reasonable guess. In the future, it could be a parameter or
629 * tunable, but that is strictly not necessary.
630 *
Li Zefanb8627832013-09-23 16:56:47 +0800631 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800632 * this constant directly from cgroup, but it is understandable that this is
633 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800634 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800635 * increase ours as well if it increases.
636 */
637#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800638#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800639
Glauber Costad7f25f82012-12-18 14:22:40 -0800640/*
641 * A lot of the calls to the cache allocation functions are expected to be
642 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
643 * conditional to this static branch, we'll have to allow modules that does
644 * kmem_cache_alloc and the such to see this symbol as well
645 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800646struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800647EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800648
Vladimir Davydovf3bb3042014-10-09 15:28:45 -0700649static void memcg_free_cache_id(int id);
650
Glauber Costaa8964b92012-12-18 14:22:09 -0800651static void disarm_kmem_keys(struct mem_cgroup *memcg)
652{
Glauber Costa55007d82012-12-18 14:22:38 -0800653 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800654 static_key_slow_dec(&memcg_kmem_enabled_key);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -0700655 memcg_free_cache_id(memcg->kmemcg_id);
Glauber Costa55007d82012-12-18 14:22:38 -0800656 }
Glauber Costabea207c2012-12-18 14:22:11 -0800657 /*
658 * This check can't live in kmem destruction function,
659 * since the charges will outlive the cgroup
660 */
661 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800662}
663#else
664static void disarm_kmem_keys(struct mem_cgroup *memcg)
665{
666}
667#endif /* CONFIG_MEMCG_KMEM */
668
669static void disarm_static_keys(struct mem_cgroup *memcg)
670{
671 disarm_sock_keys(memcg);
672 disarm_kmem_keys(memcg);
673}
674
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700675static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800676
Balbir Singhf64c3f52009-09-23 15:56:37 -0700677static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700678mem_cgroup_zone_zoneinfo(struct mem_cgroup *memcg, struct zone *zone)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700679{
Jianyu Zhane2318752014-06-06 14:38:20 -0700680 int nid = zone_to_nid(zone);
681 int zid = zone_idx(zone);
682
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700683 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700684}
685
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700686struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100687{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700688 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100689}
690
Balbir Singhf64c3f52009-09-23 15:56:37 -0700691static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700692mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700693{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700694 int nid = page_to_nid(page);
695 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700696
Jianyu Zhane2318752014-06-06 14:38:20 -0700697 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700698}
699
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700700static struct mem_cgroup_tree_per_zone *
701soft_limit_tree_node_zone(int nid, int zid)
702{
703 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
704}
705
706static struct mem_cgroup_tree_per_zone *
707soft_limit_tree_from_page(struct page *page)
708{
709 int nid = page_to_nid(page);
710 int zid = page_zonenum(page);
711
712 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
713}
714
Johannes Weinercf2c8122014-06-06 14:38:21 -0700715static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz,
716 struct mem_cgroup_tree_per_zone *mctz,
717 unsigned long long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700718{
719 struct rb_node **p = &mctz->rb_root.rb_node;
720 struct rb_node *parent = NULL;
721 struct mem_cgroup_per_zone *mz_node;
722
723 if (mz->on_tree)
724 return;
725
726 mz->usage_in_excess = new_usage_in_excess;
727 if (!mz->usage_in_excess)
728 return;
729 while (*p) {
730 parent = *p;
731 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
732 tree_node);
733 if (mz->usage_in_excess < mz_node->usage_in_excess)
734 p = &(*p)->rb_left;
735 /*
736 * We can't avoid mem cgroups that are over their soft
737 * limit by the same amount
738 */
739 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
740 p = &(*p)->rb_right;
741 }
742 rb_link_node(&mz->tree_node, parent, p);
743 rb_insert_color(&mz->tree_node, &mctz->rb_root);
744 mz->on_tree = true;
745}
746
Johannes Weinercf2c8122014-06-06 14:38:21 -0700747static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
748 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700749{
750 if (!mz->on_tree)
751 return;
752 rb_erase(&mz->tree_node, &mctz->rb_root);
753 mz->on_tree = false;
754}
755
Johannes Weinercf2c8122014-06-06 14:38:21 -0700756static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
757 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700758{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700759 unsigned long flags;
760
761 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700762 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700763 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700764}
765
766
767static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
768{
769 unsigned long long excess;
770 struct mem_cgroup_per_zone *mz;
771 struct mem_cgroup_tree_per_zone *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700772
Jianyu Zhane2318752014-06-06 14:38:20 -0700773 mctz = soft_limit_tree_from_page(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700774 /*
775 * Necessary to update all ancestors when hierarchy is used.
776 * because their event counter is not touched.
777 */
778 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Jianyu Zhane2318752014-06-06 14:38:20 -0700779 mz = mem_cgroup_page_zoneinfo(memcg, page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700780 excess = res_counter_soft_limit_excess(&memcg->res);
781 /*
782 * We have to update the tree if mz is on RB-tree or
783 * mem is over its softlimit.
784 */
785 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700786 unsigned long flags;
787
788 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700789 /* if on-tree, remove it */
790 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700791 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700792 /*
793 * Insert again. mz->usage_in_excess will be updated.
794 * If excess is 0, no tree ops.
795 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700796 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700797 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700798 }
799 }
800}
801
802static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
803{
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700804 struct mem_cgroup_tree_per_zone *mctz;
Jianyu Zhane2318752014-06-06 14:38:20 -0700805 struct mem_cgroup_per_zone *mz;
806 int nid, zid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700807
Jianyu Zhane2318752014-06-06 14:38:20 -0700808 for_each_node(nid) {
809 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
810 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
811 mctz = soft_limit_tree_node_zone(nid, zid);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700812 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700813 }
814 }
815}
816
817static struct mem_cgroup_per_zone *
818__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
819{
820 struct rb_node *rightmost = NULL;
821 struct mem_cgroup_per_zone *mz;
822
823retry:
824 mz = NULL;
825 rightmost = rb_last(&mctz->rb_root);
826 if (!rightmost)
827 goto done; /* Nothing to reclaim from */
828
829 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
830 /*
831 * Remove the node now but someone else can add it back,
832 * we will to add it back at the end of reclaim to its correct
833 * position in the tree.
834 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700835 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700836 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400837 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700838 goto retry;
839done:
840 return mz;
841}
842
843static struct mem_cgroup_per_zone *
844mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
845{
846 struct mem_cgroup_per_zone *mz;
847
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700848 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700849 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700850 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700851 return mz;
852}
853
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700854/*
855 * Implementation Note: reading percpu statistics for memcg.
856 *
857 * Both of vmstat[] and percpu_counter has threshold and do periodic
858 * synchronization to implement "quick" read. There are trade-off between
859 * reading cost and precision of value. Then, we may have a chance to implement
860 * a periodic synchronizion of counter in memcg's counter.
861 *
862 * But this _read() function is used for user interface now. The user accounts
863 * memory usage by memory cgroup and he _always_ requires exact value because
864 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
865 * have to visit all online cpus and make sum. So, for now, unnecessary
866 * synchronization is not implemented. (just implemented for cpu hotplug)
867 *
868 * If there are kernel internal actions which can make use of some not-exact
869 * value, and reading all cpu value can be performance bottleneck in some
870 * common workload, threashold and synchonization as vmstat[] should be
871 * implemented.
872 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700873static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700874 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800875{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700876 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800877 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800878
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700879 get_online_cpus();
880 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700881 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700882#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883 spin_lock(&memcg->pcp_counter_lock);
884 val += memcg->nocpu_base.count[idx];
885 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700886#endif
887 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800888 return val;
889}
890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700891static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700892 enum mem_cgroup_events_index idx)
893{
894 unsigned long val = 0;
895 int cpu;
896
David Rientjes9c567512013-10-16 13:46:43 -0700897 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700898 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700899 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700900#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700901 spin_lock(&memcg->pcp_counter_lock);
902 val += memcg->nocpu_base.events[idx];
903 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700904#endif
David Rientjes9c567512013-10-16 13:46:43 -0700905 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700906 return val;
907}
908
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700909static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700910 struct page *page,
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700911 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800912{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700913 /*
914 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
915 * counted as CACHE even if it's on ANON LRU.
916 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700917 if (PageAnon(page))
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700918 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700919 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800920 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700921 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700922 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700923
David Rientjesb070e652013-05-07 16:18:09 -0700924 if (PageTransHuge(page))
925 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
926 nr_pages);
927
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800928 /* pagein of a big page is an event. So, ignore page size */
929 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700930 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800931 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700932 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800933 nr_pages = -nr_pages; /* for event */
934 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800935
Johannes Weiner13114712012-05-29 15:07:07 -0700936 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800937}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800938
Jianyu Zhane2318752014-06-06 14:38:20 -0700939unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700940{
941 struct mem_cgroup_per_zone *mz;
942
943 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
944 return mz->lru_size[lru];
945}
946
Jianyu Zhane2318752014-06-06 14:38:20 -0700947static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
948 int nid,
949 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700950{
Jianyu Zhane2318752014-06-06 14:38:20 -0700951 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700952 int zid;
953
Jianyu Zhane2318752014-06-06 14:38:20 -0700954 VM_BUG_ON((unsigned)nid >= nr_node_ids);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700955
Jianyu Zhane2318752014-06-06 14:38:20 -0700956 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
957 struct mem_cgroup_per_zone *mz;
958 enum lru_list lru;
959
960 for_each_lru(lru) {
961 if (!(BIT(lru) & lru_mask))
962 continue;
963 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
964 nr += mz->lru_size[lru];
965 }
966 }
967 return nr;
Ying Han889976d2011-05-26 16:25:33 -0700968}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700969
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700970static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700971 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800972{
Jianyu Zhane2318752014-06-06 14:38:20 -0700973 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700974 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800975
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800976 for_each_node_state(nid, N_MEMORY)
Jianyu Zhane2318752014-06-06 14:38:20 -0700977 nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
978 return nr;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800979}
980
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800981static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
982 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800983{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700984 unsigned long val, next;
985
Johannes Weiner13114712012-05-29 15:07:07 -0700986 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700987 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700988 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800989 if ((long)next - (long)val < 0) {
990 switch (target) {
991 case MEM_CGROUP_TARGET_THRESH:
992 next = val + THRESHOLDS_EVENTS_TARGET;
993 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700994 case MEM_CGROUP_TARGET_SOFTLIMIT:
995 next = val + SOFTLIMIT_EVENTS_TARGET;
996 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800997 case MEM_CGROUP_TARGET_NUMAINFO:
998 next = val + NUMAINFO_EVENTS_TARGET;
999 break;
1000 default:
1001 break;
1002 }
1003 __this_cpu_write(memcg->stat->targets[target], next);
1004 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07001005 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001006 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001007}
1008
1009/*
1010 * Check events in order.
1011 *
1012 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001013static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001014{
1015 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001016 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1017 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001018 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001019 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001020
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001021 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1022 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001023#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001024 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1025 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001026#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001027 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001028 if (unlikely(do_softlimit))
1029 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001030#if MAX_NUMNODES > 1
1031 if (unlikely(do_numainfo))
1032 atomic_inc(&memcg->numainfo_events);
1033#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001034 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001035}
1036
Balbir Singhcf475ad2008-04-29 01:00:16 -07001037struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001038{
Balbir Singh31a78f22008-09-28 23:09:31 +01001039 /*
1040 * mm_update_next_owner() may clear mm->owner to NULL
1041 * if it races with swapoff, page migration, etc.
1042 * So this can be called with p == NULL.
1043 */
1044 if (unlikely(!p))
1045 return NULL;
1046
Tejun Heo073219e2014-02-08 10:36:58 -05001047 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001048}
1049
Johannes Weinerdf381972014-04-07 15:37:43 -07001050static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001051{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001052 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001053
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001054 rcu_read_lock();
1055 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -07001056 /*
1057 * Page cache insertions can happen withou an
1058 * actual mm context, e.g. during disk probing
1059 * on boot, loopback IO, acct() writes etc.
1060 */
1061 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -07001062 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -07001063 else {
1064 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1065 if (unlikely(!memcg))
1066 memcg = root_mem_cgroup;
1067 }
Tejun Heoec903c02014-05-13 12:11:01 -04001068 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001069 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001070 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001071}
1072
Michal Hocko16248d82013-04-29 15:07:19 -07001073/*
1074 * Returns a next (in a pre-order walk) alive memcg (with elevated css
1075 * ref. count) or NULL if the whole root's subtree has been visited.
1076 *
1077 * helper function to be used by mem_cgroup_iter
1078 */
1079static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Andrew Morton694fbc02013-09-24 15:27:37 -07001080 struct mem_cgroup *last_visited)
Michal Hocko16248d82013-04-29 15:07:19 -07001081{
Tejun Heo492eb212013-08-08 20:11:25 -04001082 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -07001083
Tejun Heobd8815a2013-08-08 20:11:27 -04001084 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -07001085skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -04001086 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -07001087
1088 /*
1089 * Even if we found a group we have to make sure it is
1090 * alive. css && !memcg means that the groups should be
1091 * skipped and we should continue the tree walk.
1092 * last_visited css is safe to use because it is
1093 * protected by css_get and the tree walk is rcu safe.
Michal Hocko0eef6152014-01-23 15:53:37 -08001094 *
1095 * We do not take a reference on the root of the tree walk
1096 * because we might race with the root removal when it would
1097 * be the only node in the iterated hierarchy and mem_cgroup_iter
1098 * would end up in an endless loop because it expects that at
1099 * least one valid node will be returned. Root cannot disappear
1100 * because caller of the iterator should hold it already so
1101 * skipping css reference should be safe.
Michal Hocko16248d82013-04-29 15:07:19 -07001102 */
Tejun Heo492eb212013-08-08 20:11:25 -04001103 if (next_css) {
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07001104 struct mem_cgroup *memcg = mem_cgroup_from_css(next_css);
1105
1106 if (next_css == &root->css)
1107 return memcg;
1108
1109 if (css_tryget_online(next_css)) {
1110 /*
1111 * Make sure the memcg is initialized:
1112 * mem_cgroup_css_online() orders the the
1113 * initialization against setting the flag.
1114 */
1115 if (smp_load_acquire(&memcg->initialized))
1116 return memcg;
1117 css_put(next_css);
1118 }
Michal Hocko0eef6152014-01-23 15:53:37 -08001119
1120 prev_css = next_css;
1121 goto skip_node;
Michal Hocko16248d82013-04-29 15:07:19 -07001122 }
1123
1124 return NULL;
1125}
1126
Johannes Weiner519ebea2013-07-03 15:04:51 -07001127static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1128{
1129 /*
1130 * When a group in the hierarchy below root is destroyed, the
1131 * hierarchy iterator can no longer be trusted since it might
1132 * have pointed to the destroyed group. Invalidate it.
1133 */
1134 atomic_inc(&root->dead_count);
1135}
1136
1137static struct mem_cgroup *
1138mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1139 struct mem_cgroup *root,
1140 int *sequence)
1141{
1142 struct mem_cgroup *position = NULL;
1143 /*
1144 * A cgroup destruction happens in two stages: offlining and
1145 * release. They are separated by a RCU grace period.
1146 *
1147 * If the iterator is valid, we may still race with an
1148 * offlining. The RCU lock ensures the object won't be
1149 * released, tryget will fail if we lost the race.
1150 */
1151 *sequence = atomic_read(&root->dead_count);
1152 if (iter->last_dead_count == *sequence) {
1153 smp_rmb();
1154 position = iter->last_visited;
Michal Hockoecc736f2014-01-23 15:53:35 -08001155
1156 /*
1157 * We cannot take a reference to root because we might race
1158 * with root removal and returning NULL would end up in
1159 * an endless loop on the iterator user level when root
1160 * would be returned all the time.
1161 */
1162 if (position && position != root &&
Tejun Heoec903c02014-05-13 12:11:01 -04001163 !css_tryget_online(&position->css))
Johannes Weiner519ebea2013-07-03 15:04:51 -07001164 position = NULL;
1165 }
1166 return position;
1167}
1168
1169static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1170 struct mem_cgroup *last_visited,
1171 struct mem_cgroup *new_position,
Michal Hockoecc736f2014-01-23 15:53:35 -08001172 struct mem_cgroup *root,
Johannes Weiner519ebea2013-07-03 15:04:51 -07001173 int sequence)
1174{
Michal Hockoecc736f2014-01-23 15:53:35 -08001175 /* root reference counting symmetric to mem_cgroup_iter_load */
1176 if (last_visited && last_visited != root)
Johannes Weiner519ebea2013-07-03 15:04:51 -07001177 css_put(&last_visited->css);
1178 /*
1179 * We store the sequence count from the time @last_visited was
1180 * loaded successfully instead of rereading it here so that we
1181 * don't lose destruction events in between. We could have
1182 * raced with the destruction of @new_position after all.
1183 */
1184 iter->last_visited = new_position;
1185 smp_wmb();
1186 iter->last_dead_count = sequence;
1187}
1188
Johannes Weiner56600482012-01-12 17:17:59 -08001189/**
1190 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1191 * @root: hierarchy root
1192 * @prev: previously returned memcg, NULL on first invocation
1193 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1194 *
1195 * Returns references to children of the hierarchy below @root, or
1196 * @root itself, or %NULL after a full round-trip.
1197 *
1198 * Caller must pass the return value in @prev on subsequent
1199 * invocations for reference counting, or use mem_cgroup_iter_break()
1200 * to cancel a hierarchy walk before the round-trip is complete.
1201 *
1202 * Reclaimers can specify a zone and a priority level in @reclaim to
1203 * divide up the memcgs in the hierarchy among all concurrent
1204 * reclaimers operating on the same zone and priority.
1205 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001206struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001207 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001208 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001209{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001210 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001211 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001212
Andrew Morton694fbc02013-09-24 15:27:37 -07001213 if (mem_cgroup_disabled())
1214 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001215
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001216 if (!root)
1217 root = root_mem_cgroup;
1218
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001219 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001220 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001221
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001222 if (!root->use_hierarchy && root != root_mem_cgroup) {
1223 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001224 goto out_css_put;
Andrew Morton694fbc02013-09-24 15:27:37 -07001225 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001226 }
1227
Michal Hocko542f85f2013-04-29 15:07:15 -07001228 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001229 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001230 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001231 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001232
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001233 if (reclaim) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001234 struct mem_cgroup_per_zone *mz;
1235
Jianyu Zhane2318752014-06-06 14:38:20 -07001236 mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001237 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001238 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001239 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001240 goto out_unlock;
1241 }
Michal Hocko5f578162013-04-29 15:07:17 -07001242
Johannes Weiner519ebea2013-07-03 15:04:51 -07001243 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001244 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001245
Andrew Morton694fbc02013-09-24 15:27:37 -07001246 memcg = __mem_cgroup_iter_next(root, last_visited);
Michal Hocko542f85f2013-04-29 15:07:15 -07001247
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001248 if (reclaim) {
Michal Hockoecc736f2014-01-23 15:53:35 -08001249 mem_cgroup_iter_update(iter, last_visited, memcg, root,
1250 seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001251
Michal Hocko19f39402013-04-29 15:07:18 -07001252 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001253 iter->generation++;
1254 else if (!prev && memcg)
1255 reclaim->generation = iter->generation;
1256 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001257
Andrew Morton694fbc02013-09-24 15:27:37 -07001258 if (prev && !memcg)
Michal Hocko542f85f2013-04-29 15:07:15 -07001259 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001260 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001261out_unlock:
1262 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001263out_css_put:
1264 if (prev && prev != root)
1265 css_put(&prev->css);
1266
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001267 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001268}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001269
Johannes Weiner56600482012-01-12 17:17:59 -08001270/**
1271 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1272 * @root: hierarchy root
1273 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1274 */
1275void mem_cgroup_iter_break(struct mem_cgroup *root,
1276 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001277{
1278 if (!root)
1279 root = root_mem_cgroup;
1280 if (prev && prev != root)
1281 css_put(&prev->css);
1282}
1283
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001284/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001285 * Iteration constructs for visiting all cgroups (under a tree). If
1286 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1287 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001288 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001289#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001290 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001291 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001292 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001293
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001294#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001295 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001296 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001297 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001298
David Rientjes68ae5642012-12-12 13:51:57 -08001299void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001300{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001301 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001302
Ying Han456f9982011-05-26 16:25:38 -07001303 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001304 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1305 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001306 goto out;
1307
1308 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001309 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001310 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1311 break;
1312 case PGMAJFAULT:
1313 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001314 break;
1315 default:
1316 BUG();
1317 }
1318out:
1319 rcu_read_unlock();
1320}
David Rientjes68ae5642012-12-12 13:51:57 -08001321EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001322
Johannes Weiner925b7672012-01-12 17:18:15 -08001323/**
1324 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1325 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001326 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001327 *
1328 * Returns the lru list vector holding pages for the given @zone and
1329 * @mem. This can be the global zone lruvec, if the memory controller
1330 * is disabled.
1331 */
1332struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1333 struct mem_cgroup *memcg)
1334{
1335 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001336 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001337
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001338 if (mem_cgroup_disabled()) {
1339 lruvec = &zone->lruvec;
1340 goto out;
1341 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001342
Jianyu Zhane2318752014-06-06 14:38:20 -07001343 mz = mem_cgroup_zone_zoneinfo(memcg, zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001344 lruvec = &mz->lruvec;
1345out:
1346 /*
1347 * Since a node can be onlined after the mem_cgroup was created,
1348 * we have to be prepared to initialize lruvec->zone here;
1349 * and if offlined then reonlined, we need to reinitialize it.
1350 */
1351 if (unlikely(lruvec->zone != zone))
1352 lruvec->zone = zone;
1353 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001354}
1355
Johannes Weiner925b7672012-01-12 17:18:15 -08001356/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001357 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001358 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001359 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001360 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001361struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001362{
1363 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001364 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001365 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001366 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001367
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001368 if (mem_cgroup_disabled()) {
1369 lruvec = &zone->lruvec;
1370 goto out;
1371 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001372
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001373 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001374 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001375
1376 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001377 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001378 * an uncharged page off lru does nothing to secure
1379 * its former mem_cgroup from sudden removal.
1380 *
1381 * Our caller holds lru_lock, and PageCgroupUsed is updated
1382 * under page_cgroup lock: between them, they make all uses
1383 * of pc->mem_cgroup safe.
1384 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001385 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001386 pc->mem_cgroup = memcg = root_mem_cgroup;
1387
Jianyu Zhane2318752014-06-06 14:38:20 -07001388 mz = mem_cgroup_page_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001389 lruvec = &mz->lruvec;
1390out:
1391 /*
1392 * Since a node can be onlined after the mem_cgroup was created,
1393 * we have to be prepared to initialize lruvec->zone here;
1394 * and if offlined then reonlined, we need to reinitialize it.
1395 */
1396 if (unlikely(lruvec->zone != zone))
1397 lruvec->zone = zone;
1398 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001399}
1400
1401/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001402 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1403 * @lruvec: mem_cgroup per zone lru vector
1404 * @lru: index of lru list the page is sitting on
1405 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001406 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001407 * This function must be called when a page is added to or removed from an
1408 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001409 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001410void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1411 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001412{
1413 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001414 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001415
1416 if (mem_cgroup_disabled())
1417 return;
1418
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001419 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1420 lru_size = mz->lru_size + lru;
1421 *lru_size += nr_pages;
1422 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001423}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001424
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001425/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001426 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001427 * hierarchy subtree
1428 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001429bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1430 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001431{
Johannes Weiner91c637342012-05-29 15:06:24 -07001432 if (root_memcg == memcg)
1433 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001434 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001435 return false;
Li Zefanb47f77b2013-09-23 16:55:43 +08001436 return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001437}
1438
1439static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1440 struct mem_cgroup *memcg)
1441{
1442 bool ret;
1443
Johannes Weiner91c637342012-05-29 15:06:24 -07001444 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001445 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001446 rcu_read_unlock();
1447 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001448}
1449
David Rientjesffbdccf2013-07-03 15:01:23 -07001450bool task_in_mem_cgroup(struct task_struct *task,
1451 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001452{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001453 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001454 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001455 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001456
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001457 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001458 if (p) {
Johannes Weinerdf381972014-04-07 15:37:43 -07001459 curr = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001460 task_unlock(p);
1461 } else {
1462 /*
1463 * All threads may have already detached their mm's, but the oom
1464 * killer still needs to detect if they have already been oom
1465 * killed to prevent needlessly killing additional tasks.
1466 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001467 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001468 curr = mem_cgroup_from_task(task);
1469 if (curr)
1470 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001471 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001472 }
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001473 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001474 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001475 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001476 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1477 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001478 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001479 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001480 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001481 return ret;
1482}
1483
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001484int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001485{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001486 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001487 unsigned long inactive;
1488 unsigned long active;
1489 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001490
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001491 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1492 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001493
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001494 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1495 if (gb)
1496 inactive_ratio = int_sqrt(10 * gb);
1497 else
1498 inactive_ratio = 1;
1499
Johannes Weiner9b272972011-11-02 13:38:23 -07001500 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001501}
1502
Balbir Singh6d61ef42009-01-07 18:08:06 -08001503#define mem_cgroup_from_res_counter(counter, member) \
1504 container_of(counter, struct mem_cgroup, member)
1505
Johannes Weiner19942822011-02-01 15:52:43 -08001506/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001507 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001508 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001509 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001510 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001511 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001512 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001513static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001514{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001515 unsigned long long margin;
1516
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001517 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001518 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001519 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001520 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001521}
1522
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001523int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001524{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001525 /* root ? */
Linus Torvalds14208b02014-06-09 15:03:33 -07001526 if (mem_cgroup_disabled() || !memcg->css.parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001527 return vm_swappiness;
1528
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001529 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001530}
1531
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001532/*
1533 * memcg->moving_account is used for checking possibility that some thread is
1534 * calling move_account(). When a thread on CPU-A starts moving pages under
1535 * a memcg, other threads should check memcg->moving_account under
1536 * rcu_read_lock(), like this:
1537 *
1538 * CPU-A CPU-B
1539 * rcu_read_lock()
1540 * memcg->moving_account+1 if (memcg->mocing_account)
1541 * take heavy locks.
1542 * synchronize_rcu() update something.
1543 * rcu_read_unlock()
1544 * start move here.
1545 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001546
1547/* for quick checking without looking up memcg */
1548atomic_t memcg_moving __read_mostly;
1549
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001550static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001551{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001552 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001553 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001554 synchronize_rcu();
1555}
1556
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001557static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001558{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001559 /*
1560 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1561 * We check NULL in callee rather than caller.
1562 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001563 if (memcg) {
1564 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001565 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001566 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001567}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001568
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001569/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001570 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001571 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001572 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1573 * moving cgroups. This is for waiting at high-memory pressure
1574 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001575 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001576static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001577{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001578 struct mem_cgroup *from;
1579 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001580 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001581 /*
1582 * Unlike task_move routines, we access mc.to, mc.from not under
1583 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1584 */
1585 spin_lock(&mc.lock);
1586 from = mc.from;
1587 to = mc.to;
1588 if (!from)
1589 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001590
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001591 ret = mem_cgroup_same_or_subtree(memcg, from)
1592 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001593unlock:
1594 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001595 return ret;
1596}
1597
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001598static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001599{
1600 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001601 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001602 DEFINE_WAIT(wait);
1603 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1604 /* moving charge context might have finished. */
1605 if (mc.moving_task)
1606 schedule();
1607 finish_wait(&mc.waitq, &wait);
1608 return true;
1609 }
1610 }
1611 return false;
1612}
1613
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001614/*
1615 * Take this lock when
1616 * - a code tries to modify page's memcg while it's USED.
1617 * - a code tries to modify page state accounting in a memcg.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001618 */
1619static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1620 unsigned long *flags)
1621{
1622 spin_lock_irqsave(&memcg->move_lock, *flags);
1623}
1624
1625static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1626 unsigned long *flags)
1627{
1628 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1629}
1630
Sha Zhengju58cf1882013-02-22 16:32:05 -08001631#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001632/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001633 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001634 * @memcg: The memory cgroup that went over limit
1635 * @p: Task that is going to be killed
1636 *
1637 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1638 * enabled
1639 */
1640void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1641{
Tejun Heoe61734c2014-02-12 09:29:50 -05001642 /* oom_info_lock ensures that parallel ooms do not interleave */
Michal Hocko08088cb2014-02-25 15:01:44 -08001643 static DEFINE_MUTEX(oom_info_lock);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001644 struct mem_cgroup *iter;
1645 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001646
Sha Zhengju58cf1882013-02-22 16:32:05 -08001647 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001648 return;
1649
Michal Hocko08088cb2014-02-25 15:01:44 -08001650 mutex_lock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001651 rcu_read_lock();
1652
Tejun Heoe61734c2014-02-12 09:29:50 -05001653 pr_info("Task in ");
1654 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1655 pr_info(" killed as a result of limit of ");
1656 pr_cont_cgroup_path(memcg->css.cgroup);
1657 pr_info("\n");
Balbir Singhe2224322009-04-02 16:57:39 -07001658
Balbir Singhe2224322009-04-02 16:57:39 -07001659 rcu_read_unlock();
1660
Andrew Mortond0451972013-02-22 16:32:06 -08001661 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001662 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1663 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1664 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001665 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001666 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1667 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1668 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001669 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001670 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1671 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1672 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001673
1674 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heoe61734c2014-02-12 09:29:50 -05001675 pr_info("Memory cgroup stats for ");
1676 pr_cont_cgroup_path(iter->css.cgroup);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001677 pr_cont(":");
1678
1679 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1680 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1681 continue;
1682 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1683 K(mem_cgroup_read_stat(iter, i)));
1684 }
1685
1686 for (i = 0; i < NR_LRU_LISTS; i++)
1687 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1688 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1689
1690 pr_cont("\n");
1691 }
Michal Hocko08088cb2014-02-25 15:01:44 -08001692 mutex_unlock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001693}
1694
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001695/*
1696 * This function returns the number of memcg under hierarchy tree. Returns
1697 * 1(self count) if no children.
1698 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001699static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001700{
1701 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001702 struct mem_cgroup *iter;
1703
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001704 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001705 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001706 return num;
1707}
1708
Balbir Singh6d61ef42009-01-07 18:08:06 -08001709/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001710 * Return the memory (and swap, if configured) limit for a memcg.
1711 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001712static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001713{
1714 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001715
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001716 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001717
David Rientjesa63d83f2010-08-09 17:19:46 -07001718 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001719 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001720 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001721 if (mem_cgroup_swappiness(memcg)) {
1722 u64 memsw;
1723
1724 limit += total_swap_pages << PAGE_SHIFT;
1725 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1726
1727 /*
1728 * If memsw is finite and limits the amount of swap space
1729 * available to this memcg, return that limit.
1730 */
1731 limit = min(limit, memsw);
1732 }
1733
1734 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001735}
1736
David Rientjes19965462012-12-11 16:00:26 -08001737static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1738 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001739{
1740 struct mem_cgroup *iter;
1741 unsigned long chosen_points = 0;
1742 unsigned long totalpages;
1743 unsigned int points = 0;
1744 struct task_struct *chosen = NULL;
1745
David Rientjes876aafb2012-07-31 16:43:48 -07001746 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001747 * If current has a pending SIGKILL or is exiting, then automatically
1748 * select it. The goal is to allow it to allocate so that it may
1749 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001750 */
David Rientjes465adcf2013-04-29 15:08:45 -07001751 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001752 set_thread_flag(TIF_MEMDIE);
1753 return;
1754 }
1755
1756 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001757 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1758 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001759 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001760 struct task_struct *task;
1761
Tejun Heo72ec7022013-08-08 20:11:26 -04001762 css_task_iter_start(&iter->css, &it);
1763 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001764 switch (oom_scan_process_thread(task, totalpages, NULL,
1765 false)) {
1766 case OOM_SCAN_SELECT:
1767 if (chosen)
1768 put_task_struct(chosen);
1769 chosen = task;
1770 chosen_points = ULONG_MAX;
1771 get_task_struct(chosen);
1772 /* fall through */
1773 case OOM_SCAN_CONTINUE:
1774 continue;
1775 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001776 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001777 mem_cgroup_iter_break(memcg, iter);
1778 if (chosen)
1779 put_task_struct(chosen);
1780 return;
1781 case OOM_SCAN_OK:
1782 break;
1783 };
1784 points = oom_badness(task, memcg, NULL, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -08001785 if (!points || points < chosen_points)
1786 continue;
1787 /* Prefer thread group leaders for display purposes */
1788 if (points == chosen_points &&
1789 thread_group_leader(chosen))
1790 continue;
1791
1792 if (chosen)
1793 put_task_struct(chosen);
1794 chosen = task;
1795 chosen_points = points;
1796 get_task_struct(chosen);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001797 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001798 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001799 }
1800
1801 if (!chosen)
1802 return;
1803 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001804 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1805 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001806}
1807
Johannes Weiner56600482012-01-12 17:17:59 -08001808static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1809 gfp_t gfp_mask,
1810 unsigned long flags)
1811{
1812 unsigned long total = 0;
1813 bool noswap = false;
1814 int loop;
1815
1816 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1817 noswap = true;
Johannes Weiner56600482012-01-12 17:17:59 -08001818
1819 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1820 if (loop)
1821 drain_all_stock_async(memcg);
1822 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1823 /*
1824 * Allow limit shrinkers, which are triggered directly
1825 * by userspace, to catch signals and stop reclaim
1826 * after minimal progress, regardless of the margin.
1827 */
1828 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1829 break;
1830 if (mem_cgroup_margin(memcg))
1831 break;
1832 /*
1833 * If nothing was reclaimed after two attempts, there
1834 * may be no reclaimable pages in this hierarchy.
1835 */
1836 if (loop && !total)
1837 break;
1838 }
1839 return total;
1840}
1841
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001842/**
1843 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001844 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001845 * @nid: the node ID to be checked.
1846 * @noswap : specify true here if the user wants flle only information.
1847 *
1848 * This function returns whether the specified memcg contains any
1849 * reclaimable pages on a node. Returns true if there are any reclaimable
1850 * pages in the node.
1851 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001852static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001853 int nid, bool noswap)
1854{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001856 return true;
1857 if (noswap || !total_swap_pages)
1858 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001860 return true;
1861 return false;
1862
1863}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001864#if MAX_NUMNODES > 1
Ying Han889976d2011-05-26 16:25:33 -07001865
1866/*
1867 * Always updating the nodemask is not very good - even if we have an empty
1868 * list or the wrong list here, we can start from some node and traverse all
1869 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1870 *
1871 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001872static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001873{
1874 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001875 /*
1876 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1877 * pagein/pageout changes since the last update.
1878 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001879 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001880 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001881 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001882 return;
1883
Ying Han889976d2011-05-26 16:25:33 -07001884 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001885 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001886
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001887 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001888
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001889 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1890 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001891 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001892
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001893 atomic_set(&memcg->numainfo_events, 0);
1894 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001895}
1896
1897/*
1898 * Selecting a node where we start reclaim from. Because what we need is just
1899 * reducing usage counter, start from anywhere is O,K. Considering
1900 * memory reclaim from current node, there are pros. and cons.
1901 *
1902 * Freeing memory from current node means freeing memory from a node which
1903 * we'll use or we've used. So, it may make LRU bad. And if several threads
1904 * hit limits, it will see a contention on a node. But freeing from remote
1905 * node means more costs for memory reclaim because of memory latency.
1906 *
1907 * Now, we use round-robin. Better algorithm is welcomed.
1908 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001909int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001910{
1911 int node;
1912
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001913 mem_cgroup_may_update_nodemask(memcg);
1914 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001915
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001916 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001917 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001918 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001919 /*
1920 * We call this when we hit limit, not when pages are added to LRU.
1921 * No LRU may hold pages because all pages are UNEVICTABLE or
1922 * memcg is too small and all pages are not on LRU. In that case,
1923 * we use curret node.
1924 */
1925 if (unlikely(node == MAX_NUMNODES))
1926 node = numa_node_id();
1927
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001928 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001929 return node;
1930}
1931
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001932/*
1933 * Check all nodes whether it contains reclaimable pages or not.
1934 * For quick scan, we make use of scan_nodes. This will allow us to skip
1935 * unused nodes. But scan_nodes is lazily updated and may not cotain
1936 * enough new information. We need to do double check.
1937 */
1938static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1939{
1940 int nid;
1941
1942 /*
1943 * quick check...making use of scan_node.
1944 * We can skip unused nodes.
1945 */
1946 if (!nodes_empty(memcg->scan_nodes)) {
1947 for (nid = first_node(memcg->scan_nodes);
1948 nid < MAX_NUMNODES;
1949 nid = next_node(nid, memcg->scan_nodes)) {
1950
1951 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
1952 return true;
1953 }
1954 }
1955 /*
1956 * Check rest of nodes.
1957 */
1958 for_each_node_state(nid, N_MEMORY) {
1959 if (node_isset(nid, memcg->scan_nodes))
1960 continue;
1961 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
1962 return true;
1963 }
1964 return false;
1965}
1966
Ying Han889976d2011-05-26 16:25:33 -07001967#else
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 return 0;
1971}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001972
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001973static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1974{
1975 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
1976}
Ying Han889976d2011-05-26 16:25:33 -07001977#endif
1978
Andrew Morton0608f432013-09-24 15:27:41 -07001979static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1980 struct zone *zone,
1981 gfp_t gfp_mask,
1982 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001983{
Andrew Morton0608f432013-09-24 15:27:41 -07001984 struct mem_cgroup *victim = NULL;
1985 int total = 0;
1986 int loop = 0;
1987 unsigned long excess;
1988 unsigned long nr_scanned;
1989 struct mem_cgroup_reclaim_cookie reclaim = {
1990 .zone = zone,
1991 .priority = 0,
1992 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001993
Andrew Morton0608f432013-09-24 15:27:41 -07001994 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001995
Andrew Morton0608f432013-09-24 15:27:41 -07001996 while (1) {
1997 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1998 if (!victim) {
1999 loop++;
2000 if (loop >= 2) {
2001 /*
2002 * If we have not been able to reclaim
2003 * anything, it might because there are
2004 * no reclaimable pages under this hierarchy
2005 */
2006 if (!total)
2007 break;
2008 /*
2009 * We want to do more targeted reclaim.
2010 * excess >> 2 is not to excessive so as to
2011 * reclaim too much, nor too less that we keep
2012 * coming back to reclaim from this cgroup
2013 */
2014 if (total >= (excess >> 2) ||
2015 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
2016 break;
2017 }
2018 continue;
2019 }
2020 if (!mem_cgroup_reclaimable(victim, false))
2021 continue;
2022 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2023 zone, &nr_scanned);
2024 *total_scanned += nr_scanned;
2025 if (!res_counter_soft_limit_excess(&root_memcg->res))
2026 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002027 }
Andrew Morton0608f432013-09-24 15:27:41 -07002028 mem_cgroup_iter_break(root_memcg, victim);
2029 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08002030}
2031
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002032#ifdef CONFIG_LOCKDEP
2033static struct lockdep_map memcg_oom_lock_dep_map = {
2034 .name = "memcg_oom_lock",
2035};
2036#endif
2037
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002038static DEFINE_SPINLOCK(memcg_oom_lock);
2039
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002040/*
2041 * Check OOM-Killer is already running under our hierarchy.
2042 * If someone is running, return false.
2043 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002044static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002045{
Michal Hocko79dfdac2011-07-26 16:08:23 -07002046 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002047
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002048 spin_lock(&memcg_oom_lock);
2049
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002050 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07002051 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002052 /*
2053 * this subtree of our hierarchy is already locked
2054 * so we cannot give a lock.
2055 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07002056 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002057 mem_cgroup_iter_break(memcg, iter);
2058 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07002059 } else
2060 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002061 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002062
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002063 if (failed) {
2064 /*
2065 * OK, we failed to lock the whole subtree so we have
2066 * to clean up what we set up to the failing subtree
2067 */
2068 for_each_mem_cgroup_tree(iter, memcg) {
2069 if (iter == failed) {
2070 mem_cgroup_iter_break(memcg, iter);
2071 break;
2072 }
2073 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002074 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002075 } else
2076 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002077
2078 spin_unlock(&memcg_oom_lock);
2079
2080 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002081}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002082
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002083static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002084{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002085 struct mem_cgroup *iter;
2086
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002087 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07002088 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002089 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002090 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002091 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002092}
2093
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002094static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002095{
2096 struct mem_cgroup *iter;
2097
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002098 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002099 atomic_inc(&iter->under_oom);
2100}
2101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002102static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002103{
2104 struct mem_cgroup *iter;
2105
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002106 /*
2107 * When a new child is created while the hierarchy is under oom,
2108 * mem_cgroup_oom_lock() may not be called. We have to use
2109 * atomic_add_unless() here.
2110 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002111 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002112 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002113}
2114
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002115static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2116
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002117struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002118 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002119 wait_queue_t wait;
2120};
2121
2122static int memcg_oom_wake_function(wait_queue_t *wait,
2123 unsigned mode, int sync, void *arg)
2124{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002125 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2126 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002127 struct oom_wait_info *oom_wait_info;
2128
2129 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002130 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002131
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002132 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002133 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002134 * Then we can use css_is_ancestor without taking care of RCU.
2135 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002136 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2137 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002138 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002139 return autoremove_wake_function(wait, mode, sync, arg);
2140}
2141
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002142static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002143{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002144 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002145 /* for filtering, pass "memcg" as argument. */
2146 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002147}
2148
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002149static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002150{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002151 if (memcg && atomic_read(&memcg->under_oom))
2152 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002153}
2154
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002155static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002156{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002157 if (!current->memcg_oom.may_oom)
2158 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002159 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002160 * We are in the middle of the charge context here, so we
2161 * don't want to block when potentially sitting on a callstack
2162 * that holds all kinds of filesystem and mm locks.
2163 *
2164 * Also, the caller may handle a failed allocation gracefully
2165 * (like optional page cache readahead) and so an OOM killer
2166 * invocation might not even be necessary.
2167 *
2168 * That's why we don't do anything here except remember the
2169 * OOM context and then deal with it at the end of the page
2170 * fault when the stack is unwound, the locks are released,
2171 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002172 */
Johannes Weiner49426422013-10-16 13:46:59 -07002173 css_get(&memcg->css);
2174 current->memcg_oom.memcg = memcg;
2175 current->memcg_oom.gfp_mask = mask;
2176 current->memcg_oom.order = order;
2177}
2178
2179/**
2180 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2181 * @handle: actually kill/wait or just clean up the OOM state
2182 *
2183 * This has to be called at the end of a page fault if the memcg OOM
2184 * handler was enabled.
2185 *
2186 * Memcg supports userspace OOM handling where failed allocations must
2187 * sleep on a waitqueue until the userspace task resolves the
2188 * situation. Sleeping directly in the charge context with all kinds
2189 * of locks held is not a good idea, instead we remember an OOM state
2190 * in the task and mem_cgroup_oom_synchronize() has to be called at
2191 * the end of the page fault to complete the OOM handling.
2192 *
2193 * Returns %true if an ongoing memcg OOM situation was detected and
2194 * completed, %false otherwise.
2195 */
2196bool mem_cgroup_oom_synchronize(bool handle)
2197{
2198 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2199 struct oom_wait_info owait;
2200 bool locked;
2201
2202 /* OOM is global, do not handle */
2203 if (!memcg)
2204 return false;
2205
2206 if (!handle)
2207 goto cleanup;
2208
2209 owait.memcg = memcg;
2210 owait.wait.flags = 0;
2211 owait.wait.func = memcg_oom_wake_function;
2212 owait.wait.private = current;
2213 INIT_LIST_HEAD(&owait.wait.task_list);
2214
2215 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002216 mem_cgroup_mark_under_oom(memcg);
2217
2218 locked = mem_cgroup_oom_trylock(memcg);
2219
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002220 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002221 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002222
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002223 if (locked && !memcg->oom_kill_disable) {
2224 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07002225 finish_wait(&memcg_oom_waitq, &owait.wait);
2226 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2227 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002228 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002229 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07002230 mem_cgroup_unmark_under_oom(memcg);
2231 finish_wait(&memcg_oom_waitq, &owait.wait);
2232 }
2233
2234 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002235 mem_cgroup_oom_unlock(memcg);
2236 /*
2237 * There is no guarantee that an OOM-lock contender
2238 * sees the wakeups triggered by the OOM kill
2239 * uncharges. Wake any sleepers explicitely.
2240 */
2241 memcg_oom_recover(memcg);
2242 }
Johannes Weiner49426422013-10-16 13:46:59 -07002243cleanup:
2244 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002245 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002246 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002247}
2248
Balbir Singhd69b0422009-06-17 16:26:34 -07002249/*
Qiang Huangb5ffc852014-06-04 16:08:22 -07002250 * Used to update mapped file or writeback or other statistics.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002251 *
2252 * Notes: Race condition
2253 *
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002254 * Charging occurs during page instantiation, while the page is
2255 * unmapped and locked in page migration, or while the page table is
2256 * locked in THP migration. No race is possible.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002257 *
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002258 * Uncharge happens to pages with zero references, no race possible.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002259 *
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002260 * Charge moving between groups is protected by checking mm->moving
2261 * account and taking the move_lock in the slowpath.
Balbir Singhd69b0422009-06-17 16:26:34 -07002262 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002263
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002264void __mem_cgroup_begin_update_page_stat(struct page *page,
2265 bool *locked, unsigned long *flags)
2266{
2267 struct mem_cgroup *memcg;
2268 struct page_cgroup *pc;
2269
2270 pc = lookup_page_cgroup(page);
2271again:
2272 memcg = pc->mem_cgroup;
2273 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2274 return;
2275 /*
2276 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002277 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002278 * rcu_read_lock(), any calls to move_account will be delayed until
Qiang Huangbdcbb652014-06-04 16:08:21 -07002279 * rcu_read_unlock().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002280 */
Qiang Huangbdcbb652014-06-04 16:08:21 -07002281 VM_BUG_ON(!rcu_read_lock_held());
2282 if (atomic_read(&memcg->moving_account) <= 0)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002283 return;
2284
2285 move_lock_mem_cgroup(memcg, flags);
2286 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2287 move_unlock_mem_cgroup(memcg, flags);
2288 goto again;
2289 }
2290 *locked = true;
2291}
2292
2293void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2294{
2295 struct page_cgroup *pc = lookup_page_cgroup(page);
2296
2297 /*
2298 * It's guaranteed that pc->mem_cgroup never changes while
2299 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002300 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002301 */
2302 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2303}
2304
Greg Thelen2a7106f2011-01-13 15:47:37 -08002305void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002306 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002307{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002308 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002309 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002310 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002311
Johannes Weinercfa44942012-01-12 17:18:38 -08002312 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002313 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002314
Sha Zhengju658b72c2013-09-12 15:13:52 -07002315 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002316 memcg = pc->mem_cgroup;
2317 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002318 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002319
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002320 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002321}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002322
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002323/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002324 * size of first charge trial. "32" comes from vmscan.c's magic value.
2325 * TODO: maybe necessary to use big numbers in big irons.
2326 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002327#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002328struct memcg_stock_pcp {
2329 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002330 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002331 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002332 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002333#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002334};
2335static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002336static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002337
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002338/**
2339 * consume_stock: Try to consume stocked charge on this cpu.
2340 * @memcg: memcg to consume from.
2341 * @nr_pages: how many pages to charge.
2342 *
2343 * The charges will only happen if @memcg matches the current cpu's memcg
2344 * stock, and at least @nr_pages are available in that stock. Failure to
2345 * service an allocation will refill the stock.
2346 *
2347 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002348 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002349static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002350{
2351 struct memcg_stock_pcp *stock;
2352 bool ret = true;
2353
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002354 if (nr_pages > CHARGE_BATCH)
2355 return false;
2356
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002357 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002358 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2359 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002360 else /* need to call res_counter_charge */
2361 ret = false;
2362 put_cpu_var(memcg_stock);
2363 return ret;
2364}
2365
2366/*
2367 * Returns stocks cached in percpu to res_counter and reset cached information.
2368 */
2369static void drain_stock(struct memcg_stock_pcp *stock)
2370{
2371 struct mem_cgroup *old = stock->cached;
2372
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002373 if (stock->nr_pages) {
2374 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2375
2376 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002377 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002378 res_counter_uncharge(&old->memsw, bytes);
2379 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002380 }
2381 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002382}
2383
2384/*
2385 * This must be called under preempt disabled or must be called by
2386 * a thread which is pinned to local cpu.
2387 */
2388static void drain_local_stock(struct work_struct *dummy)
2389{
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002390 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002391 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002392 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002393}
2394
Michal Hockoe4777492013-02-22 16:35:40 -08002395static void __init memcg_stock_init(void)
2396{
2397 int cpu;
2398
2399 for_each_possible_cpu(cpu) {
2400 struct memcg_stock_pcp *stock =
2401 &per_cpu(memcg_stock, cpu);
2402 INIT_WORK(&stock->work, drain_local_stock);
2403 }
2404}
2405
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002406/*
2407 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002408 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002409 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002410static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002411{
2412 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002414 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002415 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002417 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002418 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002419 put_cpu_var(memcg_stock);
2420}
2421
2422/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002423 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002424 * of the hierarchy under it. sync flag says whether we should block
2425 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002426 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002427static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002428{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002429 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002430
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002431 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002432 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002433 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002434 for_each_online_cpu(cpu) {
2435 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002436 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002437
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002438 memcg = stock->cached;
2439 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002440 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002441 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002442 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002443 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2444 if (cpu == curcpu)
2445 drain_local_stock(&stock->work);
2446 else
2447 schedule_work_on(cpu, &stock->work);
2448 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002449 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002450 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002451
2452 if (!sync)
2453 goto out;
2454
2455 for_each_online_cpu(cpu) {
2456 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002457 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002458 flush_work(&stock->work);
2459 }
2460out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002461 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002462}
2463
2464/*
2465 * Tries to drain stocked charges in other cpus. This function is asynchronous
2466 * and just put a work per cpu for draining localy on each cpu. Caller can
2467 * expects some charges will be back to res_counter later but cannot wait for
2468 * it.
2469 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002471{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002472 /*
2473 * If someone calls draining, avoid adding more kworker runs.
2474 */
2475 if (!mutex_trylock(&percpu_charge_mutex))
2476 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002478 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002479}
2480
2481/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002482static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002483{
2484 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002485 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002486 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002487 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002488}
2489
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002490/*
2491 * This function drains percpu counter value from DEAD cpu and
2492 * move it to local cpu. Note that this function can be preempted.
2493 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002494static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002495{
2496 int i;
2497
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002498 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002499 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002500 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002501
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002502 per_cpu(memcg->stat->count[i], cpu) = 0;
2503 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002504 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002505 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002506 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002507
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 per_cpu(memcg->stat->events[i], cpu) = 0;
2509 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002510 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002511 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002512}
2513
Paul Gortmaker0db06282013-06-19 14:53:51 -04002514static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002515 unsigned long action,
2516 void *hcpu)
2517{
2518 int cpu = (unsigned long)hcpu;
2519 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002520 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002521
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002522 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002523 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002524
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002525 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002526 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002527
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002528 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002529 mem_cgroup_drain_pcp_counter(iter, cpu);
2530
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002531 stock = &per_cpu(memcg_stock, cpu);
2532 drain_stock(stock);
2533 return NOTIFY_OK;
2534}
2535
Johannes Weiner00501b52014-08-08 14:19:20 -07002536static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2537 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002538{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002539 unsigned int batch = max(CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002540 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002541 struct mem_cgroup *mem_over_limit;
2542 struct res_counter *fail_res;
2543 unsigned long nr_reclaimed;
2544 unsigned long flags = 0;
2545 unsigned long long size;
Johannes Weiner05b84302014-08-06 16:05:59 -07002546 int ret = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002547
Johannes Weinerce00a962014-09-05 08:43:57 -04002548 if (mem_cgroup_is_root(memcg))
2549 goto done;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002550retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002551 if (consume_stock(memcg, nr_pages))
2552 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002553
Johannes Weiner6539cc02014-08-06 16:05:42 -07002554 size = batch * PAGE_SIZE;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002555 if (!do_swap_account ||
2556 !res_counter_charge(&memcg->memsw, size, &fail_res)) {
2557 if (!res_counter_charge(&memcg->res, size, &fail_res))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002558 goto done_restock;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002559 if (do_swap_account)
2560 res_counter_uncharge(&memcg->memsw, size);
2561 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
2562 } else {
Johannes Weiner6539cc02014-08-06 16:05:42 -07002563 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2564 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002565 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002566
Johannes Weiner6539cc02014-08-06 16:05:42 -07002567 if (batch > nr_pages) {
2568 batch = nr_pages;
2569 goto retry;
2570 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002571
Johannes Weiner06b078f2014-08-06 16:05:44 -07002572 /*
2573 * Unlike in global OOM situations, memcg is not in a physical
2574 * memory shortage. Allow dying and OOM-killed tasks to
2575 * bypass the last charges so that they can exit quickly and
2576 * free their memory.
2577 */
2578 if (unlikely(test_thread_flag(TIF_MEMDIE) ||
2579 fatal_signal_pending(current) ||
2580 current->flags & PF_EXITING))
2581 goto bypass;
2582
2583 if (unlikely(task_in_memcg_oom(current)))
2584 goto nomem;
2585
Johannes Weiner6539cc02014-08-06 16:05:42 -07002586 if (!(gfp_mask & __GFP_WAIT))
2587 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002588
Johannes Weiner6539cc02014-08-06 16:05:42 -07002589 nr_reclaimed = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
2590
Johannes Weiner61e02c72014-08-06 16:08:16 -07002591 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002592 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002593
2594 if (gfp_mask & __GFP_NORETRY)
2595 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002596 /*
2597 * Even though the limit is exceeded at this point, reclaim
2598 * may have been able to free some pages. Retry the charge
2599 * before killing the task.
2600 *
2601 * Only for regular pages, though: huge pages are rather
2602 * unlikely to succeed so close to the limit, and we fall back
2603 * to regular pages anyway in case of failure.
2604 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002605 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002606 goto retry;
2607 /*
2608 * At task move, charge accounts can be doubly counted. So, it's
2609 * better to wait until the end of task_move if something is going on.
2610 */
2611 if (mem_cgroup_wait_acct_move(mem_over_limit))
2612 goto retry;
2613
Johannes Weiner9b130612014-08-06 16:05:51 -07002614 if (nr_retries--)
2615 goto retry;
2616
Johannes Weiner06b078f2014-08-06 16:05:44 -07002617 if (gfp_mask & __GFP_NOFAIL)
2618 goto bypass;
2619
Johannes Weiner6539cc02014-08-06 16:05:42 -07002620 if (fatal_signal_pending(current))
2621 goto bypass;
2622
Johannes Weiner61e02c72014-08-06 16:08:16 -07002623 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages));
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002624nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002625 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002626 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002627bypass:
Johannes Weinerce00a962014-09-05 08:43:57 -04002628 return -EINTR;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002629
2630done_restock:
2631 if (batch > nr_pages)
2632 refill_stock(memcg, batch - nr_pages);
2633done:
Johannes Weiner05b84302014-08-06 16:05:59 -07002634 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002635}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002636
Johannes Weiner00501b52014-08-08 14:19:20 -07002637static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002638{
Johannes Weiner05b84302014-08-06 16:05:59 -07002639 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002640
Johannes Weinerce00a962014-09-05 08:43:57 -04002641 if (mem_cgroup_is_root(memcg))
2642 return;
2643
Johannes Weiner05b84302014-08-06 16:05:59 -07002644 res_counter_uncharge(&memcg->res, bytes);
2645 if (do_swap_account)
2646 res_counter_uncharge(&memcg->memsw, bytes);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002647}
2648
2649/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002650 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2651 * This is useful when moving usage to parent cgroup.
2652 */
2653static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2654 unsigned int nr_pages)
2655{
2656 unsigned long bytes = nr_pages * PAGE_SIZE;
2657
Johannes Weinerce00a962014-09-05 08:43:57 -04002658 if (mem_cgroup_is_root(memcg))
2659 return;
2660
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002661 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2662 if (do_swap_account)
2663 res_counter_uncharge_until(&memcg->memsw,
2664 memcg->memsw.parent, bytes);
2665}
2666
2667/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002668 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoec903c02014-05-13 12:11:01 -04002669 * rcu_read_lock(). The caller is responsible for calling
2670 * css_tryget_online() if the mem_cgroup is used for charging. (dropping
2671 * refcnt from swap can be called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002672 */
2673static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2674{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002675 /* ID 0 is unused ID */
2676 if (!id)
2677 return NULL;
Li Zefan34c00c32013-09-23 16:56:01 +08002678 return mem_cgroup_from_id(id);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002679}
2680
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002681/*
2682 * try_get_mem_cgroup_from_page - look up page's memcg association
2683 * @page: the page
2684 *
2685 * Look up, get a css reference, and return the memcg that owns @page.
2686 *
2687 * The page must be locked to prevent racing with swap-in and page
2688 * cache charges. If coming from an unlocked page table, the caller
2689 * must ensure the page is on the LRU or this can race with charging.
2690 */
Wu Fengguange42d9d52009-12-16 12:19:59 +01002691struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002692{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002693 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002694 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002695 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002696 swp_entry_t ent;
2697
Sasha Levin309381fea2014-01-23 15:52:54 -08002698 VM_BUG_ON_PAGE(!PageLocked(page), page);
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002699
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002700 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002701 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002702 memcg = pc->mem_cgroup;
Tejun Heoec903c02014-05-13 12:11:01 -04002703 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002704 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002705 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002706 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002707 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002708 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002709 memcg = mem_cgroup_lookup(id);
Tejun Heoec903c02014-05-13 12:11:01 -04002710 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002711 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002712 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002713 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002714 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002715}
2716
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002717static void lock_page_lru(struct page *page, int *isolated)
2718{
2719 struct zone *zone = page_zone(page);
2720
2721 spin_lock_irq(&zone->lru_lock);
2722 if (PageLRU(page)) {
2723 struct lruvec *lruvec;
2724
2725 lruvec = mem_cgroup_page_lruvec(page, zone);
2726 ClearPageLRU(page);
2727 del_page_from_lru_list(page, lruvec, page_lru(page));
2728 *isolated = 1;
2729 } else
2730 *isolated = 0;
2731}
2732
2733static void unlock_page_lru(struct page *page, int isolated)
2734{
2735 struct zone *zone = page_zone(page);
2736
2737 if (isolated) {
2738 struct lruvec *lruvec;
2739
2740 lruvec = mem_cgroup_page_lruvec(page, zone);
2741 VM_BUG_ON_PAGE(PageLRU(page), page);
2742 SetPageLRU(page);
2743 add_page_to_lru_list(page, lruvec, page_lru(page));
2744 }
2745 spin_unlock_irq(&zone->lru_lock);
2746}
2747
Johannes Weiner00501b52014-08-08 14:19:20 -07002748static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002749 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002750{
Johannes Weinerce587e62012-04-24 20:22:33 +02002751 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002752 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002753
Sasha Levin309381fea2014-01-23 15:52:54 -08002754 VM_BUG_ON_PAGE(PageCgroupUsed(pc), page);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002755 /*
2756 * we don't need page_cgroup_lock about tail pages, becase they are not
2757 * accessed by any other context at this point.
2758 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002759
2760 /*
2761 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2762 * may already be on some other mem_cgroup's LRU. Take care of it.
2763 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002764 if (lrucare)
2765 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002766
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002767 /*
2768 * Nobody should be changing or seriously looking at
2769 * pc->mem_cgroup and pc->flags at this point:
2770 *
2771 * - the page is uncharged
2772 *
2773 * - the page is off-LRU
2774 *
2775 * - an anonymous fault has exclusive page access, except for
2776 * a locked page table
2777 *
2778 * - a page cache insertion, a swapin fault, or a migration
2779 * have the page locked
2780 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002781 pc->mem_cgroup = memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002782 pc->flags = PCG_USED | PCG_MEM | (do_swap_account ? PCG_MEMSW : 0);
Hugh Dickins3be91272008-02-07 00:14:19 -08002783
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002784 if (lrucare)
2785 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002786}
2787
Glauber Costa7cf27982012-12-18 14:22:55 -08002788static DEFINE_MUTEX(set_limit_mutex);
2789
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002790#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovbd673142014-06-04 16:07:40 -07002791/*
2792 * The memcg_slab_mutex is held whenever a per memcg kmem cache is created or
2793 * destroyed. It protects memcg_caches arrays and memcg_slab_caches lists.
2794 */
2795static DEFINE_MUTEX(memcg_slab_mutex);
2796
Vladimir Davydovd6441632014-01-23 15:53:09 -08002797static DEFINE_MUTEX(activate_kmem_mutex);
2798
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002799static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2800{
2801 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
Vladimir Davydov6de64be2014-01-23 15:53:08 -08002802 memcg_kmem_is_active(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002803}
2804
Glauber Costa1f458cb2012-12-18 14:22:50 -08002805/*
2806 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2807 * in the memcg_cache_params struct.
2808 */
2809static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2810{
2811 struct kmem_cache *cachep;
2812
2813 VM_BUG_ON(p->is_root_cache);
2814 cachep = p->root_cache;
Qiang Huang7a67d7a2013-11-12 15:08:24 -08002815 return cache_from_memcg_idx(cachep, memcg_cache_id(p->memcg));
Glauber Costa1f458cb2012-12-18 14:22:50 -08002816}
2817
Glauber Costa749c5412012-12-18 14:23:01 -08002818#ifdef CONFIG_SLABINFO
Tejun Heo2da8ca82013-12-05 12:28:04 -05002819static int mem_cgroup_slabinfo_read(struct seq_file *m, void *v)
Glauber Costa749c5412012-12-18 14:23:01 -08002820{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002821 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Glauber Costa749c5412012-12-18 14:23:01 -08002822 struct memcg_cache_params *params;
2823
2824 if (!memcg_can_account_kmem(memcg))
2825 return -EIO;
2826
2827 print_slabinfo_header(m);
2828
Vladimir Davydovbd673142014-06-04 16:07:40 -07002829 mutex_lock(&memcg_slab_mutex);
Glauber Costa749c5412012-12-18 14:23:01 -08002830 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2831 cache_show(memcg_params_to_cache(params), m);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002832 mutex_unlock(&memcg_slab_mutex);
Glauber Costa749c5412012-12-18 14:23:01 -08002833
2834 return 0;
2835}
2836#endif
2837
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07002838static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002839{
2840 struct res_counter *fail_res;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002841 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002842
2843 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2844 if (ret)
2845 return ret;
2846
Johannes Weiner00501b52014-08-08 14:19:20 -07002847 ret = try_charge(memcg, gfp, size >> PAGE_SHIFT);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002848 if (ret == -EINTR) {
2849 /*
Johannes Weiner00501b52014-08-08 14:19:20 -07002850 * try_charge() chose to bypass to root due to OOM kill or
2851 * fatal signal. Since our only options are to either fail
2852 * the allocation or charge it to this cgroup, do it as a
2853 * temporary condition. But we can't fail. From a kmem/slab
2854 * perspective, the cache has already been selected, by
2855 * mem_cgroup_kmem_get_cache(), so it is too late to change
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002856 * our minds.
2857 *
2858 * This condition will only trigger if the task entered
Johannes Weiner00501b52014-08-08 14:19:20 -07002859 * memcg_charge_kmem in a sane state, but was OOM-killed
2860 * during try_charge() above. Tasks that were already dying
2861 * when the allocation triggers should have been already
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002862 * directed to the root cgroup in memcontrol.h
2863 */
2864 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2865 if (do_swap_account)
2866 res_counter_charge_nofail(&memcg->memsw, size,
2867 &fail_res);
2868 ret = 0;
2869 } else if (ret)
2870 res_counter_uncharge(&memcg->kmem, size);
2871
2872 return ret;
2873}
2874
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07002875static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002876{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002877 res_counter_uncharge(&memcg->res, size);
2878 if (do_swap_account)
2879 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002880
2881 /* Not down to 0 */
2882 if (res_counter_uncharge(&memcg->kmem, size))
2883 return;
2884
Li Zefan10d5ebf2013-07-08 16:00:33 -07002885 /*
2886 * Releases a reference taken in kmem_cgroup_css_offline in case
2887 * this last uncharge is racing with the offlining code or it is
2888 * outliving the memcg existence.
2889 *
2890 * The memory barrier imposed by test&clear is paired with the
2891 * explicit one in memcg_kmem_mark_dead().
2892 */
Glauber Costa7de37682012-12-18 14:22:07 -08002893 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002894 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002895}
2896
Glauber Costa2633d7a2012-12-18 14:22:34 -08002897/*
2898 * helper for acessing a memcg's index. It will be used as an index in the
2899 * child cache array in kmem_cache, and also to derive its name. This function
2900 * will return -1 when this is not a kmem-limited memcg.
2901 */
2902int memcg_cache_id(struct mem_cgroup *memcg)
2903{
2904 return memcg ? memcg->kmemcg_id : -1;
2905}
2906
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002907static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002908{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002909 int id, size;
2910 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002911
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002912 id = ida_simple_get(&kmem_limited_groups,
2913 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2914 if (id < 0)
2915 return id;
2916
2917 if (id < memcg_limited_groups_array_size)
2918 return id;
2919
2920 /*
2921 * There's no space for the new id in memcg_caches arrays,
2922 * so we have to grow them.
2923 */
2924
2925 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002926 if (size < MEMCG_CACHES_MIN_SIZE)
2927 size = MEMCG_CACHES_MIN_SIZE;
2928 else if (size > MEMCG_CACHES_MAX_SIZE)
2929 size = MEMCG_CACHES_MAX_SIZE;
2930
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002931 mutex_lock(&memcg_slab_mutex);
2932 err = memcg_update_all_caches(size);
2933 mutex_unlock(&memcg_slab_mutex);
2934
2935 if (err) {
2936 ida_simple_remove(&kmem_limited_groups, id);
2937 return err;
2938 }
2939 return id;
2940}
2941
2942static void memcg_free_cache_id(int id)
2943{
2944 ida_simple_remove(&kmem_limited_groups, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002945}
2946
2947/*
2948 * We should update the current array size iff all caches updates succeed. This
2949 * can only be done from the slab side. The slab mutex needs to be held when
2950 * calling this.
2951 */
2952void memcg_update_array_size(int num)
2953{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002954 memcg_limited_groups_array_size = num;
Glauber Costa55007d82012-12-18 14:22:38 -08002955}
2956
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002957static void memcg_register_cache(struct mem_cgroup *memcg,
2958 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08002959{
Vladimir Davydov93f39ee2014-06-04 16:08:24 -07002960 static char memcg_name_buf[NAME_MAX + 1]; /* protected by
2961 memcg_slab_mutex */
Vladimir Davydovbd673142014-06-04 16:07:40 -07002962 struct kmem_cache *cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002963 int id;
2964
Vladimir Davydovbd673142014-06-04 16:07:40 -07002965 lockdep_assert_held(&memcg_slab_mutex);
2966
2967 id = memcg_cache_id(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08002968
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002969 /*
Vladimir Davydovbd673142014-06-04 16:07:40 -07002970 * Since per-memcg caches are created asynchronously on first
2971 * allocation (see memcg_kmem_get_cache()), several threads can try to
2972 * create the same cache, but only one of them may succeed.
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002973 */
Vladimir Davydovbd673142014-06-04 16:07:40 -07002974 if (cache_from_memcg_idx(root_cache, id))
2975 return;
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002976
Vladimir Davydov073ee1c2014-06-04 16:08:23 -07002977 cgroup_name(memcg->css.cgroup, memcg_name_buf, NAME_MAX + 1);
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002978 cachep = memcg_create_kmem_cache(memcg, root_cache, memcg_name_buf);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002979 /*
2980 * If we could not create a memcg cache, do not complain, because
2981 * that's not critical at all as we can always proceed with the root
2982 * cache.
2983 */
2984 if (!cachep)
2985 return;
2986
Vladimir Davydov33a690c2014-10-09 15:28:43 -07002987 css_get(&memcg->css);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002988 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002989
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002990 /*
Vladimir Davydov959c8962014-01-23 15:52:59 -08002991 * Since readers won't lock (see cache_from_memcg_idx()), we need a
2992 * barrier here to ensure nobody will see the kmem_cache partially
2993 * initialized.
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002994 */
Vladimir Davydov959c8962014-01-23 15:52:59 -08002995 smp_wmb();
2996
Vladimir Davydovbd673142014-06-04 16:07:40 -07002997 BUG_ON(root_cache->memcg_params->memcg_caches[id]);
2998 root_cache->memcg_params->memcg_caches[id] = cachep;
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002999}
3000
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003001static void memcg_unregister_cache(struct kmem_cache *cachep)
Vladimir Davydov1aa13252014-01-23 15:52:58 -08003002{
Vladimir Davydovbd673142014-06-04 16:07:40 -07003003 struct kmem_cache *root_cache;
Vladimir Davydov1aa13252014-01-23 15:52:58 -08003004 struct mem_cgroup *memcg;
3005 int id;
3006
Vladimir Davydovbd673142014-06-04 16:07:40 -07003007 lockdep_assert_held(&memcg_slab_mutex);
Glauber Costad7f25f82012-12-18 14:22:40 -08003008
Vladimir Davydovbd673142014-06-04 16:07:40 -07003009 BUG_ON(is_root_cache(cachep));
Vladimir Davydov2edefe12014-01-23 15:53:02 -08003010
Vladimir Davydovbd673142014-06-04 16:07:40 -07003011 root_cache = cachep->memcg_params->root_cache;
3012 memcg = cachep->memcg_params->memcg;
Vladimir Davydov96403da2014-01-23 15:53:01 -08003013 id = memcg_cache_id(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08003014
Vladimir Davydovbd673142014-06-04 16:07:40 -07003015 BUG_ON(root_cache->memcg_params->memcg_caches[id] != cachep);
3016 root_cache->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003017
Vladimir Davydovbd673142014-06-04 16:07:40 -07003018 list_del(&cachep->memcg_params->list);
3019
3020 kmem_cache_destroy(cachep);
Vladimir Davydov33a690c2014-10-09 15:28:43 -07003021
3022 /* drop the reference taken in memcg_register_cache */
3023 css_put(&memcg->css);
Glauber Costa2633d7a2012-12-18 14:22:34 -08003024}
3025
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003026/*
3027 * During the creation a new cache, we need to disable our accounting mechanism
3028 * altogether. This is true even if we are not creating, but rather just
3029 * enqueing new caches to be created.
3030 *
3031 * This is because that process will trigger allocations; some visible, like
3032 * explicit kmallocs to auxiliary data structures, name strings and internal
3033 * cache structures; some well concealed, like INIT_WORK() that can allocate
3034 * objects during debug.
3035 *
3036 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3037 * to it. This may not be a bounded recursion: since the first cache creation
3038 * failed to complete (waiting on the allocation), we'll just try to create the
3039 * cache again, failing at the same point.
3040 *
3041 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3042 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3043 * inside the following two functions.
3044 */
3045static inline void memcg_stop_kmem_account(void)
3046{
3047 VM_BUG_ON(!current->mm);
3048 current->memcg_kmem_skip_account++;
3049}
3050
3051static inline void memcg_resume_kmem_account(void)
3052{
3053 VM_BUG_ON(!current->mm);
3054 current->memcg_kmem_skip_account--;
3055}
3056
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003057int __memcg_cleanup_cache_params(struct kmem_cache *s)
Glauber Costa7cf27982012-12-18 14:22:55 -08003058{
3059 struct kmem_cache *c;
Vladimir Davydovb8529902014-04-07 15:39:28 -07003060 int i, failed = 0;
Glauber Costa7cf27982012-12-18 14:22:55 -08003061
Vladimir Davydovbd673142014-06-04 16:07:40 -07003062 mutex_lock(&memcg_slab_mutex);
Qiang Huang7a67d7a2013-11-12 15:08:24 -08003063 for_each_memcg_cache_index(i) {
3064 c = cache_from_memcg_idx(s, i);
Glauber Costa7cf27982012-12-18 14:22:55 -08003065 if (!c)
3066 continue;
3067
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003068 memcg_unregister_cache(c);
Vladimir Davydovb8529902014-04-07 15:39:28 -07003069
3070 if (cache_from_memcg_idx(s, i))
3071 failed++;
Glauber Costa7cf27982012-12-18 14:22:55 -08003072 }
Vladimir Davydovbd673142014-06-04 16:07:40 -07003073 mutex_unlock(&memcg_slab_mutex);
Vladimir Davydovb8529902014-04-07 15:39:28 -07003074 return failed;
Glauber Costa7cf27982012-12-18 14:22:55 -08003075}
3076
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003077static void memcg_unregister_all_caches(struct mem_cgroup *memcg)
Glauber Costa1f458cb2012-12-18 14:22:50 -08003078{
3079 struct kmem_cache *cachep;
Vladimir Davydovbd673142014-06-04 16:07:40 -07003080 struct memcg_cache_params *params, *tmp;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003081
3082 if (!memcg_kmem_is_active(memcg))
3083 return;
3084
Vladimir Davydovbd673142014-06-04 16:07:40 -07003085 mutex_lock(&memcg_slab_mutex);
3086 list_for_each_entry_safe(params, tmp, &memcg->memcg_slab_caches, list) {
Glauber Costa1f458cb2012-12-18 14:22:50 -08003087 cachep = memcg_params_to_cache(params);
Vladimir Davydovbd673142014-06-04 16:07:40 -07003088 kmem_cache_shrink(cachep);
3089 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003090 memcg_unregister_cache(cachep);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003091 }
Vladimir Davydovbd673142014-06-04 16:07:40 -07003092 mutex_unlock(&memcg_slab_mutex);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003093}
3094
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003095struct memcg_register_cache_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07003096 struct mem_cgroup *memcg;
3097 struct kmem_cache *cachep;
3098 struct work_struct work;
3099};
3100
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003101static void memcg_register_cache_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08003102{
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003103 struct memcg_register_cache_work *cw =
3104 container_of(w, struct memcg_register_cache_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07003105 struct mem_cgroup *memcg = cw->memcg;
3106 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003107
Vladimir Davydovbd673142014-06-04 16:07:40 -07003108 mutex_lock(&memcg_slab_mutex);
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003109 memcg_register_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07003110 mutex_unlock(&memcg_slab_mutex);
3111
Vladimir Davydov5722d092014-04-07 15:39:24 -07003112 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003113 kfree(cw);
3114}
3115
3116/*
3117 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003118 */
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003119static void __memcg_schedule_register_cache(struct mem_cgroup *memcg,
3120 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003121{
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003122 struct memcg_register_cache_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08003123
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003124 cw = kmalloc(sizeof(*cw), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003125 if (cw == NULL) {
3126 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003127 return;
3128 }
3129
3130 cw->memcg = memcg;
3131 cw->cachep = cachep;
3132
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003133 INIT_WORK(&cw->work, memcg_register_cache_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08003134 schedule_work(&cw->work);
3135}
3136
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003137static void memcg_schedule_register_cache(struct mem_cgroup *memcg,
3138 struct kmem_cache *cachep)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003139{
3140 /*
3141 * We need to stop accounting when we kmalloc, because if the
3142 * corresponding kmalloc cache is not yet created, the first allocation
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003143 * in __memcg_schedule_register_cache will recurse.
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003144 *
3145 * However, it is better to enclose the whole function. Depending on
3146 * the debugging options enabled, INIT_WORK(), for instance, can
3147 * trigger an allocation. This too, will make us recurse. Because at
3148 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3149 * the safest choice is to do it like this, wrapping the whole function.
3150 */
3151 memcg_stop_kmem_account();
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003152 __memcg_schedule_register_cache(memcg, cachep);
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003153 memcg_resume_kmem_account();
3154}
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003155
3156int __memcg_charge_slab(struct kmem_cache *cachep, gfp_t gfp, int order)
3157{
3158 int res;
3159
3160 res = memcg_charge_kmem(cachep->memcg_params->memcg, gfp,
3161 PAGE_SIZE << order);
3162 if (!res)
3163 atomic_add(1 << order, &cachep->memcg_params->nr_pages);
3164 return res;
3165}
3166
3167void __memcg_uncharge_slab(struct kmem_cache *cachep, int order)
3168{
3169 memcg_uncharge_kmem(cachep->memcg_params->memcg, PAGE_SIZE << order);
3170 atomic_sub(1 << order, &cachep->memcg_params->nr_pages);
3171}
3172
Glauber Costad7f25f82012-12-18 14:22:40 -08003173/*
3174 * Return the kmem_cache we're supposed to use for a slab allocation.
3175 * We try to use the current memcg's version of the cache.
3176 *
3177 * If the cache does not exist yet, if we are the first user of it,
3178 * we either create it immediately, if possible, or create it asynchronously
3179 * in a workqueue.
3180 * In the latter case, we will let the current allocation go through with
3181 * the original cache.
3182 *
3183 * Can't be called in interrupt context or from kernel threads.
3184 * This function needs to be called with rcu_read_lock() held.
3185 */
3186struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3187 gfp_t gfp)
3188{
3189 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08003190 struct kmem_cache *memcg_cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003191
3192 VM_BUG_ON(!cachep->memcg_params);
3193 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3194
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003195 if (!current->mm || current->memcg_kmem_skip_account)
3196 return cachep;
3197
Glauber Costad7f25f82012-12-18 14:22:40 -08003198 rcu_read_lock();
3199 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003200
3201 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003202 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003203
Vladimir Davydov959c8962014-01-23 15:52:59 -08003204 memcg_cachep = cache_from_memcg_idx(cachep, memcg_cache_id(memcg));
3205 if (likely(memcg_cachep)) {
3206 cachep = memcg_cachep;
Li Zefanca0dde92013-04-29 15:08:57 -07003207 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003208 }
3209
Li Zefanca0dde92013-04-29 15:08:57 -07003210 /* The corresponding put will be done in the workqueue. */
Tejun Heoec903c02014-05-13 12:11:01 -04003211 if (!css_tryget_online(&memcg->css))
Li Zefanca0dde92013-04-29 15:08:57 -07003212 goto out;
3213 rcu_read_unlock();
3214
3215 /*
3216 * If we are in a safe context (can wait, and not in interrupt
3217 * context), we could be be predictable and return right away.
3218 * This would guarantee that the allocation being performed
3219 * already belongs in the new cache.
3220 *
3221 * However, there are some clashes that can arrive from locking.
3222 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003223 * memcg_create_kmem_cache, this means no further allocation
3224 * could happen with the slab_mutex held. So it's better to
3225 * defer everything.
Li Zefanca0dde92013-04-29 15:08:57 -07003226 */
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003227 memcg_schedule_register_cache(memcg, cachep);
Li Zefanca0dde92013-04-29 15:08:57 -07003228 return cachep;
3229out:
3230 rcu_read_unlock();
3231 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003232}
Glauber Costad7f25f82012-12-18 14:22:40 -08003233
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003234/*
3235 * We need to verify if the allocation against current->mm->owner's memcg is
3236 * possible for the given order. But the page is not allocated yet, so we'll
3237 * need a further commit step to do the final arrangements.
3238 *
3239 * It is possible for the task to switch cgroups in this mean time, so at
3240 * commit time, we can't rely on task conversion any longer. We'll then use
3241 * the handle argument to return to the caller which cgroup we should commit
3242 * against. We could also return the memcg directly and avoid the pointer
3243 * passing, but a boolean return value gives better semantics considering
3244 * the compiled-out case as well.
3245 *
3246 * Returning true means the allocation is possible.
3247 */
3248bool
3249__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3250{
3251 struct mem_cgroup *memcg;
3252 int ret;
3253
3254 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003255
3256 /*
3257 * Disabling accounting is only relevant for some specific memcg
3258 * internal allocations. Therefore we would initially not have such
Vladimir Davydov52383432014-06-04 16:06:39 -07003259 * check here, since direct calls to the page allocator that are
3260 * accounted to kmemcg (alloc_kmem_pages and friends) only happen
3261 * outside memcg core. We are mostly concerned with cache allocations,
3262 * and by having this test at memcg_kmem_get_cache, we are already able
3263 * to relay the allocation to the root cache and bypass the memcg cache
3264 * altogether.
Glauber Costa6d42c232013-07-08 16:00:00 -07003265 *
3266 * There is one exception, though: the SLUB allocator does not create
3267 * large order caches, but rather service large kmallocs directly from
3268 * the page allocator. Therefore, the following sequence when backed by
3269 * the SLUB allocator:
3270 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003271 * memcg_stop_kmem_account();
3272 * kmalloc(<large_number>)
3273 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003274 *
3275 * would effectively ignore the fact that we should skip accounting,
3276 * since it will drive us directly to this function without passing
3277 * through the cache selector memcg_kmem_get_cache. Such large
3278 * allocations are extremely rare but can happen, for instance, for the
3279 * cache arrays. We bring this test here.
3280 */
3281 if (!current->mm || current->memcg_kmem_skip_account)
3282 return true;
3283
Johannes Weinerdf381972014-04-07 15:37:43 -07003284 memcg = get_mem_cgroup_from_mm(current->mm);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003285
3286 if (!memcg_can_account_kmem(memcg)) {
3287 css_put(&memcg->css);
3288 return true;
3289 }
3290
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003291 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3292 if (!ret)
3293 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003294
3295 css_put(&memcg->css);
3296 return (ret == 0);
3297}
3298
3299void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3300 int order)
3301{
3302 struct page_cgroup *pc;
3303
3304 VM_BUG_ON(mem_cgroup_is_root(memcg));
3305
3306 /* The page allocation failed. Revert */
3307 if (!page) {
3308 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003309 return;
3310 }
Johannes Weinera840cda2014-08-06 16:06:04 -07003311 /*
3312 * The page is freshly allocated and not visible to any
3313 * outside callers yet. Set up pc non-atomically.
3314 */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003315 pc = lookup_page_cgroup(page);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003316 pc->mem_cgroup = memcg;
Johannes Weinera840cda2014-08-06 16:06:04 -07003317 pc->flags = PCG_USED;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003318}
3319
3320void __memcg_kmem_uncharge_pages(struct page *page, int order)
3321{
3322 struct mem_cgroup *memcg = NULL;
3323 struct page_cgroup *pc;
3324
3325
3326 pc = lookup_page_cgroup(page);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003327 if (!PageCgroupUsed(pc))
3328 return;
3329
Johannes Weinera840cda2014-08-06 16:06:04 -07003330 memcg = pc->mem_cgroup;
3331 pc->flags = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003332
3333 /*
3334 * We trust that only if there is a memcg associated with the page, it
3335 * is a valid allocation
3336 */
3337 if (!memcg)
3338 return;
3339
Sasha Levin309381fea2014-01-23 15:52:54 -08003340 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003341 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003342}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003343#else
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003344static inline void memcg_unregister_all_caches(struct mem_cgroup *memcg)
Glauber Costa1f458cb2012-12-18 14:22:50 -08003345{
3346}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003347#endif /* CONFIG_MEMCG_KMEM */
3348
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003349#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3350
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003351/*
3352 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003353 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3354 * charge/uncharge will be never happen and move_account() is done under
3355 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003356 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003357void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003358{
3359 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003360 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003361 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003362 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003363
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003364 if (mem_cgroup_disabled())
3365 return;
David Rientjesb070e652013-05-07 16:18:09 -07003366
3367 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003368 for (i = 1; i < HPAGE_PMD_NR; i++) {
3369 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003370 pc->mem_cgroup = memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003371 pc->flags = head_pc->flags;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003372 }
David Rientjesb070e652013-05-07 16:18:09 -07003373 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3374 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003375}
Hugh Dickins12d27102012-01-12 17:19:52 -08003376#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003377
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003378/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003379 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003380 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003381 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003382 * @pc: page_cgroup of the page.
3383 * @from: mem_cgroup which the page is moved from.
3384 * @to: mem_cgroup which the page is moved to. @from != @to.
3385 *
3386 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003387 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003388 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003389 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003390 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3391 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003392 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003393static int mem_cgroup_move_account(struct page *page,
3394 unsigned int nr_pages,
3395 struct page_cgroup *pc,
3396 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003397 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003398{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003399 unsigned long flags;
3400 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003401
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003402 VM_BUG_ON(from == to);
Sasha Levin309381fea2014-01-23 15:52:54 -08003403 VM_BUG_ON_PAGE(PageLRU(page), page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003404 /*
3405 * The page is isolated from LRU. So, collapse function
3406 * will not handle this page. But page splitting can happen.
3407 * Do this check under compound_page_lock(). The caller should
3408 * hold it.
3409 */
3410 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003411 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003412 goto out;
3413
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003414 /*
3415 * Prevent mem_cgroup_migrate() from looking at pc->mem_cgroup
3416 * of its source page while we change it: page migration takes
3417 * both pages off the LRU, but page cache replacement doesn't.
3418 */
3419 if (!trylock_page(page))
3420 goto out;
Johannes Weinerde3638d2011-03-23 16:42:28 -07003421
3422 ret = -EINVAL;
3423 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003424 goto out_unlock;
Johannes Weinerde3638d2011-03-23 16:42:28 -07003425
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003426 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003427
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003428 if (!PageAnon(page) && page_mapped(page)) {
Johannes Weiner59d1d252014-04-07 15:37:40 -07003429 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
3430 nr_pages);
3431 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
3432 nr_pages);
3433 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003434
Johannes Weiner59d1d252014-04-07 15:37:40 -07003435 if (PageWriteback(page)) {
3436 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK],
3437 nr_pages);
3438 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK],
3439 nr_pages);
3440 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003441
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003442 /*
3443 * It is safe to change pc->mem_cgroup here because the page
3444 * is referenced, charged, and isolated - we can't race with
3445 * uncharging, charging, migration, or LRU putback.
3446 */
Balbir Singhd69b0422009-06-17 16:26:34 -07003447
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003448 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003449 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003450 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003451 ret = 0;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003452
3453 local_irq_disable();
3454 mem_cgroup_charge_statistics(to, page, nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07003455 memcg_check_events(to, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003456 mem_cgroup_charge_statistics(from, page, -nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07003457 memcg_check_events(from, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003458 local_irq_enable();
3459out_unlock:
3460 unlock_page(page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003461out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003462 return ret;
3463}
3464
Michal Hocko2ef37d32012-10-26 13:37:30 +02003465/**
3466 * mem_cgroup_move_parent - moves page to the parent group
3467 * @page: the page to move
3468 * @pc: page_cgroup of the page
3469 * @child: page's cgroup
3470 *
3471 * move charges to its parent or the root cgroup if the group has no
3472 * parent (aka use_hierarchy==0).
3473 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3474 * mem_cgroup_move_account fails) the failure is always temporary and
3475 * it signals a race with a page removal/uncharge or migration. In the
3476 * first case the page is on the way out and it will vanish from the LRU
3477 * on the next attempt and the call should be retried later.
3478 * Isolation from the LRU fails only if page has been isolated from
3479 * the LRU since we looked at it and that usually means either global
3480 * reclaim or migration going on. The page will either get back to the
3481 * LRU or vanish.
3482 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3483 * (!PageCgroupUsed) or moved to a different group. The page will
3484 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003485 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003486static int mem_cgroup_move_parent(struct page *page,
3487 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003488 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003489{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003490 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003491 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003492 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003493 int ret;
3494
Michal Hockod8423012012-10-26 13:37:29 +02003495 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003496
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003497 ret = -EBUSY;
3498 if (!get_page_unless_zero(page))
3499 goto out;
3500 if (isolate_lru_page(page))
3501 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003502
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003503 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003504
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003505 parent = parent_mem_cgroup(child);
3506 /*
3507 * If no parent, move charges to root cgroup.
3508 */
3509 if (!parent)
3510 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003511
Michal Hocko2ef37d32012-10-26 13:37:30 +02003512 if (nr_pages > 1) {
Sasha Levin309381fea2014-01-23 15:52:54 -08003513 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003514 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003515 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003516
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003517 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003518 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003519 if (!ret)
3520 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003521
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003522 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003523 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003524 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003525put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003526 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003527out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003528 return ret;
3529}
3530
Andrew Mortonc255a452012-07-31 16:43:02 -07003531#ifdef CONFIG_MEMCG_SWAP
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003532static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
3533 bool charge)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003534{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003535 int val = (charge) ? 1 : -1;
3536 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003537}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003538
3539/**
3540 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3541 * @entry: swap entry to be moved
3542 * @from: mem_cgroup which the entry is moved from
3543 * @to: mem_cgroup which the entry is moved to
3544 *
3545 * It succeeds only when the swap_cgroup's record for this entry is the same
3546 * as the mem_cgroup's id of @from.
3547 *
3548 * Returns 0 on success, -EINVAL on failure.
3549 *
3550 * The caller must have charged to @to, IOW, called res_counter_charge() about
3551 * both res and memsw, and called css_get().
3552 */
3553static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003554 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003555{
3556 unsigned short old_id, new_id;
3557
Li Zefan34c00c32013-09-23 16:56:01 +08003558 old_id = mem_cgroup_id(from);
3559 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003560
3561 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003562 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003563 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003564 /*
3565 * This function is only called from task migration context now.
3566 * It postpones res_counter and refcount handling till the end
3567 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07003568 * improvement. But we cannot postpone css_get(to) because if
3569 * the process that has been moved to @to does swap-in, the
3570 * refcount of @to might be decreased to 0.
3571 *
3572 * We are in attach() phase, so the cgroup is guaranteed to be
3573 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003574 */
Li Zefan40503772013-07-08 16:00:34 -07003575 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003576 return 0;
3577 }
3578 return -EINVAL;
3579}
3580#else
3581static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003582 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003583{
3584 return -EINVAL;
3585}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003586#endif
3587
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003588#ifdef CONFIG_DEBUG_VM
3589static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3590{
3591 struct page_cgroup *pc;
3592
3593 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003594 /*
3595 * Can be NULL while feeding pages into the page allocator for
3596 * the first time, i.e. during boot or memory hotplug;
3597 * or when mem_cgroup_disabled().
3598 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003599 if (likely(pc) && PageCgroupUsed(pc))
3600 return pc;
3601 return NULL;
3602}
3603
3604bool mem_cgroup_bad_page_check(struct page *page)
3605{
3606 if (mem_cgroup_disabled())
3607 return false;
3608
3609 return lookup_page_cgroup_used(page) != NULL;
3610}
3611
3612void mem_cgroup_print_bad_page(struct page *page)
3613{
3614 struct page_cgroup *pc;
3615
3616 pc = lookup_page_cgroup_used(page);
3617 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08003618 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
3619 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003620 }
3621}
3622#endif
3623
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003624static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003625 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003626{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003627 int retry_count;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003628 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003629 int children = mem_cgroup_count_children(memcg);
3630 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003631 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003632
3633 /*
3634 * For keeping hierarchical_reclaim simple, how long we should retry
3635 * is depends on callers. We set our retry-count to be function
3636 * of # of children which we should visit in this loop.
3637 */
3638 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3639
3640 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003641
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003642 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003643 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003644 if (signal_pending(current)) {
3645 ret = -EINTR;
3646 break;
3647 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003648 /*
3649 * Rather than hide all in some function, I do this in
3650 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003651 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003652 */
3653 mutex_lock(&set_limit_mutex);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07003654 if (res_counter_read_u64(&memcg->memsw, RES_LIMIT) < val) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003655 ret = -EINVAL;
3656 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003657 break;
3658 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003659
Johannes Weiner3fbe7242014-10-09 15:28:54 -07003660 if (res_counter_read_u64(&memcg->res, RES_LIMIT) < val)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003661 enlarge = 1;
3662
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003663 ret = res_counter_set_limit(&memcg->res, val);
3664 mutex_unlock(&set_limit_mutex);
3665
3666 if (!ret)
3667 break;
3668
Johannes Weiner56600482012-01-12 17:17:59 -08003669 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3670 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003671 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3672 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003673 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003674 retry_count--;
3675 else
3676 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003677 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003678 if (!ret && enlarge)
3679 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003680
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003681 return ret;
3682}
3683
Li Zefan338c8432009-06-17 16:27:15 -07003684static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3685 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003686{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003687 int retry_count;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07003688 u64 oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003689 int children = mem_cgroup_count_children(memcg);
3690 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003691 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003692
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003693 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003694 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003695 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003696 while (retry_count) {
3697 if (signal_pending(current)) {
3698 ret = -EINTR;
3699 break;
3700 }
3701 /*
3702 * Rather than hide all in some function, I do this in
3703 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003704 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003705 */
3706 mutex_lock(&set_limit_mutex);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07003707 if (res_counter_read_u64(&memcg->res, RES_LIMIT) > val) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003708 ret = -EINVAL;
3709 mutex_unlock(&set_limit_mutex);
3710 break;
3711 }
Johannes Weiner3fbe7242014-10-09 15:28:54 -07003712 if (res_counter_read_u64(&memcg->memsw, RES_LIMIT) < val)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003713 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003714 ret = res_counter_set_limit(&memcg->memsw, val);
3715 mutex_unlock(&set_limit_mutex);
3716
3717 if (!ret)
3718 break;
3719
Johannes Weiner56600482012-01-12 17:17:59 -08003720 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3721 MEM_CGROUP_RECLAIM_NOSWAP |
3722 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003723 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003724 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003725 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003726 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003727 else
3728 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003729 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003730 if (!ret && enlarge)
3731 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003732 return ret;
3733}
3734
Andrew Morton0608f432013-09-24 15:27:41 -07003735unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
3736 gfp_t gfp_mask,
3737 unsigned long *total_scanned)
3738{
3739 unsigned long nr_reclaimed = 0;
3740 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3741 unsigned long reclaimed;
3742 int loop = 0;
3743 struct mem_cgroup_tree_per_zone *mctz;
3744 unsigned long long excess;
3745 unsigned long nr_scanned;
3746
3747 if (order > 0)
3748 return 0;
3749
3750 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
3751 /*
3752 * This loop can run a while, specially if mem_cgroup's continuously
3753 * keep exceeding their soft limit and putting the system under
3754 * pressure
3755 */
3756 do {
3757 if (next_mz)
3758 mz = next_mz;
3759 else
3760 mz = mem_cgroup_largest_soft_limit_node(mctz);
3761 if (!mz)
3762 break;
3763
3764 nr_scanned = 0;
3765 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
3766 gfp_mask, &nr_scanned);
3767 nr_reclaimed += reclaimed;
3768 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003769 spin_lock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003770
3771 /*
3772 * If we failed to reclaim anything from this memory cgroup
3773 * it is time to move on to the next cgroup
3774 */
3775 next_mz = NULL;
3776 if (!reclaimed) {
3777 do {
3778 /*
3779 * Loop until we find yet another one.
3780 *
3781 * By the time we get the soft_limit lock
3782 * again, someone might have aded the
3783 * group back on the RB tree. Iterate to
3784 * make sure we get a different mem.
3785 * mem_cgroup_largest_soft_limit_node returns
3786 * NULL if no other cgroup is present on
3787 * the tree
3788 */
3789 next_mz =
3790 __mem_cgroup_largest_soft_limit_node(mctz);
3791 if (next_mz == mz)
3792 css_put(&next_mz->memcg->css);
3793 else /* next_mz == NULL or other memcg */
3794 break;
3795 } while (1);
3796 }
Johannes Weinercf2c8122014-06-06 14:38:21 -07003797 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07003798 excess = res_counter_soft_limit_excess(&mz->memcg->res);
3799 /*
3800 * One school of thought says that we should not add
3801 * back the node to the tree if reclaim returns 0.
3802 * But our reclaim could return 0, simply because due
3803 * to priority we are exposing a smaller subset of
3804 * memory to reclaim from. Consider this as a longer
3805 * term TODO.
3806 */
3807 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003808 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003809 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003810 css_put(&mz->memcg->css);
3811 loop++;
3812 /*
3813 * Could not reclaim anything and there are no more
3814 * mem cgroups to try or we seem to be looping without
3815 * reclaiming anything.
3816 */
3817 if (!nr_reclaimed &&
3818 (next_mz == NULL ||
3819 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3820 break;
3821 } while (!nr_reclaimed);
3822 if (next_mz)
3823 css_put(&next_mz->memcg->css);
3824 return nr_reclaimed;
3825}
3826
Michal Hocko2ef37d32012-10-26 13:37:30 +02003827/**
3828 * mem_cgroup_force_empty_list - clears LRU of a group
3829 * @memcg: group to clear
3830 * @node: NUMA node
3831 * @zid: zone id
3832 * @lru: lru to to clear
3833 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003834 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003835 * reclaim the pages page themselves - pages are moved to the parent (or root)
3836 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003837 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003838static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003839 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003840{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003841 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003842 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003843 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003844 struct page *busy;
3845 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003846
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003847 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003848 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3849 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003850
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003851 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003852 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003853 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003854 struct page *page;
3855
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003856 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003857 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003858 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003859 break;
3860 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003861 page = list_entry(list->prev, struct page, lru);
3862 if (busy == page) {
3863 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003864 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003865 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003866 continue;
3867 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003868 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003869
Johannes Weiner925b7672012-01-12 17:18:15 -08003870 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003871
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003872 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003873 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003874 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003875 } else
3876 busy = NULL;
Hugh Dickins2a7a0e02014-06-04 16:11:04 -07003877 cond_resched();
Michal Hocko2ef37d32012-10-26 13:37:30 +02003878 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003879}
3880
3881/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003882 * make mem_cgroup's charge to be 0 if there is no task by moving
3883 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003884 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02003885 *
3886 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003887 */
Michal Hockoab5196c2012-10-26 13:37:32 +02003888static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003889{
Michal Hockoc26251f2012-10-26 13:37:28 +02003890 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08003891 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003892
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003893 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003894 /* This is for making all *used* pages to be on LRU. */
3895 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003896 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003897 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08003898 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003899 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003900 enum lru_list lru;
3901 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003902 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003903 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003904 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003905 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003906 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003907 mem_cgroup_end_move(memcg);
3908 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003909 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003910
Michal Hocko2ef37d32012-10-26 13:37:30 +02003911 /*
Glauber Costabea207c2012-12-18 14:22:11 -08003912 * Kernel memory may not necessarily be trackable to a specific
3913 * process. So they are not migrated, and therefore we can't
3914 * expect their value to drop to 0 here.
3915 * Having res filled up with kmem only is enough.
3916 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02003917 * This is a safety check because mem_cgroup_force_empty_list
3918 * could have raced with mem_cgroup_replace_page_cache callers
3919 * so the lru seemed empty but the page could have been added
3920 * right after the check. RES_USAGE should be safe as we always
3921 * charge before adding to the LRU.
3922 */
Glauber Costabea207c2012-12-18 14:22:11 -08003923 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
3924 res_counter_read_u64(&memcg->kmem, RES_USAGE);
3925 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02003926}
3927
Tejun Heoea280e72014-05-16 13:22:48 -04003928/*
3929 * Test whether @memcg has children, dead or alive. Note that this
3930 * function doesn't care whether @memcg has use_hierarchy enabled and
3931 * returns %true if there are child csses according to the cgroup
3932 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
3933 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003934static inline bool memcg_has_children(struct mem_cgroup *memcg)
3935{
Tejun Heoea280e72014-05-16 13:22:48 -04003936 bool ret;
3937
Johannes Weiner696ac172013-10-31 16:34:15 -07003938 /*
Tejun Heoea280e72014-05-16 13:22:48 -04003939 * The lock does not prevent addition or deletion of children, but
3940 * it prevents a new child from being initialized based on this
3941 * parent in css_online(), so it's enough to decide whether
3942 * hierarchically inherited attributes can still be changed or not.
Johannes Weiner696ac172013-10-31 16:34:15 -07003943 */
Tejun Heoea280e72014-05-16 13:22:48 -04003944 lockdep_assert_held(&memcg_create_mutex);
3945
3946 rcu_read_lock();
3947 ret = css_next_child(NULL, &memcg->css);
3948 rcu_read_unlock();
3949 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003950}
3951
3952/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003953 * Reclaims as many pages from the given memcg as possible and moves
3954 * the rest to the parent.
3955 *
3956 * Caller is responsible for holding css reference for memcg.
3957 */
3958static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3959{
3960 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003961
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003962 /* we call try-to-free pages for make this cgroup empty */
3963 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003964 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07003965 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003966 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003967
Michal Hockoc26251f2012-10-26 13:37:28 +02003968 if (signal_pending(current))
3969 return -EINTR;
3970
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003971 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003972 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003973 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003974 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003975 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003976 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003977 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003978
3979 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003980
3981 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003982}
3983
Tejun Heo6770c642014-05-13 12:16:21 -04003984static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3985 char *buf, size_t nbytes,
3986 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003987{
Tejun Heo6770c642014-05-13 12:16:21 -04003988 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003989
Michal Hockod8423012012-10-26 13:37:29 +02003990 if (mem_cgroup_is_root(memcg))
3991 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003992 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003993}
3994
Tejun Heo182446d2013-08-08 20:11:24 -04003995static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3996 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003997{
Tejun Heo182446d2013-08-08 20:11:24 -04003998 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003999}
4000
Tejun Heo182446d2013-08-08 20:11:24 -04004001static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4002 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004003{
4004 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004005 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04004006 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004007
Glauber Costa09998212013-02-22 16:34:55 -08004008 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004009
4010 if (memcg->use_hierarchy == val)
4011 goto out;
4012
Balbir Singh18f59ea2009-01-07 18:08:07 -08004013 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004014 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004015 * in the child subtrees. If it is unset, then the change can
4016 * occur, provided the current cgroup has no children.
4017 *
4018 * For the root cgroup, parent_mem is NULL, we allow value to be
4019 * set if there are no children.
4020 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004021 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004022 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04004023 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004024 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004025 else
4026 retval = -EBUSY;
4027 } else
4028 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004029
4030out:
Glauber Costa09998212013-02-22 16:34:55 -08004031 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004032
4033 return retval;
4034}
4035
Johannes Weinerce00a962014-09-05 08:43:57 -04004036static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
4037 enum mem_cgroup_stat_index idx)
4038{
4039 struct mem_cgroup *iter;
4040 long val = 0;
4041
4042 /* Per-cpu values can be negative, use a signed accumulator */
4043 for_each_mem_cgroup_tree(iter, memcg)
4044 val += mem_cgroup_read_stat(iter, idx);
4045
4046 if (val < 0) /* race ? */
4047 val = 0;
4048 return val;
4049}
4050
4051static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
4052{
4053 u64 val;
4054
4055 if (!mem_cgroup_is_root(memcg)) {
4056 if (!swap)
4057 return res_counter_read_u64(&memcg->res, RES_USAGE);
4058 else
4059 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
4060 }
4061
4062 /*
4063 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4064 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4065 */
4066 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4067 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
4068
4069 if (swap)
4070 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
4071
4072 return val << PAGE_SHIFT;
4073}
4074
4075
Tejun Heo791badb2013-12-05 12:28:02 -05004076static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07004077 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004078{
Tejun Heo182446d2013-08-08 20:11:24 -04004079 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner05b84302014-08-06 16:05:59 -07004080 enum res_type type = MEMFILE_TYPE(cft->private);
4081 int name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004082
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004083 switch (type) {
4084 case _MEM:
Johannes Weinerce00a962014-09-05 08:43:57 -04004085 if (name == RES_USAGE)
4086 return mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004087 return res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004088 case _MEMSWAP:
Johannes Weinerce00a962014-09-05 08:43:57 -04004089 if (name == RES_USAGE)
4090 return mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004091 return res_counter_read_u64(&memcg->memsw, name);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004092 case _KMEM:
Johannes Weiner05b84302014-08-06 16:05:59 -07004093 return res_counter_read_u64(&memcg->kmem, name);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004094 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004095 default:
4096 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004097 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004098}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004099
Glauber Costa510fc4e2012-12-18 14:21:47 -08004100#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovd6441632014-01-23 15:53:09 -08004101/* should be called with activate_kmem_mutex held */
4102static int __memcg_activate_kmem(struct mem_cgroup *memcg,
4103 unsigned long long limit)
4104{
4105 int err = 0;
4106 int memcg_id;
4107
4108 if (memcg_kmem_is_active(memcg))
4109 return 0;
4110
4111 /*
4112 * We are going to allocate memory for data shared by all memory
4113 * cgroups so let's stop accounting here.
4114 */
4115 memcg_stop_kmem_account();
4116
Glauber Costa510fc4e2012-12-18 14:21:47 -08004117 /*
4118 * For simplicity, we won't allow this to be disabled. It also can't
4119 * be changed if the cgroup has children already, or if tasks had
4120 * already joined.
4121 *
4122 * If tasks join before we set the limit, a person looking at
4123 * kmem.usage_in_bytes will have no way to determine when it took
4124 * place, which makes the value quite meaningless.
4125 *
4126 * After it first became limited, changes in the value of the limit are
4127 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004128 */
Glauber Costa09998212013-02-22 16:34:55 -08004129 mutex_lock(&memcg_create_mutex);
Tejun Heoea280e72014-05-16 13:22:48 -04004130 if (cgroup_has_tasks(memcg->css.cgroup) ||
4131 (memcg->use_hierarchy && memcg_has_children(memcg)))
Vladimir Davydovd6441632014-01-23 15:53:09 -08004132 err = -EBUSY;
Glauber Costa09998212013-02-22 16:34:55 -08004133 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004134 if (err)
4135 goto out;
4136
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07004137 memcg_id = memcg_alloc_cache_id();
Vladimir Davydovd6441632014-01-23 15:53:09 -08004138 if (memcg_id < 0) {
4139 err = memcg_id;
4140 goto out;
4141 }
4142
Vladimir Davydovd6441632014-01-23 15:53:09 -08004143 memcg->kmemcg_id = memcg_id;
4144 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004145
4146 /*
4147 * We couldn't have accounted to this cgroup, because it hasn't got the
4148 * active bit set yet, so this should succeed.
4149 */
4150 err = res_counter_set_limit(&memcg->kmem, limit);
4151 VM_BUG_ON(err);
4152
4153 static_key_slow_inc(&memcg_kmem_enabled_key);
4154 /*
4155 * Setting the active bit after enabling static branching will
4156 * guarantee no one starts accounting before all call sites are
4157 * patched.
4158 */
4159 memcg_kmem_set_active(memcg);
4160out:
4161 memcg_resume_kmem_account();
4162 return err;
Vladimir Davydovd6441632014-01-23 15:53:09 -08004163}
4164
4165static int memcg_activate_kmem(struct mem_cgroup *memcg,
4166 unsigned long long limit)
4167{
4168 int ret;
4169
4170 mutex_lock(&activate_kmem_mutex);
4171 ret = __memcg_activate_kmem(memcg, limit);
4172 mutex_unlock(&activate_kmem_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004173 return ret;
4174}
4175
Vladimir Davydovd6441632014-01-23 15:53:09 -08004176static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
4177 unsigned long long val)
4178{
4179 int ret;
4180
4181 if (!memcg_kmem_is_active(memcg))
4182 ret = memcg_activate_kmem(memcg, val);
4183 else
4184 ret = res_counter_set_limit(&memcg->kmem, val);
4185 return ret;
4186}
4187
Glauber Costa55007d82012-12-18 14:22:38 -08004188static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004189{
Glauber Costa55007d82012-12-18 14:22:38 -08004190 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004191 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004192
Glauber Costa510fc4e2012-12-18 14:21:47 -08004193 if (!parent)
Vladimir Davydovd6441632014-01-23 15:53:09 -08004194 return 0;
Glauber Costa55007d82012-12-18 14:22:38 -08004195
Vladimir Davydovd6441632014-01-23 15:53:09 -08004196 mutex_lock(&activate_kmem_mutex);
Glauber Costaa8964b92012-12-18 14:22:09 -08004197 /*
Vladimir Davydovd6441632014-01-23 15:53:09 -08004198 * If the parent cgroup is not kmem-active now, it cannot be activated
4199 * after this point, because it has at least one child already.
Glauber Costaa8964b92012-12-18 14:22:09 -08004200 */
Vladimir Davydovd6441632014-01-23 15:53:09 -08004201 if (memcg_kmem_is_active(parent))
4202 ret = __memcg_activate_kmem(memcg, RES_COUNTER_MAX);
4203 mutex_unlock(&activate_kmem_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08004204 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004205}
Vladimir Davydovd6441632014-01-23 15:53:09 -08004206#else
4207static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
4208 unsigned long long val)
4209{
4210 return -EINVAL;
4211}
Hugh Dickins6d0439902013-02-22 16:35:50 -08004212#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08004213
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004214/*
4215 * The user of this function is...
4216 * RES_LIMIT.
4217 */
Tejun Heo451af502014-05-13 12:16:21 -04004218static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
4219 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004220{
Tejun Heo451af502014-05-13 12:16:21 -04004221 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Glauber Costa86ae53e2012-12-18 14:21:45 -08004222 enum res_type type;
4223 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004224 unsigned long long val;
4225 int ret;
4226
Tejun Heo451af502014-05-13 12:16:21 -04004227 buf = strstrip(buf);
4228 type = MEMFILE_TYPE(of_cft(of)->private);
4229 name = MEMFILE_ATTR(of_cft(of)->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004230
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004231 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004232 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004233 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4234 ret = -EINVAL;
4235 break;
4236 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004237 /* This function does all necessary parse...reuse it */
Tejun Heo451af502014-05-13 12:16:21 -04004238 ret = res_counter_memparse_write_strategy(buf, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004239 if (ret)
4240 break;
4241 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004242 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004243 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004244 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004245 else if (type == _KMEM)
Vladimir Davydovd6441632014-01-23 15:53:09 -08004246 ret = memcg_update_kmem_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004247 else
4248 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004249 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004250 case RES_SOFT_LIMIT:
Tejun Heo451af502014-05-13 12:16:21 -04004251 ret = res_counter_memparse_write_strategy(buf, &val);
Balbir Singh296c81d2009-09-23 15:56:36 -07004252 if (ret)
4253 break;
4254 /*
4255 * For memsw, soft limits are hard to implement in terms
4256 * of semantics, for now, we support soft limits for
4257 * control without swap
4258 */
4259 if (type == _MEM)
4260 ret = res_counter_set_soft_limit(&memcg->res, val);
4261 else
4262 ret = -EINVAL;
4263 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004264 default:
4265 ret = -EINVAL; /* should be BUG() ? */
4266 break;
4267 }
Tejun Heo451af502014-05-13 12:16:21 -04004268 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004269}
4270
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004271static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4272 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4273{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004274 unsigned long long min_limit, min_memsw_limit, tmp;
4275
4276 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4277 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004278 if (!memcg->use_hierarchy)
4279 goto out;
4280
Tejun Heo5c9d5352014-05-16 13:22:48 -04004281 while (memcg->css.parent) {
4282 memcg = mem_cgroup_from_css(memcg->css.parent);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004283 if (!memcg->use_hierarchy)
4284 break;
4285 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4286 min_limit = min(min_limit, tmp);
4287 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4288 min_memsw_limit = min(min_memsw_limit, tmp);
4289 }
4290out:
4291 *mem_limit = min_limit;
4292 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004293}
4294
Tejun Heo6770c642014-05-13 12:16:21 -04004295static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
4296 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004297{
Tejun Heo6770c642014-05-13 12:16:21 -04004298 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Glauber Costa86ae53e2012-12-18 14:21:45 -08004299 int name;
4300 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004301
Tejun Heo6770c642014-05-13 12:16:21 -04004302 type = MEMFILE_TYPE(of_cft(of)->private);
4303 name = MEMFILE_ATTR(of_cft(of)->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004304
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004305 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004306 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004307 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004308 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004309 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004310 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004311 else if (type == _KMEM)
4312 res_counter_reset_max(&memcg->kmem);
4313 else
4314 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004315 break;
4316 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004317 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004318 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004319 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004320 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004321 else if (type == _KMEM)
4322 res_counter_reset_failcnt(&memcg->kmem);
4323 else
4324 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004325 break;
4326 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004327
Tejun Heo6770c642014-05-13 12:16:21 -04004328 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004329}
4330
Tejun Heo182446d2013-08-08 20:11:24 -04004331static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004332 struct cftype *cft)
4333{
Tejun Heo182446d2013-08-08 20:11:24 -04004334 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004335}
4336
Daisuke Nishimura02491442010-03-10 15:22:17 -08004337#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04004338static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004339 struct cftype *cft, u64 val)
4340{
Tejun Heo182446d2013-08-08 20:11:24 -04004341 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004342
4343 if (val >= (1 << NR_MOVE_TYPE))
4344 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004345
Glauber Costaee5e8472013-02-22 16:34:50 -08004346 /*
4347 * No kind of locking is needed in here, because ->can_attach() will
4348 * check this value once in the beginning of the process, and then carry
4349 * on with stale data. This means that changes to this value will only
4350 * affect task migrations starting after the change.
4351 */
4352 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004353 return 0;
4354}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004355#else
Tejun Heo182446d2013-08-08 20:11:24 -04004356static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004357 struct cftype *cft, u64 val)
4358{
4359 return -ENOSYS;
4360}
4361#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004362
Ying Han406eb0c2011-05-26 16:25:37 -07004363#ifdef CONFIG_NUMA
Tejun Heo2da8ca82013-12-05 12:28:04 -05004364static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07004365{
Greg Thelen25485de2013-11-12 15:07:40 -08004366 struct numa_stat {
4367 const char *name;
4368 unsigned int lru_mask;
4369 };
4370
4371 static const struct numa_stat stats[] = {
4372 { "total", LRU_ALL },
4373 { "file", LRU_ALL_FILE },
4374 { "anon", LRU_ALL_ANON },
4375 { "unevictable", BIT(LRU_UNEVICTABLE) },
4376 };
4377 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07004378 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08004379 unsigned long nr;
Tejun Heo2da8ca82013-12-05 12:28:04 -05004380 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Ying Han406eb0c2011-05-26 16:25:37 -07004381
Greg Thelen25485de2013-11-12 15:07:40 -08004382 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4383 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
4384 seq_printf(m, "%s=%lu", stat->name, nr);
4385 for_each_node_state(nid, N_MEMORY) {
4386 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
4387 stat->lru_mask);
4388 seq_printf(m, " N%d=%lu", nid, nr);
4389 }
4390 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07004391 }
Ying Han406eb0c2011-05-26 16:25:37 -07004392
Ying Han071aee12013-11-12 15:07:41 -08004393 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4394 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07004395
Ying Han071aee12013-11-12 15:07:41 -08004396 nr = 0;
4397 for_each_mem_cgroup_tree(iter, memcg)
4398 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
4399 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
4400 for_each_node_state(nid, N_MEMORY) {
4401 nr = 0;
4402 for_each_mem_cgroup_tree(iter, memcg)
4403 nr += mem_cgroup_node_nr_lru_pages(
4404 iter, nid, stat->lru_mask);
4405 seq_printf(m, " N%d=%lu", nid, nr);
4406 }
4407 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07004408 }
Ying Han406eb0c2011-05-26 16:25:37 -07004409
Ying Han406eb0c2011-05-26 16:25:37 -07004410 return 0;
4411}
4412#endif /* CONFIG_NUMA */
4413
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004414static inline void mem_cgroup_lru_names_not_uptodate(void)
4415{
4416 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4417}
4418
Tejun Heo2da8ca82013-12-05 12:28:04 -05004419static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004420{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004421 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004422 struct mem_cgroup *mi;
4423 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004424
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004425 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004426 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004427 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004428 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4429 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004430 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004431
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004432 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4433 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4434 mem_cgroup_read_events(memcg, i));
4435
4436 for (i = 0; i < NR_LRU_LISTS; i++)
4437 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4438 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4439
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004440 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004441 {
4442 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004443 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004444 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004445 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004446 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4447 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004448 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004449
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004450 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4451 long long val = 0;
4452
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004453 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004454 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004455 for_each_mem_cgroup_tree(mi, memcg)
4456 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4457 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4458 }
4459
4460 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4461 unsigned long long val = 0;
4462
4463 for_each_mem_cgroup_tree(mi, memcg)
4464 val += mem_cgroup_read_events(mi, i);
4465 seq_printf(m, "total_%s %llu\n",
4466 mem_cgroup_events_names[i], val);
4467 }
4468
4469 for (i = 0; i < NR_LRU_LISTS; i++) {
4470 unsigned long long val = 0;
4471
4472 for_each_mem_cgroup_tree(mi, memcg)
4473 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4474 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004475 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004476
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004477#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004478 {
4479 int nid, zid;
4480 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004481 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004482 unsigned long recent_rotated[2] = {0, 0};
4483 unsigned long recent_scanned[2] = {0, 0};
4484
4485 for_each_online_node(nid)
4486 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Jianyu Zhane2318752014-06-06 14:38:20 -07004487 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
Hugh Dickins89abfab2012-05-29 15:06:53 -07004488 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004489
Hugh Dickins89abfab2012-05-29 15:06:53 -07004490 recent_rotated[0] += rstat->recent_rotated[0];
4491 recent_rotated[1] += rstat->recent_rotated[1];
4492 recent_scanned[0] += rstat->recent_scanned[0];
4493 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004494 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004495 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4496 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4497 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4498 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004499 }
4500#endif
4501
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004502 return 0;
4503}
4504
Tejun Heo182446d2013-08-08 20:11:24 -04004505static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4506 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004507{
Tejun Heo182446d2013-08-08 20:11:24 -04004508 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004509
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004510 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004511}
4512
Tejun Heo182446d2013-08-08 20:11:24 -04004513static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4514 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004515{
Tejun Heo182446d2013-08-08 20:11:24 -04004516 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08004517
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004518 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004519 return -EINVAL;
4520
Linus Torvalds14208b02014-06-09 15:03:33 -07004521 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004522 memcg->swappiness = val;
4523 else
4524 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08004525
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004526 return 0;
4527}
4528
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004529static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4530{
4531 struct mem_cgroup_threshold_ary *t;
4532 u64 usage;
4533 int i;
4534
4535 rcu_read_lock();
4536 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004537 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004538 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004539 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004540
4541 if (!t)
4542 goto unlock;
4543
Johannes Weinerce00a962014-09-05 08:43:57 -04004544 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004545
4546 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004547 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004548 * If it's not true, a threshold was crossed after last
4549 * call of __mem_cgroup_threshold().
4550 */
Phil Carmody5407a562010-05-26 14:42:42 -07004551 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004552
4553 /*
4554 * Iterate backward over array of thresholds starting from
4555 * current_threshold and check if a threshold is crossed.
4556 * If none of thresholds below usage is crossed, we read
4557 * only one element of the array here.
4558 */
4559 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4560 eventfd_signal(t->entries[i].eventfd, 1);
4561
4562 /* i = current_threshold + 1 */
4563 i++;
4564
4565 /*
4566 * Iterate forward over array of thresholds starting from
4567 * current_threshold+1 and check if a threshold is crossed.
4568 * If none of thresholds above usage is crossed, we read
4569 * only one element of the array here.
4570 */
4571 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4572 eventfd_signal(t->entries[i].eventfd, 1);
4573
4574 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004575 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004576unlock:
4577 rcu_read_unlock();
4578}
4579
4580static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4581{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004582 while (memcg) {
4583 __mem_cgroup_threshold(memcg, false);
4584 if (do_swap_account)
4585 __mem_cgroup_threshold(memcg, true);
4586
4587 memcg = parent_mem_cgroup(memcg);
4588 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004589}
4590
4591static int compare_thresholds(const void *a, const void *b)
4592{
4593 const struct mem_cgroup_threshold *_a = a;
4594 const struct mem_cgroup_threshold *_b = b;
4595
Greg Thelen2bff24a2013-09-11 14:23:08 -07004596 if (_a->threshold > _b->threshold)
4597 return 1;
4598
4599 if (_a->threshold < _b->threshold)
4600 return -1;
4601
4602 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004603}
4604
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004605static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004606{
4607 struct mem_cgroup_eventfd_list *ev;
4608
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004609 spin_lock(&memcg_oom_lock);
4610
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004611 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004612 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004613
4614 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004615 return 0;
4616}
4617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004618static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004619{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004620 struct mem_cgroup *iter;
4621
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004622 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004623 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004624}
4625
Tejun Heo59b6f872013-11-22 18:20:43 -05004626static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004627 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004628{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004629 struct mem_cgroup_thresholds *thresholds;
4630 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004631 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004632 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004633
4634 ret = res_counter_memparse_write_strategy(args, &threshold);
4635 if (ret)
4636 return ret;
4637
4638 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004639
Johannes Weiner05b84302014-08-06 16:05:59 -07004640 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004641 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004642 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004643 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004644 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004645 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004646 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004647 BUG();
4648
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004649 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004650 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004651 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4652
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004653 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004654
4655 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004656 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004657 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004658 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004659 ret = -ENOMEM;
4660 goto unlock;
4661 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004662 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004663
4664 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004665 if (thresholds->primary) {
4666 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004667 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004668 }
4669
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004670 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004671 new->entries[size - 1].eventfd = eventfd;
4672 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004673
4674 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004675 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004676 compare_thresholds, NULL);
4677
4678 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004679 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004680 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004681 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004682 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004683 * new->current_threshold will not be used until
4684 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004685 * it here.
4686 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004687 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004688 } else
4689 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004690 }
4691
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004692 /* Free old spare buffer and save old primary buffer as spare */
4693 kfree(thresholds->spare);
4694 thresholds->spare = thresholds->primary;
4695
4696 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004697
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004698 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004699 synchronize_rcu();
4700
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004701unlock:
4702 mutex_unlock(&memcg->thresholds_lock);
4703
4704 return ret;
4705}
4706
Tejun Heo59b6f872013-11-22 18:20:43 -05004707static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004708 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004709{
Tejun Heo59b6f872013-11-22 18:20:43 -05004710 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004711}
4712
Tejun Heo59b6f872013-11-22 18:20:43 -05004713static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004714 struct eventfd_ctx *eventfd, const char *args)
4715{
Tejun Heo59b6f872013-11-22 18:20:43 -05004716 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004717}
4718
Tejun Heo59b6f872013-11-22 18:20:43 -05004719static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004720 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004721{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004722 struct mem_cgroup_thresholds *thresholds;
4723 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004724 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004725 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004726
4727 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07004728
4729 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004730 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004731 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004732 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004733 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004734 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004735 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004736 BUG();
4737
Anton Vorontsov371528c2012-02-24 05:14:46 +04004738 if (!thresholds->primary)
4739 goto unlock;
4740
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004741 /* Check if a threshold crossed before removing */
4742 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4743
4744 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004745 size = 0;
4746 for (i = 0; i < thresholds->primary->size; i++) {
4747 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004748 size++;
4749 }
4750
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004751 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004752
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004753 /* Set thresholds array to NULL if we don't have thresholds */
4754 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004755 kfree(new);
4756 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004757 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004758 }
4759
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004760 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004761
4762 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004763 new->current_threshold = -1;
4764 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4765 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004766 continue;
4767
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004768 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004769 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004770 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004771 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004772 * until rcu_assign_pointer(), so it's safe to increment
4773 * it here.
4774 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004775 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004776 }
4777 j++;
4778 }
4779
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004780swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004781 /* Swap primary and spare array */
4782 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004783 /* If all events are unregistered, free the spare array */
4784 if (!new) {
4785 kfree(thresholds->spare);
4786 thresholds->spare = NULL;
4787 }
4788
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004789 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004790
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004791 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004792 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004793unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004794 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004795}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004796
Tejun Heo59b6f872013-11-22 18:20:43 -05004797static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004798 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004799{
Tejun Heo59b6f872013-11-22 18:20:43 -05004800 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004801}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004802
Tejun Heo59b6f872013-11-22 18:20:43 -05004803static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004804 struct eventfd_ctx *eventfd)
4805{
Tejun Heo59b6f872013-11-22 18:20:43 -05004806 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004807}
4808
Tejun Heo59b6f872013-11-22 18:20:43 -05004809static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004810 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004811{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004812 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004813
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004814 event = kmalloc(sizeof(*event), GFP_KERNEL);
4815 if (!event)
4816 return -ENOMEM;
4817
Michal Hocko1af8efe2011-07-26 16:08:24 -07004818 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004819
4820 event->eventfd = eventfd;
4821 list_add(&event->list, &memcg->oom_notify);
4822
4823 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004824 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004825 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004826 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004827
4828 return 0;
4829}
4830
Tejun Heo59b6f872013-11-22 18:20:43 -05004831static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004832 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004833{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004834 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004835
Michal Hocko1af8efe2011-07-26 16:08:24 -07004836 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004837
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004838 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004839 if (ev->eventfd == eventfd) {
4840 list_del(&ev->list);
4841 kfree(ev);
4842 }
4843 }
4844
Michal Hocko1af8efe2011-07-26 16:08:24 -07004845 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004846}
4847
Tejun Heo2da8ca82013-12-05 12:28:04 -05004848static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004849{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004850 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004851
Tejun Heo791badb2013-12-05 12:28:02 -05004852 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
4853 seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004854 return 0;
4855}
4856
Tejun Heo182446d2013-08-08 20:11:24 -04004857static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004858 struct cftype *cft, u64 val)
4859{
Tejun Heo182446d2013-08-08 20:11:24 -04004860 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004861
4862 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07004863 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004864 return -EINVAL;
4865
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004866 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004867 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004868 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004869
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004870 return 0;
4871}
4872
Andrew Mortonc255a452012-07-31 16:43:02 -07004873#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004874static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004875{
Glauber Costa55007d82012-12-18 14:22:38 -08004876 int ret;
4877
Glauber Costa2633d7a2012-12-18 14:22:34 -08004878 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08004879 ret = memcg_propagate_kmem(memcg);
4880 if (ret)
4881 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08004882
Glauber Costa1d62e432012-04-09 19:36:33 -03004883 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07004884}
Glauber Costae5671df2011-12-11 21:47:01 +00004885
Li Zefan10d5ebf2013-07-08 16:00:33 -07004886static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004887{
Glauber Costa1d62e432012-04-09 19:36:33 -03004888 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07004889}
4890
4891static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
4892{
4893 if (!memcg_kmem_is_active(memcg))
4894 return;
4895
4896 /*
4897 * kmem charges can outlive the cgroup. In the case of slab
4898 * pages, for instance, a page contain objects from various
4899 * processes. As we prevent from taking a reference for every
4900 * such allocation we have to be careful when doing uncharge
4901 * (see memcg_uncharge_kmem) and here during offlining.
4902 *
4903 * The idea is that that only the _last_ uncharge which sees
4904 * the dead memcg will drop the last reference. An additional
4905 * reference is taken here before the group is marked dead
4906 * which is then paired with css_put during uncharge resp. here.
4907 *
4908 * Although this might sound strange as this path is called from
Tejun Heoec903c02014-05-13 12:11:01 -04004909 * css_offline() when the referencemight have dropped down to 0 and
4910 * shouldn't be incremented anymore (css_tryget_online() would
4911 * fail) we do not have other options because of the kmem
4912 * allocations lifetime.
Li Zefan10d5ebf2013-07-08 16:00:33 -07004913 */
4914 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08004915
4916 memcg_kmem_mark_dead(memcg);
4917
4918 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
4919 return;
4920
Glauber Costa7de37682012-12-18 14:22:07 -08004921 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07004922 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004923}
Glauber Costae5671df2011-12-11 21:47:01 +00004924#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004925static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004926{
4927 return 0;
4928}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004929
Li Zefan10d5ebf2013-07-08 16:00:33 -07004930static void memcg_destroy_kmem(struct mem_cgroup *memcg)
4931{
4932}
4933
4934static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004935{
4936}
Glauber Costae5671df2011-12-11 21:47:01 +00004937#endif
4938
Tejun Heo79bd9812013-11-22 18:20:42 -05004939/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004940 * DO NOT USE IN NEW FILES.
4941 *
4942 * "cgroup.event_control" implementation.
4943 *
4944 * This is way over-engineered. It tries to support fully configurable
4945 * events for each user. Such level of flexibility is completely
4946 * unnecessary especially in the light of the planned unified hierarchy.
4947 *
4948 * Please deprecate this and replace with something simpler if at all
4949 * possible.
4950 */
4951
4952/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004953 * Unregister event and free resources.
4954 *
4955 * Gets called from workqueue.
4956 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004957static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004958{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004959 struct mem_cgroup_event *event =
4960 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004961 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004962
4963 remove_wait_queue(event->wqh, &event->wait);
4964
Tejun Heo59b6f872013-11-22 18:20:43 -05004965 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004966
4967 /* Notify userspace the event is going away. */
4968 eventfd_signal(event->eventfd, 1);
4969
4970 eventfd_ctx_put(event->eventfd);
4971 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004972 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004973}
4974
4975/*
4976 * Gets called on POLLHUP on eventfd when user closes it.
4977 *
4978 * Called with wqh->lock held and interrupts disabled.
4979 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004980static int memcg_event_wake(wait_queue_t *wait, unsigned mode,
4981 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004982{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004983 struct mem_cgroup_event *event =
4984 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004985 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004986 unsigned long flags = (unsigned long)key;
4987
4988 if (flags & POLLHUP) {
4989 /*
4990 * If the event has been detached at cgroup removal, we
4991 * can simply return knowing the other side will cleanup
4992 * for us.
4993 *
4994 * We can't race against event freeing since the other
4995 * side will require wqh->lock via remove_wait_queue(),
4996 * which we hold.
4997 */
Tejun Heofba94802013-11-22 18:20:43 -05004998 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004999 if (!list_empty(&event->list)) {
5000 list_del_init(&event->list);
5001 /*
5002 * We are in atomic context, but cgroup_event_remove()
5003 * may sleep, so we have to call it in workqueue.
5004 */
5005 schedule_work(&event->remove);
5006 }
Tejun Heofba94802013-11-22 18:20:43 -05005007 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05005008 }
5009
5010 return 0;
5011}
5012
Tejun Heo3bc942f2013-11-22 18:20:44 -05005013static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05005014 wait_queue_head_t *wqh, poll_table *pt)
5015{
Tejun Heo3bc942f2013-11-22 18:20:44 -05005016 struct mem_cgroup_event *event =
5017 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05005018
5019 event->wqh = wqh;
5020 add_wait_queue(wqh, &event->wait);
5021}
5022
5023/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05005024 * DO NOT USE IN NEW FILES.
5025 *
Tejun Heo79bd9812013-11-22 18:20:42 -05005026 * Parse input and register new cgroup event handler.
5027 *
5028 * Input must be in format '<event_fd> <control_fd> <args>'.
5029 * Interpretation of args is defined by control file implementation.
5030 */
Tejun Heo451af502014-05-13 12:16:21 -04005031static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
5032 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05005033{
Tejun Heo451af502014-05-13 12:16:21 -04005034 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05005035 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005036 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05005037 struct cgroup_subsys_state *cfile_css;
5038 unsigned int efd, cfd;
5039 struct fd efile;
5040 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05005041 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05005042 char *endp;
5043 int ret;
5044
Tejun Heo451af502014-05-13 12:16:21 -04005045 buf = strstrip(buf);
5046
5047 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05005048 if (*endp != ' ')
5049 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04005050 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05005051
Tejun Heo451af502014-05-13 12:16:21 -04005052 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05005053 if ((*endp != ' ') && (*endp != '\0'))
5054 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04005055 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05005056
5057 event = kzalloc(sizeof(*event), GFP_KERNEL);
5058 if (!event)
5059 return -ENOMEM;
5060
Tejun Heo59b6f872013-11-22 18:20:43 -05005061 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05005062 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005063 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
5064 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
5065 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05005066
5067 efile = fdget(efd);
5068 if (!efile.file) {
5069 ret = -EBADF;
5070 goto out_kfree;
5071 }
5072
5073 event->eventfd = eventfd_ctx_fileget(efile.file);
5074 if (IS_ERR(event->eventfd)) {
5075 ret = PTR_ERR(event->eventfd);
5076 goto out_put_efile;
5077 }
5078
5079 cfile = fdget(cfd);
5080 if (!cfile.file) {
5081 ret = -EBADF;
5082 goto out_put_eventfd;
5083 }
5084
5085 /* the process need read permission on control file */
5086 /* AV: shouldn't we check that it's been opened for read instead? */
5087 ret = inode_permission(file_inode(cfile.file), MAY_READ);
5088 if (ret < 0)
5089 goto out_put_cfile;
5090
Tejun Heo79bd9812013-11-22 18:20:42 -05005091 /*
Tejun Heofba94802013-11-22 18:20:43 -05005092 * Determine the event callbacks and set them in @event. This used
5093 * to be done via struct cftype but cgroup core no longer knows
5094 * about these events. The following is crude but the whole thing
5095 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05005096 *
5097 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05005098 */
5099 name = cfile.file->f_dentry->d_name.name;
5100
5101 if (!strcmp(name, "memory.usage_in_bytes")) {
5102 event->register_event = mem_cgroup_usage_register_event;
5103 event->unregister_event = mem_cgroup_usage_unregister_event;
5104 } else if (!strcmp(name, "memory.oom_control")) {
5105 event->register_event = mem_cgroup_oom_register_event;
5106 event->unregister_event = mem_cgroup_oom_unregister_event;
5107 } else if (!strcmp(name, "memory.pressure_level")) {
5108 event->register_event = vmpressure_register_event;
5109 event->unregister_event = vmpressure_unregister_event;
5110 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05005111 event->register_event = memsw_cgroup_usage_register_event;
5112 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05005113 } else {
5114 ret = -EINVAL;
5115 goto out_put_cfile;
5116 }
5117
5118 /*
Tejun Heob5557c42013-11-22 18:20:42 -05005119 * Verify @cfile should belong to @css. Also, remaining events are
5120 * automatically removed on cgroup destruction but the removal is
5121 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05005122 */
Tejun Heoec903c02014-05-13 12:11:01 -04005123 cfile_css = css_tryget_online_from_dir(cfile.file->f_dentry->d_parent,
5124 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05005125 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05005126 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05005127 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05005128 if (cfile_css != css) {
5129 css_put(cfile_css);
5130 goto out_put_cfile;
5131 }
Tejun Heo79bd9812013-11-22 18:20:42 -05005132
Tejun Heo451af502014-05-13 12:16:21 -04005133 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05005134 if (ret)
5135 goto out_put_css;
5136
5137 efile.file->f_op->poll(efile.file, &event->pt);
5138
Tejun Heofba94802013-11-22 18:20:43 -05005139 spin_lock(&memcg->event_list_lock);
5140 list_add(&event->list, &memcg->event_list);
5141 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05005142
5143 fdput(cfile);
5144 fdput(efile);
5145
Tejun Heo451af502014-05-13 12:16:21 -04005146 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05005147
5148out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05005149 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05005150out_put_cfile:
5151 fdput(cfile);
5152out_put_eventfd:
5153 eventfd_ctx_put(event->eventfd);
5154out_put_efile:
5155 fdput(efile);
5156out_kfree:
5157 kfree(event);
5158
5159 return ret;
5160}
5161
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005162static struct cftype mem_cgroup_files[] = {
5163 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005164 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005165 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05005166 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005167 },
5168 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005169 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005170 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04005171 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005172 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005173 },
5174 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005175 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005176 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04005177 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05005178 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005179 },
5180 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005181 .name = "soft_limit_in_bytes",
5182 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04005183 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05005184 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07005185 },
5186 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005187 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005188 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04005189 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005190 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005191 },
Balbir Singh8697d332008-02-07 00:13:59 -08005192 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005193 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005194 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005195 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005196 {
5197 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04005198 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005199 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005200 {
5201 .name = "use_hierarchy",
5202 .write_u64 = mem_cgroup_hierarchy_write,
5203 .read_u64 = mem_cgroup_hierarchy_read,
5204 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005205 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05005206 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04005207 .write = memcg_write_event_control,
Tejun Heo79bd9812013-11-22 18:20:42 -05005208 .flags = CFTYPE_NO_PREFIX,
5209 .mode = S_IWUGO,
5210 },
5211 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005212 .name = "swappiness",
5213 .read_u64 = mem_cgroup_swappiness_read,
5214 .write_u64 = mem_cgroup_swappiness_write,
5215 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005216 {
5217 .name = "move_charge_at_immigrate",
5218 .read_u64 = mem_cgroup_move_charge_read,
5219 .write_u64 = mem_cgroup_move_charge_write,
5220 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005221 {
5222 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005223 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005224 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005225 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5226 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005227 {
5228 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005229 },
Ying Han406eb0c2011-05-26 16:25:37 -07005230#ifdef CONFIG_NUMA
5231 {
5232 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005233 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005234 },
5235#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005236#ifdef CONFIG_MEMCG_KMEM
5237 {
5238 .name = "kmem.limit_in_bytes",
5239 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04005240 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05005241 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08005242 },
5243 {
5244 .name = "kmem.usage_in_bytes",
5245 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05005246 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08005247 },
5248 {
5249 .name = "kmem.failcnt",
5250 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04005251 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005252 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08005253 },
5254 {
5255 .name = "kmem.max_usage_in_bytes",
5256 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04005257 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005258 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08005259 },
Glauber Costa749c5412012-12-18 14:23:01 -08005260#ifdef CONFIG_SLABINFO
5261 {
5262 .name = "kmem.slabinfo",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005263 .seq_show = mem_cgroup_slabinfo_read,
Glauber Costa749c5412012-12-18 14:23:01 -08005264 },
5265#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005266#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005267 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005268};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005269
Michal Hocko2d110852013-02-22 16:34:43 -08005270#ifdef CONFIG_MEMCG_SWAP
5271static struct cftype memsw_cgroup_files[] = {
5272 {
5273 .name = "memsw.usage_in_bytes",
5274 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05005275 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08005276 },
5277 {
5278 .name = "memsw.max_usage_in_bytes",
5279 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04005280 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005281 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08005282 },
5283 {
5284 .name = "memsw.limit_in_bytes",
5285 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04005286 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05005287 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08005288 },
5289 {
5290 .name = "memsw.failcnt",
5291 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04005292 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05005293 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08005294 },
5295 { }, /* terminate */
5296};
5297#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005298static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005299{
5300 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005301 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005302 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005303 /*
5304 * This routine is called against possible nodes.
5305 * But it's BUG to call kmalloc() against offline node.
5306 *
5307 * TODO: this routine can waste much memory for nodes which will
5308 * never be onlined. It's better to use memory hotplug callback
5309 * function.
5310 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005311 if (!node_state(node, N_NORMAL_MEMORY))
5312 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005313 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005314 if (!pn)
5315 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005316
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005317 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5318 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005319 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07005320 mz->usage_in_excess = 0;
5321 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005322 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005323 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005324 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005325 return 0;
5326}
5327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005328static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005329{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005330 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005331}
5332
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005333static struct mem_cgroup *mem_cgroup_alloc(void)
5334{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005335 struct mem_cgroup *memcg;
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005336 size_t size;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005337
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005338 size = sizeof(struct mem_cgroup);
5339 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005340
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005341 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005342 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005343 return NULL;
5344
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005345 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5346 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005347 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005348 spin_lock_init(&memcg->pcp_counter_lock);
5349 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005350
5351out_free:
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005352 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005353 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005354}
5355
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005356/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005357 * At destroying mem_cgroup, references from swap_cgroup can remain.
5358 * (scanning all at force_empty is too costly...)
5359 *
5360 * Instead of clearing all references at force_empty, we remember
5361 * the number of reference from swap_cgroup and free mem_cgroup when
5362 * it goes down to 0.
5363 *
5364 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005365 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005366
5367static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005368{
Glauber Costac8b2a362012-12-18 14:22:13 -08005369 int node;
Hugh Dickins59927fb2012-03-15 15:17:07 -07005370
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07005371 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08005372
5373 for_each_node(node)
5374 free_mem_cgroup_per_zone_info(memcg, node);
5375
5376 free_percpu(memcg->stat);
5377
Glauber Costa3f134612012-05-29 15:07:11 -07005378 /*
5379 * We need to make sure that (at least for now), the jump label
5380 * destruction code runs outside of the cgroup lock. This is because
5381 * get_online_cpus(), which is called from the static_branch update,
5382 * can't be called inside the cgroup_lock. cpusets are the ones
5383 * enforcing this dependency, so if they ever change, we might as well.
5384 *
5385 * schedule_work() will guarantee this happens. Be careful if you need
5386 * to move this code around, and make sure it is outside
5387 * the cgroup_lock.
5388 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005389 disarm_static_keys(memcg);
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005390 kfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005391}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005392
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005393/*
5394 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5395 */
Glauber Costae1aab162011-12-11 21:47:03 +00005396struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005397{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005398 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005399 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005400 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005401}
Glauber Costae1aab162011-12-11 21:47:03 +00005402EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005403
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07005404static void __init mem_cgroup_soft_limit_tree_init(void)
5405{
5406 struct mem_cgroup_tree_per_node *rtpn;
5407 struct mem_cgroup_tree_per_zone *rtpz;
5408 int tmp, node, zone;
5409
5410 for_each_node(node) {
5411 tmp = node;
5412 if (!node_state(node, N_NORMAL_MEMORY))
5413 tmp = -1;
5414 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5415 BUG_ON(!rtpn);
5416
5417 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5418
5419 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5420 rtpz = &rtpn->rb_tree_per_zone[zone];
5421 rtpz->rb_root = RB_ROOT;
5422 spin_lock_init(&rtpz->lock);
5423 }
5424 }
5425}
5426
Li Zefan0eb253e2009-01-15 13:51:25 -08005427static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04005428mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005429{
Glauber Costad142e3e2013-02-22 16:34:52 -08005430 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005431 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005432 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005433
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005434 memcg = mem_cgroup_alloc();
5435 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005436 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005437
Bob Liu3ed28fa2012-01-12 17:19:04 -08005438 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005439 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005440 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005441
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005442 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04005443 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08005444 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08005445 res_counter_init(&memcg->res, NULL);
5446 res_counter_init(&memcg->memsw, NULL);
5447 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005448 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005449
Glauber Costad142e3e2013-02-22 16:34:52 -08005450 memcg->last_scanned_node = MAX_NUMNODES;
5451 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005452 memcg->move_charge_at_immigrate = 0;
5453 mutex_init(&memcg->thresholds_lock);
5454 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005455 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05005456 INIT_LIST_HEAD(&memcg->event_list);
5457 spin_lock_init(&memcg->event_list_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08005458
5459 return &memcg->css;
5460
5461free_out:
5462 __mem_cgroup_free(memcg);
5463 return ERR_PTR(error);
5464}
5465
5466static int
Tejun Heoeb954192013-08-08 20:11:23 -04005467mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005468{
Tejun Heoeb954192013-08-08 20:11:23 -04005469 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04005470 struct mem_cgroup *parent = mem_cgroup_from_css(css->parent);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005471 int ret;
Glauber Costad142e3e2013-02-22 16:34:52 -08005472
Tejun Heo15a4c832014-05-04 15:09:14 -04005473 if (css->id > MEM_CGROUP_ID_MAX)
Li Zefan4219b2d2013-09-23 16:56:29 +08005474 return -ENOSPC;
5475
Tejun Heo63876982013-08-08 20:11:23 -04005476 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08005477 return 0;
5478
Glauber Costa09998212013-02-22 16:34:55 -08005479 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08005480
5481 memcg->use_hierarchy = parent->use_hierarchy;
5482 memcg->oom_kill_disable = parent->oom_kill_disable;
5483 memcg->swappiness = mem_cgroup_swappiness(parent);
5484
5485 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005486 res_counter_init(&memcg->res, &parent->res);
5487 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005488 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08005489
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005490 /*
Li Zefan8d76a972013-07-08 16:00:36 -07005491 * No need to take a reference to the parent because cgroup
5492 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005493 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08005494 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04005495 res_counter_init(&memcg->res, NULL);
5496 res_counter_init(&memcg->memsw, NULL);
5497 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005498 /*
5499 * Deeper hierachy with use_hierarchy == false doesn't make
5500 * much sense so let cgroup subsystem know about this
5501 * unfortunate state in our controller.
5502 */
Glauber Costad142e3e2013-02-22 16:34:52 -08005503 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05005504 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005505 }
Glauber Costa09998212013-02-22 16:34:55 -08005506 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08005507
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005508 ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
5509 if (ret)
5510 return ret;
5511
5512 /*
5513 * Make sure the memcg is initialized: mem_cgroup_iter()
5514 * orders reading memcg->initialized against its callers
5515 * reading the memcg members.
5516 */
5517 smp_store_release(&memcg->initialized, 1);
5518
5519 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005520}
5521
Michal Hocko5f578162013-04-29 15:07:17 -07005522/*
5523 * Announce all parents that a group from their hierarchy is gone.
5524 */
5525static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
5526{
5527 struct mem_cgroup *parent = memcg;
5528
5529 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07005530 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07005531
5532 /*
5533 * if the root memcg is not hierarchical we have to check it
5534 * explicitely.
5535 */
5536 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07005537 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07005538}
5539
Tejun Heoeb954192013-08-08 20:11:23 -04005540static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005541{
Tejun Heoeb954192013-08-08 20:11:23 -04005542 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005543 struct mem_cgroup_event *event, *tmp;
Filipe Brandenburger4fb1a862014-03-03 15:38:25 -08005544 struct cgroup_subsys_state *iter;
Tejun Heo79bd9812013-11-22 18:20:42 -05005545
5546 /*
5547 * Unregister events and notify userspace.
5548 * Notify userspace about cgroup removing only after rmdir of cgroup
5549 * directory to avoid race between userspace and kernelspace.
5550 */
Tejun Heofba94802013-11-22 18:20:43 -05005551 spin_lock(&memcg->event_list_lock);
5552 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05005553 list_del_init(&event->list);
5554 schedule_work(&event->remove);
5555 }
Tejun Heofba94802013-11-22 18:20:43 -05005556 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005557
Li Zefan10d5ebf2013-07-08 16:00:33 -07005558 kmem_cgroup_css_offline(memcg);
5559
Michal Hocko5f578162013-04-29 15:07:17 -07005560 mem_cgroup_invalidate_reclaim_iterators(memcg);
Filipe Brandenburger4fb1a862014-03-03 15:38:25 -08005561
5562 /*
5563 * This requires that offlining is serialized. Right now that is
5564 * guaranteed because css_killed_work_fn() holds the cgroup_mutex.
5565 */
5566 css_for_each_descendant_post(iter, css)
5567 mem_cgroup_reparent_charges(mem_cgroup_from_css(iter));
5568
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07005569 memcg_unregister_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07005570 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005571}
5572
Tejun Heoeb954192013-08-08 20:11:23 -04005573static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005574{
Tejun Heoeb954192013-08-08 20:11:23 -04005575 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner96f1c582013-12-12 17:12:34 -08005576 /*
5577 * XXX: css_offline() would be where we should reparent all
5578 * memory to prepare the cgroup for destruction. However,
Tejun Heoec903c02014-05-13 12:11:01 -04005579 * memcg does not do css_tryget_online() and res_counter charging
Johannes Weiner96f1c582013-12-12 17:12:34 -08005580 * under the same RCU lock region, which means that charging
5581 * could race with offlining. Offlining only happens to
5582 * cgroups with no tasks in them but charges can show up
5583 * without any tasks from the swapin path when the target
5584 * memcg is looked up from the swapout record and not from the
5585 * current task as it usually is. A race like this can leak
5586 * charges and put pages with stale cgroup pointers into
5587 * circulation:
5588 *
5589 * #0 #1
5590 * lookup_swap_cgroup_id()
5591 * rcu_read_lock()
5592 * mem_cgroup_lookup()
Tejun Heoec903c02014-05-13 12:11:01 -04005593 * css_tryget_online()
Johannes Weiner96f1c582013-12-12 17:12:34 -08005594 * rcu_read_unlock()
Tejun Heoec903c02014-05-13 12:11:01 -04005595 * disable css_tryget_online()
Johannes Weiner96f1c582013-12-12 17:12:34 -08005596 * call_rcu()
5597 * offline_css()
5598 * reparent_charges()
5599 * res_counter_charge()
5600 * css_put()
5601 * css_free()
5602 * pc->mem_cgroup = dead memcg
5603 * add page to lru
5604 *
5605 * The bulk of the charges are still moved in offline_css() to
5606 * avoid pinning a lot of pages in case a long-term reference
5607 * like a swapout record is deferring the css_free() to long
5608 * after offlining. But this makes sure we catch any charges
5609 * made after offlining:
5610 */
5611 mem_cgroup_reparent_charges(memcg);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005612
Li Zefan10d5ebf2013-07-08 16:00:33 -07005613 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07005614 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005615}
5616
Tejun Heo1ced9532014-07-08 18:02:57 -04005617/**
5618 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5619 * @css: the target css
5620 *
5621 * Reset the states of the mem_cgroup associated with @css. This is
5622 * invoked when the userland requests disabling on the default hierarchy
5623 * but the memcg is pinned through dependency. The memcg should stop
5624 * applying policies and should revert to the vanilla state as it may be
5625 * made visible again.
5626 *
5627 * The current implementation only resets the essential configurations.
5628 * This needs to be expanded to cover all the visible parts.
5629 */
5630static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5631{
5632 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5633
5634 mem_cgroup_resize_limit(memcg, ULLONG_MAX);
5635 mem_cgroup_resize_memsw_limit(memcg, ULLONG_MAX);
5636 memcg_update_kmem_limit(memcg, ULLONG_MAX);
5637 res_counter_set_soft_limit(&memcg->res, ULLONG_MAX);
5638}
5639
Daisuke Nishimura02491442010-03-10 15:22:17 -08005640#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005641/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005642static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005643{
Johannes Weiner05b84302014-08-06 16:05:59 -07005644 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07005645
5646 /* Try a single bulk charge without reclaim first */
Johannes Weiner00501b52014-08-08 14:19:20 -07005647 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07005648 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005649 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005650 return ret;
5651 }
Johannes Weiner692e7c42014-08-06 16:05:57 -07005652 if (ret == -EINTR) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005653 cancel_charge(root_mem_cgroup, count);
Johannes Weiner692e7c42014-08-06 16:05:57 -07005654 return ret;
5655 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005656
5657 /* Try charges one by one with reclaim */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005658 while (count--) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005659 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1);
Johannes Weiner9476db92014-08-06 16:05:55 -07005660 /*
5661 * In case of failure, any residual charges against
5662 * mc.to will be dropped by mem_cgroup_clear_mc()
Johannes Weiner692e7c42014-08-06 16:05:57 -07005663 * later on. However, cancel any charges that are
5664 * bypassed to root right away or they'll be lost.
Johannes Weiner9476db92014-08-06 16:05:55 -07005665 */
Johannes Weiner692e7c42014-08-06 16:05:57 -07005666 if (ret == -EINTR)
Johannes Weiner00501b52014-08-08 14:19:20 -07005667 cancel_charge(root_mem_cgroup, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005668 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005669 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005670 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07005671 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005672 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005673 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005674}
5675
5676/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005677 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005678 * @vma: the vma the pte to be checked belongs
5679 * @addr: the address corresponding to the pte to be checked
5680 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005681 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005682 *
5683 * Returns
5684 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5685 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5686 * move charge. if @target is not NULL, the page is stored in target->page
5687 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005688 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5689 * target for charge migration. if @target is not NULL, the entry is stored
5690 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005691 *
5692 * Called with pte lock held.
5693 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005694union mc_target {
5695 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005696 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005697};
5698
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005699enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005700 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005701 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005702 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005703};
5704
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005705static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5706 unsigned long addr, pte_t ptent)
5707{
5708 struct page *page = vm_normal_page(vma, addr, ptent);
5709
5710 if (!page || !page_mapped(page))
5711 return NULL;
5712 if (PageAnon(page)) {
5713 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005714 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005715 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005716 } else if (!move_file())
5717 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005718 return NULL;
5719 if (!get_page_unless_zero(page))
5720 return NULL;
5721
5722 return page;
5723}
5724
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005725#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005726static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5727 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5728{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005729 struct page *page = NULL;
5730 swp_entry_t ent = pte_to_swp_entry(ptent);
5731
5732 if (!move_anon() || non_swap_entry(ent))
5733 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005734 /*
5735 * Because lookup_swap_cache() updates some statistics counter,
5736 * we call find_get_page() with swapper_space directly.
5737 */
Shaohua Li33806f02013-02-22 16:34:37 -08005738 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005739 if (do_swap_account)
5740 entry->val = ent.val;
5741
5742 return page;
5743}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005744#else
5745static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5746 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5747{
5748 return NULL;
5749}
5750#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005751
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005752static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5753 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5754{
5755 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005756 struct address_space *mapping;
5757 pgoff_t pgoff;
5758
5759 if (!vma->vm_file) /* anonymous vma */
5760 return NULL;
5761 if (!move_file())
5762 return NULL;
5763
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005764 mapping = vma->vm_file->f_mapping;
5765 if (pte_none(ptent))
5766 pgoff = linear_page_index(vma, addr);
5767 else /* pte_file(ptent) is true */
5768 pgoff = pte_to_pgoff(ptent);
5769
5770 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005771#ifdef CONFIG_SWAP
5772 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07005773 if (shmem_mapping(mapping)) {
5774 page = find_get_entry(mapping, pgoff);
5775 if (radix_tree_exceptional_entry(page)) {
5776 swp_entry_t swp = radix_to_swp_entry(page);
5777 if (do_swap_account)
5778 *entry = swp;
5779 page = find_get_page(swap_address_space(swp), swp.val);
5780 }
5781 } else
5782 page = find_get_page(mapping, pgoff);
5783#else
5784 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005785#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005786 return page;
5787}
5788
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005789static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005790 unsigned long addr, pte_t ptent, union mc_target *target)
5791{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005792 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005793 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005794 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005795 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005796
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005797 if (pte_present(ptent))
5798 page = mc_handle_present_pte(vma, addr, ptent);
5799 else if (is_swap_pte(ptent))
5800 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005801 else if (pte_none(ptent) || pte_file(ptent))
5802 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005803
5804 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005805 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005806 if (page) {
5807 pc = lookup_page_cgroup(page);
5808 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005809 * Do only loose check w/o serialization.
5810 * mem_cgroup_move_account() checks the pc is valid or
5811 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08005812 */
5813 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5814 ret = MC_TARGET_PAGE;
5815 if (target)
5816 target->page = page;
5817 }
5818 if (!ret || !target)
5819 put_page(page);
5820 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005821 /* There is a swap entry and a page doesn't exist or isn't charged */
5822 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08005823 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005824 ret = MC_TARGET_SWAP;
5825 if (target)
5826 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005827 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005828 return ret;
5829}
5830
Naoya Horiguchi12724852012-03-21 16:34:28 -07005831#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5832/*
5833 * We don't consider swapping or file mapped pages because THP does not
5834 * support them for now.
5835 * Caller should make sure that pmd_trans_huge(pmd) is true.
5836 */
5837static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5838 unsigned long addr, pmd_t pmd, union mc_target *target)
5839{
5840 struct page *page = NULL;
5841 struct page_cgroup *pc;
5842 enum mc_target_type ret = MC_TARGET_NONE;
5843
5844 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08005845 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005846 if (!move_anon())
5847 return ret;
5848 pc = lookup_page_cgroup(page);
5849 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5850 ret = MC_TARGET_PAGE;
5851 if (target) {
5852 get_page(page);
5853 target->page = page;
5854 }
5855 }
5856 return ret;
5857}
5858#else
5859static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5860 unsigned long addr, pmd_t pmd, union mc_target *target)
5861{
5862 return MC_TARGET_NONE;
5863}
5864#endif
5865
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005866static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5867 unsigned long addr, unsigned long end,
5868 struct mm_walk *walk)
5869{
5870 struct vm_area_struct *vma = walk->private;
5871 pte_t *pte;
5872 spinlock_t *ptl;
5873
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005874 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005875 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5876 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005877 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005878 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005879 }
Dave Hansen03319322011-03-22 16:32:56 -07005880
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005881 if (pmd_trans_unstable(pmd))
5882 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005883 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5884 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005885 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005886 mc.precharge++; /* increment precharge temporarily */
5887 pte_unmap_unlock(pte - 1, ptl);
5888 cond_resched();
5889
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005890 return 0;
5891}
5892
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005893static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5894{
5895 unsigned long precharge;
5896 struct vm_area_struct *vma;
5897
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005898 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005899 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5900 struct mm_walk mem_cgroup_count_precharge_walk = {
5901 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5902 .mm = mm,
5903 .private = vma,
5904 };
5905 if (is_vm_hugetlb_page(vma))
5906 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005907 walk_page_range(vma->vm_start, vma->vm_end,
5908 &mem_cgroup_count_precharge_walk);
5909 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005910 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005911
5912 precharge = mc.precharge;
5913 mc.precharge = 0;
5914
5915 return precharge;
5916}
5917
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005918static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5919{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005920 unsigned long precharge = mem_cgroup_count_precharge(mm);
5921
5922 VM_BUG_ON(mc.moving_task);
5923 mc.moving_task = current;
5924 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005925}
5926
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005927/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5928static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005929{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005930 struct mem_cgroup *from = mc.from;
5931 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07005932 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005933
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005934 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005935 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005936 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005937 mc.precharge = 0;
5938 }
5939 /*
5940 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5941 * we must uncharge here.
5942 */
5943 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005944 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005945 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005946 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005947 /* we must fixup refcnts and charges */
5948 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005949 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005950 if (!mem_cgroup_is_root(mc.from))
5951 res_counter_uncharge(&mc.from->memsw,
5952 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07005953
5954 for (i = 0; i < mc.moved_swap; i++)
5955 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005956
Johannes Weiner05b84302014-08-06 16:05:59 -07005957 /*
5958 * we charged both to->res and to->memsw, so we should
5959 * uncharge to->res.
5960 */
Johannes Weinerce00a962014-09-05 08:43:57 -04005961 if (!mem_cgroup_is_root(mc.to))
5962 res_counter_uncharge(&mc.to->res,
5963 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07005964 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005965 mc.moved_swap = 0;
5966 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005967 memcg_oom_recover(from);
5968 memcg_oom_recover(to);
5969 wake_up_all(&mc.waitq);
5970}
5971
5972static void mem_cgroup_clear_mc(void)
5973{
5974 struct mem_cgroup *from = mc.from;
5975
5976 /*
5977 * we must clear moving_task before waking up waiters at the end of
5978 * task migration.
5979 */
5980 mc.moving_task = NULL;
5981 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005982 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005983 mc.from = NULL;
5984 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005985 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005986 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005987}
5988
Tejun Heoeb954192013-08-08 20:11:23 -04005989static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005990 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005991{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005992 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005993 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04005994 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08005995 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005996
Glauber Costaee5e8472013-02-22 16:34:50 -08005997 /*
5998 * We are now commited to this value whatever it is. Changes in this
5999 * tunable will only affect upcoming migrations, not the current one.
6000 * So we need to save it, and keep it going.
6001 */
6002 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6003 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006004 struct mm_struct *mm;
6005 struct mem_cgroup *from = mem_cgroup_from_task(p);
6006
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006007 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006008
6009 mm = get_task_mm(p);
6010 if (!mm)
6011 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006012 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006013 if (mm->owner == p) {
6014 VM_BUG_ON(mc.from);
6015 VM_BUG_ON(mc.to);
6016 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006017 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006018 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006019 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006020 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006021 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006022 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006023 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006024 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006025 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006026
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006027 ret = mem_cgroup_precharge_mc(mm);
6028 if (ret)
6029 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006030 }
6031 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006032 }
6033 return ret;
6034}
6035
Tejun Heoeb954192013-08-08 20:11:23 -04006036static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006037 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006038{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006039 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006040}
6041
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006042static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6043 unsigned long addr, unsigned long end,
6044 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006045{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006046 int ret = 0;
6047 struct vm_area_struct *vma = walk->private;
6048 pte_t *pte;
6049 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006050 enum mc_target_type target_type;
6051 union mc_target target;
6052 struct page *page;
6053 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006054
Naoya Horiguchi12724852012-03-21 16:34:28 -07006055 /*
6056 * We don't take compound_lock() here but no race with splitting thp
6057 * happens because:
6058 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6059 * under splitting, which means there's no concurrent thp split,
6060 * - if another thread runs into split_huge_page() just after we
6061 * entered this if-block, the thread must wait for page table lock
6062 * to be unlocked in __split_huge_page_splitting(), where the main
6063 * part of thp split is not executed yet.
6064 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08006065 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006066 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08006067 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07006068 return 0;
6069 }
6070 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6071 if (target_type == MC_TARGET_PAGE) {
6072 page = target.page;
6073 if (!isolate_lru_page(page)) {
6074 pc = lookup_page_cgroup(page);
6075 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006076 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006077 mc.precharge -= HPAGE_PMD_NR;
6078 mc.moved_charge += HPAGE_PMD_NR;
6079 }
6080 putback_lru_page(page);
6081 }
6082 put_page(page);
6083 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08006084 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006085 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006086 }
6087
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006088 if (pmd_trans_unstable(pmd))
6089 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006090retry:
6091 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6092 for (; addr != end; addr += PAGE_SIZE) {
6093 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006094 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006095
6096 if (!mc.precharge)
6097 break;
6098
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006099 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006100 case MC_TARGET_PAGE:
6101 page = target.page;
6102 if (isolate_lru_page(page))
6103 goto put;
6104 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006105 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006106 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006107 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006108 /* we uncharge from mc.from later. */
6109 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006110 }
6111 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006112put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006113 put_page(page);
6114 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006115 case MC_TARGET_SWAP:
6116 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006117 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006118 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006119 /* we fixup refcnts and charges later. */
6120 mc.moved_swap++;
6121 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006122 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006123 default:
6124 break;
6125 }
6126 }
6127 pte_unmap_unlock(pte - 1, ptl);
6128 cond_resched();
6129
6130 if (addr != end) {
6131 /*
6132 * We have consumed all precharges we got in can_attach().
6133 * We try charge one by one, but don't do any additional
6134 * charges to mc.to if we have failed in charge once in attach()
6135 * phase.
6136 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006137 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006138 if (!ret)
6139 goto retry;
6140 }
6141
6142 return ret;
6143}
6144
6145static void mem_cgroup_move_charge(struct mm_struct *mm)
6146{
6147 struct vm_area_struct *vma;
6148
6149 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006150retry:
6151 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6152 /*
6153 * Someone who are holding the mmap_sem might be waiting in
6154 * waitq. So we cancel all extra charges, wake up all waiters,
6155 * and retry. Because we cancel precharges, we might not be able
6156 * to move enough charges, but moving charge is a best-effort
6157 * feature anyway, so it wouldn't be a big problem.
6158 */
6159 __mem_cgroup_clear_mc();
6160 cond_resched();
6161 goto retry;
6162 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006163 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6164 int ret;
6165 struct mm_walk mem_cgroup_move_charge_walk = {
6166 .pmd_entry = mem_cgroup_move_charge_pte_range,
6167 .mm = mm,
6168 .private = vma,
6169 };
6170 if (is_vm_hugetlb_page(vma))
6171 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006172 ret = walk_page_range(vma->vm_start, vma->vm_end,
6173 &mem_cgroup_move_charge_walk);
6174 if (ret)
6175 /*
6176 * means we have consumed all precharges and failed in
6177 * doing additional charge. Just abandon here.
6178 */
6179 break;
6180 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006181 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006182}
6183
Tejun Heoeb954192013-08-08 20:11:23 -04006184static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006185 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006186{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006187 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006188 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006189
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006190 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006191 if (mc.to)
6192 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006193 mmput(mm);
6194 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006195 if (mc.to)
6196 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006197}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006198#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006199static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006200 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006201{
6202 return 0;
6203}
Tejun Heoeb954192013-08-08 20:11:23 -04006204static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006205 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006206{
6207}
Tejun Heoeb954192013-08-08 20:11:23 -04006208static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006209 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006210{
6211}
6212#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006213
Tejun Heof00baae2013-04-15 13:41:15 -07006214/*
6215 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04006216 * to verify whether we're attached to the default hierarchy on each mount
6217 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07006218 */
Tejun Heoeb954192013-08-08 20:11:23 -04006219static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006220{
6221 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04006222 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07006223 * guarantees that @root doesn't have any children, so turning it
6224 * on for the root memcg is enough.
6225 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04006226 if (cgroup_on_dfl(root_css->cgroup))
Tejun Heoeb954192013-08-08 20:11:23 -04006227 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006228}
6229
Tejun Heo073219e2014-02-08 10:36:58 -05006230struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08006231 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006232 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006233 .css_offline = mem_cgroup_css_offline,
6234 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04006235 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006236 .can_attach = mem_cgroup_can_attach,
6237 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006238 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006239 .bind = mem_cgroup_bind,
Tejun Heo55779642014-07-15 11:05:09 -04006240 .legacy_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006241 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006242};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006243
Andrew Mortonc255a452012-07-31 16:43:02 -07006244#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006245static int __init enable_swap_account(char *s)
6246{
Michal Hockoa2c89902011-05-24 17:12:50 -07006247 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006248 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006249 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006250 really_do_swap_account = 0;
6251 return 1;
6252}
Michal Hockoa2c89902011-05-24 17:12:50 -07006253__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006254
Michal Hocko2d110852013-02-22 16:34:43 -08006255static void __init memsw_file_init(void)
6256{
Tejun Heo2cf669a2014-07-15 11:05:09 -04006257 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
6258 memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006259}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006260
6261static void __init enable_swap_cgroup(void)
6262{
6263 if (!mem_cgroup_disabled() && really_do_swap_account) {
6264 do_swap_account = 1;
6265 memsw_file_init();
6266 }
6267}
6268
Michal Hocko2d110852013-02-22 16:34:43 -08006269#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006270static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006271{
6272}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006273#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006274
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006275#ifdef CONFIG_MEMCG_SWAP
6276/**
6277 * mem_cgroup_swapout - transfer a memsw charge to swap
6278 * @page: page whose memsw charge to transfer
6279 * @entry: swap entry to move the charge to
6280 *
6281 * Transfer the memsw charge of @page to @entry.
6282 */
6283void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
6284{
6285 struct page_cgroup *pc;
6286 unsigned short oldid;
6287
6288 VM_BUG_ON_PAGE(PageLRU(page), page);
6289 VM_BUG_ON_PAGE(page_count(page), page);
6290
6291 if (!do_swap_account)
6292 return;
6293
6294 pc = lookup_page_cgroup(page);
6295
6296 /* Readahead page, never charged */
6297 if (!PageCgroupUsed(pc))
6298 return;
6299
6300 VM_BUG_ON_PAGE(!(pc->flags & PCG_MEMSW), page);
6301
6302 oldid = swap_cgroup_record(entry, mem_cgroup_id(pc->mem_cgroup));
6303 VM_BUG_ON_PAGE(oldid, page);
6304
6305 pc->flags &= ~PCG_MEMSW;
6306 css_get(&pc->mem_cgroup->css);
6307 mem_cgroup_swap_statistics(pc->mem_cgroup, true);
6308}
6309
6310/**
6311 * mem_cgroup_uncharge_swap - uncharge a swap entry
6312 * @entry: swap entry to uncharge
6313 *
6314 * Drop the memsw charge associated with @entry.
6315 */
6316void mem_cgroup_uncharge_swap(swp_entry_t entry)
6317{
6318 struct mem_cgroup *memcg;
6319 unsigned short id;
6320
6321 if (!do_swap_account)
6322 return;
6323
6324 id = swap_cgroup_record(entry, 0);
6325 rcu_read_lock();
6326 memcg = mem_cgroup_lookup(id);
6327 if (memcg) {
Johannes Weinerce00a962014-09-05 08:43:57 -04006328 if (!mem_cgroup_is_root(memcg))
6329 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006330 mem_cgroup_swap_statistics(memcg, false);
6331 css_put(&memcg->css);
6332 }
6333 rcu_read_unlock();
6334}
6335#endif
6336
Johannes Weiner00501b52014-08-08 14:19:20 -07006337/**
6338 * mem_cgroup_try_charge - try charging a page
6339 * @page: page to charge
6340 * @mm: mm context of the victim
6341 * @gfp_mask: reclaim mode
6342 * @memcgp: charged memcg return
6343 *
6344 * Try to charge @page to the memcg that @mm belongs to, reclaiming
6345 * pages according to @gfp_mask if necessary.
6346 *
6347 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
6348 * Otherwise, an error code is returned.
6349 *
6350 * After page->mapping has been set up, the caller must finalize the
6351 * charge with mem_cgroup_commit_charge(). Or abort the transaction
6352 * with mem_cgroup_cancel_charge() in case page instantiation fails.
6353 */
6354int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
6355 gfp_t gfp_mask, struct mem_cgroup **memcgp)
6356{
6357 struct mem_cgroup *memcg = NULL;
6358 unsigned int nr_pages = 1;
6359 int ret = 0;
6360
6361 if (mem_cgroup_disabled())
6362 goto out;
6363
6364 if (PageSwapCache(page)) {
6365 struct page_cgroup *pc = lookup_page_cgroup(page);
6366 /*
6367 * Every swap fault against a single page tries to charge the
6368 * page, bail as early as possible. shmem_unuse() encounters
6369 * already charged pages, too. The USED bit is protected by
6370 * the page lock, which serializes swap cache removal, which
6371 * in turn serializes uncharging.
6372 */
6373 if (PageCgroupUsed(pc))
6374 goto out;
6375 }
6376
6377 if (PageTransHuge(page)) {
6378 nr_pages <<= compound_order(page);
6379 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6380 }
6381
6382 if (do_swap_account && PageSwapCache(page))
6383 memcg = try_get_mem_cgroup_from_page(page);
6384 if (!memcg)
6385 memcg = get_mem_cgroup_from_mm(mm);
6386
6387 ret = try_charge(memcg, gfp_mask, nr_pages);
6388
6389 css_put(&memcg->css);
6390
6391 if (ret == -EINTR) {
6392 memcg = root_mem_cgroup;
6393 ret = 0;
6394 }
6395out:
6396 *memcgp = memcg;
6397 return ret;
6398}
6399
6400/**
6401 * mem_cgroup_commit_charge - commit a page charge
6402 * @page: page to charge
6403 * @memcg: memcg to charge the page to
6404 * @lrucare: page might be on LRU already
6405 *
6406 * Finalize a charge transaction started by mem_cgroup_try_charge(),
6407 * after page->mapping has been set up. This must happen atomically
6408 * as part of the page instantiation, i.e. under the page table lock
6409 * for anonymous pages, under the page lock for page and swap cache.
6410 *
6411 * In addition, the page must not be on the LRU during the commit, to
6412 * prevent racing with task migration. If it might be, use @lrucare.
6413 *
6414 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
6415 */
6416void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
6417 bool lrucare)
6418{
6419 unsigned int nr_pages = 1;
6420
6421 VM_BUG_ON_PAGE(!page->mapping, page);
6422 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
6423
6424 if (mem_cgroup_disabled())
6425 return;
6426 /*
6427 * Swap faults will attempt to charge the same page multiple
6428 * times. But reuse_swap_page() might have removed the page
6429 * from swapcache already, so we can't check PageSwapCache().
6430 */
6431 if (!memcg)
6432 return;
6433
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006434 commit_charge(page, memcg, lrucare);
6435
Johannes Weiner00501b52014-08-08 14:19:20 -07006436 if (PageTransHuge(page)) {
6437 nr_pages <<= compound_order(page);
6438 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6439 }
6440
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006441 local_irq_disable();
6442 mem_cgroup_charge_statistics(memcg, page, nr_pages);
6443 memcg_check_events(memcg, page);
6444 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07006445
6446 if (do_swap_account && PageSwapCache(page)) {
6447 swp_entry_t entry = { .val = page_private(page) };
6448 /*
6449 * The swap entry might not get freed for a long time,
6450 * let's not wait for it. The page already received a
6451 * memory+swap charge, drop the swap entry duplicate.
6452 */
6453 mem_cgroup_uncharge_swap(entry);
6454 }
6455}
6456
6457/**
6458 * mem_cgroup_cancel_charge - cancel a page charge
6459 * @page: page to charge
6460 * @memcg: memcg to charge the page to
6461 *
6462 * Cancel a charge transaction started by mem_cgroup_try_charge().
6463 */
6464void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg)
6465{
6466 unsigned int nr_pages = 1;
6467
6468 if (mem_cgroup_disabled())
6469 return;
6470 /*
6471 * Swap faults will attempt to charge the same page multiple
6472 * times. But reuse_swap_page() might have removed the page
6473 * from swapcache already, so we can't check PageSwapCache().
6474 */
6475 if (!memcg)
6476 return;
6477
6478 if (PageTransHuge(page)) {
6479 nr_pages <<= compound_order(page);
6480 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6481 }
6482
6483 cancel_charge(memcg, nr_pages);
6484}
6485
Johannes Weiner747db952014-08-08 14:19:24 -07006486static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
6487 unsigned long nr_mem, unsigned long nr_memsw,
6488 unsigned long nr_anon, unsigned long nr_file,
6489 unsigned long nr_huge, struct page *dummy_page)
6490{
6491 unsigned long flags;
6492
Johannes Weinerce00a962014-09-05 08:43:57 -04006493 if (!mem_cgroup_is_root(memcg)) {
6494 if (nr_mem)
6495 res_counter_uncharge(&memcg->res,
6496 nr_mem * PAGE_SIZE);
6497 if (nr_memsw)
6498 res_counter_uncharge(&memcg->memsw,
6499 nr_memsw * PAGE_SIZE);
6500 memcg_oom_recover(memcg);
6501 }
Johannes Weiner747db952014-08-08 14:19:24 -07006502
6503 local_irq_save(flags);
6504 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
6505 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
6506 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
6507 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
6508 __this_cpu_add(memcg->stat->nr_page_events, nr_anon + nr_file);
6509 memcg_check_events(memcg, dummy_page);
6510 local_irq_restore(flags);
6511}
6512
6513static void uncharge_list(struct list_head *page_list)
6514{
6515 struct mem_cgroup *memcg = NULL;
6516 unsigned long nr_memsw = 0;
6517 unsigned long nr_anon = 0;
6518 unsigned long nr_file = 0;
6519 unsigned long nr_huge = 0;
6520 unsigned long pgpgout = 0;
6521 unsigned long nr_mem = 0;
6522 struct list_head *next;
6523 struct page *page;
6524
6525 next = page_list->next;
6526 do {
6527 unsigned int nr_pages = 1;
6528 struct page_cgroup *pc;
6529
6530 page = list_entry(next, struct page, lru);
6531 next = page->lru.next;
6532
6533 VM_BUG_ON_PAGE(PageLRU(page), page);
6534 VM_BUG_ON_PAGE(page_count(page), page);
6535
6536 pc = lookup_page_cgroup(page);
6537 if (!PageCgroupUsed(pc))
6538 continue;
6539
6540 /*
6541 * Nobody should be changing or seriously looking at
6542 * pc->mem_cgroup and pc->flags at this point, we have
6543 * fully exclusive access to the page.
6544 */
6545
6546 if (memcg != pc->mem_cgroup) {
6547 if (memcg) {
6548 uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw,
6549 nr_anon, nr_file, nr_huge, page);
6550 pgpgout = nr_mem = nr_memsw = 0;
6551 nr_anon = nr_file = nr_huge = 0;
6552 }
6553 memcg = pc->mem_cgroup;
6554 }
6555
6556 if (PageTransHuge(page)) {
6557 nr_pages <<= compound_order(page);
6558 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6559 nr_huge += nr_pages;
6560 }
6561
6562 if (PageAnon(page))
6563 nr_anon += nr_pages;
6564 else
6565 nr_file += nr_pages;
6566
6567 if (pc->flags & PCG_MEM)
6568 nr_mem += nr_pages;
6569 if (pc->flags & PCG_MEMSW)
6570 nr_memsw += nr_pages;
6571 pc->flags = 0;
6572
6573 pgpgout++;
6574 } while (next != page_list);
6575
6576 if (memcg)
6577 uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw,
6578 nr_anon, nr_file, nr_huge, page);
6579}
6580
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006581/**
6582 * mem_cgroup_uncharge - uncharge a page
6583 * @page: page to uncharge
6584 *
6585 * Uncharge a page previously charged with mem_cgroup_try_charge() and
6586 * mem_cgroup_commit_charge().
6587 */
6588void mem_cgroup_uncharge(struct page *page)
6589{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006590 struct page_cgroup *pc;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006591
6592 if (mem_cgroup_disabled())
6593 return;
6594
Johannes Weiner747db952014-08-08 14:19:24 -07006595 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006596 pc = lookup_page_cgroup(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006597 if (!PageCgroupUsed(pc))
6598 return;
6599
Johannes Weiner747db952014-08-08 14:19:24 -07006600 INIT_LIST_HEAD(&page->lru);
6601 uncharge_list(&page->lru);
6602}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006603
Johannes Weiner747db952014-08-08 14:19:24 -07006604/**
6605 * mem_cgroup_uncharge_list - uncharge a list of page
6606 * @page_list: list of pages to uncharge
6607 *
6608 * Uncharge a list of pages previously charged with
6609 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
6610 */
6611void mem_cgroup_uncharge_list(struct list_head *page_list)
6612{
6613 if (mem_cgroup_disabled())
6614 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006615
Johannes Weiner747db952014-08-08 14:19:24 -07006616 if (!list_empty(page_list))
6617 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006618}
6619
6620/**
6621 * mem_cgroup_migrate - migrate a charge to another page
6622 * @oldpage: currently charged page
6623 * @newpage: page to transfer the charge to
6624 * @lrucare: both pages might be on the LRU already
6625 *
6626 * Migrate the charge from @oldpage to @newpage.
6627 *
6628 * Both pages must be locked, @newpage->mapping must be set up.
6629 */
6630void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
6631 bool lrucare)
6632{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006633 struct page_cgroup *pc;
6634 int isolated;
6635
6636 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
6637 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
6638 VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage);
6639 VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage);
6640 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006641 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
6642 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006643
6644 if (mem_cgroup_disabled())
6645 return;
6646
6647 /* Page cache replacement: new page already charged? */
6648 pc = lookup_page_cgroup(newpage);
6649 if (PageCgroupUsed(pc))
6650 return;
6651
6652 /* Re-entrant migration: old page already uncharged? */
6653 pc = lookup_page_cgroup(oldpage);
6654 if (!PageCgroupUsed(pc))
6655 return;
6656
6657 VM_BUG_ON_PAGE(!(pc->flags & PCG_MEM), oldpage);
6658 VM_BUG_ON_PAGE(do_swap_account && !(pc->flags & PCG_MEMSW), oldpage);
6659
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006660 if (lrucare)
6661 lock_page_lru(oldpage, &isolated);
6662
6663 pc->flags = 0;
6664
6665 if (lrucare)
6666 unlock_page_lru(oldpage, isolated);
6667
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006668 commit_charge(newpage, pc->mem_cgroup, lrucare);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006669}
6670
Michal Hocko2d110852013-02-22 16:34:43 -08006671/*
Michal Hocko10813122013-02-22 16:35:41 -08006672 * subsys_initcall() for memory controller.
6673 *
6674 * Some parts like hotcpu_notifier() have to be initialized from this context
6675 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6676 * everything that doesn't depend on a specific mem_cgroup structure should
6677 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006678 */
6679static int __init mem_cgroup_init(void)
6680{
6681 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006682 enable_swap_cgroup();
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006683 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08006684 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006685 return 0;
6686}
6687subsys_initcall(mem_cgroup_init);