blob: d18d3a6e7337d944a36e1375b1f50fdce483961f [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
Johannes Weiner3e32cb22014-12-10 15:42:31 -080028#include <linux/page_counter.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080029#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>
Johannes Weiner5d1ea482014-12-10 15:44:55 -080054#include <linux/swap_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
Johannes Weiner21afa382015-02-11 15:26:36 -080075/* Whether the swap controller is active */
Andrew Mortonc255a452012-07-31 16:43:02 -070076#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080077int do_swap_account __read_mostly;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080078#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070079#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080080#endif
81
Johannes Weineraf7c4b02012-05-29 15:07:08 -070082static const char * const mem_cgroup_stat_names[] = {
83 "cache",
84 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070085 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070086 "mapped_file",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070087 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070088 "swap",
89};
90
Johannes Weineraf7c4b02012-05-29 15:07:08 -070091static const char * const mem_cgroup_events_names[] = {
92 "pgpgin",
93 "pgpgout",
94 "pgfault",
95 "pgmajfault",
96};
97
Sha Zhengju58cf1882013-02-22 16:32:05 -080098static const char * const mem_cgroup_lru_names[] = {
99 "inactive_anon",
100 "active_anon",
101 "inactive_file",
102 "active_file",
103 "unevictable",
104};
105
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700106/*
107 * Per memcg event counter is incremented at every pagein/pageout. With THP,
108 * it will be incremated by the number of pages. This counter is used for
109 * for trigger some periodic events. This is straightforward and better
110 * than using jiffies etc. to handle periodic memcg event.
111 */
112enum mem_cgroup_events_target {
113 MEM_CGROUP_TARGET_THRESH,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700114 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700115 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700116 MEM_CGROUP_NTARGETS,
117};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700118#define THRESHOLDS_EVENTS_TARGET 128
119#define SOFTLIMIT_EVENTS_TARGET 1024
120#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700121
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800122struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weiner241994e2015-02-11 15:26:06 -0800124 unsigned long events[MEMCG_NR_EVENTS];
Johannes Weiner13114712012-05-29 15:07:07 -0700125 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700126 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800127};
128
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800129struct reclaim_iter {
130 struct mem_cgroup *position;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800131 /* scan generation, increased every round-trip */
132 unsigned int generation;
133};
134
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800135/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800136 * per-zone information in memory controller.
137 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800138struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800139 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700140 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800141
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800142 struct reclaim_iter iter[DEF_PRIORITY + 1];
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800143
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700144 struct rb_node tree_node; /* RB tree node */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800145 unsigned long usage_in_excess;/* Set to the value by which */
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700146 /* the soft limit is exceeded*/
147 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700148 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700149 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800150};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800151
152struct mem_cgroup_per_node {
153 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
154};
155
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700156/*
157 * Cgroups above their limits are maintained in a RB-Tree, independent of
158 * their hierarchy representation
159 */
160
161struct mem_cgroup_tree_per_zone {
162 struct rb_root rb_root;
163 spinlock_t lock;
164};
165
166struct mem_cgroup_tree_per_node {
167 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
168};
169
170struct mem_cgroup_tree {
171 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
172};
173
174static struct mem_cgroup_tree soft_limit_tree __read_mostly;
175
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800176struct mem_cgroup_threshold {
177 struct eventfd_ctx *eventfd;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800178 unsigned long threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800179};
180
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700181/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800182struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700183 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700184 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800185 /* Size of entries[] */
186 unsigned int size;
187 /* Array of thresholds */
188 struct mem_cgroup_threshold entries[0];
189};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700190
191struct mem_cgroup_thresholds {
192 /* Primary thresholds array */
193 struct mem_cgroup_threshold_ary *primary;
194 /*
195 * Spare threshold array.
196 * This is needed to make mem_cgroup_unregister_event() "never fail".
197 * It must be able to store at least primary->size - 1 entries.
198 */
199 struct mem_cgroup_threshold_ary *spare;
200};
201
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700202/* for OOM */
203struct mem_cgroup_eventfd_list {
204 struct list_head list;
205 struct eventfd_ctx *eventfd;
206};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800207
Tejun Heo79bd9812013-11-22 18:20:42 -0500208/*
209 * cgroup_event represents events which userspace want to receive.
210 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500211struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500212 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500213 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500214 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500215 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500216 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500217 * eventfd to signal userspace about the event.
218 */
219 struct eventfd_ctx *eventfd;
220 /*
221 * Each of these stored in a list by the cgroup.
222 */
223 struct list_head list;
224 /*
Tejun Heofba94802013-11-22 18:20:43 -0500225 * register_event() callback will be used to add new userspace
226 * waiter for changes related to this event. Use eventfd_signal()
227 * on eventfd to send notification to userspace.
228 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500229 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500230 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500231 /*
232 * unregister_event() callback will be called when userspace closes
233 * the eventfd or on cgroup removing. This callback must be set,
234 * if you want provide notification functionality.
235 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500236 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500237 struct eventfd_ctx *eventfd);
238 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500239 * All fields below needed to unregister event when
240 * userspace closes eventfd.
241 */
242 poll_table pt;
243 wait_queue_head_t *wqh;
244 wait_queue_t wait;
245 struct work_struct remove;
246};
247
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700248static void mem_cgroup_threshold(struct mem_cgroup *memcg);
249static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800250
Balbir Singhf64c3f52009-09-23 15:56:37 -0700251/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800252 * The memory controller data structure. The memory controller controls both
253 * page cache and RSS per cgroup. We would eventually like to provide
254 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
255 * to help the administrator determine what knobs to tune.
256 *
257 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800258 * we hit the water mark. May be even add a low water mark, such that
259 * no reclaim occurs from a cgroup at it's low water mark, this is
260 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800261 */
262struct mem_cgroup {
263 struct cgroup_subsys_state css;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800264
265 /* Accounted resources */
266 struct page_counter memory;
267 struct page_counter memsw;
268 struct page_counter kmem;
269
Johannes Weiner241994e2015-02-11 15:26:06 -0800270 /* Normal memory consumption range */
271 unsigned long low;
272 unsigned long high;
273
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800274 unsigned long soft_limit;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700275
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700276 /* vmpressure notifications */
277 struct vmpressure vmpressure;
278
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -0700279 /* css_online() has been completed */
280 int initialized;
281
Li Zefan465939a2013-07-08 16:00:38 -0700282 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800283 * Should the accounting and control be hierarchical, per subtree?
284 */
285 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700286
287 bool oom_lock;
288 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700289 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700290
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700291 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700292 /* OOM-Killer disable */
293 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800294
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800295 /* protect arrays of thresholds */
296 struct mutex thresholds_lock;
297
298 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700299 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700300
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800301 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700302 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700303
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700304 /* For oom notifier event fd */
305 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700306
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800307 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800308 * Should we move charges of a task when a task is moved into this
309 * mem_cgroup ? And what type of charges should we move ?
310 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700311 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800312 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700313 * set > 0 if pages under this cgroup are moving to other cgroup.
314 */
Johannes Weiner6de22612015-02-11 15:25:01 -0800315 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700316 /* taken only while moving_account > 0 */
Johannes Weiner6de22612015-02-11 15:25:01 -0800317 spinlock_t move_lock;
318 struct task_struct *move_lock_task;
319 unsigned long move_lock_flags;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700320 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800321 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800322 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700323 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700324 /*
325 * used when a cpu is offlined or other synchronizations
326 * See mem_cgroup_read_stat().
327 */
328 struct mem_cgroup_stat_cpu nocpu_base;
329 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000330
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700331#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700332 struct cg_proto tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000333#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800334#if defined(CONFIG_MEMCG_KMEM)
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800335 /* Index in the kmem_cache->memcg_params.memcg_caches array */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800336 int kmemcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800337 bool kmem_acct_activated;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -0800338 bool kmem_acct_active;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800339#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800340
341 int last_scanned_node;
342#if MAX_NUMNODES > 1
343 nodemask_t scan_nodes;
344 atomic_t numainfo_events;
345 atomic_t numainfo_updating;
346#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700347
Tejun Heofba94802013-11-22 18:20:43 -0500348 /* List of events which userspace want to receive */
349 struct list_head event_list;
350 spinlock_t event_list_lock;
351
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700352 struct mem_cgroup_per_node *nodeinfo[0];
353 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800354};
355
Glauber Costa510fc4e2012-12-18 14:21:47 -0800356#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800357bool memcg_kmem_is_active(struct mem_cgroup *memcg)
Glauber Costa7de37682012-12-18 14:22:07 -0800358{
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -0800359 return memcg->kmem_acct_active;
Glauber Costa7de37682012-12-18 14:22:07 -0800360}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800361#endif
362
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800363/* Stuffs for move charges at task migration. */
364/*
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800365 * Types of charges to be moved.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800366 */
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800367#define MOVE_ANON 0x1U
368#define MOVE_FILE 0x2U
369#define MOVE_MASK (MOVE_ANON | MOVE_FILE)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800370
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800371/* "mc" and its members are protected by cgroup_mutex */
372static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800373 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800374 struct mem_cgroup *from;
375 struct mem_cgroup *to;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800376 unsigned long flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800377 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800378 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800379 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800380 struct task_struct *moving_task; /* a task moving charges */
381 wait_queue_head_t waitq; /* a waitq for other context */
382} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700383 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800384 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
385};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800386
Balbir Singh4e416952009-09-23 15:56:39 -0700387/*
388 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
389 * limit reclaim to prevent infinite loops, if they ever occur.
390 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700391#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700392#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700393
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800394enum charge_type {
395 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700396 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800397 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700398 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700399 NR_CHARGE_TYPE,
400};
401
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800402/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800403enum res_type {
404 _MEM,
405 _MEMSWAP,
406 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800407 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800408};
409
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700410#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
411#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800412#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700413/* Used for OOM nofiier */
414#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800415
Balbir Singh75822b42009-09-23 15:56:38 -0700416/*
Glauber Costa09998212013-02-22 16:34:55 -0800417 * The memcg_create_mutex will be held whenever a new cgroup is created.
418 * As a consequence, any change that needs to protect against new child cgroups
419 * appearing has to hold it as well.
420 */
421static DEFINE_MUTEX(memcg_create_mutex);
422
Wanpeng Lib2145142012-07-31 16:46:01 -0700423struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
424{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400425 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700426}
427
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700428/* Some nice accessors for the vmpressure. */
429struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
430{
431 if (!memcg)
432 memcg = root_mem_cgroup;
433 return &memcg->vmpressure;
434}
435
436struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
437{
438 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
439}
440
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700441static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
442{
443 return (memcg == root_mem_cgroup);
444}
445
Li Zefan4219b2d2013-09-23 16:56:29 +0800446/*
447 * We restrict the id in the range of [1, 65535], so it can fit into
448 * an unsigned short.
449 */
450#define MEM_CGROUP_ID_MAX USHRT_MAX
451
Li Zefan34c00c32013-09-23 16:56:01 +0800452static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
453{
Tejun Heo15a4c832014-05-04 15:09:14 -0400454 return memcg->css.id;
Li Zefan34c00c32013-09-23 16:56:01 +0800455}
456
457static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
458{
459 struct cgroup_subsys_state *css;
460
Tejun Heo7d699dd2014-05-04 15:09:13 -0400461 css = css_from_id(id, &memory_cgrp_subsys);
Li Zefan34c00c32013-09-23 16:56:01 +0800462 return mem_cgroup_from_css(css);
463}
464
Glauber Costae1aab162011-12-11 21:47:03 +0000465/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700466#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000467
Glauber Costae1aab162011-12-11 21:47:03 +0000468void sock_update_memcg(struct sock *sk)
469{
Glauber Costa376be5f2012-01-20 04:57:14 +0000470 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000471 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700472 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000473
474 BUG_ON(!sk->sk_prot->proto_cgroup);
475
Glauber Costaf3f511e2012-01-05 20:16:39 +0000476 /* Socket cloning can throw us here with sk_cgrp already
477 * filled. It won't however, necessarily happen from
478 * process context. So the test for root memcg given
479 * the current task's memcg won't help us in this case.
480 *
481 * Respecting the original socket's memcg is a better
482 * decision in this case.
483 */
484 if (sk->sk_cgrp) {
485 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700486 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000487 return;
488 }
489
Glauber Costae1aab162011-12-11 21:47:03 +0000490 rcu_read_lock();
491 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700492 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700493 if (!mem_cgroup_is_root(memcg) &&
Tejun Heoec903c02014-05-13 12:11:01 -0400494 memcg_proto_active(cg_proto) &&
495 css_tryget_online(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700496 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000497 }
498 rcu_read_unlock();
499 }
500}
501EXPORT_SYMBOL(sock_update_memcg);
502
503void sock_release_memcg(struct sock *sk)
504{
Glauber Costa376be5f2012-01-20 04:57:14 +0000505 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000506 struct mem_cgroup *memcg;
507 WARN_ON(!sk->sk_cgrp->memcg);
508 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700509 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000510 }
511}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000512
513struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
514{
515 if (!memcg || mem_cgroup_is_root(memcg))
516 return NULL;
517
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700518 return &memcg->tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000519}
520EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000521
Glauber Costa3f134612012-05-29 15:07:11 -0700522#endif
523
Glauber Costaa8964b92012-12-18 14:22:09 -0800524#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800525/*
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800526 * This will be the memcg's index in each cache's ->memcg_params.memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800527 * The main reason for not using cgroup id for this:
528 * this works better in sparse environments, where we have a lot of memcgs,
529 * but only a few kmem-limited. Or also, if we have, for instance, 200
530 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
531 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800532 *
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800533 * The current size of the caches array is stored in memcg_nr_cache_ids. It
534 * will double each time we have to increase it.
Glauber Costa55007d82012-12-18 14:22:38 -0800535 */
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800536static DEFINE_IDA(memcg_cache_ida);
537int memcg_nr_cache_ids;
Glauber Costa749c5412012-12-18 14:23:01 -0800538
Vladimir Davydov05257a12015-02-12 14:59:01 -0800539/* Protects memcg_nr_cache_ids */
540static DECLARE_RWSEM(memcg_cache_ids_sem);
541
542void memcg_get_cache_ids(void)
543{
544 down_read(&memcg_cache_ids_sem);
545}
546
547void memcg_put_cache_ids(void)
548{
549 up_read(&memcg_cache_ids_sem);
550}
551
Glauber Costa55007d82012-12-18 14:22:38 -0800552/*
553 * MIN_SIZE is different than 1, because we would like to avoid going through
554 * the alloc/free process all the time. In a small machine, 4 kmem-limited
555 * cgroups is a reasonable guess. In the future, it could be a parameter or
556 * tunable, but that is strictly not necessary.
557 *
Li Zefanb8627832013-09-23 16:56:47 +0800558 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800559 * this constant directly from cgroup, but it is understandable that this is
560 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800561 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800562 * increase ours as well if it increases.
563 */
564#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800565#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800566
Glauber Costad7f25f82012-12-18 14:22:40 -0800567/*
568 * A lot of the calls to the cache allocation functions are expected to be
569 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
570 * conditional to this static branch, we'll have to allow modules that does
571 * kmem_cache_alloc and the such to see this symbol as well
572 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800573struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800574EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800575
Glauber Costaa8964b92012-12-18 14:22:09 -0800576#endif /* CONFIG_MEMCG_KMEM */
577
Balbir Singhf64c3f52009-09-23 15:56:37 -0700578static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700579mem_cgroup_zone_zoneinfo(struct mem_cgroup *memcg, struct zone *zone)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700580{
Jianyu Zhane2318752014-06-06 14:38:20 -0700581 int nid = zone_to_nid(zone);
582 int zid = zone_idx(zone);
583
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700584 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700585}
586
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700587struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100588{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700589 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100590}
591
Balbir Singhf64c3f52009-09-23 15:56:37 -0700592static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700593mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700594{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700595 int nid = page_to_nid(page);
596 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700597
Jianyu Zhane2318752014-06-06 14:38:20 -0700598 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700599}
600
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700601static struct mem_cgroup_tree_per_zone *
602soft_limit_tree_node_zone(int nid, int zid)
603{
604 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
605}
606
607static struct mem_cgroup_tree_per_zone *
608soft_limit_tree_from_page(struct page *page)
609{
610 int nid = page_to_nid(page);
611 int zid = page_zonenum(page);
612
613 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
614}
615
Johannes Weinercf2c8122014-06-06 14:38:21 -0700616static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz,
617 struct mem_cgroup_tree_per_zone *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800618 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700619{
620 struct rb_node **p = &mctz->rb_root.rb_node;
621 struct rb_node *parent = NULL;
622 struct mem_cgroup_per_zone *mz_node;
623
624 if (mz->on_tree)
625 return;
626
627 mz->usage_in_excess = new_usage_in_excess;
628 if (!mz->usage_in_excess)
629 return;
630 while (*p) {
631 parent = *p;
632 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
633 tree_node);
634 if (mz->usage_in_excess < mz_node->usage_in_excess)
635 p = &(*p)->rb_left;
636 /*
637 * We can't avoid mem cgroups that are over their soft
638 * limit by the same amount
639 */
640 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
641 p = &(*p)->rb_right;
642 }
643 rb_link_node(&mz->tree_node, parent, p);
644 rb_insert_color(&mz->tree_node, &mctz->rb_root);
645 mz->on_tree = true;
646}
647
Johannes Weinercf2c8122014-06-06 14:38:21 -0700648static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
649 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700650{
651 if (!mz->on_tree)
652 return;
653 rb_erase(&mz->tree_node, &mctz->rb_root);
654 mz->on_tree = false;
655}
656
Johannes Weinercf2c8122014-06-06 14:38:21 -0700657static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
658 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700659{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700660 unsigned long flags;
661
662 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700663 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700664 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700665}
666
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800667static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
668{
669 unsigned long nr_pages = page_counter_read(&memcg->memory);
670 unsigned long soft_limit = ACCESS_ONCE(memcg->soft_limit);
671 unsigned long excess = 0;
672
673 if (nr_pages > soft_limit)
674 excess = nr_pages - soft_limit;
675
676 return excess;
677}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700678
679static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
680{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800681 unsigned long excess;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700682 struct mem_cgroup_per_zone *mz;
683 struct mem_cgroup_tree_per_zone *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700684
Jianyu Zhane2318752014-06-06 14:38:20 -0700685 mctz = soft_limit_tree_from_page(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700686 /*
687 * Necessary to update all ancestors when hierarchy is used.
688 * because their event counter is not touched.
689 */
690 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Jianyu Zhane2318752014-06-06 14:38:20 -0700691 mz = mem_cgroup_page_zoneinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800692 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700693 /*
694 * We have to update the tree if mz is on RB-tree or
695 * mem is over its softlimit.
696 */
697 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700698 unsigned long flags;
699
700 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700701 /* if on-tree, remove it */
702 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700703 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700704 /*
705 * Insert again. mz->usage_in_excess will be updated.
706 * If excess is 0, no tree ops.
707 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700708 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700709 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700710 }
711 }
712}
713
714static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
715{
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700716 struct mem_cgroup_tree_per_zone *mctz;
Jianyu Zhane2318752014-06-06 14:38:20 -0700717 struct mem_cgroup_per_zone *mz;
718 int nid, zid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700719
Jianyu Zhane2318752014-06-06 14:38:20 -0700720 for_each_node(nid) {
721 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
722 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
723 mctz = soft_limit_tree_node_zone(nid, zid);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700724 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700725 }
726 }
727}
728
729static struct mem_cgroup_per_zone *
730__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
731{
732 struct rb_node *rightmost = NULL;
733 struct mem_cgroup_per_zone *mz;
734
735retry:
736 mz = NULL;
737 rightmost = rb_last(&mctz->rb_root);
738 if (!rightmost)
739 goto done; /* Nothing to reclaim from */
740
741 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
742 /*
743 * Remove the node now but someone else can add it back,
744 * we will to add it back at the end of reclaim to its correct
745 * position in the tree.
746 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700747 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800748 if (!soft_limit_excess(mz->memcg) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400749 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700750 goto retry;
751done:
752 return mz;
753}
754
755static struct mem_cgroup_per_zone *
756mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
757{
758 struct mem_cgroup_per_zone *mz;
759
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700760 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700761 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700762 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700763 return mz;
764}
765
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700766/*
767 * Implementation Note: reading percpu statistics for memcg.
768 *
769 * Both of vmstat[] and percpu_counter has threshold and do periodic
770 * synchronization to implement "quick" read. There are trade-off between
771 * reading cost and precision of value. Then, we may have a chance to implement
772 * a periodic synchronizion of counter in memcg's counter.
773 *
774 * But this _read() function is used for user interface now. The user accounts
775 * memory usage by memory cgroup and he _always_ requires exact value because
776 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
777 * have to visit all online cpus and make sum. So, for now, unnecessary
778 * synchronization is not implemented. (just implemented for cpu hotplug)
779 *
780 * If there are kernel internal actions which can make use of some not-exact
781 * value, and reading all cpu value can be performance bottleneck in some
782 * common workload, threashold and synchonization as vmstat[] should be
783 * implemented.
784 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700785static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700786 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800787{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700788 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800789 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800790
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700791 get_online_cpus();
792 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700794#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700795 spin_lock(&memcg->pcp_counter_lock);
796 val += memcg->nocpu_base.count[idx];
797 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700798#endif
799 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800800 return val;
801}
802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700803static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700804 enum mem_cgroup_events_index idx)
805{
806 unsigned long val = 0;
807 int cpu;
808
David Rientjes9c567512013-10-16 13:46:43 -0700809 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700810 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700811 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700812#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700813 spin_lock(&memcg->pcp_counter_lock);
814 val += memcg->nocpu_base.events[idx];
815 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700816#endif
David Rientjes9c567512013-10-16 13:46:43 -0700817 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700818 return val;
819}
820
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700821static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700822 struct page *page,
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700823 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800824{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700825 /*
826 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
827 * counted as CACHE even if it's on ANON LRU.
828 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700829 if (PageAnon(page))
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700830 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700831 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800832 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700833 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700834 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700835
David Rientjesb070e652013-05-07 16:18:09 -0700836 if (PageTransHuge(page))
837 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
838 nr_pages);
839
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800840 /* pagein of a big page is an event. So, ignore page size */
841 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800843 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700844 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800845 nr_pages = -nr_pages; /* for event */
846 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800847
Johannes Weiner13114712012-05-29 15:07:07 -0700848 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800849}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800850
Jianyu Zhane2318752014-06-06 14:38:20 -0700851unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700852{
853 struct mem_cgroup_per_zone *mz;
854
855 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
856 return mz->lru_size[lru];
857}
858
Jianyu Zhane2318752014-06-06 14:38:20 -0700859static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
860 int nid,
861 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700862{
Jianyu Zhane2318752014-06-06 14:38:20 -0700863 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700864 int zid;
865
Jianyu Zhane2318752014-06-06 14:38:20 -0700866 VM_BUG_ON((unsigned)nid >= nr_node_ids);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700867
Jianyu Zhane2318752014-06-06 14:38:20 -0700868 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
869 struct mem_cgroup_per_zone *mz;
870 enum lru_list lru;
871
872 for_each_lru(lru) {
873 if (!(BIT(lru) & lru_mask))
874 continue;
875 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
876 nr += mz->lru_size[lru];
877 }
878 }
879 return nr;
Ying Han889976d2011-05-26 16:25:33 -0700880}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700882static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700883 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800884{
Jianyu Zhane2318752014-06-06 14:38:20 -0700885 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700886 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800887
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800888 for_each_node_state(nid, N_MEMORY)
Jianyu Zhane2318752014-06-06 14:38:20 -0700889 nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
890 return nr;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800891}
892
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800893static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
894 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800895{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700896 unsigned long val, next;
897
Johannes Weiner13114712012-05-29 15:07:07 -0700898 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700899 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700900 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800901 if ((long)next - (long)val < 0) {
902 switch (target) {
903 case MEM_CGROUP_TARGET_THRESH:
904 next = val + THRESHOLDS_EVENTS_TARGET;
905 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700906 case MEM_CGROUP_TARGET_SOFTLIMIT:
907 next = val + SOFTLIMIT_EVENTS_TARGET;
908 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800909 case MEM_CGROUP_TARGET_NUMAINFO:
910 next = val + NUMAINFO_EVENTS_TARGET;
911 break;
912 default:
913 break;
914 }
915 __this_cpu_write(memcg->stat->targets[target], next);
916 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700917 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800918 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800919}
920
921/*
922 * Check events in order.
923 *
924 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700925static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800926{
927 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800928 if (unlikely(mem_cgroup_event_ratelimit(memcg,
929 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700930 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800931 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800932
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700933 do_softlimit = mem_cgroup_event_ratelimit(memcg,
934 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700935#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800936 do_numainfo = mem_cgroup_event_ratelimit(memcg,
937 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700938#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800939 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700940 if (unlikely(do_softlimit))
941 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800942#if MAX_NUMNODES > 1
943 if (unlikely(do_numainfo))
944 atomic_inc(&memcg->numainfo_events);
945#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700946 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800947}
948
Balbir Singhcf475ad2008-04-29 01:00:16 -0700949struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800950{
Balbir Singh31a78f22008-09-28 23:09:31 +0100951 /*
952 * mm_update_next_owner() may clear mm->owner to NULL
953 * if it races with swapoff, page migration, etc.
954 * So this can be called with p == NULL.
955 */
956 if (unlikely(!p))
957 return NULL;
958
Tejun Heo073219e2014-02-08 10:36:58 -0500959 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800960}
961
Johannes Weinerdf381972014-04-07 15:37:43 -0700962static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800963{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700964 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700965
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800966 rcu_read_lock();
967 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -0700968 /*
969 * Page cache insertions can happen withou an
970 * actual mm context, e.g. during disk probing
971 * on boot, loopback IO, acct() writes etc.
972 */
973 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -0700974 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -0700975 else {
976 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
977 if (unlikely(!memcg))
978 memcg = root_mem_cgroup;
979 }
Tejun Heoec903c02014-05-13 12:11:01 -0400980 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800981 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700982 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800983}
984
Johannes Weiner56600482012-01-12 17:17:59 -0800985/**
986 * mem_cgroup_iter - iterate over memory cgroup hierarchy
987 * @root: hierarchy root
988 * @prev: previously returned memcg, NULL on first invocation
989 * @reclaim: cookie for shared reclaim walks, NULL for full walks
990 *
991 * Returns references to children of the hierarchy below @root, or
992 * @root itself, or %NULL after a full round-trip.
993 *
994 * Caller must pass the return value in @prev on subsequent
995 * invocations for reference counting, or use mem_cgroup_iter_break()
996 * to cancel a hierarchy walk before the round-trip is complete.
997 *
998 * Reclaimers can specify a zone and a priority level in @reclaim to
999 * divide up the memcgs in the hierarchy among all concurrent
1000 * reclaimers operating on the same zone and priority.
1001 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001002struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001003 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001004 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001005{
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001006 struct reclaim_iter *uninitialized_var(iter);
1007 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001008 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001009 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001010
Andrew Morton694fbc02013-09-24 15:27:37 -07001011 if (mem_cgroup_disabled())
1012 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001013
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001014 if (!root)
1015 root = root_mem_cgroup;
1016
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001017 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001018 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001019
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001020 if (!root->use_hierarchy && root != root_mem_cgroup) {
1021 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001022 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001023 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001024 }
1025
Michal Hocko542f85f2013-04-29 15:07:15 -07001026 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001027
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001028 if (reclaim) {
1029 struct mem_cgroup_per_zone *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001030
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001031 mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone);
1032 iter = &mz->iter[reclaim->priority];
Michal Hocko5f578162013-04-29 15:07:17 -07001033
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001034 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001035 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001036
1037 do {
1038 pos = ACCESS_ONCE(iter->position);
1039 /*
1040 * A racing update may change the position and
1041 * put the last reference, hence css_tryget(),
1042 * or retry to see the updated position.
1043 */
1044 } while (pos && !css_tryget(&pos->css));
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001045 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001046
1047 if (pos)
1048 css = &pos->css;
1049
1050 for (;;) {
1051 css = css_next_descendant_pre(css, &root->css);
1052 if (!css) {
1053 /*
1054 * Reclaimers share the hierarchy walk, and a
1055 * new one might jump in right at the end of
1056 * the hierarchy - make sure they see at least
1057 * one group and restart from the beginning.
1058 */
1059 if (!prev)
1060 continue;
1061 break;
1062 }
1063
1064 /*
1065 * Verify the css and acquire a reference. The root
1066 * is provided by the caller, so we know it's alive
1067 * and kicking, and don't take an extra reference.
1068 */
1069 memcg = mem_cgroup_from_css(css);
1070
1071 if (css == &root->css)
1072 break;
1073
Johannes Weinerb2052562014-12-10 15:42:48 -08001074 if (css_tryget(css)) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001075 /*
1076 * Make sure the memcg is initialized:
1077 * mem_cgroup_css_online() orders the the
1078 * initialization against setting the flag.
1079 */
1080 if (smp_load_acquire(&memcg->initialized))
1081 break;
1082
1083 css_put(css);
1084 }
1085
1086 memcg = NULL;
1087 }
1088
1089 if (reclaim) {
1090 if (cmpxchg(&iter->position, pos, memcg) == pos) {
1091 if (memcg)
1092 css_get(&memcg->css);
1093 if (pos)
1094 css_put(&pos->css);
1095 }
1096
1097 /*
1098 * pairs with css_tryget when dereferencing iter->position
1099 * above.
1100 */
1101 if (pos)
1102 css_put(&pos->css);
1103
1104 if (!memcg)
1105 iter->generation++;
1106 else if (!prev)
1107 reclaim->generation = iter->generation;
1108 }
1109
Michal Hocko542f85f2013-04-29 15:07:15 -07001110out_unlock:
1111 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001112out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001113 if (prev && prev != root)
1114 css_put(&prev->css);
1115
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001116 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001117}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001118
Johannes Weiner56600482012-01-12 17:17:59 -08001119/**
1120 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1121 * @root: hierarchy root
1122 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1123 */
1124void mem_cgroup_iter_break(struct mem_cgroup *root,
1125 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126{
1127 if (!root)
1128 root = root_mem_cgroup;
1129 if (prev && prev != root)
1130 css_put(&prev->css);
1131}
1132
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001133/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001134 * Iteration constructs for visiting all cgroups (under a tree). If
1135 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1136 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001137 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001138#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001139 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001140 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001141 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001142
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001143#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001144 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001145 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001146 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001147
David Rientjes68ae5642012-12-12 13:51:57 -08001148void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001149{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001150 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001151
Ying Han456f9982011-05-26 16:25:38 -07001152 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001153 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1154 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001155 goto out;
1156
1157 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001158 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001159 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1160 break;
1161 case PGMAJFAULT:
1162 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001163 break;
1164 default:
1165 BUG();
1166 }
1167out:
1168 rcu_read_unlock();
1169}
David Rientjes68ae5642012-12-12 13:51:57 -08001170EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001171
Johannes Weiner925b7672012-01-12 17:18:15 -08001172/**
1173 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1174 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001175 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001176 *
1177 * Returns the lru list vector holding pages for the given @zone and
1178 * @mem. This can be the global zone lruvec, if the memory controller
1179 * is disabled.
1180 */
1181struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1182 struct mem_cgroup *memcg)
1183{
1184 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001185 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001186
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001187 if (mem_cgroup_disabled()) {
1188 lruvec = &zone->lruvec;
1189 goto out;
1190 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001191
Jianyu Zhane2318752014-06-06 14:38:20 -07001192 mz = mem_cgroup_zone_zoneinfo(memcg, zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001193 lruvec = &mz->lruvec;
1194out:
1195 /*
1196 * Since a node can be onlined after the mem_cgroup was created,
1197 * we have to be prepared to initialize lruvec->zone here;
1198 * and if offlined then reonlined, we need to reinitialize it.
1199 */
1200 if (unlikely(lruvec->zone != zone))
1201 lruvec->zone = zone;
1202 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001203}
1204
Johannes Weiner925b7672012-01-12 17:18:15 -08001205/**
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001206 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
Johannes Weiner925b7672012-01-12 17:18:15 -08001207 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001208 * @zone: zone of the page
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001209 *
1210 * This function is only safe when following the LRU page isolation
1211 * and putback protocol: the LRU lock must be held, and the page must
1212 * either be PageLRU() or the caller must have isolated/allocated it.
Minchan Kim3f58a822011-03-22 16:32:53 -07001213 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001214struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001215{
1216 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001217 struct mem_cgroup *memcg;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001218 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001219
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001220 if (mem_cgroup_disabled()) {
1221 lruvec = &zone->lruvec;
1222 goto out;
1223 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001224
Johannes Weiner1306a852014-12-10 15:44:52 -08001225 memcg = page->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001226 /*
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001227 * Swapcache readahead pages are added to the LRU - and
Johannes Weiner29833312014-12-10 15:44:02 -08001228 * possibly migrated - before they are charged.
Hugh Dickins75121022012-03-05 14:59:18 -08001229 */
Johannes Weiner29833312014-12-10 15:44:02 -08001230 if (!memcg)
1231 memcg = root_mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001232
Jianyu Zhane2318752014-06-06 14:38:20 -07001233 mz = mem_cgroup_page_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001234 lruvec = &mz->lruvec;
1235out:
1236 /*
1237 * Since a node can be onlined after the mem_cgroup was created,
1238 * we have to be prepared to initialize lruvec->zone here;
1239 * and if offlined then reonlined, we need to reinitialize it.
1240 */
1241 if (unlikely(lruvec->zone != zone))
1242 lruvec->zone = zone;
1243 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001244}
1245
1246/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001247 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1248 * @lruvec: mem_cgroup per zone lru vector
1249 * @lru: index of lru list the page is sitting on
1250 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001251 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001252 * This function must be called when a page is added to or removed from an
1253 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001254 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001255void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1256 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001257{
1258 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001259 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001260
1261 if (mem_cgroup_disabled())
1262 return;
1263
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001264 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1265 lru_size = mz->lru_size + lru;
1266 *lru_size += nr_pages;
1267 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001268}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001269
Johannes Weiner2314b422014-12-10 15:44:33 -08001270bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, struct mem_cgroup *root)
Michal Hocko3e920412011-07-26 16:08:29 -07001271{
Johannes Weiner2314b422014-12-10 15:44:33 -08001272 if (root == memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001273 return true;
Johannes Weiner2314b422014-12-10 15:44:33 -08001274 if (!root->use_hierarchy)
Johannes Weiner91c637342012-05-29 15:06:24 -07001275 return false;
Johannes Weiner2314b422014-12-10 15:44:33 -08001276 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001277}
1278
Johannes Weiner2314b422014-12-10 15:44:33 -08001279bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg)
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001280{
Johannes Weiner2314b422014-12-10 15:44:33 -08001281 struct mem_cgroup *task_memcg;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001282 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001283 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001284
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001285 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001286 if (p) {
Johannes Weiner2314b422014-12-10 15:44:33 -08001287 task_memcg = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001288 task_unlock(p);
1289 } else {
1290 /*
1291 * All threads may have already detached their mm's, but the oom
1292 * killer still needs to detect if they have already been oom
1293 * killed to prevent needlessly killing additional tasks.
1294 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001295 rcu_read_lock();
Johannes Weiner2314b422014-12-10 15:44:33 -08001296 task_memcg = mem_cgroup_from_task(task);
1297 css_get(&task_memcg->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001298 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001299 }
Johannes Weiner2314b422014-12-10 15:44:33 -08001300 ret = mem_cgroup_is_descendant(task_memcg, memcg);
1301 css_put(&task_memcg->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001302 return ret;
1303}
1304
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001305int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001306{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001307 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001308 unsigned long inactive;
1309 unsigned long active;
1310 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001311
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001312 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1313 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001314
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001315 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1316 if (gb)
1317 inactive_ratio = int_sqrt(10 * gb);
1318 else
1319 inactive_ratio = 1;
1320
Johannes Weiner9b272972011-11-02 13:38:23 -07001321 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001322}
1323
Vladimir Davydov90cbc252015-02-11 15:25:55 -08001324bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
1325{
1326 struct mem_cgroup_per_zone *mz;
1327 struct mem_cgroup *memcg;
1328
1329 if (mem_cgroup_disabled())
1330 return true;
1331
1332 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1333 memcg = mz->memcg;
1334
1335 return !!(memcg->css.flags & CSS_ONLINE);
1336}
1337
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001338#define mem_cgroup_from_counter(counter, member) \
Balbir Singh6d61ef42009-01-07 18:08:06 -08001339 container_of(counter, struct mem_cgroup, member)
1340
Johannes Weiner19942822011-02-01 15:52:43 -08001341/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001342 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001343 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001344 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001345 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001346 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001347 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001348static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001349{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001350 unsigned long margin = 0;
1351 unsigned long count;
1352 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001353
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001354 count = page_counter_read(&memcg->memory);
1355 limit = ACCESS_ONCE(memcg->memory.limit);
1356 if (count < limit)
1357 margin = limit - count;
1358
1359 if (do_swap_account) {
1360 count = page_counter_read(&memcg->memsw);
1361 limit = ACCESS_ONCE(memcg->memsw.limit);
1362 if (count <= limit)
1363 margin = min(margin, limit - count);
1364 }
1365
1366 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001367}
1368
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001369int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001370{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001371 /* root ? */
Linus Torvalds14208b02014-06-09 15:03:33 -07001372 if (mem_cgroup_disabled() || !memcg->css.parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001373 return vm_swappiness;
1374
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001375 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001376}
1377
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001378/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001379 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001380 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001381 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1382 * moving cgroups. This is for waiting at high-memory pressure
1383 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001384 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001385static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001386{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001387 struct mem_cgroup *from;
1388 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001389 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001390 /*
1391 * Unlike task_move routines, we access mc.to, mc.from not under
1392 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1393 */
1394 spin_lock(&mc.lock);
1395 from = mc.from;
1396 to = mc.to;
1397 if (!from)
1398 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001399
Johannes Weiner2314b422014-12-10 15:44:33 -08001400 ret = mem_cgroup_is_descendant(from, memcg) ||
1401 mem_cgroup_is_descendant(to, memcg);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001402unlock:
1403 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001404 return ret;
1405}
1406
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001407static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001408{
1409 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001410 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001411 DEFINE_WAIT(wait);
1412 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1413 /* moving charge context might have finished. */
1414 if (mc.moving_task)
1415 schedule();
1416 finish_wait(&mc.waitq, &wait);
1417 return true;
1418 }
1419 }
1420 return false;
1421}
1422
Sha Zhengju58cf1882013-02-22 16:32:05 -08001423#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001424/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001425 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001426 * @memcg: The memory cgroup that went over limit
1427 * @p: Task that is going to be killed
1428 *
1429 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1430 * enabled
1431 */
1432void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1433{
Tejun Heoe61734c2014-02-12 09:29:50 -05001434 /* oom_info_lock ensures that parallel ooms do not interleave */
Michal Hocko08088cb2014-02-25 15:01:44 -08001435 static DEFINE_MUTEX(oom_info_lock);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001436 struct mem_cgroup *iter;
1437 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001438
Sha Zhengju58cf1882013-02-22 16:32:05 -08001439 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001440 return;
1441
Michal Hocko08088cb2014-02-25 15:01:44 -08001442 mutex_lock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001443 rcu_read_lock();
1444
Tejun Heoe61734c2014-02-12 09:29:50 -05001445 pr_info("Task in ");
1446 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
Greg Thelen0346dad2015-01-26 12:58:38 -08001447 pr_cont(" killed as a result of limit of ");
Tejun Heoe61734c2014-02-12 09:29:50 -05001448 pr_cont_cgroup_path(memcg->css.cgroup);
Greg Thelen0346dad2015-01-26 12:58:38 -08001449 pr_cont("\n");
Balbir Singhe2224322009-04-02 16:57:39 -07001450
Balbir Singhe2224322009-04-02 16:57:39 -07001451 rcu_read_unlock();
1452
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001453 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1454 K((u64)page_counter_read(&memcg->memory)),
1455 K((u64)memcg->memory.limit), memcg->memory.failcnt);
1456 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1457 K((u64)page_counter_read(&memcg->memsw)),
1458 K((u64)memcg->memsw.limit), memcg->memsw.failcnt);
1459 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1460 K((u64)page_counter_read(&memcg->kmem)),
1461 K((u64)memcg->kmem.limit), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001462
1463 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heoe61734c2014-02-12 09:29:50 -05001464 pr_info("Memory cgroup stats for ");
1465 pr_cont_cgroup_path(iter->css.cgroup);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001466 pr_cont(":");
1467
1468 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1469 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1470 continue;
1471 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1472 K(mem_cgroup_read_stat(iter, i)));
1473 }
1474
1475 for (i = 0; i < NR_LRU_LISTS; i++)
1476 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1477 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1478
1479 pr_cont("\n");
1480 }
Michal Hocko08088cb2014-02-25 15:01:44 -08001481 mutex_unlock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001482}
1483
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001484/*
1485 * This function returns the number of memcg under hierarchy tree. Returns
1486 * 1(self count) if no children.
1487 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001488static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001489{
1490 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001491 struct mem_cgroup *iter;
1492
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001493 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001494 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001495 return num;
1496}
1497
Balbir Singh6d61ef42009-01-07 18:08:06 -08001498/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001499 * Return the memory (and swap, if configured) limit for a memcg.
1500 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001501static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001502{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001503 unsigned long limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001504
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001505 limit = memcg->memory.limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001506 if (mem_cgroup_swappiness(memcg)) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001507 unsigned long memsw_limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001508
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001509 memsw_limit = memcg->memsw.limit;
1510 limit = min(limit + total_swap_pages, memsw_limit);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001511 }
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001512 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001513}
1514
David Rientjes19965462012-12-11 16:00:26 -08001515static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1516 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001517{
1518 struct mem_cgroup *iter;
1519 unsigned long chosen_points = 0;
1520 unsigned long totalpages;
1521 unsigned int points = 0;
1522 struct task_struct *chosen = NULL;
1523
David Rientjes876aafb2012-07-31 16:43:48 -07001524 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001525 * If current has a pending SIGKILL or is exiting, then automatically
1526 * select it. The goal is to allow it to allocate so that it may
1527 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001528 */
Oleg Nesterovd003f372014-12-12 16:56:24 -08001529 if (fatal_signal_pending(current) || task_will_free_mem(current)) {
Michal Hocko49550b62015-02-11 15:26:12 -08001530 mark_tsk_oom_victim(current);
David Rientjes876aafb2012-07-31 16:43:48 -07001531 return;
1532 }
1533
1534 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001535 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001536 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001537 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001538 struct task_struct *task;
1539
Tejun Heo72ec7022013-08-08 20:11:26 -04001540 css_task_iter_start(&iter->css, &it);
1541 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001542 switch (oom_scan_process_thread(task, totalpages, NULL,
1543 false)) {
1544 case OOM_SCAN_SELECT:
1545 if (chosen)
1546 put_task_struct(chosen);
1547 chosen = task;
1548 chosen_points = ULONG_MAX;
1549 get_task_struct(chosen);
1550 /* fall through */
1551 case OOM_SCAN_CONTINUE:
1552 continue;
1553 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001554 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001555 mem_cgroup_iter_break(memcg, iter);
1556 if (chosen)
1557 put_task_struct(chosen);
1558 return;
1559 case OOM_SCAN_OK:
1560 break;
1561 };
1562 points = oom_badness(task, memcg, NULL, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -08001563 if (!points || points < chosen_points)
1564 continue;
1565 /* Prefer thread group leaders for display purposes */
1566 if (points == chosen_points &&
1567 thread_group_leader(chosen))
1568 continue;
1569
1570 if (chosen)
1571 put_task_struct(chosen);
1572 chosen = task;
1573 chosen_points = points;
1574 get_task_struct(chosen);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001575 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001576 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001577 }
1578
1579 if (!chosen)
1580 return;
1581 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001582 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1583 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001584}
1585
Michele Curtiae6e71d2014-12-12 16:56:35 -08001586#if MAX_NUMNODES > 1
1587
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001588/**
1589 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001590 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001591 * @nid: the node ID to be checked.
1592 * @noswap : specify true here if the user wants flle only information.
1593 *
1594 * This function returns whether the specified memcg contains any
1595 * reclaimable pages on a node. Returns true if there are any reclaimable
1596 * pages in the node.
1597 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001598static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001599 int nid, bool noswap)
1600{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001601 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001602 return true;
1603 if (noswap || !total_swap_pages)
1604 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001605 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001606 return true;
1607 return false;
1608
1609}
Ying Han889976d2011-05-26 16:25:33 -07001610
1611/*
1612 * Always updating the nodemask is not very good - even if we have an empty
1613 * list or the wrong list here, we can start from some node and traverse all
1614 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1615 *
1616 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001617static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001618{
1619 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001620 /*
1621 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1622 * pagein/pageout changes since the last update.
1623 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001624 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001625 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001626 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001627 return;
1628
Ying Han889976d2011-05-26 16:25:33 -07001629 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001630 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001631
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001632 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001633
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001634 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1635 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001636 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001637
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001638 atomic_set(&memcg->numainfo_events, 0);
1639 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001640}
1641
1642/*
1643 * Selecting a node where we start reclaim from. Because what we need is just
1644 * reducing usage counter, start from anywhere is O,K. Considering
1645 * memory reclaim from current node, there are pros. and cons.
1646 *
1647 * Freeing memory from current node means freeing memory from a node which
1648 * we'll use or we've used. So, it may make LRU bad. And if several threads
1649 * hit limits, it will see a contention on a node. But freeing from remote
1650 * node means more costs for memory reclaim because of memory latency.
1651 *
1652 * Now, we use round-robin. Better algorithm is welcomed.
1653 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001654int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001655{
1656 int node;
1657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001658 mem_cgroup_may_update_nodemask(memcg);
1659 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001661 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001662 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001663 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001664 /*
1665 * We call this when we hit limit, not when pages are added to LRU.
1666 * No LRU may hold pages because all pages are UNEVICTABLE or
1667 * memcg is too small and all pages are not on LRU. In that case,
1668 * we use curret node.
1669 */
1670 if (unlikely(node == MAX_NUMNODES))
1671 node = numa_node_id();
1672
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001674 return node;
1675}
Ying Han889976d2011-05-26 16:25:33 -07001676#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001677int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001678{
1679 return 0;
1680}
1681#endif
1682
Andrew Morton0608f432013-09-24 15:27:41 -07001683static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1684 struct zone *zone,
1685 gfp_t gfp_mask,
1686 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001687{
Andrew Morton0608f432013-09-24 15:27:41 -07001688 struct mem_cgroup *victim = NULL;
1689 int total = 0;
1690 int loop = 0;
1691 unsigned long excess;
1692 unsigned long nr_scanned;
1693 struct mem_cgroup_reclaim_cookie reclaim = {
1694 .zone = zone,
1695 .priority = 0,
1696 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001697
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001698 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001699
Andrew Morton0608f432013-09-24 15:27:41 -07001700 while (1) {
1701 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1702 if (!victim) {
1703 loop++;
1704 if (loop >= 2) {
1705 /*
1706 * If we have not been able to reclaim
1707 * anything, it might because there are
1708 * no reclaimable pages under this hierarchy
1709 */
1710 if (!total)
1711 break;
1712 /*
1713 * We want to do more targeted reclaim.
1714 * excess >> 2 is not to excessive so as to
1715 * reclaim too much, nor too less that we keep
1716 * coming back to reclaim from this cgroup
1717 */
1718 if (total >= (excess >> 2) ||
1719 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1720 break;
1721 }
1722 continue;
1723 }
Andrew Morton0608f432013-09-24 15:27:41 -07001724 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1725 zone, &nr_scanned);
1726 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001727 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001728 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001729 }
Andrew Morton0608f432013-09-24 15:27:41 -07001730 mem_cgroup_iter_break(root_memcg, victim);
1731 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001732}
1733
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001734#ifdef CONFIG_LOCKDEP
1735static struct lockdep_map memcg_oom_lock_dep_map = {
1736 .name = "memcg_oom_lock",
1737};
1738#endif
1739
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001740static DEFINE_SPINLOCK(memcg_oom_lock);
1741
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001742/*
1743 * Check OOM-Killer is already running under our hierarchy.
1744 * If someone is running, return false.
1745 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001746static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001747{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001748 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001749
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001750 spin_lock(&memcg_oom_lock);
1751
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001752 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001753 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001754 /*
1755 * this subtree of our hierarchy is already locked
1756 * so we cannot give a lock.
1757 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001758 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001759 mem_cgroup_iter_break(memcg, iter);
1760 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001761 } else
1762 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001763 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001764
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001765 if (failed) {
1766 /*
1767 * OK, we failed to lock the whole subtree so we have
1768 * to clean up what we set up to the failing subtree
1769 */
1770 for_each_mem_cgroup_tree(iter, memcg) {
1771 if (iter == failed) {
1772 mem_cgroup_iter_break(memcg, iter);
1773 break;
1774 }
1775 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001776 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001777 } else
1778 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001779
1780 spin_unlock(&memcg_oom_lock);
1781
1782 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001783}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001784
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001785static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001786{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001787 struct mem_cgroup *iter;
1788
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001789 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001790 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001791 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001792 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001793 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001794}
1795
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001796static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001797{
1798 struct mem_cgroup *iter;
1799
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001800 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001801 atomic_inc(&iter->under_oom);
1802}
1803
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001804static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001805{
1806 struct mem_cgroup *iter;
1807
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001808 /*
1809 * When a new child is created while the hierarchy is under oom,
1810 * mem_cgroup_oom_lock() may not be called. We have to use
1811 * atomic_add_unless() here.
1812 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001814 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001815}
1816
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001817static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1818
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001819struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001820 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001821 wait_queue_t wait;
1822};
1823
1824static int memcg_oom_wake_function(wait_queue_t *wait,
1825 unsigned mode, int sync, void *arg)
1826{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001827 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1828 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829 struct oom_wait_info *oom_wait_info;
1830
1831 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001832 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001833
Johannes Weiner2314b422014-12-10 15:44:33 -08001834 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1835 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001836 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001837 return autoremove_wake_function(wait, mode, sync, arg);
1838}
1839
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001840static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001841{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001842 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001843 /* for filtering, pass "memcg" as argument. */
1844 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001845}
1846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001847static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001848{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001849 if (memcg && atomic_read(&memcg->under_oom))
1850 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001851}
1852
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001853static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001854{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001855 if (!current->memcg_oom.may_oom)
1856 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001857 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001858 * We are in the middle of the charge context here, so we
1859 * don't want to block when potentially sitting on a callstack
1860 * that holds all kinds of filesystem and mm locks.
1861 *
1862 * Also, the caller may handle a failed allocation gracefully
1863 * (like optional page cache readahead) and so an OOM killer
1864 * invocation might not even be necessary.
1865 *
1866 * That's why we don't do anything here except remember the
1867 * OOM context and then deal with it at the end of the page
1868 * fault when the stack is unwound, the locks are released,
1869 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001870 */
Johannes Weiner49426422013-10-16 13:46:59 -07001871 css_get(&memcg->css);
1872 current->memcg_oom.memcg = memcg;
1873 current->memcg_oom.gfp_mask = mask;
1874 current->memcg_oom.order = order;
1875}
1876
1877/**
1878 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1879 * @handle: actually kill/wait or just clean up the OOM state
1880 *
1881 * This has to be called at the end of a page fault if the memcg OOM
1882 * handler was enabled.
1883 *
1884 * Memcg supports userspace OOM handling where failed allocations must
1885 * sleep on a waitqueue until the userspace task resolves the
1886 * situation. Sleeping directly in the charge context with all kinds
1887 * of locks held is not a good idea, instead we remember an OOM state
1888 * in the task and mem_cgroup_oom_synchronize() has to be called at
1889 * the end of the page fault to complete the OOM handling.
1890 *
1891 * Returns %true if an ongoing memcg OOM situation was detected and
1892 * completed, %false otherwise.
1893 */
1894bool mem_cgroup_oom_synchronize(bool handle)
1895{
1896 struct mem_cgroup *memcg = current->memcg_oom.memcg;
1897 struct oom_wait_info owait;
1898 bool locked;
1899
1900 /* OOM is global, do not handle */
1901 if (!memcg)
1902 return false;
1903
Michal Hockoc32b3cb2015-02-11 15:26:24 -08001904 if (!handle || oom_killer_disabled)
Johannes Weiner49426422013-10-16 13:46:59 -07001905 goto cleanup;
1906
1907 owait.memcg = memcg;
1908 owait.wait.flags = 0;
1909 owait.wait.func = memcg_oom_wake_function;
1910 owait.wait.private = current;
1911 INIT_LIST_HEAD(&owait.wait.task_list);
1912
1913 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001914 mem_cgroup_mark_under_oom(memcg);
1915
1916 locked = mem_cgroup_oom_trylock(memcg);
1917
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001918 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001919 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001920
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001921 if (locked && !memcg->oom_kill_disable) {
1922 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07001923 finish_wait(&memcg_oom_waitq, &owait.wait);
1924 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
1925 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001926 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001927 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07001928 mem_cgroup_unmark_under_oom(memcg);
1929 finish_wait(&memcg_oom_waitq, &owait.wait);
1930 }
1931
1932 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001933 mem_cgroup_oom_unlock(memcg);
1934 /*
1935 * There is no guarantee that an OOM-lock contender
1936 * sees the wakeups triggered by the OOM kill
1937 * uncharges. Wake any sleepers explicitely.
1938 */
1939 memcg_oom_recover(memcg);
1940 }
Johannes Weiner49426422013-10-16 13:46:59 -07001941cleanup:
1942 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001943 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001944 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001945}
1946
Johannes Weinerd7365e72014-10-29 14:50:48 -07001947/**
1948 * mem_cgroup_begin_page_stat - begin a page state statistics transaction
1949 * @page: page that is going to change accounted state
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001950 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07001951 * This function must mark the beginning of an accounted page state
1952 * change to prevent double accounting when the page is concurrently
1953 * being moved to another memcg:
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001954 *
Johannes Weiner6de22612015-02-11 15:25:01 -08001955 * memcg = mem_cgroup_begin_page_stat(page);
Johannes Weinerd7365e72014-10-29 14:50:48 -07001956 * if (TestClearPageState(page))
1957 * mem_cgroup_update_page_stat(memcg, state, -1);
Johannes Weiner6de22612015-02-11 15:25:01 -08001958 * mem_cgroup_end_page_stat(memcg);
Balbir Singhd69b0422009-06-17 16:26:34 -07001959 */
Johannes Weiner6de22612015-02-11 15:25:01 -08001960struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001961{
1962 struct mem_cgroup *memcg;
Johannes Weiner6de22612015-02-11 15:25:01 -08001963 unsigned long flags;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001964
Johannes Weiner6de22612015-02-11 15:25:01 -08001965 /*
1966 * The RCU lock is held throughout the transaction. The fast
1967 * path can get away without acquiring the memcg->move_lock
1968 * because page moving starts with an RCU grace period.
1969 *
1970 * The RCU lock also protects the memcg from being freed when
1971 * the page state that is going to change is the only thing
1972 * preventing the page from being uncharged.
1973 * E.g. end-writeback clearing PageWriteback(), which allows
1974 * migration to go ahead and uncharge the page before the
1975 * account transaction might be complete.
1976 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07001977 rcu_read_lock();
1978
1979 if (mem_cgroup_disabled())
1980 return NULL;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001981again:
Johannes Weiner1306a852014-12-10 15:44:52 -08001982 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08001983 if (unlikely(!memcg))
Johannes Weinerd7365e72014-10-29 14:50:48 -07001984 return NULL;
1985
Qiang Huangbdcbb652014-06-04 16:08:21 -07001986 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weinerd7365e72014-10-29 14:50:48 -07001987 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001988
Johannes Weiner6de22612015-02-11 15:25:01 -08001989 spin_lock_irqsave(&memcg->move_lock, flags);
Johannes Weiner1306a852014-12-10 15:44:52 -08001990 if (memcg != page->mem_cgroup) {
Johannes Weiner6de22612015-02-11 15:25:01 -08001991 spin_unlock_irqrestore(&memcg->move_lock, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001992 goto again;
1993 }
Johannes Weiner6de22612015-02-11 15:25:01 -08001994
1995 /*
1996 * When charge migration first begins, we can have locked and
1997 * unlocked page stat updates happening concurrently. Track
1998 * the task who has the lock for mem_cgroup_end_page_stat().
1999 */
2000 memcg->move_lock_task = current;
2001 memcg->move_lock_flags = flags;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002002
2003 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002004}
2005
Johannes Weinerd7365e72014-10-29 14:50:48 -07002006/**
2007 * mem_cgroup_end_page_stat - finish a page state statistics transaction
2008 * @memcg: the memcg that was accounted against
Johannes Weinerd7365e72014-10-29 14:50:48 -07002009 */
Johannes Weiner6de22612015-02-11 15:25:01 -08002010void mem_cgroup_end_page_stat(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002011{
Johannes Weiner6de22612015-02-11 15:25:01 -08002012 if (memcg && memcg->move_lock_task == current) {
2013 unsigned long flags = memcg->move_lock_flags;
2014
2015 memcg->move_lock_task = NULL;
2016 memcg->move_lock_flags = 0;
2017
2018 spin_unlock_irqrestore(&memcg->move_lock, flags);
2019 }
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002020
Johannes Weinerd7365e72014-10-29 14:50:48 -07002021 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002022}
2023
Johannes Weinerd7365e72014-10-29 14:50:48 -07002024/**
2025 * mem_cgroup_update_page_stat - update page state statistics
2026 * @memcg: memcg to account against
2027 * @idx: page state item to account
2028 * @val: number of pages (positive or negative)
2029 *
2030 * See mem_cgroup_begin_page_stat() for locking requirements.
2031 */
2032void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
Sha Zhengju68b48762013-09-12 15:13:50 -07002033 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002034{
Sha Zhengju658b72c2013-09-12 15:13:52 -07002035 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002036
Johannes Weinerd7365e72014-10-29 14:50:48 -07002037 if (memcg)
2038 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002039}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002040
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002041/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002042 * size of first charge trial. "32" comes from vmscan.c's magic value.
2043 * TODO: maybe necessary to use big numbers in big irons.
2044 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002045#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002046struct memcg_stock_pcp {
2047 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002048 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002049 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002050 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002051#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002052};
2053static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002054static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002055
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002056/**
2057 * consume_stock: Try to consume stocked charge on this cpu.
2058 * @memcg: memcg to consume from.
2059 * @nr_pages: how many pages to charge.
2060 *
2061 * The charges will only happen if @memcg matches the current cpu's memcg
2062 * stock, and at least @nr_pages are available in that stock. Failure to
2063 * service an allocation will refill the stock.
2064 *
2065 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002067static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068{
2069 struct memcg_stock_pcp *stock;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002070 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002071
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002072 if (nr_pages > CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002073 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002074
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002075 stock = &get_cpu_var(memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002076 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002077 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002078 ret = true;
2079 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080 put_cpu_var(memcg_stock);
2081 return ret;
2082}
2083
2084/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002085 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002086 */
2087static void drain_stock(struct memcg_stock_pcp *stock)
2088{
2089 struct mem_cgroup *old = stock->cached;
2090
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002091 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002092 page_counter_uncharge(&old->memory, stock->nr_pages);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002093 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002094 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002095 css_put_many(&old->css, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002096 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002097 }
2098 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002099}
2100
2101/*
2102 * This must be called under preempt disabled or must be called by
2103 * a thread which is pinned to local cpu.
2104 */
2105static void drain_local_stock(struct work_struct *dummy)
2106{
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002107 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002108 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002109 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002110}
2111
2112/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002113 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002114 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002115 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002116static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002117{
2118 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2119
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002120 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002121 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002122 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002123 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002124 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002125 put_cpu_var(memcg_stock);
2126}
2127
2128/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002129 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002130 * of the hierarchy under it.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002131 */
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002132static void drain_all_stock(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002134 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002135
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002136 /* If someone's already draining, avoid adding running more workers. */
2137 if (!mutex_trylock(&percpu_charge_mutex))
2138 return;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002139 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002140 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002141 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002142 for_each_online_cpu(cpu) {
2143 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002144 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002145
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 memcg = stock->cached;
2147 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002148 continue;
Johannes Weiner2314b422014-12-10 15:44:33 -08002149 if (!mem_cgroup_is_descendant(memcg, root_memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002150 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002151 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2152 if (cpu == curcpu)
2153 drain_local_stock(&stock->work);
2154 else
2155 schedule_work_on(cpu, &stock->work);
2156 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002157 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002158 put_cpu();
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002159 put_online_cpus();
Michal Hocko9f50fad2011-08-09 11:56:26 +02002160 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002161}
2162
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002163/*
2164 * This function drains percpu counter value from DEAD cpu and
2165 * move it to local cpu. Note that this function can be preempted.
2166 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002167static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002168{
2169 int i;
2170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002171 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002172 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002173 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002174
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002175 per_cpu(memcg->stat->count[i], cpu) = 0;
2176 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002177 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002178 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002179 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002180
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002181 per_cpu(memcg->stat->events[i], cpu) = 0;
2182 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002183 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002184 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002185}
2186
Paul Gortmaker0db06282013-06-19 14:53:51 -04002187static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002188 unsigned long action,
2189 void *hcpu)
2190{
2191 int cpu = (unsigned long)hcpu;
2192 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002193 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002194
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002195 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002196 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002197
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002198 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002199 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002200
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002201 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002202 mem_cgroup_drain_pcp_counter(iter, cpu);
2203
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002204 stock = &per_cpu(memcg_stock, cpu);
2205 drain_stock(stock);
2206 return NOTIFY_OK;
2207}
2208
Johannes Weiner00501b52014-08-08 14:19:20 -07002209static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2210 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002211{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002212 unsigned int batch = max(CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002213 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002214 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002215 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002216 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002217 bool may_swap = true;
2218 bool drained = false;
Johannes Weiner05b84302014-08-06 16:05:59 -07002219 int ret = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002220
Johannes Weinerce00a962014-09-05 08:43:57 -04002221 if (mem_cgroup_is_root(memcg))
2222 goto done;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002223retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002224 if (consume_stock(memcg, nr_pages))
2225 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002226
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002227 if (!do_swap_account ||
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002228 !page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2229 if (!page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002230 goto done_restock;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002231 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002232 page_counter_uncharge(&memcg->memsw, batch);
2233 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002234 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002235 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002236 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002237 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002238
Johannes Weiner6539cc02014-08-06 16:05:42 -07002239 if (batch > nr_pages) {
2240 batch = nr_pages;
2241 goto retry;
2242 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002243
Johannes Weiner06b078f2014-08-06 16:05:44 -07002244 /*
2245 * Unlike in global OOM situations, memcg is not in a physical
2246 * memory shortage. Allow dying and OOM-killed tasks to
2247 * bypass the last charges so that they can exit quickly and
2248 * free their memory.
2249 */
2250 if (unlikely(test_thread_flag(TIF_MEMDIE) ||
2251 fatal_signal_pending(current) ||
2252 current->flags & PF_EXITING))
2253 goto bypass;
2254
2255 if (unlikely(task_in_memcg_oom(current)))
2256 goto nomem;
2257
Johannes Weiner6539cc02014-08-06 16:05:42 -07002258 if (!(gfp_mask & __GFP_WAIT))
2259 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002260
Johannes Weiner241994e2015-02-11 15:26:06 -08002261 mem_cgroup_events(mem_over_limit, MEMCG_MAX, 1);
2262
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002263 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2264 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002265
Johannes Weiner61e02c72014-08-06 16:08:16 -07002266 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002267 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002268
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002269 if (!drained) {
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002270 drain_all_stock(mem_over_limit);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002271 drained = true;
2272 goto retry;
2273 }
2274
Johannes Weiner28c34c22014-08-06 16:05:47 -07002275 if (gfp_mask & __GFP_NORETRY)
2276 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002277 /*
2278 * Even though the limit is exceeded at this point, reclaim
2279 * may have been able to free some pages. Retry the charge
2280 * before killing the task.
2281 *
2282 * Only for regular pages, though: huge pages are rather
2283 * unlikely to succeed so close to the limit, and we fall back
2284 * to regular pages anyway in case of failure.
2285 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002286 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002287 goto retry;
2288 /*
2289 * At task move, charge accounts can be doubly counted. So, it's
2290 * better to wait until the end of task_move if something is going on.
2291 */
2292 if (mem_cgroup_wait_acct_move(mem_over_limit))
2293 goto retry;
2294
Johannes Weiner9b130612014-08-06 16:05:51 -07002295 if (nr_retries--)
2296 goto retry;
2297
Johannes Weiner06b078f2014-08-06 16:05:44 -07002298 if (gfp_mask & __GFP_NOFAIL)
2299 goto bypass;
2300
Johannes Weiner6539cc02014-08-06 16:05:42 -07002301 if (fatal_signal_pending(current))
2302 goto bypass;
2303
Johannes Weiner241994e2015-02-11 15:26:06 -08002304 mem_cgroup_events(mem_over_limit, MEMCG_OOM, 1);
2305
Johannes Weiner61e02c72014-08-06 16:08:16 -07002306 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages));
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002307nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002308 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002309 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002310bypass:
Johannes Weinerce00a962014-09-05 08:43:57 -04002311 return -EINTR;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002312
2313done_restock:
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002314 css_get_many(&memcg->css, batch);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002315 if (batch > nr_pages)
2316 refill_stock(memcg, batch - nr_pages);
Johannes Weiner241994e2015-02-11 15:26:06 -08002317 /*
2318 * If the hierarchy is above the normal consumption range,
2319 * make the charging task trim their excess contribution.
2320 */
2321 do {
2322 if (page_counter_read(&memcg->memory) <= memcg->high)
2323 continue;
2324 mem_cgroup_events(memcg, MEMCG_HIGH, 1);
2325 try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true);
2326 } while ((memcg = parent_mem_cgroup(memcg)));
Johannes Weiner6539cc02014-08-06 16:05:42 -07002327done:
Johannes Weiner05b84302014-08-06 16:05:59 -07002328 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002329}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002330
Johannes Weiner00501b52014-08-08 14:19:20 -07002331static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002332{
Johannes Weinerce00a962014-09-05 08:43:57 -04002333 if (mem_cgroup_is_root(memcg))
2334 return;
2335
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002336 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner05b84302014-08-06 16:05:59 -07002337 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002338 page_counter_uncharge(&memcg->memsw, nr_pages);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002339
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002340 css_put_many(&memcg->css, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002341}
2342
2343/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002344 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoec903c02014-05-13 12:11:01 -04002345 * rcu_read_lock(). The caller is responsible for calling
2346 * css_tryget_online() if the mem_cgroup is used for charging. (dropping
2347 * refcnt from swap can be called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002348 */
2349static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2350{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002351 /* ID 0 is unused ID */
2352 if (!id)
2353 return NULL;
Li Zefan34c00c32013-09-23 16:56:01 +08002354 return mem_cgroup_from_id(id);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002355}
2356
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002357/*
2358 * try_get_mem_cgroup_from_page - look up page's memcg association
2359 * @page: the page
2360 *
2361 * Look up, get a css reference, and return the memcg that owns @page.
2362 *
2363 * The page must be locked to prevent racing with swap-in and page
2364 * cache charges. If coming from an unlocked page table, the caller
2365 * must ensure the page is on the LRU or this can race with charging.
2366 */
Wu Fengguange42d9d52009-12-16 12:19:59 +01002367struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002368{
Johannes Weiner29833312014-12-10 15:44:02 -08002369 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002370 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002371 swp_entry_t ent;
2372
Sasha Levin309381fea2014-01-23 15:52:54 -08002373 VM_BUG_ON_PAGE(!PageLocked(page), page);
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002374
Johannes Weiner1306a852014-12-10 15:44:52 -08002375 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002376 if (memcg) {
2377 if (!css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002378 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002379 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002380 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002381 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002382 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002383 memcg = mem_cgroup_lookup(id);
Tejun Heoec903c02014-05-13 12:11:01 -04002384 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002385 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002386 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002387 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002388 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002389}
2390
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002391static void lock_page_lru(struct page *page, int *isolated)
2392{
2393 struct zone *zone = page_zone(page);
2394
2395 spin_lock_irq(&zone->lru_lock);
2396 if (PageLRU(page)) {
2397 struct lruvec *lruvec;
2398
2399 lruvec = mem_cgroup_page_lruvec(page, zone);
2400 ClearPageLRU(page);
2401 del_page_from_lru_list(page, lruvec, page_lru(page));
2402 *isolated = 1;
2403 } else
2404 *isolated = 0;
2405}
2406
2407static void unlock_page_lru(struct page *page, int isolated)
2408{
2409 struct zone *zone = page_zone(page);
2410
2411 if (isolated) {
2412 struct lruvec *lruvec;
2413
2414 lruvec = mem_cgroup_page_lruvec(page, zone);
2415 VM_BUG_ON_PAGE(PageLRU(page), page);
2416 SetPageLRU(page);
2417 add_page_to_lru_list(page, lruvec, page_lru(page));
2418 }
2419 spin_unlock_irq(&zone->lru_lock);
2420}
2421
Johannes Weiner00501b52014-08-08 14:19:20 -07002422static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002423 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002424{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002425 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002426
Johannes Weiner1306a852014-12-10 15:44:52 -08002427 VM_BUG_ON_PAGE(page->mem_cgroup, page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002428
2429 /*
2430 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2431 * may already be on some other mem_cgroup's LRU. Take care of it.
2432 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002433 if (lrucare)
2434 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002435
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002436 /*
2437 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08002438 * page->mem_cgroup at this point:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002439 *
2440 * - the page is uncharged
2441 *
2442 * - the page is off-LRU
2443 *
2444 * - an anonymous fault has exclusive page access, except for
2445 * a locked page table
2446 *
2447 * - a page cache insertion, a swapin fault, or a migration
2448 * have the page locked
2449 */
Johannes Weiner1306a852014-12-10 15:44:52 -08002450 page->mem_cgroup = memcg;
Hugh Dickins3be91272008-02-07 00:14:19 -08002451
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002452 if (lrucare)
2453 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002454}
2455
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002456#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002457int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
2458 unsigned long nr_pages)
Glauber Costa749c5412012-12-18 14:23:01 -08002459{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002460 struct page_counter *counter;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002461 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002462
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002463 ret = page_counter_try_charge(&memcg->kmem, nr_pages, &counter);
2464 if (ret < 0)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002465 return ret;
2466
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002467 ret = try_charge(memcg, gfp, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002468 if (ret == -EINTR) {
2469 /*
Johannes Weiner00501b52014-08-08 14:19:20 -07002470 * try_charge() chose to bypass to root due to OOM kill or
2471 * fatal signal. Since our only options are to either fail
2472 * the allocation or charge it to this cgroup, do it as a
2473 * temporary condition. But we can't fail. From a kmem/slab
2474 * perspective, the cache has already been selected, by
2475 * mem_cgroup_kmem_get_cache(), so it is too late to change
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002476 * our minds.
2477 *
2478 * This condition will only trigger if the task entered
Johannes Weiner00501b52014-08-08 14:19:20 -07002479 * memcg_charge_kmem in a sane state, but was OOM-killed
2480 * during try_charge() above. Tasks that were already dying
2481 * when the allocation triggers should have been already
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002482 * directed to the root cgroup in memcontrol.h
2483 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002484 page_counter_charge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002485 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002486 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002487 css_get_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002488 ret = 0;
2489 } else if (ret)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002490 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002491
2492 return ret;
2493}
2494
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002495void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002496{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002497 page_counter_uncharge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002498 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002499 page_counter_uncharge(&memcg->memsw, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002500
Johannes Weiner64f21992014-12-10 15:42:45 -08002501 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002502
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002503 css_put_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002504}
2505
Glauber Costa2633d7a2012-12-18 14:22:34 -08002506/*
2507 * helper for acessing a memcg's index. It will be used as an index in the
2508 * child cache array in kmem_cache, and also to derive its name. This function
2509 * will return -1 when this is not a kmem-limited memcg.
2510 */
2511int memcg_cache_id(struct mem_cgroup *memcg)
2512{
2513 return memcg ? memcg->kmemcg_id : -1;
2514}
2515
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002516static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002517{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002518 int id, size;
2519 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002520
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002521 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002522 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2523 if (id < 0)
2524 return id;
2525
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002526 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002527 return id;
2528
2529 /*
2530 * There's no space for the new id in memcg_caches arrays,
2531 * so we have to grow them.
2532 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002533 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002534
2535 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002536 if (size < MEMCG_CACHES_MIN_SIZE)
2537 size = MEMCG_CACHES_MIN_SIZE;
2538 else if (size > MEMCG_CACHES_MAX_SIZE)
2539 size = MEMCG_CACHES_MAX_SIZE;
2540
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002541 err = memcg_update_all_caches(size);
Vladimir Davydov05257a12015-02-12 14:59:01 -08002542 if (!err)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002543 err = memcg_update_all_list_lrus(size);
2544 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002545 memcg_nr_cache_ids = size;
2546
2547 up_write(&memcg_cache_ids_sem);
2548
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002549 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002550 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002551 return err;
2552 }
2553 return id;
2554}
2555
2556static void memcg_free_cache_id(int id)
2557{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002558 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002559}
2560
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002561struct memcg_kmem_cache_create_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07002562 struct mem_cgroup *memcg;
2563 struct kmem_cache *cachep;
2564 struct work_struct work;
2565};
2566
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002567static void memcg_kmem_cache_create_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08002568{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002569 struct memcg_kmem_cache_create_work *cw =
2570 container_of(w, struct memcg_kmem_cache_create_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07002571 struct mem_cgroup *memcg = cw->memcg;
2572 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002573
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002574 memcg_create_kmem_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002575
Vladimir Davydov5722d092014-04-07 15:39:24 -07002576 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002577 kfree(cw);
2578}
2579
2580/*
2581 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002582 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002583static void __memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2584 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002585{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002586 struct memcg_kmem_cache_create_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08002587
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002588 cw = kmalloc(sizeof(*cw), GFP_NOWAIT);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002589 if (!cw)
Glauber Costad7f25f82012-12-18 14:22:40 -08002590 return;
Vladimir Davydov8135be52014-12-12 16:56:38 -08002591
2592 css_get(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002593
2594 cw->memcg = memcg;
2595 cw->cachep = cachep;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002596 INIT_WORK(&cw->work, memcg_kmem_cache_create_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08002597
Glauber Costad7f25f82012-12-18 14:22:40 -08002598 schedule_work(&cw->work);
2599}
2600
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002601static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2602 struct kmem_cache *cachep)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002603{
2604 /*
2605 * We need to stop accounting when we kmalloc, because if the
2606 * corresponding kmalloc cache is not yet created, the first allocation
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002607 * in __memcg_schedule_kmem_cache_create will recurse.
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002608 *
2609 * However, it is better to enclose the whole function. Depending on
2610 * the debugging options enabled, INIT_WORK(), for instance, can
2611 * trigger an allocation. This too, will make us recurse. Because at
2612 * this point we can't allow ourselves back into memcg_kmem_get_cache,
2613 * the safest choice is to do it like this, wrapping the whole function.
2614 */
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002615 current->memcg_kmem_skip_account = 1;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002616 __memcg_schedule_kmem_cache_create(memcg, cachep);
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002617 current->memcg_kmem_skip_account = 0;
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002618}
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07002619
Glauber Costad7f25f82012-12-18 14:22:40 -08002620/*
2621 * Return the kmem_cache we're supposed to use for a slab allocation.
2622 * We try to use the current memcg's version of the cache.
2623 *
2624 * If the cache does not exist yet, if we are the first user of it,
2625 * we either create it immediately, if possible, or create it asynchronously
2626 * in a workqueue.
2627 * In the latter case, we will let the current allocation go through with
2628 * the original cache.
2629 *
2630 * Can't be called in interrupt context or from kernel threads.
2631 * This function needs to be called with rcu_read_lock() held.
2632 */
Zhang Zhen056b7cc2014-12-12 16:55:38 -08002633struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002634{
2635 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08002636 struct kmem_cache *memcg_cachep;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002637 int kmemcg_id;
Glauber Costad7f25f82012-12-18 14:22:40 -08002638
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002639 VM_BUG_ON(!is_root_cache(cachep));
Glauber Costad7f25f82012-12-18 14:22:40 -08002640
Vladimir Davydov9d100c52014-12-12 16:54:53 -08002641 if (current->memcg_kmem_skip_account)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002642 return cachep;
2643
Vladimir Davydov8135be52014-12-12 16:56:38 -08002644 memcg = get_mem_cgroup_from_mm(current->mm);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002645 kmemcg_id = ACCESS_ONCE(memcg->kmemcg_id);
2646 if (kmemcg_id < 0)
Li Zefanca0dde92013-04-29 15:08:57 -07002647 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08002648
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002649 memcg_cachep = cache_from_memcg_idx(cachep, kmemcg_id);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002650 if (likely(memcg_cachep))
2651 return memcg_cachep;
Li Zefanca0dde92013-04-29 15:08:57 -07002652
2653 /*
2654 * If we are in a safe context (can wait, and not in interrupt
2655 * context), we could be be predictable and return right away.
2656 * This would guarantee that the allocation being performed
2657 * already belongs in the new cache.
2658 *
2659 * However, there are some clashes that can arrive from locking.
2660 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002661 * memcg_create_kmem_cache, this means no further allocation
2662 * could happen with the slab_mutex held. So it's better to
2663 * defer everything.
Li Zefanca0dde92013-04-29 15:08:57 -07002664 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002665 memcg_schedule_kmem_cache_create(memcg, cachep);
Li Zefanca0dde92013-04-29 15:08:57 -07002666out:
Vladimir Davydov8135be52014-12-12 16:56:38 -08002667 css_put(&memcg->css);
Li Zefanca0dde92013-04-29 15:08:57 -07002668 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002669}
Glauber Costad7f25f82012-12-18 14:22:40 -08002670
Vladimir Davydov8135be52014-12-12 16:56:38 -08002671void __memcg_kmem_put_cache(struct kmem_cache *cachep)
2672{
2673 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002674 css_put(&cachep->memcg_params.memcg->css);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002675}
2676
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002677/*
2678 * We need to verify if the allocation against current->mm->owner's memcg is
2679 * possible for the given order. But the page is not allocated yet, so we'll
2680 * need a further commit step to do the final arrangements.
2681 *
2682 * It is possible for the task to switch cgroups in this mean time, so at
2683 * commit time, we can't rely on task conversion any longer. We'll then use
2684 * the handle argument to return to the caller which cgroup we should commit
2685 * against. We could also return the memcg directly and avoid the pointer
2686 * passing, but a boolean return value gives better semantics considering
2687 * the compiled-out case as well.
2688 *
2689 * Returning true means the allocation is possible.
2690 */
2691bool
2692__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2693{
2694 struct mem_cgroup *memcg;
2695 int ret;
2696
2697 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07002698
Johannes Weinerdf381972014-04-07 15:37:43 -07002699 memcg = get_mem_cgroup_from_mm(current->mm);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002700
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07002701 if (!memcg_kmem_is_active(memcg)) {
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002702 css_put(&memcg->css);
2703 return true;
2704 }
2705
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002706 ret = memcg_charge_kmem(memcg, gfp, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002707 if (!ret)
2708 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002709
2710 css_put(&memcg->css);
2711 return (ret == 0);
2712}
2713
2714void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2715 int order)
2716{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002717 VM_BUG_ON(mem_cgroup_is_root(memcg));
2718
2719 /* The page allocation failed. Revert */
2720 if (!page) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002721 memcg_uncharge_kmem(memcg, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002722 return;
2723 }
Johannes Weiner1306a852014-12-10 15:44:52 -08002724 page->mem_cgroup = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002725}
2726
2727void __memcg_kmem_uncharge_pages(struct page *page, int order)
2728{
Johannes Weiner1306a852014-12-10 15:44:52 -08002729 struct mem_cgroup *memcg = page->mem_cgroup;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002730
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002731 if (!memcg)
2732 return;
2733
Sasha Levin309381fea2014-01-23 15:52:54 -08002734 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Johannes Weiner29833312014-12-10 15:44:02 -08002735
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002736 memcg_uncharge_kmem(memcg, 1 << order);
Johannes Weiner1306a852014-12-10 15:44:52 -08002737 page->mem_cgroup = NULL;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002738}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002739
2740struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr)
2741{
2742 struct mem_cgroup *memcg = NULL;
2743 struct kmem_cache *cachep;
2744 struct page *page;
2745
2746 page = virt_to_head_page(ptr);
2747 if (PageSlab(page)) {
2748 cachep = page->slab_cache;
2749 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002750 memcg = cachep->memcg_params.memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002751 } else
2752 /* page allocated by alloc_kmem_pages */
2753 memcg = page->mem_cgroup;
2754
2755 return memcg;
2756}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002757#endif /* CONFIG_MEMCG_KMEM */
2758
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002759#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2760
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002761/*
2762 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002763 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2764 * charge/uncharge will be never happen and move_account() is done under
2765 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002766 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002767void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002768{
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002769 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002770
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002771 if (mem_cgroup_disabled())
2772 return;
David Rientjesb070e652013-05-07 16:18:09 -07002773
Johannes Weiner29833312014-12-10 15:44:02 -08002774 for (i = 1; i < HPAGE_PMD_NR; i++)
Johannes Weiner1306a852014-12-10 15:44:52 -08002775 head[i].mem_cgroup = head->mem_cgroup;
Michal Hockob9982f82014-12-10 15:43:51 -08002776
Johannes Weiner1306a852014-12-10 15:44:52 -08002777 __this_cpu_sub(head->mem_cgroup->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
David Rientjesb070e652013-05-07 16:18:09 -07002778 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002779}
Hugh Dickins12d27102012-01-12 17:19:52 -08002780#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002781
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002782/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002783 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002784 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002785 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002786 * @from: mem_cgroup which the page is moved from.
2787 * @to: mem_cgroup which the page is moved to. @from != @to.
2788 *
2789 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002790 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002791 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002792 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002793 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2794 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002795 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002796static int mem_cgroup_move_account(struct page *page,
2797 unsigned int nr_pages,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002798 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002799 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002800{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002801 unsigned long flags;
2802 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002803
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002804 VM_BUG_ON(from == to);
Sasha Levin309381fea2014-01-23 15:52:54 -08002805 VM_BUG_ON_PAGE(PageLRU(page), page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002806 /*
2807 * The page is isolated from LRU. So, collapse function
2808 * will not handle this page. But page splitting can happen.
2809 * Do this check under compound_page_lock(). The caller should
2810 * hold it.
2811 */
2812 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002813 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002814 goto out;
2815
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002816 /*
Johannes Weiner1306a852014-12-10 15:44:52 -08002817 * Prevent mem_cgroup_migrate() from looking at page->mem_cgroup
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002818 * of its source page while we change it: page migration takes
2819 * both pages off the LRU, but page cache replacement doesn't.
2820 */
2821 if (!trylock_page(page))
2822 goto out;
Johannes Weinerde3638d2011-03-23 16:42:28 -07002823
2824 ret = -EINVAL;
Johannes Weiner1306a852014-12-10 15:44:52 -08002825 if (page->mem_cgroup != from)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002826 goto out_unlock;
Johannes Weinerde3638d2011-03-23 16:42:28 -07002827
Johannes Weiner354a4782014-12-10 15:44:05 -08002828 spin_lock_irqsave(&from->move_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002829
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002830 if (!PageAnon(page) && page_mapped(page)) {
Johannes Weiner59d1d252014-04-07 15:37:40 -07002831 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
2832 nr_pages);
2833 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
2834 nr_pages);
2835 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07002836
Johannes Weiner59d1d252014-04-07 15:37:40 -07002837 if (PageWriteback(page)) {
2838 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK],
2839 nr_pages);
2840 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK],
2841 nr_pages);
2842 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07002843
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002844 /*
Johannes Weiner1306a852014-12-10 15:44:52 -08002845 * It is safe to change page->mem_cgroup here because the page
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002846 * is referenced, charged, and isolated - we can't race with
2847 * uncharging, charging, migration, or LRU putback.
2848 */
Balbir Singhd69b0422009-06-17 16:26:34 -07002849
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002850 /* caller should have done css_get */
Johannes Weiner1306a852014-12-10 15:44:52 -08002851 page->mem_cgroup = to;
Johannes Weiner354a4782014-12-10 15:44:05 -08002852 spin_unlock_irqrestore(&from->move_lock, flags);
2853
Johannes Weinerde3638d2011-03-23 16:42:28 -07002854 ret = 0;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002855
2856 local_irq_disable();
2857 mem_cgroup_charge_statistics(to, page, nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07002858 memcg_check_events(to, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002859 mem_cgroup_charge_statistics(from, page, -nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07002860 memcg_check_events(from, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002861 local_irq_enable();
2862out_unlock:
2863 unlock_page(page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002864out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002865 return ret;
2866}
2867
Andrew Mortonc255a452012-07-31 16:43:02 -07002868#ifdef CONFIG_MEMCG_SWAP
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002869static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
2870 bool charge)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002871{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002872 int val = (charge) ? 1 : -1;
2873 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002874}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002875
2876/**
2877 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2878 * @entry: swap entry to be moved
2879 * @from: mem_cgroup which the entry is moved from
2880 * @to: mem_cgroup which the entry is moved to
2881 *
2882 * It succeeds only when the swap_cgroup's record for this entry is the same
2883 * as the mem_cgroup's id of @from.
2884 *
2885 * Returns 0 on success, -EINVAL on failure.
2886 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002887 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08002888 * both res and memsw, and called css_get().
2889 */
2890static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002891 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002892{
2893 unsigned short old_id, new_id;
2894
Li Zefan34c00c32013-09-23 16:56:01 +08002895 old_id = mem_cgroup_id(from);
2896 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002897
2898 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002899 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002900 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002901 return 0;
2902 }
2903 return -EINVAL;
2904}
2905#else
2906static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002907 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002908{
2909 return -EINVAL;
2910}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002911#endif
2912
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002913static DEFINE_MUTEX(memcg_limit_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07002914
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002915static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002916 unsigned long limit)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002917{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002918 unsigned long curusage;
2919 unsigned long oldusage;
2920 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002921 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002922 int ret;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002923
2924 /*
2925 * For keeping hierarchical_reclaim simple, how long we should retry
2926 * is depends on callers. We set our retry-count to be function
2927 * of # of children which we should visit in this loop.
2928 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002929 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2930 mem_cgroup_count_children(memcg);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002931
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002932 oldusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002933
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002934 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002935 if (signal_pending(current)) {
2936 ret = -EINTR;
2937 break;
2938 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002939
2940 mutex_lock(&memcg_limit_mutex);
2941 if (limit > memcg->memsw.limit) {
2942 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002943 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002944 break;
2945 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002946 if (limit > memcg->memory.limit)
2947 enlarge = true;
2948 ret = page_counter_limit(&memcg->memory, limit);
2949 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002950
2951 if (!ret)
2952 break;
2953
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002954 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, true);
2955
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002956 curusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002957 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002958 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002959 retry_count--;
2960 else
2961 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002962 } while (retry_count);
2963
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002964 if (!ret && enlarge)
2965 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002966
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002967 return ret;
2968}
2969
Li Zefan338c8432009-06-17 16:27:15 -07002970static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002971 unsigned long limit)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002972{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002973 unsigned long curusage;
2974 unsigned long oldusage;
2975 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002976 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002977 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002978
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002979 /* see mem_cgroup_resize_res_limit */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002980 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2981 mem_cgroup_count_children(memcg);
2982
2983 oldusage = page_counter_read(&memcg->memsw);
2984
2985 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002986 if (signal_pending(current)) {
2987 ret = -EINTR;
2988 break;
2989 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002990
2991 mutex_lock(&memcg_limit_mutex);
2992 if (limit < memcg->memory.limit) {
2993 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002994 ret = -EINVAL;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002995 break;
2996 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002997 if (limit > memcg->memsw.limit)
2998 enlarge = true;
2999 ret = page_counter_limit(&memcg->memsw, limit);
3000 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003001
3002 if (!ret)
3003 break;
3004
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003005 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, false);
3006
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003007 curusage = page_counter_read(&memcg->memsw);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003008 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003009 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003010 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003011 else
3012 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003013 } while (retry_count);
3014
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003015 if (!ret && enlarge)
3016 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003017
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003018 return ret;
3019}
3020
Andrew Morton0608f432013-09-24 15:27:41 -07003021unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
3022 gfp_t gfp_mask,
3023 unsigned long *total_scanned)
3024{
3025 unsigned long nr_reclaimed = 0;
3026 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3027 unsigned long reclaimed;
3028 int loop = 0;
3029 struct mem_cgroup_tree_per_zone *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003030 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07003031 unsigned long nr_scanned;
3032
3033 if (order > 0)
3034 return 0;
3035
3036 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
3037 /*
3038 * This loop can run a while, specially if mem_cgroup's continuously
3039 * keep exceeding their soft limit and putting the system under
3040 * pressure
3041 */
3042 do {
3043 if (next_mz)
3044 mz = next_mz;
3045 else
3046 mz = mem_cgroup_largest_soft_limit_node(mctz);
3047 if (!mz)
3048 break;
3049
3050 nr_scanned = 0;
3051 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
3052 gfp_mask, &nr_scanned);
3053 nr_reclaimed += reclaimed;
3054 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003055 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003056 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07003057
3058 /*
3059 * If we failed to reclaim anything from this memory cgroup
3060 * it is time to move on to the next cgroup
3061 */
3062 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003063 if (!reclaimed)
3064 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3065
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003066 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07003067 /*
3068 * One school of thought says that we should not add
3069 * back the node to the tree if reclaim returns 0.
3070 * But our reclaim could return 0, simply because due
3071 * to priority we are exposing a smaller subset of
3072 * memory to reclaim from. Consider this as a longer
3073 * term TODO.
3074 */
3075 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003076 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003077 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003078 css_put(&mz->memcg->css);
3079 loop++;
3080 /*
3081 * Could not reclaim anything and there are no more
3082 * mem cgroups to try or we seem to be looping without
3083 * reclaiming anything.
3084 */
3085 if (!nr_reclaimed &&
3086 (next_mz == NULL ||
3087 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3088 break;
3089 } while (!nr_reclaimed);
3090 if (next_mz)
3091 css_put(&next_mz->memcg->css);
3092 return nr_reclaimed;
3093}
3094
Tejun Heoea280e72014-05-16 13:22:48 -04003095/*
3096 * Test whether @memcg has children, dead or alive. Note that this
3097 * function doesn't care whether @memcg has use_hierarchy enabled and
3098 * returns %true if there are child csses according to the cgroup
3099 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
3100 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003101static inline bool memcg_has_children(struct mem_cgroup *memcg)
3102{
Tejun Heoea280e72014-05-16 13:22:48 -04003103 bool ret;
3104
Johannes Weiner696ac172013-10-31 16:34:15 -07003105 /*
Tejun Heoea280e72014-05-16 13:22:48 -04003106 * The lock does not prevent addition or deletion of children, but
3107 * it prevents a new child from being initialized based on this
3108 * parent in css_online(), so it's enough to decide whether
3109 * hierarchically inherited attributes can still be changed or not.
Johannes Weiner696ac172013-10-31 16:34:15 -07003110 */
Tejun Heoea280e72014-05-16 13:22:48 -04003111 lockdep_assert_held(&memcg_create_mutex);
3112
3113 rcu_read_lock();
3114 ret = css_next_child(NULL, &memcg->css);
3115 rcu_read_unlock();
3116 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003117}
3118
3119/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003120 * Reclaims as many pages from the given memcg as possible and moves
3121 * the rest to the parent.
3122 *
3123 * Caller is responsible for holding css reference for memcg.
3124 */
3125static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3126{
3127 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003128
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003129 /* we call try-to-free pages for make this cgroup empty */
3130 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003131 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003132 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003133 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003134
Michal Hockoc26251f2012-10-26 13:37:28 +02003135 if (signal_pending(current))
3136 return -EINTR;
3137
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003138 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3139 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003140 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003141 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003142 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003143 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003144 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003145
3146 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003147
3148 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003149}
3150
Tejun Heo6770c642014-05-13 12:16:21 -04003151static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3152 char *buf, size_t nbytes,
3153 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003154{
Tejun Heo6770c642014-05-13 12:16:21 -04003155 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003156
Michal Hockod8423012012-10-26 13:37:29 +02003157 if (mem_cgroup_is_root(memcg))
3158 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003159 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003160}
3161
Tejun Heo182446d2013-08-08 20:11:24 -04003162static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3163 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003164{
Tejun Heo182446d2013-08-08 20:11:24 -04003165 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003166}
3167
Tejun Heo182446d2013-08-08 20:11:24 -04003168static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3169 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003170{
3171 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003172 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003173 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003174
Glauber Costa09998212013-02-22 16:34:55 -08003175 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07003176
3177 if (memcg->use_hierarchy == val)
3178 goto out;
3179
Balbir Singh18f59ea2009-01-07 18:08:07 -08003180 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003181 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003182 * in the child subtrees. If it is unset, then the change can
3183 * occur, provided the current cgroup has no children.
3184 *
3185 * For the root cgroup, parent_mem is NULL, we allow value to be
3186 * set if there are no children.
3187 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003188 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003189 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003190 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003191 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003192 else
3193 retval = -EBUSY;
3194 } else
3195 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003196
3197out:
Glauber Costa09998212013-02-22 16:34:55 -08003198 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003199
3200 return retval;
3201}
3202
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003203static unsigned long tree_stat(struct mem_cgroup *memcg,
3204 enum mem_cgroup_stat_index idx)
Johannes Weinerce00a962014-09-05 08:43:57 -04003205{
3206 struct mem_cgroup *iter;
3207 long val = 0;
3208
3209 /* Per-cpu values can be negative, use a signed accumulator */
3210 for_each_mem_cgroup_tree(iter, memcg)
3211 val += mem_cgroup_read_stat(iter, idx);
3212
3213 if (val < 0) /* race ? */
3214 val = 0;
3215 return val;
3216}
3217
3218static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3219{
3220 u64 val;
3221
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003222 if (mem_cgroup_is_root(memcg)) {
3223 val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE);
3224 val += tree_stat(memcg, MEM_CGROUP_STAT_RSS);
3225 if (swap)
3226 val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP);
3227 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003228 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003229 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003230 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003231 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003232 }
Johannes Weinerce00a962014-09-05 08:43:57 -04003233 return val << PAGE_SHIFT;
3234}
3235
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003236enum {
3237 RES_USAGE,
3238 RES_LIMIT,
3239 RES_MAX_USAGE,
3240 RES_FAILCNT,
3241 RES_SOFT_LIMIT,
3242};
Johannes Weinerce00a962014-09-05 08:43:57 -04003243
Tejun Heo791badb2013-12-05 12:28:02 -05003244static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003245 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003246{
Tejun Heo182446d2013-08-08 20:11:24 -04003247 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003248 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003249
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003250 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003251 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003252 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003253 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003254 case _MEMSWAP:
3255 counter = &memcg->memsw;
3256 break;
3257 case _KMEM:
3258 counter = &memcg->kmem;
3259 break;
3260 default:
3261 BUG();
3262 }
3263
3264 switch (MEMFILE_ATTR(cft->private)) {
3265 case RES_USAGE:
3266 if (counter == &memcg->memory)
3267 return mem_cgroup_usage(memcg, false);
3268 if (counter == &memcg->memsw)
3269 return mem_cgroup_usage(memcg, true);
3270 return (u64)page_counter_read(counter) * PAGE_SIZE;
3271 case RES_LIMIT:
3272 return (u64)counter->limit * PAGE_SIZE;
3273 case RES_MAX_USAGE:
3274 return (u64)counter->watermark * PAGE_SIZE;
3275 case RES_FAILCNT:
3276 return counter->failcnt;
3277 case RES_SOFT_LIMIT:
3278 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003279 default:
3280 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003281 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003282}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003283
Glauber Costa510fc4e2012-12-18 14:21:47 -08003284#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003285static int memcg_activate_kmem(struct mem_cgroup *memcg,
3286 unsigned long nr_pages)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003287{
3288 int err = 0;
3289 int memcg_id;
3290
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003291 BUG_ON(memcg->kmemcg_id >= 0);
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003292 BUG_ON(memcg->kmem_acct_activated);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003293 BUG_ON(memcg->kmem_acct_active);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003294
3295 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -08003296 * For simplicity, we won't allow this to be disabled. It also can't
3297 * be changed if the cgroup has children already, or if tasks had
3298 * already joined.
3299 *
3300 * If tasks join before we set the limit, a person looking at
3301 * kmem.usage_in_bytes will have no way to determine when it took
3302 * place, which makes the value quite meaningless.
3303 *
3304 * After it first became limited, changes in the value of the limit are
3305 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08003306 */
Glauber Costa09998212013-02-22 16:34:55 -08003307 mutex_lock(&memcg_create_mutex);
Tejun Heoea280e72014-05-16 13:22:48 -04003308 if (cgroup_has_tasks(memcg->css.cgroup) ||
3309 (memcg->use_hierarchy && memcg_has_children(memcg)))
Vladimir Davydovd6441632014-01-23 15:53:09 -08003310 err = -EBUSY;
Glauber Costa09998212013-02-22 16:34:55 -08003311 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003312 if (err)
3313 goto out;
3314
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003315 memcg_id = memcg_alloc_cache_id();
Vladimir Davydovd6441632014-01-23 15:53:09 -08003316 if (memcg_id < 0) {
3317 err = memcg_id;
3318 goto out;
3319 }
3320
Vladimir Davydovd6441632014-01-23 15:53:09 -08003321 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003322 * We couldn't have accounted to this cgroup, because it hasn't got
3323 * activated yet, so this should succeed.
Vladimir Davydovd6441632014-01-23 15:53:09 -08003324 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003325 err = page_counter_limit(&memcg->kmem, nr_pages);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003326 VM_BUG_ON(err);
3327
3328 static_key_slow_inc(&memcg_kmem_enabled_key);
3329 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003330 * A memory cgroup is considered kmem-active as soon as it gets
3331 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003332 * guarantee no one starts accounting before all call sites are
3333 * patched.
3334 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003335 memcg->kmemcg_id = memcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003336 memcg->kmem_acct_activated = true;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003337 memcg->kmem_acct_active = true;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003338out:
Vladimir Davydovd6441632014-01-23 15:53:09 -08003339 return err;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003340}
3341
Vladimir Davydovd6441632014-01-23 15:53:09 -08003342static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003343 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003344{
3345 int ret;
3346
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003347 mutex_lock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003348 if (!memcg_kmem_is_active(memcg))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003349 ret = memcg_activate_kmem(memcg, limit);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003350 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003351 ret = page_counter_limit(&memcg->kmem, limit);
3352 mutex_unlock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003353 return ret;
3354}
3355
Glauber Costa55007d82012-12-18 14:22:38 -08003356static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08003357{
Glauber Costa55007d82012-12-18 14:22:38 -08003358 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003359 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003360
Glauber Costa510fc4e2012-12-18 14:21:47 -08003361 if (!parent)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003362 return 0;
Glauber Costa55007d82012-12-18 14:22:38 -08003363
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003364 mutex_lock(&memcg_limit_mutex);
Glauber Costaa8964b92012-12-18 14:22:09 -08003365 /*
Vladimir Davydovd6441632014-01-23 15:53:09 -08003366 * If the parent cgroup is not kmem-active now, it cannot be activated
3367 * after this point, because it has at least one child already.
Glauber Costaa8964b92012-12-18 14:22:09 -08003368 */
Vladimir Davydovd6441632014-01-23 15:53:09 -08003369 if (memcg_kmem_is_active(parent))
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003370 ret = memcg_activate_kmem(memcg, PAGE_COUNTER_MAX);
3371 mutex_unlock(&memcg_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08003372 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003373}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003374#else
3375static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003376 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003377{
3378 return -EINVAL;
3379}
Hugh Dickins6d0439902013-02-22 16:35:50 -08003380#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08003381
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003382/*
3383 * The user of this function is...
3384 * RES_LIMIT.
3385 */
Tejun Heo451af502014-05-13 12:16:21 -04003386static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3387 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003388{
Tejun Heo451af502014-05-13 12:16:21 -04003389 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003390 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003391 int ret;
3392
Tejun Heo451af502014-05-13 12:16:21 -04003393 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003394 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003395 if (ret)
3396 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003397
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003398 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003399 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003400 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3401 ret = -EINVAL;
3402 break;
3403 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003404 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3405 case _MEM:
3406 ret = mem_cgroup_resize_limit(memcg, nr_pages);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003407 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003408 case _MEMSWAP:
3409 ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages);
3410 break;
3411 case _KMEM:
3412 ret = memcg_update_kmem_limit(memcg, nr_pages);
3413 break;
3414 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003415 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003416 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003417 memcg->soft_limit = nr_pages;
3418 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003419 break;
3420 }
Tejun Heo451af502014-05-13 12:16:21 -04003421 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003422}
3423
Tejun Heo6770c642014-05-13 12:16:21 -04003424static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3425 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003426{
Tejun Heo6770c642014-05-13 12:16:21 -04003427 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003428 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003429
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003430 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3431 case _MEM:
3432 counter = &memcg->memory;
3433 break;
3434 case _MEMSWAP:
3435 counter = &memcg->memsw;
3436 break;
3437 case _KMEM:
3438 counter = &memcg->kmem;
3439 break;
3440 default:
3441 BUG();
3442 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003443
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003444 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003445 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003446 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003447 break;
3448 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003449 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003450 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003451 default:
3452 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003453 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003454
Tejun Heo6770c642014-05-13 12:16:21 -04003455 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003456}
3457
Tejun Heo182446d2013-08-08 20:11:24 -04003458static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003459 struct cftype *cft)
3460{
Tejun Heo182446d2013-08-08 20:11:24 -04003461 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003462}
3463
Daisuke Nishimura02491442010-03-10 15:22:17 -08003464#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003465static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003466 struct cftype *cft, u64 val)
3467{
Tejun Heo182446d2013-08-08 20:11:24 -04003468 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003469
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003470 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003471 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003472
Glauber Costaee5e8472013-02-22 16:34:50 -08003473 /*
3474 * No kind of locking is needed in here, because ->can_attach() will
3475 * check this value once in the beginning of the process, and then carry
3476 * on with stale data. This means that changes to this value will only
3477 * affect task migrations starting after the change.
3478 */
3479 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003480 return 0;
3481}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003482#else
Tejun Heo182446d2013-08-08 20:11:24 -04003483static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003484 struct cftype *cft, u64 val)
3485{
3486 return -ENOSYS;
3487}
3488#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003489
Ying Han406eb0c2011-05-26 16:25:37 -07003490#ifdef CONFIG_NUMA
Tejun Heo2da8ca82013-12-05 12:28:04 -05003491static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003492{
Greg Thelen25485de2013-11-12 15:07:40 -08003493 struct numa_stat {
3494 const char *name;
3495 unsigned int lru_mask;
3496 };
3497
3498 static const struct numa_stat stats[] = {
3499 { "total", LRU_ALL },
3500 { "file", LRU_ALL_FILE },
3501 { "anon", LRU_ALL_ANON },
3502 { "unevictable", BIT(LRU_UNEVICTABLE) },
3503 };
3504 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003505 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08003506 unsigned long nr;
Tejun Heo2da8ca82013-12-05 12:28:04 -05003507 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Ying Han406eb0c2011-05-26 16:25:37 -07003508
Greg Thelen25485de2013-11-12 15:07:40 -08003509 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3510 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
3511 seq_printf(m, "%s=%lu", stat->name, nr);
3512 for_each_node_state(nid, N_MEMORY) {
3513 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
3514 stat->lru_mask);
3515 seq_printf(m, " N%d=%lu", nid, nr);
3516 }
3517 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003518 }
Ying Han406eb0c2011-05-26 16:25:37 -07003519
Ying Han071aee12013-11-12 15:07:41 -08003520 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3521 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07003522
Ying Han071aee12013-11-12 15:07:41 -08003523 nr = 0;
3524 for_each_mem_cgroup_tree(iter, memcg)
3525 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
3526 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
3527 for_each_node_state(nid, N_MEMORY) {
3528 nr = 0;
3529 for_each_mem_cgroup_tree(iter, memcg)
3530 nr += mem_cgroup_node_nr_lru_pages(
3531 iter, nid, stat->lru_mask);
3532 seq_printf(m, " N%d=%lu", nid, nr);
3533 }
3534 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003535 }
Ying Han406eb0c2011-05-26 16:25:37 -07003536
Ying Han406eb0c2011-05-26 16:25:37 -07003537 return 0;
3538}
3539#endif /* CONFIG_NUMA */
3540
Tejun Heo2da8ca82013-12-05 12:28:04 -05003541static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003542{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003543 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003544 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003545 struct mem_cgroup *mi;
3546 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003547
Greg Thelen0ca44b12015-02-11 15:25:58 -08003548 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_stat_names) !=
3549 MEM_CGROUP_STAT_NSTATS);
3550 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_events_names) !=
3551 MEM_CGROUP_EVENTS_NSTATS);
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003552 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
3553
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003554 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003555 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003556 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003557 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
3558 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003559 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003560
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003561 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
3562 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
3563 mem_cgroup_read_events(memcg, i));
3564
3565 for (i = 0; i < NR_LRU_LISTS; i++)
3566 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
3567 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
3568
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003569 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003570 memory = memsw = PAGE_COUNTER_MAX;
3571 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
3572 memory = min(memory, mi->memory.limit);
3573 memsw = min(memsw, mi->memsw.limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003574 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003575 seq_printf(m, "hierarchical_memory_limit %llu\n",
3576 (u64)memory * PAGE_SIZE);
3577 if (do_swap_account)
3578 seq_printf(m, "hierarchical_memsw_limit %llu\n",
3579 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003580
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003581 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
3582 long long val = 0;
3583
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003584 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003585 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003586 for_each_mem_cgroup_tree(mi, memcg)
3587 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
3588 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
3589 }
3590
3591 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
3592 unsigned long long val = 0;
3593
3594 for_each_mem_cgroup_tree(mi, memcg)
3595 val += mem_cgroup_read_events(mi, i);
3596 seq_printf(m, "total_%s %llu\n",
3597 mem_cgroup_events_names[i], val);
3598 }
3599
3600 for (i = 0; i < NR_LRU_LISTS; i++) {
3601 unsigned long long val = 0;
3602
3603 for_each_mem_cgroup_tree(mi, memcg)
3604 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
3605 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003606 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003607
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003608#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003609 {
3610 int nid, zid;
3611 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07003612 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003613 unsigned long recent_rotated[2] = {0, 0};
3614 unsigned long recent_scanned[2] = {0, 0};
3615
3616 for_each_online_node(nid)
3617 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Jianyu Zhane2318752014-06-06 14:38:20 -07003618 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
Hugh Dickins89abfab2012-05-29 15:06:53 -07003619 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003620
Hugh Dickins89abfab2012-05-29 15:06:53 -07003621 recent_rotated[0] += rstat->recent_rotated[0];
3622 recent_rotated[1] += rstat->recent_rotated[1];
3623 recent_scanned[0] += rstat->recent_scanned[0];
3624 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003625 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07003626 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
3627 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
3628 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
3629 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003630 }
3631#endif
3632
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003633 return 0;
3634}
3635
Tejun Heo182446d2013-08-08 20:11:24 -04003636static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
3637 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003638{
Tejun Heo182446d2013-08-08 20:11:24 -04003639 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003640
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07003641 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003642}
3643
Tejun Heo182446d2013-08-08 20:11:24 -04003644static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
3645 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003646{
Tejun Heo182446d2013-08-08 20:11:24 -04003647 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08003648
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003649 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003650 return -EINVAL;
3651
Linus Torvalds14208b02014-06-09 15:03:33 -07003652 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003653 memcg->swappiness = val;
3654 else
3655 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08003656
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003657 return 0;
3658}
3659
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003660static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3661{
3662 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003663 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003664 int i;
3665
3666 rcu_read_lock();
3667 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003668 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003669 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003670 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003671
3672 if (!t)
3673 goto unlock;
3674
Johannes Weinerce00a962014-09-05 08:43:57 -04003675 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003676
3677 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07003678 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003679 * If it's not true, a threshold was crossed after last
3680 * call of __mem_cgroup_threshold().
3681 */
Phil Carmody5407a562010-05-26 14:42:42 -07003682 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003683
3684 /*
3685 * Iterate backward over array of thresholds starting from
3686 * current_threshold and check if a threshold is crossed.
3687 * If none of thresholds below usage is crossed, we read
3688 * only one element of the array here.
3689 */
3690 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3691 eventfd_signal(t->entries[i].eventfd, 1);
3692
3693 /* i = current_threshold + 1 */
3694 i++;
3695
3696 /*
3697 * Iterate forward over array of thresholds starting from
3698 * current_threshold+1 and check if a threshold is crossed.
3699 * If none of thresholds above usage is crossed, we read
3700 * only one element of the array here.
3701 */
3702 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3703 eventfd_signal(t->entries[i].eventfd, 1);
3704
3705 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003706 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003707unlock:
3708 rcu_read_unlock();
3709}
3710
3711static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3712{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003713 while (memcg) {
3714 __mem_cgroup_threshold(memcg, false);
3715 if (do_swap_account)
3716 __mem_cgroup_threshold(memcg, true);
3717
3718 memcg = parent_mem_cgroup(memcg);
3719 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003720}
3721
3722static int compare_thresholds(const void *a, const void *b)
3723{
3724 const struct mem_cgroup_threshold *_a = a;
3725 const struct mem_cgroup_threshold *_b = b;
3726
Greg Thelen2bff24a2013-09-11 14:23:08 -07003727 if (_a->threshold > _b->threshold)
3728 return 1;
3729
3730 if (_a->threshold < _b->threshold)
3731 return -1;
3732
3733 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003734}
3735
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003736static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003737{
3738 struct mem_cgroup_eventfd_list *ev;
3739
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003740 spin_lock(&memcg_oom_lock);
3741
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003742 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003743 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003744
3745 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003746 return 0;
3747}
3748
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003749static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003750{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003751 struct mem_cgroup *iter;
3752
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003753 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003754 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003755}
3756
Tejun Heo59b6f872013-11-22 18:20:43 -05003757static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003758 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003759{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003760 struct mem_cgroup_thresholds *thresholds;
3761 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003762 unsigned long threshold;
3763 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003764 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003765
Johannes Weiner650c5e52015-02-11 15:26:03 -08003766 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003767 if (ret)
3768 return ret;
3769
3770 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003771
Johannes Weiner05b84302014-08-06 16:05:59 -07003772 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003773 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003774 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003775 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003776 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003777 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003778 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003779 BUG();
3780
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003781 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003782 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003783 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3784
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003785 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003786
3787 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003788 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003789 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003790 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003791 ret = -ENOMEM;
3792 goto unlock;
3793 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003794 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003795
3796 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003797 if (thresholds->primary) {
3798 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003799 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003800 }
3801
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003802 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003803 new->entries[size - 1].eventfd = eventfd;
3804 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003805
3806 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003807 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003808 compare_thresholds, NULL);
3809
3810 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003811 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003812 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07003813 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003814 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003815 * new->current_threshold will not be used until
3816 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003817 * it here.
3818 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003819 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07003820 } else
3821 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003822 }
3823
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003824 /* Free old spare buffer and save old primary buffer as spare */
3825 kfree(thresholds->spare);
3826 thresholds->spare = thresholds->primary;
3827
3828 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003829
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003830 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003831 synchronize_rcu();
3832
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003833unlock:
3834 mutex_unlock(&memcg->thresholds_lock);
3835
3836 return ret;
3837}
3838
Tejun Heo59b6f872013-11-22 18:20:43 -05003839static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003840 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003841{
Tejun Heo59b6f872013-11-22 18:20:43 -05003842 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003843}
3844
Tejun Heo59b6f872013-11-22 18:20:43 -05003845static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003846 struct eventfd_ctx *eventfd, const char *args)
3847{
Tejun Heo59b6f872013-11-22 18:20:43 -05003848 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003849}
3850
Tejun Heo59b6f872013-11-22 18:20:43 -05003851static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003852 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003853{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003854 struct mem_cgroup_thresholds *thresholds;
3855 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003856 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003857 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003858
3859 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07003860
3861 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003862 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003863 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003864 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003865 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003866 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003867 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003868 BUG();
3869
Anton Vorontsov371528c2012-02-24 05:14:46 +04003870 if (!thresholds->primary)
3871 goto unlock;
3872
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003873 /* Check if a threshold crossed before removing */
3874 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3875
3876 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003877 size = 0;
3878 for (i = 0; i < thresholds->primary->size; i++) {
3879 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003880 size++;
3881 }
3882
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003883 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003884
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003885 /* Set thresholds array to NULL if we don't have thresholds */
3886 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003887 kfree(new);
3888 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003889 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003890 }
3891
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003892 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003893
3894 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003895 new->current_threshold = -1;
3896 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3897 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003898 continue;
3899
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003900 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07003901 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003902 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003903 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003904 * until rcu_assign_pointer(), so it's safe to increment
3905 * it here.
3906 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003907 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003908 }
3909 j++;
3910 }
3911
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003912swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003913 /* Swap primary and spare array */
3914 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07003915 /* If all events are unregistered, free the spare array */
3916 if (!new) {
3917 kfree(thresholds->spare);
3918 thresholds->spare = NULL;
3919 }
3920
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003921 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003922
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003923 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003924 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04003925unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003926 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003927}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003928
Tejun Heo59b6f872013-11-22 18:20:43 -05003929static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003930 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003931{
Tejun Heo59b6f872013-11-22 18:20:43 -05003932 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003933}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003934
Tejun Heo59b6f872013-11-22 18:20:43 -05003935static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003936 struct eventfd_ctx *eventfd)
3937{
Tejun Heo59b6f872013-11-22 18:20:43 -05003938 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003939}
3940
Tejun Heo59b6f872013-11-22 18:20:43 -05003941static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003942 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003943{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003944 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003945
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003946 event = kmalloc(sizeof(*event), GFP_KERNEL);
3947 if (!event)
3948 return -ENOMEM;
3949
Michal Hocko1af8efe2011-07-26 16:08:24 -07003950 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003951
3952 event->eventfd = eventfd;
3953 list_add(&event->list, &memcg->oom_notify);
3954
3955 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07003956 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003957 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07003958 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003959
3960 return 0;
3961}
3962
Tejun Heo59b6f872013-11-22 18:20:43 -05003963static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003964 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003965{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003966 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003967
Michal Hocko1af8efe2011-07-26 16:08:24 -07003968 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003969
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003970 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003971 if (ev->eventfd == eventfd) {
3972 list_del(&ev->list);
3973 kfree(ev);
3974 }
3975 }
3976
Michal Hocko1af8efe2011-07-26 16:08:24 -07003977 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003978}
3979
Tejun Heo2da8ca82013-12-05 12:28:04 -05003980static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003981{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003982 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003983
Tejun Heo791badb2013-12-05 12:28:02 -05003984 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
3985 seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003986 return 0;
3987}
3988
Tejun Heo182446d2013-08-08 20:11:24 -04003989static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003990 struct cftype *cft, u64 val)
3991{
Tejun Heo182446d2013-08-08 20:11:24 -04003992 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003993
3994 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07003995 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003996 return -EINVAL;
3997
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003998 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003999 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004000 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004001
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004002 return 0;
4003}
4004
Andrew Mortonc255a452012-07-31 16:43:02 -07004005#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004006static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004007{
Glauber Costa55007d82012-12-18 14:22:38 -08004008 int ret;
4009
Glauber Costa55007d82012-12-18 14:22:38 -08004010 ret = memcg_propagate_kmem(memcg);
4011 if (ret)
4012 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08004013
Glauber Costa1d62e432012-04-09 19:36:33 -03004014 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07004015}
Glauber Costae5671df2011-12-11 21:47:01 +00004016
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004017static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
4018{
Vladimir Davydov2788cf02015-02-12 14:59:38 -08004019 struct cgroup_subsys_state *css;
4020 struct mem_cgroup *parent, *child;
4021 int kmemcg_id;
4022
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004023 if (!memcg->kmem_acct_active)
4024 return;
4025
4026 /*
4027 * Clear the 'active' flag before clearing memcg_caches arrays entries.
4028 * Since we take the slab_mutex in memcg_deactivate_kmem_caches(), it
4029 * guarantees no cache will be created for this cgroup after we are
4030 * done (see memcg_create_kmem_cache()).
4031 */
4032 memcg->kmem_acct_active = false;
4033
4034 memcg_deactivate_kmem_caches(memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -08004035
4036 kmemcg_id = memcg->kmemcg_id;
4037 BUG_ON(kmemcg_id < 0);
4038
4039 parent = parent_mem_cgroup(memcg);
4040 if (!parent)
4041 parent = root_mem_cgroup;
4042
4043 /*
4044 * Change kmemcg_id of this cgroup and all its descendants to the
4045 * parent's id, and then move all entries from this cgroup's list_lrus
4046 * to ones of the parent. After we have finished, all list_lrus
4047 * corresponding to this cgroup are guaranteed to remain empty. The
4048 * ordering is imposed by list_lru_node->lock taken by
4049 * memcg_drain_all_list_lrus().
4050 */
4051 css_for_each_descendant_pre(css, &memcg->css) {
4052 child = mem_cgroup_from_css(css);
4053 BUG_ON(child->kmemcg_id != kmemcg_id);
4054 child->kmemcg_id = parent->kmemcg_id;
4055 if (!memcg->use_hierarchy)
4056 break;
4057 }
4058 memcg_drain_all_list_lrus(kmemcg_id, parent->kmemcg_id);
4059
4060 memcg_free_cache_id(kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004061}
4062
Li Zefan10d5ebf2013-07-08 16:00:33 -07004063static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004064{
Vladimir Davydovf48b80a2015-02-12 14:59:56 -08004065 if (memcg->kmem_acct_activated) {
4066 memcg_destroy_kmem_caches(memcg);
4067 static_key_slow_dec(&memcg_kmem_enabled_key);
4068 WARN_ON(page_counter_read(&memcg->kmem));
4069 }
Glauber Costa1d62e432012-04-09 19:36:33 -03004070 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07004071}
Glauber Costae5671df2011-12-11 21:47:01 +00004072#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004073static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004074{
4075 return 0;
4076}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004077
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004078static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
4079{
4080}
4081
Li Zefan10d5ebf2013-07-08 16:00:33 -07004082static void memcg_destroy_kmem(struct mem_cgroup *memcg)
4083{
4084}
Glauber Costae5671df2011-12-11 21:47:01 +00004085#endif
4086
Tejun Heo79bd9812013-11-22 18:20:42 -05004087/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004088 * DO NOT USE IN NEW FILES.
4089 *
4090 * "cgroup.event_control" implementation.
4091 *
4092 * This is way over-engineered. It tries to support fully configurable
4093 * events for each user. Such level of flexibility is completely
4094 * unnecessary especially in the light of the planned unified hierarchy.
4095 *
4096 * Please deprecate this and replace with something simpler if at all
4097 * possible.
4098 */
4099
4100/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004101 * Unregister event and free resources.
4102 *
4103 * Gets called from workqueue.
4104 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004105static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004106{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004107 struct mem_cgroup_event *event =
4108 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004109 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004110
4111 remove_wait_queue(event->wqh, &event->wait);
4112
Tejun Heo59b6f872013-11-22 18:20:43 -05004113 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004114
4115 /* Notify userspace the event is going away. */
4116 eventfd_signal(event->eventfd, 1);
4117
4118 eventfd_ctx_put(event->eventfd);
4119 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004120 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004121}
4122
4123/*
4124 * Gets called on POLLHUP on eventfd when user closes it.
4125 *
4126 * Called with wqh->lock held and interrupts disabled.
4127 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004128static int memcg_event_wake(wait_queue_t *wait, unsigned mode,
4129 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004130{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004131 struct mem_cgroup_event *event =
4132 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004133 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004134 unsigned long flags = (unsigned long)key;
4135
4136 if (flags & POLLHUP) {
4137 /*
4138 * If the event has been detached at cgroup removal, we
4139 * can simply return knowing the other side will cleanup
4140 * for us.
4141 *
4142 * We can't race against event freeing since the other
4143 * side will require wqh->lock via remove_wait_queue(),
4144 * which we hold.
4145 */
Tejun Heofba94802013-11-22 18:20:43 -05004146 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004147 if (!list_empty(&event->list)) {
4148 list_del_init(&event->list);
4149 /*
4150 * We are in atomic context, but cgroup_event_remove()
4151 * may sleep, so we have to call it in workqueue.
4152 */
4153 schedule_work(&event->remove);
4154 }
Tejun Heofba94802013-11-22 18:20:43 -05004155 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004156 }
4157
4158 return 0;
4159}
4160
Tejun Heo3bc942f2013-11-22 18:20:44 -05004161static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004162 wait_queue_head_t *wqh, poll_table *pt)
4163{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004164 struct mem_cgroup_event *event =
4165 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004166
4167 event->wqh = wqh;
4168 add_wait_queue(wqh, &event->wait);
4169}
4170
4171/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004172 * DO NOT USE IN NEW FILES.
4173 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004174 * Parse input and register new cgroup event handler.
4175 *
4176 * Input must be in format '<event_fd> <control_fd> <args>'.
4177 * Interpretation of args is defined by control file implementation.
4178 */
Tejun Heo451af502014-05-13 12:16:21 -04004179static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4180 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004181{
Tejun Heo451af502014-05-13 12:16:21 -04004182 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004183 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004184 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004185 struct cgroup_subsys_state *cfile_css;
4186 unsigned int efd, cfd;
4187 struct fd efile;
4188 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004189 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004190 char *endp;
4191 int ret;
4192
Tejun Heo451af502014-05-13 12:16:21 -04004193 buf = strstrip(buf);
4194
4195 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004196 if (*endp != ' ')
4197 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004198 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004199
Tejun Heo451af502014-05-13 12:16:21 -04004200 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004201 if ((*endp != ' ') && (*endp != '\0'))
4202 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004203 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004204
4205 event = kzalloc(sizeof(*event), GFP_KERNEL);
4206 if (!event)
4207 return -ENOMEM;
4208
Tejun Heo59b6f872013-11-22 18:20:43 -05004209 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004210 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004211 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4212 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4213 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004214
4215 efile = fdget(efd);
4216 if (!efile.file) {
4217 ret = -EBADF;
4218 goto out_kfree;
4219 }
4220
4221 event->eventfd = eventfd_ctx_fileget(efile.file);
4222 if (IS_ERR(event->eventfd)) {
4223 ret = PTR_ERR(event->eventfd);
4224 goto out_put_efile;
4225 }
4226
4227 cfile = fdget(cfd);
4228 if (!cfile.file) {
4229 ret = -EBADF;
4230 goto out_put_eventfd;
4231 }
4232
4233 /* the process need read permission on control file */
4234 /* AV: shouldn't we check that it's been opened for read instead? */
4235 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4236 if (ret < 0)
4237 goto out_put_cfile;
4238
Tejun Heo79bd9812013-11-22 18:20:42 -05004239 /*
Tejun Heofba94802013-11-22 18:20:43 -05004240 * Determine the event callbacks and set them in @event. This used
4241 * to be done via struct cftype but cgroup core no longer knows
4242 * about these events. The following is crude but the whole thing
4243 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004244 *
4245 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004246 */
Al Virob5830432014-10-31 01:22:04 -04004247 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004248
4249 if (!strcmp(name, "memory.usage_in_bytes")) {
4250 event->register_event = mem_cgroup_usage_register_event;
4251 event->unregister_event = mem_cgroup_usage_unregister_event;
4252 } else if (!strcmp(name, "memory.oom_control")) {
4253 event->register_event = mem_cgroup_oom_register_event;
4254 event->unregister_event = mem_cgroup_oom_unregister_event;
4255 } else if (!strcmp(name, "memory.pressure_level")) {
4256 event->register_event = vmpressure_register_event;
4257 event->unregister_event = vmpressure_unregister_event;
4258 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004259 event->register_event = memsw_cgroup_usage_register_event;
4260 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004261 } else {
4262 ret = -EINVAL;
4263 goto out_put_cfile;
4264 }
4265
4266 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004267 * Verify @cfile should belong to @css. Also, remaining events are
4268 * automatically removed on cgroup destruction but the removal is
4269 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004270 */
Al Virob5830432014-10-31 01:22:04 -04004271 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004272 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004273 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004274 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004275 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004276 if (cfile_css != css) {
4277 css_put(cfile_css);
4278 goto out_put_cfile;
4279 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004280
Tejun Heo451af502014-05-13 12:16:21 -04004281 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004282 if (ret)
4283 goto out_put_css;
4284
4285 efile.file->f_op->poll(efile.file, &event->pt);
4286
Tejun Heofba94802013-11-22 18:20:43 -05004287 spin_lock(&memcg->event_list_lock);
4288 list_add(&event->list, &memcg->event_list);
4289 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004290
4291 fdput(cfile);
4292 fdput(efile);
4293
Tejun Heo451af502014-05-13 12:16:21 -04004294 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004295
4296out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004297 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004298out_put_cfile:
4299 fdput(cfile);
4300out_put_eventfd:
4301 eventfd_ctx_put(event->eventfd);
4302out_put_efile:
4303 fdput(efile);
4304out_kfree:
4305 kfree(event);
4306
4307 return ret;
4308}
4309
Johannes Weiner241994e2015-02-11 15:26:06 -08004310static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004311 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004312 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004313 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004314 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004315 },
4316 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004317 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004318 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004319 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004320 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004321 },
4322 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004323 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004324 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004325 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004326 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004327 },
4328 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004329 .name = "soft_limit_in_bytes",
4330 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004331 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004332 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004333 },
4334 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004335 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004336 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004337 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004338 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004339 },
Balbir Singh8697d332008-02-07 00:13:59 -08004340 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004341 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004342 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004343 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004344 {
4345 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004346 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004347 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004348 {
4349 .name = "use_hierarchy",
4350 .write_u64 = mem_cgroup_hierarchy_write,
4351 .read_u64 = mem_cgroup_hierarchy_read,
4352 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004353 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004354 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004355 .write = memcg_write_event_control,
Tejun Heo79bd9812013-11-22 18:20:42 -05004356 .flags = CFTYPE_NO_PREFIX,
4357 .mode = S_IWUGO,
4358 },
4359 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004360 .name = "swappiness",
4361 .read_u64 = mem_cgroup_swappiness_read,
4362 .write_u64 = mem_cgroup_swappiness_write,
4363 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004364 {
4365 .name = "move_charge_at_immigrate",
4366 .read_u64 = mem_cgroup_move_charge_read,
4367 .write_u64 = mem_cgroup_move_charge_write,
4368 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004369 {
4370 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004371 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004372 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004373 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4374 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004375 {
4376 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004377 },
Ying Han406eb0c2011-05-26 16:25:37 -07004378#ifdef CONFIG_NUMA
4379 {
4380 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004381 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004382 },
4383#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004384#ifdef CONFIG_MEMCG_KMEM
4385 {
4386 .name = "kmem.limit_in_bytes",
4387 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004388 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004389 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004390 },
4391 {
4392 .name = "kmem.usage_in_bytes",
4393 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004394 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004395 },
4396 {
4397 .name = "kmem.failcnt",
4398 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004399 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004400 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004401 },
4402 {
4403 .name = "kmem.max_usage_in_bytes",
4404 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004405 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004406 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004407 },
Glauber Costa749c5412012-12-18 14:23:01 -08004408#ifdef CONFIG_SLABINFO
4409 {
4410 .name = "kmem.slabinfo",
Vladimir Davydovb0475012014-12-10 15:44:19 -08004411 .seq_start = slab_start,
4412 .seq_next = slab_next,
4413 .seq_stop = slab_stop,
4414 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004415 },
4416#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004417#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004418 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004419};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004420
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004421static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004422{
4423 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004424 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004425 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004426 /*
4427 * This routine is called against possible nodes.
4428 * But it's BUG to call kmalloc() against offline node.
4429 *
4430 * TODO: this routine can waste much memory for nodes which will
4431 * never be onlined. It's better to use memory hotplug callback
4432 * function.
4433 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004434 if (!node_state(node, N_NORMAL_MEMORY))
4435 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004436 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004437 if (!pn)
4438 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004439
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004440 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4441 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004442 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004443 mz->usage_in_excess = 0;
4444 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004445 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004446 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004447 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004448 return 0;
4449}
4450
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004451static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004452{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004453 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004454}
4455
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004456static struct mem_cgroup *mem_cgroup_alloc(void)
4457{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004458 struct mem_cgroup *memcg;
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004459 size_t size;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004460
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004461 size = sizeof(struct mem_cgroup);
4462 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004463
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004464 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004465 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004466 return NULL;
4467
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004468 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4469 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004470 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004471 spin_lock_init(&memcg->pcp_counter_lock);
4472 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004473
4474out_free:
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004475 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004476 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004477}
4478
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004479/*
Glauber Costac8b2a362012-12-18 14:22:13 -08004480 * At destroying mem_cgroup, references from swap_cgroup can remain.
4481 * (scanning all at force_empty is too costly...)
4482 *
4483 * Instead of clearing all references at force_empty, we remember
4484 * the number of reference from swap_cgroup and free mem_cgroup when
4485 * it goes down to 0.
4486 *
4487 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07004488 */
Glauber Costac8b2a362012-12-18 14:22:13 -08004489
4490static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004491{
Glauber Costac8b2a362012-12-18 14:22:13 -08004492 int node;
Hugh Dickins59927fb2012-03-15 15:17:07 -07004493
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004494 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08004495
4496 for_each_node(node)
4497 free_mem_cgroup_per_zone_info(memcg, node);
4498
4499 free_percpu(memcg->stat);
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004500 kfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004501}
Glauber Costa3afe36b2012-05-29 15:07:10 -07004502
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004503/*
4504 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4505 */
Glauber Costae1aab162011-12-11 21:47:03 +00004506struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004507{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004508 if (!memcg->memory.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004509 return NULL;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004510 return mem_cgroup_from_counter(memcg->memory.parent, memory);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004511}
Glauber Costae1aab162011-12-11 21:47:03 +00004512EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004513
Li Zefan0eb253e2009-01-15 13:51:25 -08004514static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04004515mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004516{
Glauber Costad142e3e2013-02-22 16:34:52 -08004517 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004518 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004519 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004520
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004521 memcg = mem_cgroup_alloc();
4522 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004523 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004524
Bob Liu3ed28fa2012-01-12 17:19:04 -08004525 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004526 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004527 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004528
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004529 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04004530 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08004531 root_mem_cgroup = memcg;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004532 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994e2015-02-11 15:26:06 -08004533 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004534 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004535 page_counter_init(&memcg->memsw, NULL);
4536 page_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004537 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004538
Glauber Costad142e3e2013-02-22 16:34:52 -08004539 memcg->last_scanned_node = MAX_NUMNODES;
4540 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08004541 memcg->move_charge_at_immigrate = 0;
4542 mutex_init(&memcg->thresholds_lock);
4543 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004544 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05004545 INIT_LIST_HEAD(&memcg->event_list);
4546 spin_lock_init(&memcg->event_list_lock);
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004547#ifdef CONFIG_MEMCG_KMEM
4548 memcg->kmemcg_id = -1;
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004549#endif
Glauber Costad142e3e2013-02-22 16:34:52 -08004550
4551 return &memcg->css;
4552
4553free_out:
4554 __mem_cgroup_free(memcg);
4555 return ERR_PTR(error);
4556}
4557
4558static int
Tejun Heoeb954192013-08-08 20:11:23 -04004559mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08004560{
Tejun Heoeb954192013-08-08 20:11:23 -04004561 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04004562 struct mem_cgroup *parent = mem_cgroup_from_css(css->parent);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004563 int ret;
Glauber Costad142e3e2013-02-22 16:34:52 -08004564
Tejun Heo15a4c832014-05-04 15:09:14 -04004565 if (css->id > MEM_CGROUP_ID_MAX)
Li Zefan4219b2d2013-09-23 16:56:29 +08004566 return -ENOSPC;
4567
Tejun Heo63876982013-08-08 20:11:23 -04004568 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08004569 return 0;
4570
Glauber Costa09998212013-02-22 16:34:55 -08004571 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08004572
4573 memcg->use_hierarchy = parent->use_hierarchy;
4574 memcg->oom_kill_disable = parent->oom_kill_disable;
4575 memcg->swappiness = mem_cgroup_swappiness(parent);
4576
4577 if (parent->use_hierarchy) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004578 page_counter_init(&memcg->memory, &parent->memory);
Johannes Weiner241994e2015-02-11 15:26:06 -08004579 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004580 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004581 page_counter_init(&memcg->memsw, &parent->memsw);
4582 page_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08004583
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004584 /*
Li Zefan8d76a972013-07-08 16:00:36 -07004585 * No need to take a reference to the parent because cgroup
4586 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004587 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08004588 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004589 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994e2015-02-11 15:26:06 -08004590 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004591 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004592 page_counter_init(&memcg->memsw, NULL);
4593 page_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004594 /*
4595 * Deeper hierachy with use_hierarchy == false doesn't make
4596 * much sense so let cgroup subsystem know about this
4597 * unfortunate state in our controller.
4598 */
Glauber Costad142e3e2013-02-22 16:34:52 -08004599 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05004600 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004601 }
Glauber Costa09998212013-02-22 16:34:55 -08004602 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004603
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004604 ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
4605 if (ret)
4606 return ret;
4607
4608 /*
4609 * Make sure the memcg is initialized: mem_cgroup_iter()
4610 * orders reading memcg->initialized against its callers
4611 * reading the memcg members.
4612 */
4613 smp_store_release(&memcg->initialized, 1);
4614
4615 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004616}
4617
Tejun Heoeb954192013-08-08 20:11:23 -04004618static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004619{
Tejun Heoeb954192013-08-08 20:11:23 -04004620 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004621 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05004622
4623 /*
4624 * Unregister events and notify userspace.
4625 * Notify userspace about cgroup removing only after rmdir of cgroup
4626 * directory to avoid race between userspace and kernelspace.
4627 */
Tejun Heofba94802013-11-22 18:20:43 -05004628 spin_lock(&memcg->event_list_lock);
4629 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004630 list_del_init(&event->list);
4631 schedule_work(&event->remove);
4632 }
Tejun Heofba94802013-11-22 18:20:43 -05004633 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004634
Michal Hocko33cb8762013-07-31 13:53:51 -07004635 vmpressure_cleanup(&memcg->vmpressure);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004636
4637 memcg_deactivate_kmem(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004638}
4639
Tejun Heoeb954192013-08-08 20:11:23 -04004640static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004641{
Tejun Heoeb954192013-08-08 20:11:23 -04004642 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004643
Li Zefan10d5ebf2013-07-08 16:00:33 -07004644 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07004645 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004646}
4647
Tejun Heo1ced9532014-07-08 18:02:57 -04004648/**
4649 * mem_cgroup_css_reset - reset the states of a mem_cgroup
4650 * @css: the target css
4651 *
4652 * Reset the states of the mem_cgroup associated with @css. This is
4653 * invoked when the userland requests disabling on the default hierarchy
4654 * but the memcg is pinned through dependency. The memcg should stop
4655 * applying policies and should revert to the vanilla state as it may be
4656 * made visible again.
4657 *
4658 * The current implementation only resets the essential configurations.
4659 * This needs to be expanded to cover all the visible parts.
4660 */
4661static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
4662{
4663 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4664
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004665 mem_cgroup_resize_limit(memcg, PAGE_COUNTER_MAX);
4666 mem_cgroup_resize_memsw_limit(memcg, PAGE_COUNTER_MAX);
4667 memcg_update_kmem_limit(memcg, PAGE_COUNTER_MAX);
Johannes Weiner241994e2015-02-11 15:26:06 -08004668 memcg->low = 0;
4669 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004670 memcg->soft_limit = PAGE_COUNTER_MAX;
Tejun Heo1ced9532014-07-08 18:02:57 -04004671}
4672
Daisuke Nishimura02491442010-03-10 15:22:17 -08004673#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004674/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004675static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004676{
Johannes Weiner05b84302014-08-06 16:05:59 -07004677 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07004678
4679 /* Try a single bulk charge without reclaim first */
Johannes Weiner00501b52014-08-08 14:19:20 -07004680 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07004681 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004682 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004683 return ret;
4684 }
Johannes Weiner692e7c42014-08-06 16:05:57 -07004685 if (ret == -EINTR) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004686 cancel_charge(root_mem_cgroup, count);
Johannes Weiner692e7c42014-08-06 16:05:57 -07004687 return ret;
4688 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004689
4690 /* Try charges one by one with reclaim */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004691 while (count--) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004692 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1);
Johannes Weiner9476db92014-08-06 16:05:55 -07004693 /*
4694 * In case of failure, any residual charges against
4695 * mc.to will be dropped by mem_cgroup_clear_mc()
Johannes Weiner692e7c42014-08-06 16:05:57 -07004696 * later on. However, cancel any charges that are
4697 * bypassed to root right away or they'll be lost.
Johannes Weiner9476db92014-08-06 16:05:55 -07004698 */
Johannes Weiner692e7c42014-08-06 16:05:57 -07004699 if (ret == -EINTR)
Johannes Weiner00501b52014-08-08 14:19:20 -07004700 cancel_charge(root_mem_cgroup, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004701 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004702 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004703 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07004704 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004705 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004706 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004707}
4708
4709/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004710 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004711 * @vma: the vma the pte to be checked belongs
4712 * @addr: the address corresponding to the pte to be checked
4713 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004714 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004715 *
4716 * Returns
4717 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4718 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4719 * move charge. if @target is not NULL, the page is stored in target->page
4720 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004721 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4722 * target for charge migration. if @target is not NULL, the entry is stored
4723 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004724 *
4725 * Called with pte lock held.
4726 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004727union mc_target {
4728 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004729 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004730};
4731
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004732enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004733 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004734 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004735 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004736};
4737
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004738static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4739 unsigned long addr, pte_t ptent)
4740{
4741 struct page *page = vm_normal_page(vma, addr, ptent);
4742
4743 if (!page || !page_mapped(page))
4744 return NULL;
4745 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004746 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004747 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004748 } else {
4749 if (!(mc.flags & MOVE_FILE))
4750 return NULL;
4751 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004752 if (!get_page_unless_zero(page))
4753 return NULL;
4754
4755 return page;
4756}
4757
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004758#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004759static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4760 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4761{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004762 struct page *page = NULL;
4763 swp_entry_t ent = pte_to_swp_entry(ptent);
4764
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004765 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004766 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004767 /*
4768 * Because lookup_swap_cache() updates some statistics counter,
4769 * we call find_get_page() with swapper_space directly.
4770 */
Shaohua Li33806f02013-02-22 16:34:37 -08004771 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004772 if (do_swap_account)
4773 entry->val = ent.val;
4774
4775 return page;
4776}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004777#else
4778static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4779 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4780{
4781 return NULL;
4782}
4783#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004784
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004785static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4786 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4787{
4788 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004789 struct address_space *mapping;
4790 pgoff_t pgoff;
4791
4792 if (!vma->vm_file) /* anonymous vma */
4793 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004794 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004795 return NULL;
4796
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004797 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004798 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004799
4800 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004801#ifdef CONFIG_SWAP
4802 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07004803 if (shmem_mapping(mapping)) {
4804 page = find_get_entry(mapping, pgoff);
4805 if (radix_tree_exceptional_entry(page)) {
4806 swp_entry_t swp = radix_to_swp_entry(page);
4807 if (do_swap_account)
4808 *entry = swp;
4809 page = find_get_page(swap_address_space(swp), swp.val);
4810 }
4811 } else
4812 page = find_get_page(mapping, pgoff);
4813#else
4814 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004815#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004816 return page;
4817}
4818
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004819static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004820 unsigned long addr, pte_t ptent, union mc_target *target)
4821{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004822 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004823 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004824 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004825
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004826 if (pte_present(ptent))
4827 page = mc_handle_present_pte(vma, addr, ptent);
4828 else if (is_swap_pte(ptent))
4829 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004830 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004831 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004832
4833 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004834 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004835 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004836 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004837 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08004838 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004839 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08004840 */
Johannes Weiner1306a852014-12-10 15:44:52 -08004841 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004842 ret = MC_TARGET_PAGE;
4843 if (target)
4844 target->page = page;
4845 }
4846 if (!ret || !target)
4847 put_page(page);
4848 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004849 /* There is a swap entry and a page doesn't exist or isn't charged */
4850 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08004851 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004852 ret = MC_TARGET_SWAP;
4853 if (target)
4854 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004855 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004856 return ret;
4857}
4858
Naoya Horiguchi12724852012-03-21 16:34:28 -07004859#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4860/*
4861 * We don't consider swapping or file mapped pages because THP does not
4862 * support them for now.
4863 * Caller should make sure that pmd_trans_huge(pmd) is true.
4864 */
4865static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4866 unsigned long addr, pmd_t pmd, union mc_target *target)
4867{
4868 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07004869 enum mc_target_type ret = MC_TARGET_NONE;
4870
4871 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08004872 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004873 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07004874 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08004875 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07004876 ret = MC_TARGET_PAGE;
4877 if (target) {
4878 get_page(page);
4879 target->page = page;
4880 }
4881 }
4882 return ret;
4883}
4884#else
4885static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4886 unsigned long addr, pmd_t pmd, union mc_target *target)
4887{
4888 return MC_TARGET_NONE;
4889}
4890#endif
4891
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004892static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4893 unsigned long addr, unsigned long end,
4894 struct mm_walk *walk)
4895{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004896 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004897 pte_t *pte;
4898 spinlock_t *ptl;
4899
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08004900 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07004901 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
4902 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08004903 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07004904 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07004905 }
Dave Hansen03319322011-03-22 16:32:56 -07004906
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07004907 if (pmd_trans_unstable(pmd))
4908 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004909 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4910 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004911 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004912 mc.precharge++; /* increment precharge temporarily */
4913 pte_unmap_unlock(pte - 1, ptl);
4914 cond_resched();
4915
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004916 return 0;
4917}
4918
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004919static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4920{
4921 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004922
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004923 struct mm_walk mem_cgroup_count_precharge_walk = {
4924 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4925 .mm = mm,
4926 };
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004927 down_read(&mm->mmap_sem);
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004928 walk_page_range(0, ~0UL, &mem_cgroup_count_precharge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004929 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004930
4931 precharge = mc.precharge;
4932 mc.precharge = 0;
4933
4934 return precharge;
4935}
4936
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004937static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4938{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004939 unsigned long precharge = mem_cgroup_count_precharge(mm);
4940
4941 VM_BUG_ON(mc.moving_task);
4942 mc.moving_task = current;
4943 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004944}
4945
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004946/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
4947static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004948{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004949 struct mem_cgroup *from = mc.from;
4950 struct mem_cgroup *to = mc.to;
4951
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004952 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004953 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004954 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004955 mc.precharge = 0;
4956 }
4957 /*
4958 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4959 * we must uncharge here.
4960 */
4961 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004962 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004963 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004964 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004965 /* we must fixup refcnts and charges */
4966 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004967 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04004968 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004969 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004970
Johannes Weiner05b84302014-08-06 16:05:59 -07004971 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004972 * we charged both to->memory and to->memsw, so we
4973 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07004974 */
Johannes Weinerce00a962014-09-05 08:43:57 -04004975 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004976 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004977
Johannes Weinere8ea14c2014-12-10 15:42:42 -08004978 css_put_many(&mc.from->css, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004979
Li Zefan40503772013-07-08 16:00:34 -07004980 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004981 mc.moved_swap = 0;
4982 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004983 memcg_oom_recover(from);
4984 memcg_oom_recover(to);
4985 wake_up_all(&mc.waitq);
4986}
4987
4988static void mem_cgroup_clear_mc(void)
4989{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004990 /*
4991 * we must clear moving_task before waking up waiters at the end of
4992 * task migration.
4993 */
4994 mc.moving_task = NULL;
4995 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004996 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004997 mc.from = NULL;
4998 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004999 spin_unlock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005000}
5001
Tejun Heoeb954192013-08-08 20:11:23 -04005002static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005003 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005004{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005005 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005006 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04005007 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005008 unsigned long move_flags;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005009
Glauber Costaee5e8472013-02-22 16:34:50 -08005010 /*
5011 * We are now commited to this value whatever it is. Changes in this
5012 * tunable will only affect upcoming migrations, not the current one.
5013 * So we need to save it, and keep it going.
5014 */
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005015 move_flags = ACCESS_ONCE(memcg->move_charge_at_immigrate);
5016 if (move_flags) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005017 struct mm_struct *mm;
5018 struct mem_cgroup *from = mem_cgroup_from_task(p);
5019
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005020 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005021
5022 mm = get_task_mm(p);
5023 if (!mm)
5024 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005025 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005026 if (mm->owner == p) {
5027 VM_BUG_ON(mc.from);
5028 VM_BUG_ON(mc.to);
5029 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005030 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005031 VM_BUG_ON(mc.moved_swap);
Johannes Weiner247b1442014-12-10 15:44:11 -08005032
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005033 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005034 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005035 mc.to = memcg;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005036 mc.flags = move_flags;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005037 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005038 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005039
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005040 ret = mem_cgroup_precharge_mc(mm);
5041 if (ret)
5042 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005043 }
5044 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005045 }
5046 return ret;
5047}
5048
Tejun Heoeb954192013-08-08 20:11:23 -04005049static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005050 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005051{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005052 if (mc.to)
5053 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005054}
5055
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005056static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5057 unsigned long addr, unsigned long end,
5058 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005059{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005060 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005061 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005062 pte_t *pte;
5063 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005064 enum mc_target_type target_type;
5065 union mc_target target;
5066 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005067
Naoya Horiguchi12724852012-03-21 16:34:28 -07005068 /*
5069 * We don't take compound_lock() here but no race with splitting thp
5070 * happens because:
5071 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5072 * under splitting, which means there's no concurrent thp split,
5073 * - if another thread runs into split_huge_page() just after we
5074 * entered this if-block, the thread must wait for page table lock
5075 * to be unlocked in __split_huge_page_splitting(), where the main
5076 * part of thp split is not executed yet.
5077 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005078 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005079 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005080 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005081 return 0;
5082 }
5083 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5084 if (target_type == MC_TARGET_PAGE) {
5085 page = target.page;
5086 if (!isolate_lru_page(page)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005087 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
Johannes Weiner1306a852014-12-10 15:44:52 -08005088 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005089 mc.precharge -= HPAGE_PMD_NR;
5090 mc.moved_charge += HPAGE_PMD_NR;
5091 }
5092 putback_lru_page(page);
5093 }
5094 put_page(page);
5095 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005096 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005097 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005098 }
5099
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005100 if (pmd_trans_unstable(pmd))
5101 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005102retry:
5103 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5104 for (; addr != end; addr += PAGE_SIZE) {
5105 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005106 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005107
5108 if (!mc.precharge)
5109 break;
5110
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005111 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005112 case MC_TARGET_PAGE:
5113 page = target.page;
5114 if (isolate_lru_page(page))
5115 goto put;
Johannes Weiner1306a852014-12-10 15:44:52 -08005116 if (!mem_cgroup_move_account(page, 1, mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005117 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005118 /* we uncharge from mc.from later. */
5119 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005120 }
5121 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005122put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005123 put_page(page);
5124 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005125 case MC_TARGET_SWAP:
5126 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005127 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005128 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005129 /* we fixup refcnts and charges later. */
5130 mc.moved_swap++;
5131 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005132 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005133 default:
5134 break;
5135 }
5136 }
5137 pte_unmap_unlock(pte - 1, ptl);
5138 cond_resched();
5139
5140 if (addr != end) {
5141 /*
5142 * We have consumed all precharges we got in can_attach().
5143 * We try charge one by one, but don't do any additional
5144 * charges to mc.to if we have failed in charge once in attach()
5145 * phase.
5146 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005147 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005148 if (!ret)
5149 goto retry;
5150 }
5151
5152 return ret;
5153}
5154
5155static void mem_cgroup_move_charge(struct mm_struct *mm)
5156{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005157 struct mm_walk mem_cgroup_move_charge_walk = {
5158 .pmd_entry = mem_cgroup_move_charge_pte_range,
5159 .mm = mm,
5160 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005161
5162 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08005163 /*
5164 * Signal mem_cgroup_begin_page_stat() to take the memcg's
5165 * move_lock while we're moving its pages to another memcg.
5166 * Then wait for already started RCU-only updates to finish.
5167 */
5168 atomic_inc(&mc.from->moving_account);
5169 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005170retry:
5171 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5172 /*
5173 * Someone who are holding the mmap_sem might be waiting in
5174 * waitq. So we cancel all extra charges, wake up all waiters,
5175 * and retry. Because we cancel precharges, we might not be able
5176 * to move enough charges, but moving charge is a best-effort
5177 * feature anyway, so it wouldn't be a big problem.
5178 */
5179 __mem_cgroup_clear_mc();
5180 cond_resched();
5181 goto retry;
5182 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005183 /*
5184 * When we have consumed all precharges and failed in doing
5185 * additional charge, the page walk just aborts.
5186 */
5187 walk_page_range(0, ~0UL, &mem_cgroup_move_charge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005188 up_read(&mm->mmap_sem);
Johannes Weiner312722c2014-12-10 15:44:25 -08005189 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005190}
5191
Tejun Heoeb954192013-08-08 20:11:23 -04005192static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005193 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005194{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005195 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005196 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005197
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005198 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005199 if (mc.to)
5200 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005201 mmput(mm);
5202 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005203 if (mc.to)
5204 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005205}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005206#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04005207static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005208 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005209{
5210 return 0;
5211}
Tejun Heoeb954192013-08-08 20:11:23 -04005212static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005213 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005214{
5215}
Tejun Heoeb954192013-08-08 20:11:23 -04005216static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005217 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005218{
5219}
5220#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005221
Tejun Heof00baae2013-04-15 13:41:15 -07005222/*
5223 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04005224 * to verify whether we're attached to the default hierarchy on each mount
5225 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07005226 */
Tejun Heoeb954192013-08-08 20:11:23 -04005227static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07005228{
5229 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04005230 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07005231 * guarantees that @root doesn't have any children, so turning it
5232 * on for the root memcg is enough.
5233 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04005234 if (cgroup_on_dfl(root_css->cgroup))
Tejun Heoeb954192013-08-08 20:11:23 -04005235 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07005236}
5237
Johannes Weiner241994e2015-02-11 15:26:06 -08005238static u64 memory_current_read(struct cgroup_subsys_state *css,
5239 struct cftype *cft)
5240{
5241 return mem_cgroup_usage(mem_cgroup_from_css(css), false);
5242}
5243
5244static int memory_low_show(struct seq_file *m, void *v)
5245{
5246 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5247 unsigned long low = ACCESS_ONCE(memcg->low);
5248
5249 if (low == PAGE_COUNTER_MAX)
5250 seq_puts(m, "infinity\n");
5251 else
5252 seq_printf(m, "%llu\n", (u64)low * PAGE_SIZE);
5253
5254 return 0;
5255}
5256
5257static ssize_t memory_low_write(struct kernfs_open_file *of,
5258 char *buf, size_t nbytes, loff_t off)
5259{
5260 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5261 unsigned long low;
5262 int err;
5263
5264 buf = strstrip(buf);
5265 err = page_counter_memparse(buf, "infinity", &low);
5266 if (err)
5267 return err;
5268
5269 memcg->low = low;
5270
5271 return nbytes;
5272}
5273
5274static int memory_high_show(struct seq_file *m, void *v)
5275{
5276 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5277 unsigned long high = ACCESS_ONCE(memcg->high);
5278
5279 if (high == PAGE_COUNTER_MAX)
5280 seq_puts(m, "infinity\n");
5281 else
5282 seq_printf(m, "%llu\n", (u64)high * PAGE_SIZE);
5283
5284 return 0;
5285}
5286
5287static ssize_t memory_high_write(struct kernfs_open_file *of,
5288 char *buf, size_t nbytes, loff_t off)
5289{
5290 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5291 unsigned long high;
5292 int err;
5293
5294 buf = strstrip(buf);
5295 err = page_counter_memparse(buf, "infinity", &high);
5296 if (err)
5297 return err;
5298
5299 memcg->high = high;
5300
5301 return nbytes;
5302}
5303
5304static int memory_max_show(struct seq_file *m, void *v)
5305{
5306 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5307 unsigned long max = ACCESS_ONCE(memcg->memory.limit);
5308
5309 if (max == PAGE_COUNTER_MAX)
5310 seq_puts(m, "infinity\n");
5311 else
5312 seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE);
5313
5314 return 0;
5315}
5316
5317static ssize_t memory_max_write(struct kernfs_open_file *of,
5318 char *buf, size_t nbytes, loff_t off)
5319{
5320 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5321 unsigned long max;
5322 int err;
5323
5324 buf = strstrip(buf);
5325 err = page_counter_memparse(buf, "infinity", &max);
5326 if (err)
5327 return err;
5328
5329 err = mem_cgroup_resize_limit(memcg, max);
5330 if (err)
5331 return err;
5332
5333 return nbytes;
5334}
5335
5336static int memory_events_show(struct seq_file *m, void *v)
5337{
5338 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5339
5340 seq_printf(m, "low %lu\n", mem_cgroup_read_events(memcg, MEMCG_LOW));
5341 seq_printf(m, "high %lu\n", mem_cgroup_read_events(memcg, MEMCG_HIGH));
5342 seq_printf(m, "max %lu\n", mem_cgroup_read_events(memcg, MEMCG_MAX));
5343 seq_printf(m, "oom %lu\n", mem_cgroup_read_events(memcg, MEMCG_OOM));
5344
5345 return 0;
5346}
5347
5348static struct cftype memory_files[] = {
5349 {
5350 .name = "current",
5351 .read_u64 = memory_current_read,
5352 },
5353 {
5354 .name = "low",
5355 .flags = CFTYPE_NOT_ON_ROOT,
5356 .seq_show = memory_low_show,
5357 .write = memory_low_write,
5358 },
5359 {
5360 .name = "high",
5361 .flags = CFTYPE_NOT_ON_ROOT,
5362 .seq_show = memory_high_show,
5363 .write = memory_high_write,
5364 },
5365 {
5366 .name = "max",
5367 .flags = CFTYPE_NOT_ON_ROOT,
5368 .seq_show = memory_max_show,
5369 .write = memory_max_write,
5370 },
5371 {
5372 .name = "events",
5373 .flags = CFTYPE_NOT_ON_ROOT,
5374 .seq_show = memory_events_show,
5375 },
5376 { } /* terminate */
5377};
5378
Tejun Heo073219e2014-02-08 10:36:58 -05005379struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005380 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08005381 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08005382 .css_offline = mem_cgroup_css_offline,
5383 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04005384 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005385 .can_attach = mem_cgroup_can_attach,
5386 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005387 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07005388 .bind = mem_cgroup_bind,
Johannes Weiner241994e2015-02-11 15:26:06 -08005389 .dfl_cftypes = memory_files,
5390 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005391 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005392};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005393
Johannes Weiner241994e2015-02-11 15:26:06 -08005394/**
5395 * mem_cgroup_events - count memory events against a cgroup
5396 * @memcg: the memory cgroup
5397 * @idx: the event index
5398 * @nr: the number of events to account for
5399 */
5400void mem_cgroup_events(struct mem_cgroup *memcg,
5401 enum mem_cgroup_events_index idx,
5402 unsigned int nr)
5403{
5404 this_cpu_add(memcg->stat->events[idx], nr);
5405}
5406
5407/**
5408 * mem_cgroup_low - check if memory consumption is below the normal range
5409 * @root: the highest ancestor to consider
5410 * @memcg: the memory cgroup to check
5411 *
5412 * Returns %true if memory consumption of @memcg, and that of all
5413 * configurable ancestors up to @root, is below the normal range.
5414 */
5415bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg)
5416{
5417 if (mem_cgroup_disabled())
5418 return false;
5419
5420 /*
5421 * The toplevel group doesn't have a configurable range, so
5422 * it's never low when looked at directly, and it is not
5423 * considered an ancestor when assessing the hierarchy.
5424 */
5425
5426 if (memcg == root_mem_cgroup)
5427 return false;
5428
5429 if (page_counter_read(&memcg->memory) > memcg->low)
5430 return false;
5431
5432 while (memcg != root) {
5433 memcg = parent_mem_cgroup(memcg);
5434
5435 if (memcg == root_mem_cgroup)
5436 break;
5437
5438 if (page_counter_read(&memcg->memory) > memcg->low)
5439 return false;
5440 }
5441 return true;
5442}
5443
Johannes Weiner00501b52014-08-08 14:19:20 -07005444/**
5445 * mem_cgroup_try_charge - try charging a page
5446 * @page: page to charge
5447 * @mm: mm context of the victim
5448 * @gfp_mask: reclaim mode
5449 * @memcgp: charged memcg return
5450 *
5451 * Try to charge @page to the memcg that @mm belongs to, reclaiming
5452 * pages according to @gfp_mask if necessary.
5453 *
5454 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
5455 * Otherwise, an error code is returned.
5456 *
5457 * After page->mapping has been set up, the caller must finalize the
5458 * charge with mem_cgroup_commit_charge(). Or abort the transaction
5459 * with mem_cgroup_cancel_charge() in case page instantiation fails.
5460 */
5461int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
5462 gfp_t gfp_mask, struct mem_cgroup **memcgp)
5463{
5464 struct mem_cgroup *memcg = NULL;
5465 unsigned int nr_pages = 1;
5466 int ret = 0;
5467
5468 if (mem_cgroup_disabled())
5469 goto out;
5470
5471 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005472 /*
5473 * Every swap fault against a single page tries to charge the
5474 * page, bail as early as possible. shmem_unuse() encounters
5475 * already charged pages, too. The USED bit is protected by
5476 * the page lock, which serializes swap cache removal, which
5477 * in turn serializes uncharging.
5478 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005479 if (page->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07005480 goto out;
5481 }
5482
5483 if (PageTransHuge(page)) {
5484 nr_pages <<= compound_order(page);
5485 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5486 }
5487
5488 if (do_swap_account && PageSwapCache(page))
5489 memcg = try_get_mem_cgroup_from_page(page);
5490 if (!memcg)
5491 memcg = get_mem_cgroup_from_mm(mm);
5492
5493 ret = try_charge(memcg, gfp_mask, nr_pages);
5494
5495 css_put(&memcg->css);
5496
5497 if (ret == -EINTR) {
5498 memcg = root_mem_cgroup;
5499 ret = 0;
5500 }
5501out:
5502 *memcgp = memcg;
5503 return ret;
5504}
5505
5506/**
5507 * mem_cgroup_commit_charge - commit a page charge
5508 * @page: page to charge
5509 * @memcg: memcg to charge the page to
5510 * @lrucare: page might be on LRU already
5511 *
5512 * Finalize a charge transaction started by mem_cgroup_try_charge(),
5513 * after page->mapping has been set up. This must happen atomically
5514 * as part of the page instantiation, i.e. under the page table lock
5515 * for anonymous pages, under the page lock for page and swap cache.
5516 *
5517 * In addition, the page must not be on the LRU during the commit, to
5518 * prevent racing with task migration. If it might be, use @lrucare.
5519 *
5520 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
5521 */
5522void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
5523 bool lrucare)
5524{
5525 unsigned int nr_pages = 1;
5526
5527 VM_BUG_ON_PAGE(!page->mapping, page);
5528 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
5529
5530 if (mem_cgroup_disabled())
5531 return;
5532 /*
5533 * Swap faults will attempt to charge the same page multiple
5534 * times. But reuse_swap_page() might have removed the page
5535 * from swapcache already, so we can't check PageSwapCache().
5536 */
5537 if (!memcg)
5538 return;
5539
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005540 commit_charge(page, memcg, lrucare);
5541
Johannes Weiner00501b52014-08-08 14:19:20 -07005542 if (PageTransHuge(page)) {
5543 nr_pages <<= compound_order(page);
5544 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5545 }
5546
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005547 local_irq_disable();
5548 mem_cgroup_charge_statistics(memcg, page, nr_pages);
5549 memcg_check_events(memcg, page);
5550 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07005551
5552 if (do_swap_account && PageSwapCache(page)) {
5553 swp_entry_t entry = { .val = page_private(page) };
5554 /*
5555 * The swap entry might not get freed for a long time,
5556 * let's not wait for it. The page already received a
5557 * memory+swap charge, drop the swap entry duplicate.
5558 */
5559 mem_cgroup_uncharge_swap(entry);
5560 }
5561}
5562
5563/**
5564 * mem_cgroup_cancel_charge - cancel a page charge
5565 * @page: page to charge
5566 * @memcg: memcg to charge the page to
5567 *
5568 * Cancel a charge transaction started by mem_cgroup_try_charge().
5569 */
5570void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg)
5571{
5572 unsigned int nr_pages = 1;
5573
5574 if (mem_cgroup_disabled())
5575 return;
5576 /*
5577 * Swap faults will attempt to charge the same page multiple
5578 * times. But reuse_swap_page() might have removed the page
5579 * from swapcache already, so we can't check PageSwapCache().
5580 */
5581 if (!memcg)
5582 return;
5583
5584 if (PageTransHuge(page)) {
5585 nr_pages <<= compound_order(page);
5586 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5587 }
5588
5589 cancel_charge(memcg, nr_pages);
5590}
5591
Johannes Weiner747db952014-08-08 14:19:24 -07005592static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
Johannes Weiner747db952014-08-08 14:19:24 -07005593 unsigned long nr_anon, unsigned long nr_file,
5594 unsigned long nr_huge, struct page *dummy_page)
5595{
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005596 unsigned long nr_pages = nr_anon + nr_file;
Johannes Weiner747db952014-08-08 14:19:24 -07005597 unsigned long flags;
5598
Johannes Weinerce00a962014-09-05 08:43:57 -04005599 if (!mem_cgroup_is_root(memcg)) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005600 page_counter_uncharge(&memcg->memory, nr_pages);
5601 if (do_swap_account)
5602 page_counter_uncharge(&memcg->memsw, nr_pages);
Johannes Weinerce00a962014-09-05 08:43:57 -04005603 memcg_oom_recover(memcg);
5604 }
Johannes Weiner747db952014-08-08 14:19:24 -07005605
5606 local_irq_save(flags);
5607 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
5608 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
5609 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
5610 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005611 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005612 memcg_check_events(memcg, dummy_page);
5613 local_irq_restore(flags);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08005614
5615 if (!mem_cgroup_is_root(memcg))
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005616 css_put_many(&memcg->css, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005617}
5618
5619static void uncharge_list(struct list_head *page_list)
5620{
5621 struct mem_cgroup *memcg = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005622 unsigned long nr_anon = 0;
5623 unsigned long nr_file = 0;
5624 unsigned long nr_huge = 0;
5625 unsigned long pgpgout = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005626 struct list_head *next;
5627 struct page *page;
5628
5629 next = page_list->next;
5630 do {
5631 unsigned int nr_pages = 1;
Johannes Weiner747db952014-08-08 14:19:24 -07005632
5633 page = list_entry(next, struct page, lru);
5634 next = page->lru.next;
5635
5636 VM_BUG_ON_PAGE(PageLRU(page), page);
5637 VM_BUG_ON_PAGE(page_count(page), page);
5638
Johannes Weiner1306a852014-12-10 15:44:52 -08005639 if (!page->mem_cgroup)
Johannes Weiner747db952014-08-08 14:19:24 -07005640 continue;
5641
5642 /*
5643 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08005644 * page->mem_cgroup at this point, we have fully
Johannes Weiner29833312014-12-10 15:44:02 -08005645 * exclusive access to the page.
Johannes Weiner747db952014-08-08 14:19:24 -07005646 */
5647
Johannes Weiner1306a852014-12-10 15:44:52 -08005648 if (memcg != page->mem_cgroup) {
Johannes Weiner747db952014-08-08 14:19:24 -07005649 if (memcg) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005650 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5651 nr_huge, page);
5652 pgpgout = nr_anon = nr_file = nr_huge = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005653 }
Johannes Weiner1306a852014-12-10 15:44:52 -08005654 memcg = page->mem_cgroup;
Johannes Weiner747db952014-08-08 14:19:24 -07005655 }
5656
5657 if (PageTransHuge(page)) {
5658 nr_pages <<= compound_order(page);
5659 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5660 nr_huge += nr_pages;
5661 }
5662
5663 if (PageAnon(page))
5664 nr_anon += nr_pages;
5665 else
5666 nr_file += nr_pages;
5667
Johannes Weiner1306a852014-12-10 15:44:52 -08005668 page->mem_cgroup = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005669
5670 pgpgout++;
5671 } while (next != page_list);
5672
5673 if (memcg)
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005674 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5675 nr_huge, page);
Johannes Weiner747db952014-08-08 14:19:24 -07005676}
5677
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005678/**
5679 * mem_cgroup_uncharge - uncharge a page
5680 * @page: page to uncharge
5681 *
5682 * Uncharge a page previously charged with mem_cgroup_try_charge() and
5683 * mem_cgroup_commit_charge().
5684 */
5685void mem_cgroup_uncharge(struct page *page)
5686{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005687 if (mem_cgroup_disabled())
5688 return;
5689
Johannes Weiner747db952014-08-08 14:19:24 -07005690 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08005691 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005692 return;
5693
Johannes Weiner747db952014-08-08 14:19:24 -07005694 INIT_LIST_HEAD(&page->lru);
5695 uncharge_list(&page->lru);
5696}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005697
Johannes Weiner747db952014-08-08 14:19:24 -07005698/**
5699 * mem_cgroup_uncharge_list - uncharge a list of page
5700 * @page_list: list of pages to uncharge
5701 *
5702 * Uncharge a list of pages previously charged with
5703 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
5704 */
5705void mem_cgroup_uncharge_list(struct list_head *page_list)
5706{
5707 if (mem_cgroup_disabled())
5708 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005709
Johannes Weiner747db952014-08-08 14:19:24 -07005710 if (!list_empty(page_list))
5711 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005712}
5713
5714/**
5715 * mem_cgroup_migrate - migrate a charge to another page
5716 * @oldpage: currently charged page
5717 * @newpage: page to transfer the charge to
Michal Hockof5e03a42015-02-05 12:25:14 -08005718 * @lrucare: either or both pages might be on the LRU already
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005719 *
5720 * Migrate the charge from @oldpage to @newpage.
5721 *
5722 * Both pages must be locked, @newpage->mapping must be set up.
5723 */
5724void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
5725 bool lrucare)
5726{
Johannes Weiner29833312014-12-10 15:44:02 -08005727 struct mem_cgroup *memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005728 int isolated;
5729
5730 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
5731 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
5732 VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage);
5733 VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage);
5734 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005735 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
5736 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005737
5738 if (mem_cgroup_disabled())
5739 return;
5740
5741 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08005742 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005743 return;
5744
Johannes Weiner7d5e3242014-12-10 15:43:46 -08005745 /*
5746 * Swapcache readahead pages can get migrated before being
5747 * charged, and migration from compaction can happen to an
5748 * uncharged page when the PFN walker finds a page that
5749 * reclaim just put back on the LRU but has not released yet.
5750 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005751 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08005752 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005753 return;
5754
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005755 if (lrucare)
5756 lock_page_lru(oldpage, &isolated);
5757
Johannes Weiner1306a852014-12-10 15:44:52 -08005758 oldpage->mem_cgroup = NULL;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005759
5760 if (lrucare)
5761 unlock_page_lru(oldpage, isolated);
5762
Johannes Weiner29833312014-12-10 15:44:02 -08005763 commit_charge(newpage, memcg, lrucare);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005764}
5765
Michal Hocko2d110852013-02-22 16:34:43 -08005766/*
Michal Hocko10813122013-02-22 16:35:41 -08005767 * subsys_initcall() for memory controller.
5768 *
5769 * Some parts like hotcpu_notifier() have to be initialized from this context
5770 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
5771 * everything that doesn't depend on a specific mem_cgroup structure should
5772 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08005773 */
5774static int __init mem_cgroup_init(void)
5775{
Johannes Weiner95a045f2015-02-11 15:26:33 -08005776 int cpu, node;
5777
Michal Hocko2d110852013-02-22 16:34:43 -08005778 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Johannes Weiner95a045f2015-02-11 15:26:33 -08005779
5780 for_each_possible_cpu(cpu)
5781 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
5782 drain_local_stock);
5783
5784 for_each_node(node) {
5785 struct mem_cgroup_tree_per_node *rtpn;
5786 int zone;
5787
5788 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
5789 node_online(node) ? node : NUMA_NO_NODE);
5790
5791 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5792 struct mem_cgroup_tree_per_zone *rtpz;
5793
5794 rtpz = &rtpn->rb_tree_per_zone[zone];
5795 rtpz->rb_root = RB_ROOT;
5796 spin_lock_init(&rtpz->lock);
5797 }
5798 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5799 }
5800
Michal Hocko2d110852013-02-22 16:34:43 -08005801 return 0;
5802}
5803subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08005804
5805#ifdef CONFIG_MEMCG_SWAP
5806/**
5807 * mem_cgroup_swapout - transfer a memsw charge to swap
5808 * @page: page whose memsw charge to transfer
5809 * @entry: swap entry to move the charge to
5810 *
5811 * Transfer the memsw charge of @page to @entry.
5812 */
5813void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
5814{
5815 struct mem_cgroup *memcg;
5816 unsigned short oldid;
5817
5818 VM_BUG_ON_PAGE(PageLRU(page), page);
5819 VM_BUG_ON_PAGE(page_count(page), page);
5820
5821 if (!do_swap_account)
5822 return;
5823
5824 memcg = page->mem_cgroup;
5825
5826 /* Readahead page, never charged */
5827 if (!memcg)
5828 return;
5829
5830 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg));
5831 VM_BUG_ON_PAGE(oldid, page);
5832 mem_cgroup_swap_statistics(memcg, true);
5833
5834 page->mem_cgroup = NULL;
5835
5836 if (!mem_cgroup_is_root(memcg))
5837 page_counter_uncharge(&memcg->memory, 1);
5838
5839 /* XXX: caller holds IRQ-safe mapping->tree_lock */
5840 VM_BUG_ON(!irqs_disabled());
5841
5842 mem_cgroup_charge_statistics(memcg, page, -1);
5843 memcg_check_events(memcg, page);
5844}
5845
5846/**
5847 * mem_cgroup_uncharge_swap - uncharge a swap entry
5848 * @entry: swap entry to uncharge
5849 *
5850 * Drop the memsw charge associated with @entry.
5851 */
5852void mem_cgroup_uncharge_swap(swp_entry_t entry)
5853{
5854 struct mem_cgroup *memcg;
5855 unsigned short id;
5856
5857 if (!do_swap_account)
5858 return;
5859
5860 id = swap_cgroup_record(entry, 0);
5861 rcu_read_lock();
5862 memcg = mem_cgroup_lookup(id);
5863 if (memcg) {
5864 if (!mem_cgroup_is_root(memcg))
5865 page_counter_uncharge(&memcg->memsw, 1);
5866 mem_cgroup_swap_statistics(memcg, false);
5867 css_put(&memcg->css);
5868 }
5869 rcu_read_unlock();
5870}
5871
5872/* for remember boot option*/
5873#ifdef CONFIG_MEMCG_SWAP_ENABLED
5874static int really_do_swap_account __initdata = 1;
5875#else
5876static int really_do_swap_account __initdata;
5877#endif
5878
5879static int __init enable_swap_account(char *s)
5880{
5881 if (!strcmp(s, "1"))
5882 really_do_swap_account = 1;
5883 else if (!strcmp(s, "0"))
5884 really_do_swap_account = 0;
5885 return 1;
5886}
5887__setup("swapaccount=", enable_swap_account);
5888
5889static struct cftype memsw_cgroup_files[] = {
5890 {
5891 .name = "memsw.usage_in_bytes",
5892 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5893 .read_u64 = mem_cgroup_read_u64,
5894 },
5895 {
5896 .name = "memsw.max_usage_in_bytes",
5897 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5898 .write = mem_cgroup_reset,
5899 .read_u64 = mem_cgroup_read_u64,
5900 },
5901 {
5902 .name = "memsw.limit_in_bytes",
5903 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5904 .write = mem_cgroup_write,
5905 .read_u64 = mem_cgroup_read_u64,
5906 },
5907 {
5908 .name = "memsw.failcnt",
5909 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5910 .write = mem_cgroup_reset,
5911 .read_u64 = mem_cgroup_read_u64,
5912 },
5913 { }, /* terminate */
5914};
5915
5916static int __init mem_cgroup_swap_init(void)
5917{
5918 if (!mem_cgroup_disabled() && really_do_swap_account) {
5919 do_swap_account = 1;
5920 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
5921 memsw_cgroup_files));
5922 }
5923 return 0;
5924}
5925subsys_initcall(mem_cgroup_swap_init);
5926
5927#endif /* CONFIG_MEMCG_SWAP */