blob: f90e43c1499f0c79b03e89af5979caee63b30011 [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>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070054#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080055#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070056#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070057#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050058#include <linux/file.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080059#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000060#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070061#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000062#include <net/tcp_memcontrol.h>
Qiang Huangf35c3a82013-11-12 15:08:22 -080063#include "slab.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080064
Balbir Singh8697d332008-02-07 00:13:59 -080065#include <asm/uaccess.h>
66
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070067#include <trace/events/vmscan.h>
68
Tejun Heo073219e2014-02-08 10:36:58 -050069struct cgroup_subsys memory_cgrp_subsys __read_mostly;
70EXPORT_SYMBOL(memory_cgrp_subsys);
David Rientjes68ae5642012-12-12 13:51:57 -080071
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070072#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070073static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080074
Andrew Mortonc255a452012-07-31 16:43:02 -070075#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070076/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080077int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080078
79/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070080#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080081static int really_do_swap_account __initdata = 1;
82#else
Fabian Frederickada4ba52014-06-04 16:08:08 -070083static int really_do_swap_account __initdata;
Michal Hockoa42c3902010-11-24 12:57:08 -080084#endif
85
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080086#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070087#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080088#endif
89
90
Johannes Weineraf7c4b02012-05-29 15:07:08 -070091static const char * const mem_cgroup_stat_names[] = {
92 "cache",
93 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070094 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070095 "mapped_file",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070096 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070097 "swap",
98};
99
Johannes Weinere9f89742011-03-23 16:42:37 -0700100enum mem_cgroup_events_index {
101 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
102 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700103 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
104 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700105 MEM_CGROUP_EVENTS_NSTATS,
106};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700107
108static const char * const mem_cgroup_events_names[] = {
109 "pgpgin",
110 "pgpgout",
111 "pgfault",
112 "pgmajfault",
113};
114
Sha Zhengju58cf1882013-02-22 16:32:05 -0800115static const char * const mem_cgroup_lru_names[] = {
116 "inactive_anon",
117 "active_anon",
118 "inactive_file",
119 "active_file",
120 "unevictable",
121};
122
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123/*
124 * Per memcg event counter is incremented at every pagein/pageout. With THP,
125 * it will be incremated by the number of pages. This counter is used for
126 * for trigger some periodic events. This is straightforward and better
127 * than using jiffies etc. to handle periodic memcg event.
128 */
129enum mem_cgroup_events_target {
130 MEM_CGROUP_TARGET_THRESH,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700131 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700132 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133 MEM_CGROUP_NTARGETS,
134};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700135#define THRESHOLDS_EVENTS_TARGET 128
136#define SOFTLIMIT_EVENTS_TARGET 1024
137#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700138
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800139struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700140 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700141 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700142 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700143 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800144};
145
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800146struct reclaim_iter {
147 struct mem_cgroup *position;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800148 /* scan generation, increased every round-trip */
149 unsigned int generation;
150};
151
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800152/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800153 * per-zone information in memory controller.
154 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800155struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800156 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700157 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800158
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800159 struct reclaim_iter iter[DEF_PRIORITY + 1];
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800160
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700161 struct rb_node tree_node; /* RB tree node */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800162 unsigned long usage_in_excess;/* Set to the value by which */
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700163 /* the soft limit is exceeded*/
164 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700165 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700166 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800167};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800168
169struct mem_cgroup_per_node {
170 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
171};
172
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700173/*
174 * Cgroups above their limits are maintained in a RB-Tree, independent of
175 * their hierarchy representation
176 */
177
178struct mem_cgroup_tree_per_zone {
179 struct rb_root rb_root;
180 spinlock_t lock;
181};
182
183struct mem_cgroup_tree_per_node {
184 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
185};
186
187struct mem_cgroup_tree {
188 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
189};
190
191static struct mem_cgroup_tree soft_limit_tree __read_mostly;
192
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800193struct mem_cgroup_threshold {
194 struct eventfd_ctx *eventfd;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800195 unsigned long threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800196};
197
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700198/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800199struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700200 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700201 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800202 /* Size of entries[] */
203 unsigned int size;
204 /* Array of thresholds */
205 struct mem_cgroup_threshold entries[0];
206};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700207
208struct mem_cgroup_thresholds {
209 /* Primary thresholds array */
210 struct mem_cgroup_threshold_ary *primary;
211 /*
212 * Spare threshold array.
213 * This is needed to make mem_cgroup_unregister_event() "never fail".
214 * It must be able to store at least primary->size - 1 entries.
215 */
216 struct mem_cgroup_threshold_ary *spare;
217};
218
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700219/* for OOM */
220struct mem_cgroup_eventfd_list {
221 struct list_head list;
222 struct eventfd_ctx *eventfd;
223};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800224
Tejun Heo79bd9812013-11-22 18:20:42 -0500225/*
226 * cgroup_event represents events which userspace want to receive.
227 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500228struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500229 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500230 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500231 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500232 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500233 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500234 * eventfd to signal userspace about the event.
235 */
236 struct eventfd_ctx *eventfd;
237 /*
238 * Each of these stored in a list by the cgroup.
239 */
240 struct list_head list;
241 /*
Tejun Heofba94802013-11-22 18:20:43 -0500242 * register_event() callback will be used to add new userspace
243 * waiter for changes related to this event. Use eventfd_signal()
244 * on eventfd to send notification to userspace.
245 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500246 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500247 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500248 /*
249 * unregister_event() callback will be called when userspace closes
250 * the eventfd or on cgroup removing. This callback must be set,
251 * if you want provide notification functionality.
252 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500253 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500254 struct eventfd_ctx *eventfd);
255 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500256 * All fields below needed to unregister event when
257 * userspace closes eventfd.
258 */
259 poll_table pt;
260 wait_queue_head_t *wqh;
261 wait_queue_t wait;
262 struct work_struct remove;
263};
264
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700265static void mem_cgroup_threshold(struct mem_cgroup *memcg);
266static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800267
Balbir Singhf64c3f52009-09-23 15:56:37 -0700268/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800269 * The memory controller data structure. The memory controller controls both
270 * page cache and RSS per cgroup. We would eventually like to provide
271 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
272 * to help the administrator determine what knobs to tune.
273 *
274 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800275 * we hit the water mark. May be even add a low water mark, such that
276 * no reclaim occurs from a cgroup at it's low water mark, this is
277 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800278 */
279struct mem_cgroup {
280 struct cgroup_subsys_state css;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800281
282 /* Accounted resources */
283 struct page_counter memory;
284 struct page_counter memsw;
285 struct page_counter kmem;
286
287 unsigned long soft_limit;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700288
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700289 /* vmpressure notifications */
290 struct vmpressure vmpressure;
291
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -0700292 /* css_online() has been completed */
293 int initialized;
294
Li Zefan465939a2013-07-08 16:00:38 -0700295 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800296 * Should the accounting and control be hierarchical, per subtree?
297 */
298 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800299 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700300
301 bool oom_lock;
302 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700303 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700304
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700305 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700306 /* OOM-Killer disable */
307 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800308
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800309 /* protect arrays of thresholds */
310 struct mutex thresholds_lock;
311
312 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700313 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700314
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800315 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700316 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700317
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700318 /* For oom notifier event fd */
319 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700320
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800321 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800322 * Should we move charges of a task when a task is moved into this
323 * mem_cgroup ? And what type of charges should we move ?
324 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700325 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800326 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700327 * set > 0 if pages under this cgroup are moving to other cgroup.
328 */
329 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700330 /* taken only while moving_account > 0 */
331 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700332 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800333 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800334 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700335 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700336 /*
337 * used when a cpu is offlined or other synchronizations
338 * See mem_cgroup_read_stat().
339 */
340 struct mem_cgroup_stat_cpu nocpu_base;
341 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000342
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700343#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Eric W. Biederman2e685cad2013-10-19 16:26:19 -0700344 struct cg_proto tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000345#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800346#if defined(CONFIG_MEMCG_KMEM)
Vladimir Davydovbd673142014-06-04 16:07:40 -0700347 /* analogous to slab_common's slab_caches list, but per-memcg;
348 * protected by memcg_slab_mutex */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800349 struct list_head memcg_slab_caches;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800350 /* Index in the kmem_cache->memcg_params->memcg_caches array */
351 int kmemcg_id;
352#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800353
354 int last_scanned_node;
355#if MAX_NUMNODES > 1
356 nodemask_t scan_nodes;
357 atomic_t numainfo_events;
358 atomic_t numainfo_updating;
359#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700360
Tejun Heofba94802013-11-22 18:20:43 -0500361 /* List of events which userspace want to receive */
362 struct list_head event_list;
363 spinlock_t event_list_lock;
364
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700365 struct mem_cgroup_per_node *nodeinfo[0];
366 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800367};
368
Glauber Costa510fc4e2012-12-18 14:21:47 -0800369/* internal only representation about the status of kmem accounting. */
370enum {
Vladimir Davydov6de64be2014-01-23 15:53:08 -0800371 KMEM_ACCOUNTED_ACTIVE, /* accounted by this cgroup itself */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800372};
373
Glauber Costa510fc4e2012-12-18 14:21:47 -0800374#ifdef CONFIG_MEMCG_KMEM
375static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
376{
377 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
378}
Glauber Costa7de37682012-12-18 14:22:07 -0800379
380static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
381{
382 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
383}
384
Glauber Costa510fc4e2012-12-18 14:21:47 -0800385#endif
386
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800387/* Stuffs for move charges at task migration. */
388/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800389 * Types of charges to be moved. "move_charge_at_immitgrate" and
390 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800391 */
392enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800393 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700394 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800395 NR_MOVE_TYPE,
396};
397
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800398/* "mc" and its members are protected by cgroup_mutex */
399static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800400 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800401 struct mem_cgroup *from;
402 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800403 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800404 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800405 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800406 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800407 struct task_struct *moving_task; /* a task moving charges */
408 wait_queue_head_t waitq; /* a waitq for other context */
409} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700410 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800411 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
412};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800413
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700414static bool move_anon(void)
415{
Glauber Costaee5e8472013-02-22 16:34:50 -0800416 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700417}
418
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700419static bool move_file(void)
420{
Glauber Costaee5e8472013-02-22 16:34:50 -0800421 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700422}
423
Balbir Singh4e416952009-09-23 15:56:39 -0700424/*
425 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
426 * limit reclaim to prevent infinite loops, if they ever occur.
427 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700428#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700429#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700430
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800431enum charge_type {
432 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700433 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800434 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700435 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700436 NR_CHARGE_TYPE,
437};
438
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800439/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800440enum res_type {
441 _MEM,
442 _MEMSWAP,
443 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800444 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800445};
446
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700447#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
448#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800449#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700450/* Used for OOM nofiier */
451#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800452
Balbir Singh75822b42009-09-23 15:56:38 -0700453/*
Glauber Costa09998212013-02-22 16:34:55 -0800454 * The memcg_create_mutex will be held whenever a new cgroup is created.
455 * As a consequence, any change that needs to protect against new child cgroups
456 * appearing has to hold it as well.
457 */
458static DEFINE_MUTEX(memcg_create_mutex);
459
Wanpeng Lib2145142012-07-31 16:46:01 -0700460struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
461{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400462 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700463}
464
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700465/* Some nice accessors for the vmpressure. */
466struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
467{
468 if (!memcg)
469 memcg = root_mem_cgroup;
470 return &memcg->vmpressure;
471}
472
473struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
474{
475 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
476}
477
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700478static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
479{
480 return (memcg == root_mem_cgroup);
481}
482
Li Zefan4219b2d2013-09-23 16:56:29 +0800483/*
484 * We restrict the id in the range of [1, 65535], so it can fit into
485 * an unsigned short.
486 */
487#define MEM_CGROUP_ID_MAX USHRT_MAX
488
Li Zefan34c00c32013-09-23 16:56:01 +0800489static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
490{
Tejun Heo15a4c832014-05-04 15:09:14 -0400491 return memcg->css.id;
Li Zefan34c00c32013-09-23 16:56:01 +0800492}
493
494static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
495{
496 struct cgroup_subsys_state *css;
497
Tejun Heo7d699dd2014-05-04 15:09:13 -0400498 css = css_from_id(id, &memory_cgrp_subsys);
Li Zefan34c00c32013-09-23 16:56:01 +0800499 return mem_cgroup_from_css(css);
500}
501
Glauber Costae1aab162011-12-11 21:47:03 +0000502/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700503#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000504
Glauber Costae1aab162011-12-11 21:47:03 +0000505void sock_update_memcg(struct sock *sk)
506{
Glauber Costa376be5f2012-01-20 04:57:14 +0000507 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000508 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700509 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000510
511 BUG_ON(!sk->sk_prot->proto_cgroup);
512
Glauber Costaf3f511e2012-01-05 20:16:39 +0000513 /* Socket cloning can throw us here with sk_cgrp already
514 * filled. It won't however, necessarily happen from
515 * process context. So the test for root memcg given
516 * the current task's memcg won't help us in this case.
517 *
518 * Respecting the original socket's memcg is a better
519 * decision in this case.
520 */
521 if (sk->sk_cgrp) {
522 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700523 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000524 return;
525 }
526
Glauber Costae1aab162011-12-11 21:47:03 +0000527 rcu_read_lock();
528 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700529 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700530 if (!mem_cgroup_is_root(memcg) &&
Tejun Heoec903c02014-05-13 12:11:01 -0400531 memcg_proto_active(cg_proto) &&
532 css_tryget_online(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700533 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000534 }
535 rcu_read_unlock();
536 }
537}
538EXPORT_SYMBOL(sock_update_memcg);
539
540void sock_release_memcg(struct sock *sk)
541{
Glauber Costa376be5f2012-01-20 04:57:14 +0000542 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000543 struct mem_cgroup *memcg;
544 WARN_ON(!sk->sk_cgrp->memcg);
545 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700546 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000547 }
548}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000549
550struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
551{
552 if (!memcg || mem_cgroup_is_root(memcg))
553 return NULL;
554
Eric W. Biederman2e685cad2013-10-19 16:26:19 -0700555 return &memcg->tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000556}
557EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000558
Glauber Costa3f134612012-05-29 15:07:11 -0700559static void disarm_sock_keys(struct mem_cgroup *memcg)
560{
Eric W. Biederman2e685cad2013-10-19 16:26:19 -0700561 if (!memcg_proto_activated(&memcg->tcp_mem))
Glauber Costa3f134612012-05-29 15:07:11 -0700562 return;
563 static_key_slow_dec(&memcg_socket_limit_enabled);
564}
565#else
566static void disarm_sock_keys(struct mem_cgroup *memcg)
567{
568}
569#endif
570
Glauber Costaa8964b92012-12-18 14:22:09 -0800571#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800572/*
573 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800574 * The main reason for not using cgroup id for this:
575 * this works better in sparse environments, where we have a lot of memcgs,
576 * but only a few kmem-limited. Or also, if we have, for instance, 200
577 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
578 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800579 *
580 * The current size of the caches array is stored in
581 * memcg_limited_groups_array_size. It will double each time we have to
582 * increase it.
583 */
584static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800585int memcg_limited_groups_array_size;
586
Glauber Costa55007d82012-12-18 14:22:38 -0800587/*
588 * MIN_SIZE is different than 1, because we would like to avoid going through
589 * the alloc/free process all the time. In a small machine, 4 kmem-limited
590 * cgroups is a reasonable guess. In the future, it could be a parameter or
591 * tunable, but that is strictly not necessary.
592 *
Li Zefanb8627832013-09-23 16:56:47 +0800593 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800594 * this constant directly from cgroup, but it is understandable that this is
595 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800596 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800597 * increase ours as well if it increases.
598 */
599#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800600#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800601
Glauber Costad7f25f82012-12-18 14:22:40 -0800602/*
603 * A lot of the calls to the cache allocation functions are expected to be
604 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
605 * conditional to this static branch, we'll have to allow modules that does
606 * kmem_cache_alloc and the such to see this symbol as well
607 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800608struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800609EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800610
Vladimir Davydovf3bb3042014-10-09 15:28:45 -0700611static void memcg_free_cache_id(int id);
612
Glauber Costaa8964b92012-12-18 14:22:09 -0800613static void disarm_kmem_keys(struct mem_cgroup *memcg)
614{
Glauber Costa55007d82012-12-18 14:22:38 -0800615 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800616 static_key_slow_dec(&memcg_kmem_enabled_key);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -0700617 memcg_free_cache_id(memcg->kmemcg_id);
Glauber Costa55007d82012-12-18 14:22:38 -0800618 }
Glauber Costabea207c2012-12-18 14:22:11 -0800619 /*
620 * This check can't live in kmem destruction function,
621 * since the charges will outlive the cgroup
622 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800623 WARN_ON(page_counter_read(&memcg->kmem));
Glauber Costaa8964b92012-12-18 14:22:09 -0800624}
625#else
626static void disarm_kmem_keys(struct mem_cgroup *memcg)
627{
628}
629#endif /* CONFIG_MEMCG_KMEM */
630
631static void disarm_static_keys(struct mem_cgroup *memcg)
632{
633 disarm_sock_keys(memcg);
634 disarm_kmem_keys(memcg);
635}
636
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700637static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800638
Balbir Singhf64c3f52009-09-23 15:56:37 -0700639static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700640mem_cgroup_zone_zoneinfo(struct mem_cgroup *memcg, struct zone *zone)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700641{
Jianyu Zhane2318752014-06-06 14:38:20 -0700642 int nid = zone_to_nid(zone);
643 int zid = zone_idx(zone);
644
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700645 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700646}
647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700648struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100649{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700650 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100651}
652
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700654mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700655{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700656 int nid = page_to_nid(page);
657 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700658
Jianyu Zhane2318752014-06-06 14:38:20 -0700659 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700660}
661
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700662static struct mem_cgroup_tree_per_zone *
663soft_limit_tree_node_zone(int nid, int zid)
664{
665 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
666}
667
668static struct mem_cgroup_tree_per_zone *
669soft_limit_tree_from_page(struct page *page)
670{
671 int nid = page_to_nid(page);
672 int zid = page_zonenum(page);
673
674 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
675}
676
Johannes Weinercf2c8122014-06-06 14:38:21 -0700677static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz,
678 struct mem_cgroup_tree_per_zone *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800679 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700680{
681 struct rb_node **p = &mctz->rb_root.rb_node;
682 struct rb_node *parent = NULL;
683 struct mem_cgroup_per_zone *mz_node;
684
685 if (mz->on_tree)
686 return;
687
688 mz->usage_in_excess = new_usage_in_excess;
689 if (!mz->usage_in_excess)
690 return;
691 while (*p) {
692 parent = *p;
693 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
694 tree_node);
695 if (mz->usage_in_excess < mz_node->usage_in_excess)
696 p = &(*p)->rb_left;
697 /*
698 * We can't avoid mem cgroups that are over their soft
699 * limit by the same amount
700 */
701 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
702 p = &(*p)->rb_right;
703 }
704 rb_link_node(&mz->tree_node, parent, p);
705 rb_insert_color(&mz->tree_node, &mctz->rb_root);
706 mz->on_tree = true;
707}
708
Johannes Weinercf2c8122014-06-06 14:38:21 -0700709static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
710 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700711{
712 if (!mz->on_tree)
713 return;
714 rb_erase(&mz->tree_node, &mctz->rb_root);
715 mz->on_tree = false;
716}
717
Johannes Weinercf2c8122014-06-06 14:38:21 -0700718static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
719 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700720{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700721 unsigned long flags;
722
723 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700724 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700725 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700726}
727
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800728static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
729{
730 unsigned long nr_pages = page_counter_read(&memcg->memory);
731 unsigned long soft_limit = ACCESS_ONCE(memcg->soft_limit);
732 unsigned long excess = 0;
733
734 if (nr_pages > soft_limit)
735 excess = nr_pages - soft_limit;
736
737 return excess;
738}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700739
740static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
741{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800742 unsigned long excess;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700743 struct mem_cgroup_per_zone *mz;
744 struct mem_cgroup_tree_per_zone *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700745
Jianyu Zhane2318752014-06-06 14:38:20 -0700746 mctz = soft_limit_tree_from_page(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700747 /*
748 * Necessary to update all ancestors when hierarchy is used.
749 * because their event counter is not touched.
750 */
751 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Jianyu Zhane2318752014-06-06 14:38:20 -0700752 mz = mem_cgroup_page_zoneinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800753 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700754 /*
755 * We have to update the tree if mz is on RB-tree or
756 * mem is over its softlimit.
757 */
758 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700759 unsigned long flags;
760
761 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700762 /* if on-tree, remove it */
763 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700764 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700765 /*
766 * Insert again. mz->usage_in_excess will be updated.
767 * If excess is 0, no tree ops.
768 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700769 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700770 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700771 }
772 }
773}
774
775static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
776{
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700777 struct mem_cgroup_tree_per_zone *mctz;
Jianyu Zhane2318752014-06-06 14:38:20 -0700778 struct mem_cgroup_per_zone *mz;
779 int nid, zid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700780
Jianyu Zhane2318752014-06-06 14:38:20 -0700781 for_each_node(nid) {
782 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
783 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
784 mctz = soft_limit_tree_node_zone(nid, zid);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700785 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700786 }
787 }
788}
789
790static struct mem_cgroup_per_zone *
791__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
792{
793 struct rb_node *rightmost = NULL;
794 struct mem_cgroup_per_zone *mz;
795
796retry:
797 mz = NULL;
798 rightmost = rb_last(&mctz->rb_root);
799 if (!rightmost)
800 goto done; /* Nothing to reclaim from */
801
802 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
803 /*
804 * Remove the node now but someone else can add it back,
805 * we will to add it back at the end of reclaim to its correct
806 * position in the tree.
807 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700808 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800809 if (!soft_limit_excess(mz->memcg) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400810 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700811 goto retry;
812done:
813 return mz;
814}
815
816static struct mem_cgroup_per_zone *
817mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
818{
819 struct mem_cgroup_per_zone *mz;
820
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700821 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700822 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700823 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700824 return mz;
825}
826
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700827/*
828 * Implementation Note: reading percpu statistics for memcg.
829 *
830 * Both of vmstat[] and percpu_counter has threshold and do periodic
831 * synchronization to implement "quick" read. There are trade-off between
832 * reading cost and precision of value. Then, we may have a chance to implement
833 * a periodic synchronizion of counter in memcg's counter.
834 *
835 * But this _read() function is used for user interface now. The user accounts
836 * memory usage by memory cgroup and he _always_ requires exact value because
837 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
838 * have to visit all online cpus and make sum. So, for now, unnecessary
839 * synchronization is not implemented. (just implemented for cpu hotplug)
840 *
841 * If there are kernel internal actions which can make use of some not-exact
842 * value, and reading all cpu value can be performance bottleneck in some
843 * common workload, threashold and synchonization as vmstat[] should be
844 * implemented.
845 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700846static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700847 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800848{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700849 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800850 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800851
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700852 get_online_cpus();
853 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700854 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700855#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700856 spin_lock(&memcg->pcp_counter_lock);
857 val += memcg->nocpu_base.count[idx];
858 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700859#endif
860 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800861 return val;
862}
863
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700864static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700865 enum mem_cgroup_events_index idx)
866{
867 unsigned long val = 0;
868 int cpu;
869
David Rientjes9c567512013-10-16 13:46:43 -0700870 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700871 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700872 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700873#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700874 spin_lock(&memcg->pcp_counter_lock);
875 val += memcg->nocpu_base.events[idx];
876 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700877#endif
David Rientjes9c567512013-10-16 13:46:43 -0700878 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700879 return val;
880}
881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700882static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700883 struct page *page,
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700884 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800885{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700886 /*
887 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
888 * counted as CACHE even if it's on ANON LRU.
889 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700890 if (PageAnon(page))
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700891 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700892 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800893 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700894 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700895 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700896
David Rientjesb070e652013-05-07 16:18:09 -0700897 if (PageTransHuge(page))
898 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
899 nr_pages);
900
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800901 /* pagein of a big page is an event. So, ignore page size */
902 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700903 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800904 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700905 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800906 nr_pages = -nr_pages; /* for event */
907 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800908
Johannes Weiner13114712012-05-29 15:07:07 -0700909 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800910}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800911
Jianyu Zhane2318752014-06-06 14:38:20 -0700912unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700913{
914 struct mem_cgroup_per_zone *mz;
915
916 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
917 return mz->lru_size[lru];
918}
919
Jianyu Zhane2318752014-06-06 14:38:20 -0700920static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
921 int nid,
922 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700923{
Jianyu Zhane2318752014-06-06 14:38:20 -0700924 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700925 int zid;
926
Jianyu Zhane2318752014-06-06 14:38:20 -0700927 VM_BUG_ON((unsigned)nid >= nr_node_ids);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700928
Jianyu Zhane2318752014-06-06 14:38:20 -0700929 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
930 struct mem_cgroup_per_zone *mz;
931 enum lru_list lru;
932
933 for_each_lru(lru) {
934 if (!(BIT(lru) & lru_mask))
935 continue;
936 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
937 nr += mz->lru_size[lru];
938 }
939 }
940 return nr;
Ying Han889976d2011-05-26 16:25:33 -0700941}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700942
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700943static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700944 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800945{
Jianyu Zhane2318752014-06-06 14:38:20 -0700946 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700947 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800948
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800949 for_each_node_state(nid, N_MEMORY)
Jianyu Zhane2318752014-06-06 14:38:20 -0700950 nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
951 return nr;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800952}
953
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800954static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
955 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800956{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700957 unsigned long val, next;
958
Johannes Weiner13114712012-05-29 15:07:07 -0700959 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700960 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700961 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800962 if ((long)next - (long)val < 0) {
963 switch (target) {
964 case MEM_CGROUP_TARGET_THRESH:
965 next = val + THRESHOLDS_EVENTS_TARGET;
966 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700967 case MEM_CGROUP_TARGET_SOFTLIMIT:
968 next = val + SOFTLIMIT_EVENTS_TARGET;
969 break;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800970 case MEM_CGROUP_TARGET_NUMAINFO:
971 next = val + NUMAINFO_EVENTS_TARGET;
972 break;
973 default:
974 break;
975 }
976 __this_cpu_write(memcg->stat->targets[target], next);
977 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700978 }
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800979 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800980}
981
982/*
983 * Check events in order.
984 *
985 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700986static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800987{
988 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800989 if (unlikely(mem_cgroup_event_ratelimit(memcg,
990 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700991 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800992 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800993
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700994 do_softlimit = mem_cgroup_event_ratelimit(memcg,
995 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700996#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800997 do_numainfo = mem_cgroup_event_ratelimit(memcg,
998 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700999#endif
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001000 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001001 if (unlikely(do_softlimit))
1002 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001003#if MAX_NUMNODES > 1
1004 if (unlikely(do_numainfo))
1005 atomic_inc(&memcg->numainfo_events);
1006#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001007 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001008}
1009
Balbir Singhcf475ad2008-04-29 01:00:16 -07001010struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001011{
Balbir Singh31a78f22008-09-28 23:09:31 +01001012 /*
1013 * mm_update_next_owner() may clear mm->owner to NULL
1014 * if it races with swapoff, page migration, etc.
1015 * So this can be called with p == NULL.
1016 */
1017 if (unlikely(!p))
1018 return NULL;
1019
Tejun Heo073219e2014-02-08 10:36:58 -05001020 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001021}
1022
Johannes Weinerdf381972014-04-07 15:37:43 -07001023static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001024{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001025 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001026
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001027 rcu_read_lock();
1028 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -07001029 /*
1030 * Page cache insertions can happen withou an
1031 * actual mm context, e.g. during disk probing
1032 * on boot, loopback IO, acct() writes etc.
1033 */
1034 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -07001035 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -07001036 else {
1037 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1038 if (unlikely(!memcg))
1039 memcg = root_mem_cgroup;
1040 }
Tejun Heoec903c02014-05-13 12:11:01 -04001041 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001042 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001043 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001044}
1045
Johannes Weiner56600482012-01-12 17:17:59 -08001046/**
1047 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1048 * @root: hierarchy root
1049 * @prev: previously returned memcg, NULL on first invocation
1050 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1051 *
1052 * Returns references to children of the hierarchy below @root, or
1053 * @root itself, or %NULL after a full round-trip.
1054 *
1055 * Caller must pass the return value in @prev on subsequent
1056 * invocations for reference counting, or use mem_cgroup_iter_break()
1057 * to cancel a hierarchy walk before the round-trip is complete.
1058 *
1059 * Reclaimers can specify a zone and a priority level in @reclaim to
1060 * divide up the memcgs in the hierarchy among all concurrent
1061 * reclaimers operating on the same zone and priority.
1062 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001063struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001064 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001065 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001066{
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001067 struct reclaim_iter *uninitialized_var(iter);
1068 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001069 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001070 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001071
Andrew Morton694fbc02013-09-24 15:27:37 -07001072 if (mem_cgroup_disabled())
1073 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001074
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001075 if (!root)
1076 root = root_mem_cgroup;
1077
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001078 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001079 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001080
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001081 if (!root->use_hierarchy && root != root_mem_cgroup) {
1082 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001083 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001084 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001085 }
1086
Michal Hocko542f85f2013-04-29 15:07:15 -07001087 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001088
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001089 if (reclaim) {
1090 struct mem_cgroup_per_zone *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001091
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001092 mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone);
1093 iter = &mz->iter[reclaim->priority];
Michal Hocko5f578162013-04-29 15:07:17 -07001094
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001095 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001096 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001097
1098 do {
1099 pos = ACCESS_ONCE(iter->position);
1100 /*
1101 * A racing update may change the position and
1102 * put the last reference, hence css_tryget(),
1103 * or retry to see the updated position.
1104 */
1105 } while (pos && !css_tryget(&pos->css));
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001106 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001107
1108 if (pos)
1109 css = &pos->css;
1110
1111 for (;;) {
1112 css = css_next_descendant_pre(css, &root->css);
1113 if (!css) {
1114 /*
1115 * Reclaimers share the hierarchy walk, and a
1116 * new one might jump in right at the end of
1117 * the hierarchy - make sure they see at least
1118 * one group and restart from the beginning.
1119 */
1120 if (!prev)
1121 continue;
1122 break;
1123 }
1124
1125 /*
1126 * Verify the css and acquire a reference. The root
1127 * is provided by the caller, so we know it's alive
1128 * and kicking, and don't take an extra reference.
1129 */
1130 memcg = mem_cgroup_from_css(css);
1131
1132 if (css == &root->css)
1133 break;
1134
Johannes Weinerb2052562014-12-10 15:42:48 -08001135 if (css_tryget(css)) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001136 /*
1137 * Make sure the memcg is initialized:
1138 * mem_cgroup_css_online() orders the the
1139 * initialization against setting the flag.
1140 */
1141 if (smp_load_acquire(&memcg->initialized))
1142 break;
1143
1144 css_put(css);
1145 }
1146
1147 memcg = NULL;
1148 }
1149
1150 if (reclaim) {
1151 if (cmpxchg(&iter->position, pos, memcg) == pos) {
1152 if (memcg)
1153 css_get(&memcg->css);
1154 if (pos)
1155 css_put(&pos->css);
1156 }
1157
1158 /*
1159 * pairs with css_tryget when dereferencing iter->position
1160 * above.
1161 */
1162 if (pos)
1163 css_put(&pos->css);
1164
1165 if (!memcg)
1166 iter->generation++;
1167 else if (!prev)
1168 reclaim->generation = iter->generation;
1169 }
1170
Michal Hocko542f85f2013-04-29 15:07:15 -07001171out_unlock:
1172 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001173out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001174 if (prev && prev != root)
1175 css_put(&prev->css);
1176
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001177 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001178}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001179
Johannes Weiner56600482012-01-12 17:17:59 -08001180/**
1181 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1182 * @root: hierarchy root
1183 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1184 */
1185void mem_cgroup_iter_break(struct mem_cgroup *root,
1186 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001187{
1188 if (!root)
1189 root = root_mem_cgroup;
1190 if (prev && prev != root)
1191 css_put(&prev->css);
1192}
1193
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001194/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001195 * Iteration constructs for visiting all cgroups (under a tree). If
1196 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1197 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001198 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001199#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001200 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001201 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001202 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001203
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001204#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001205 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001206 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001207 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001208
David Rientjes68ae5642012-12-12 13:51:57 -08001209void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001210{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001211 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001212
Ying Han456f9982011-05-26 16:25:38 -07001213 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001214 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1215 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001216 goto out;
1217
1218 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001219 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001220 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1221 break;
1222 case PGMAJFAULT:
1223 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001224 break;
1225 default:
1226 BUG();
1227 }
1228out:
1229 rcu_read_unlock();
1230}
David Rientjes68ae5642012-12-12 13:51:57 -08001231EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001232
Johannes Weiner925b7672012-01-12 17:18:15 -08001233/**
1234 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1235 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001236 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001237 *
1238 * Returns the lru list vector holding pages for the given @zone and
1239 * @mem. This can be the global zone lruvec, if the memory controller
1240 * is disabled.
1241 */
1242struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1243 struct mem_cgroup *memcg)
1244{
1245 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001246 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001247
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001248 if (mem_cgroup_disabled()) {
1249 lruvec = &zone->lruvec;
1250 goto out;
1251 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001252
Jianyu Zhane2318752014-06-06 14:38:20 -07001253 mz = mem_cgroup_zone_zoneinfo(memcg, zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001254 lruvec = &mz->lruvec;
1255out:
1256 /*
1257 * Since a node can be onlined after the mem_cgroup was created,
1258 * we have to be prepared to initialize lruvec->zone here;
1259 * and if offlined then reonlined, we need to reinitialize it.
1260 */
1261 if (unlikely(lruvec->zone != zone))
1262 lruvec->zone = zone;
1263 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001264}
1265
Johannes Weiner925b7672012-01-12 17:18:15 -08001266/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001267 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001268 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001269 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001270 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001271struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001272{
1273 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001274 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001275 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001276 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001277
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001278 if (mem_cgroup_disabled()) {
1279 lruvec = &zone->lruvec;
1280 goto out;
1281 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001282
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001283 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001284 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001285
1286 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001287 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001288 * an uncharged page off lru does nothing to secure
1289 * its former mem_cgroup from sudden removal.
1290 *
1291 * Our caller holds lru_lock, and PageCgroupUsed is updated
1292 * under page_cgroup lock: between them, they make all uses
1293 * of pc->mem_cgroup safe.
1294 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001295 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001296 pc->mem_cgroup = memcg = root_mem_cgroup;
1297
Jianyu Zhane2318752014-06-06 14:38:20 -07001298 mz = mem_cgroup_page_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001299 lruvec = &mz->lruvec;
1300out:
1301 /*
1302 * Since a node can be onlined after the mem_cgroup was created,
1303 * we have to be prepared to initialize lruvec->zone here;
1304 * and if offlined then reonlined, we need to reinitialize it.
1305 */
1306 if (unlikely(lruvec->zone != zone))
1307 lruvec->zone = zone;
1308 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001309}
1310
1311/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001312 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1313 * @lruvec: mem_cgroup per zone lru vector
1314 * @lru: index of lru list the page is sitting on
1315 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001316 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001317 * This function must be called when a page is added to or removed from an
1318 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001319 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001320void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1321 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001322{
1323 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001324 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001325
1326 if (mem_cgroup_disabled())
1327 return;
1328
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001329 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1330 lru_size = mz->lru_size + lru;
1331 *lru_size += nr_pages;
1332 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001333}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001334
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001335/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001336 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001337 * hierarchy subtree
1338 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001339bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1340 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001341{
Johannes Weiner91c637342012-05-29 15:06:24 -07001342 if (root_memcg == memcg)
1343 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001344 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001345 return false;
Li Zefanb47f77b2013-09-23 16:55:43 +08001346 return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001347}
1348
1349static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1350 struct mem_cgroup *memcg)
1351{
1352 bool ret;
1353
Johannes Weiner91c637342012-05-29 15:06:24 -07001354 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001355 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001356 rcu_read_unlock();
1357 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001358}
1359
David Rientjesffbdccf2013-07-03 15:01:23 -07001360bool task_in_mem_cgroup(struct task_struct *task,
1361 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001362{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001363 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001364 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001365 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001366
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001367 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001368 if (p) {
Johannes Weinerdf381972014-04-07 15:37:43 -07001369 curr = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001370 task_unlock(p);
1371 } else {
1372 /*
1373 * All threads may have already detached their mm's, but the oom
1374 * killer still needs to detect if they have already been oom
1375 * killed to prevent needlessly killing additional tasks.
1376 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001377 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001378 curr = mem_cgroup_from_task(task);
1379 if (curr)
1380 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001381 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001382 }
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001383 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001384 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001385 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001386 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1387 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001388 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001389 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001390 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001391 return ret;
1392}
1393
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001394int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001395{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001396 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001397 unsigned long inactive;
1398 unsigned long active;
1399 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001400
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001401 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1402 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001403
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001404 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1405 if (gb)
1406 inactive_ratio = int_sqrt(10 * gb);
1407 else
1408 inactive_ratio = 1;
1409
Johannes Weiner9b272972011-11-02 13:38:23 -07001410 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001411}
1412
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001413#define mem_cgroup_from_counter(counter, member) \
Balbir Singh6d61ef42009-01-07 18:08:06 -08001414 container_of(counter, struct mem_cgroup, member)
1415
Johannes Weiner19942822011-02-01 15:52:43 -08001416/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001417 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001418 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001419 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001420 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001421 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001422 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001423static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001424{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001425 unsigned long margin = 0;
1426 unsigned long count;
1427 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001428
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001429 count = page_counter_read(&memcg->memory);
1430 limit = ACCESS_ONCE(memcg->memory.limit);
1431 if (count < limit)
1432 margin = limit - count;
1433
1434 if (do_swap_account) {
1435 count = page_counter_read(&memcg->memsw);
1436 limit = ACCESS_ONCE(memcg->memsw.limit);
1437 if (count <= limit)
1438 margin = min(margin, limit - count);
1439 }
1440
1441 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001442}
1443
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001444int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001445{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001446 /* root ? */
Linus Torvalds14208b02014-06-09 15:03:33 -07001447 if (mem_cgroup_disabled() || !memcg->css.parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001448 return vm_swappiness;
1449
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001450 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001451}
1452
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001453/*
1454 * memcg->moving_account is used for checking possibility that some thread is
1455 * calling move_account(). When a thread on CPU-A starts moving pages under
1456 * a memcg, other threads should check memcg->moving_account under
1457 * rcu_read_lock(), like this:
1458 *
1459 * CPU-A CPU-B
1460 * rcu_read_lock()
1461 * memcg->moving_account+1 if (memcg->mocing_account)
1462 * take heavy locks.
1463 * synchronize_rcu() update something.
1464 * rcu_read_unlock()
1465 * start move here.
1466 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001467
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001468static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001469{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001470 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001471 synchronize_rcu();
1472}
1473
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001474static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001475{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001476 /*
1477 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1478 * We check NULL in callee rather than caller.
1479 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07001480 if (memcg)
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001481 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001482}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001483
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001484/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001485 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001486 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001487 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1488 * moving cgroups. This is for waiting at high-memory pressure
1489 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001490 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001491static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001492{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001493 struct mem_cgroup *from;
1494 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001495 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001496 /*
1497 * Unlike task_move routines, we access mc.to, mc.from not under
1498 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1499 */
1500 spin_lock(&mc.lock);
1501 from = mc.from;
1502 to = mc.to;
1503 if (!from)
1504 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001505
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001506 ret = mem_cgroup_same_or_subtree(memcg, from)
1507 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001508unlock:
1509 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001510 return ret;
1511}
1512
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001513static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001514{
1515 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001517 DEFINE_WAIT(wait);
1518 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1519 /* moving charge context might have finished. */
1520 if (mc.moving_task)
1521 schedule();
1522 finish_wait(&mc.waitq, &wait);
1523 return true;
1524 }
1525 }
1526 return false;
1527}
1528
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001529/*
1530 * Take this lock when
1531 * - a code tries to modify page's memcg while it's USED.
1532 * - a code tries to modify page state accounting in a memcg.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001533 */
1534static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1535 unsigned long *flags)
1536{
1537 spin_lock_irqsave(&memcg->move_lock, *flags);
1538}
1539
1540static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1541 unsigned long *flags)
1542{
1543 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1544}
1545
Sha Zhengju58cf1882013-02-22 16:32:05 -08001546#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001547/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001548 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001549 * @memcg: The memory cgroup that went over limit
1550 * @p: Task that is going to be killed
1551 *
1552 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1553 * enabled
1554 */
1555void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1556{
Tejun Heoe61734c2014-02-12 09:29:50 -05001557 /* oom_info_lock ensures that parallel ooms do not interleave */
Michal Hocko08088cb2014-02-25 15:01:44 -08001558 static DEFINE_MUTEX(oom_info_lock);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001559 struct mem_cgroup *iter;
1560 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001561
Sha Zhengju58cf1882013-02-22 16:32:05 -08001562 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001563 return;
1564
Michal Hocko08088cb2014-02-25 15:01:44 -08001565 mutex_lock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001566 rcu_read_lock();
1567
Tejun Heoe61734c2014-02-12 09:29:50 -05001568 pr_info("Task in ");
1569 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1570 pr_info(" killed as a result of limit of ");
1571 pr_cont_cgroup_path(memcg->css.cgroup);
1572 pr_info("\n");
Balbir Singhe2224322009-04-02 16:57:39 -07001573
Balbir Singhe2224322009-04-02 16:57:39 -07001574 rcu_read_unlock();
1575
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001576 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1577 K((u64)page_counter_read(&memcg->memory)),
1578 K((u64)memcg->memory.limit), memcg->memory.failcnt);
1579 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1580 K((u64)page_counter_read(&memcg->memsw)),
1581 K((u64)memcg->memsw.limit), memcg->memsw.failcnt);
1582 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1583 K((u64)page_counter_read(&memcg->kmem)),
1584 K((u64)memcg->kmem.limit), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001585
1586 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heoe61734c2014-02-12 09:29:50 -05001587 pr_info("Memory cgroup stats for ");
1588 pr_cont_cgroup_path(iter->css.cgroup);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001589 pr_cont(":");
1590
1591 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1592 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1593 continue;
1594 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1595 K(mem_cgroup_read_stat(iter, i)));
1596 }
1597
1598 for (i = 0; i < NR_LRU_LISTS; i++)
1599 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1600 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1601
1602 pr_cont("\n");
1603 }
Michal Hocko08088cb2014-02-25 15:01:44 -08001604 mutex_unlock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001605}
1606
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001607/*
1608 * This function returns the number of memcg under hierarchy tree. Returns
1609 * 1(self count) if no children.
1610 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001611static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001612{
1613 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001614 struct mem_cgroup *iter;
1615
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001616 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001617 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001618 return num;
1619}
1620
Balbir Singh6d61ef42009-01-07 18:08:06 -08001621/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001622 * Return the memory (and swap, if configured) limit for a memcg.
1623 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001624static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001625{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001626 unsigned long limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001627
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001628 limit = memcg->memory.limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001629 if (mem_cgroup_swappiness(memcg)) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001630 unsigned long memsw_limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001631
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001632 memsw_limit = memcg->memsw.limit;
1633 limit = min(limit + total_swap_pages, memsw_limit);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001634 }
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001635 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001636}
1637
David Rientjes19965462012-12-11 16:00:26 -08001638static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1639 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001640{
1641 struct mem_cgroup *iter;
1642 unsigned long chosen_points = 0;
1643 unsigned long totalpages;
1644 unsigned int points = 0;
1645 struct task_struct *chosen = NULL;
1646
David Rientjes876aafb2012-07-31 16:43:48 -07001647 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001648 * If current has a pending SIGKILL or is exiting, then automatically
1649 * select it. The goal is to allow it to allocate so that it may
1650 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001651 */
David Rientjes465adcf2013-04-29 15:08:45 -07001652 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001653 set_thread_flag(TIF_MEMDIE);
1654 return;
1655 }
1656
1657 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001658 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001659 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001660 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001661 struct task_struct *task;
1662
Tejun Heo72ec7022013-08-08 20:11:26 -04001663 css_task_iter_start(&iter->css, &it);
1664 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001665 switch (oom_scan_process_thread(task, totalpages, NULL,
1666 false)) {
1667 case OOM_SCAN_SELECT:
1668 if (chosen)
1669 put_task_struct(chosen);
1670 chosen = task;
1671 chosen_points = ULONG_MAX;
1672 get_task_struct(chosen);
1673 /* fall through */
1674 case OOM_SCAN_CONTINUE:
1675 continue;
1676 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001677 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001678 mem_cgroup_iter_break(memcg, iter);
1679 if (chosen)
1680 put_task_struct(chosen);
1681 return;
1682 case OOM_SCAN_OK:
1683 break;
1684 };
1685 points = oom_badness(task, memcg, NULL, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -08001686 if (!points || points < chosen_points)
1687 continue;
1688 /* Prefer thread group leaders for display purposes */
1689 if (points == chosen_points &&
1690 thread_group_leader(chosen))
1691 continue;
1692
1693 if (chosen)
1694 put_task_struct(chosen);
1695 chosen = task;
1696 chosen_points = points;
1697 get_task_struct(chosen);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001698 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001699 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001700 }
1701
1702 if (!chosen)
1703 return;
1704 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001705 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1706 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001707}
1708
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001709/**
1710 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001711 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001712 * @nid: the node ID to be checked.
1713 * @noswap : specify true here if the user wants flle only information.
1714 *
1715 * This function returns whether the specified memcg contains any
1716 * reclaimable pages on a node. Returns true if there are any reclaimable
1717 * pages in the node.
1718 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001719static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001720 int nid, bool noswap)
1721{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001722 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001723 return true;
1724 if (noswap || !total_swap_pages)
1725 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001726 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001727 return true;
1728 return false;
1729
1730}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001731#if MAX_NUMNODES > 1
Ying Han889976d2011-05-26 16:25:33 -07001732
1733/*
1734 * Always updating the nodemask is not very good - even if we have an empty
1735 * list or the wrong list here, we can start from some node and traverse all
1736 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1737 *
1738 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001739static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001740{
1741 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001742 /*
1743 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1744 * pagein/pageout changes since the last update.
1745 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001746 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001747 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001748 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001749 return;
1750
Ying Han889976d2011-05-26 16:25:33 -07001751 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001752 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001753
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001754 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001755
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001756 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1757 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001758 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001759
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001760 atomic_set(&memcg->numainfo_events, 0);
1761 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001762}
1763
1764/*
1765 * Selecting a node where we start reclaim from. Because what we need is just
1766 * reducing usage counter, start from anywhere is O,K. Considering
1767 * memory reclaim from current node, there are pros. and cons.
1768 *
1769 * Freeing memory from current node means freeing memory from a node which
1770 * we'll use or we've used. So, it may make LRU bad. And if several threads
1771 * hit limits, it will see a contention on a node. But freeing from remote
1772 * node means more costs for memory reclaim because of memory latency.
1773 *
1774 * Now, we use round-robin. Better algorithm is welcomed.
1775 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001776int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001777{
1778 int node;
1779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001780 mem_cgroup_may_update_nodemask(memcg);
1781 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001782
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001783 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001784 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001785 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001786 /*
1787 * We call this when we hit limit, not when pages are added to LRU.
1788 * No LRU may hold pages because all pages are UNEVICTABLE or
1789 * memcg is too small and all pages are not on LRU. In that case,
1790 * we use curret node.
1791 */
1792 if (unlikely(node == MAX_NUMNODES))
1793 node = numa_node_id();
1794
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001795 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001796 return node;
1797}
1798
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001799/*
1800 * Check all nodes whether it contains reclaimable pages or not.
1801 * For quick scan, we make use of scan_nodes. This will allow us to skip
1802 * unused nodes. But scan_nodes is lazily updated and may not cotain
1803 * enough new information. We need to do double check.
1804 */
1805static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1806{
1807 int nid;
1808
1809 /*
1810 * quick check...making use of scan_node.
1811 * We can skip unused nodes.
1812 */
1813 if (!nodes_empty(memcg->scan_nodes)) {
1814 for (nid = first_node(memcg->scan_nodes);
1815 nid < MAX_NUMNODES;
1816 nid = next_node(nid, memcg->scan_nodes)) {
1817
1818 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
1819 return true;
1820 }
1821 }
1822 /*
1823 * Check rest of nodes.
1824 */
1825 for_each_node_state(nid, N_MEMORY) {
1826 if (node_isset(nid, memcg->scan_nodes))
1827 continue;
1828 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
1829 return true;
1830 }
1831 return false;
1832}
1833
Ying Han889976d2011-05-26 16:25:33 -07001834#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001835int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001836{
1837 return 0;
1838}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001839
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07001840static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
1841{
1842 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
1843}
Ying Han889976d2011-05-26 16:25:33 -07001844#endif
1845
Andrew Morton0608f432013-09-24 15:27:41 -07001846static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1847 struct zone *zone,
1848 gfp_t gfp_mask,
1849 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001850{
Andrew Morton0608f432013-09-24 15:27:41 -07001851 struct mem_cgroup *victim = NULL;
1852 int total = 0;
1853 int loop = 0;
1854 unsigned long excess;
1855 unsigned long nr_scanned;
1856 struct mem_cgroup_reclaim_cookie reclaim = {
1857 .zone = zone,
1858 .priority = 0,
1859 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001860
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001861 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001862
Andrew Morton0608f432013-09-24 15:27:41 -07001863 while (1) {
1864 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1865 if (!victim) {
1866 loop++;
1867 if (loop >= 2) {
1868 /*
1869 * If we have not been able to reclaim
1870 * anything, it might because there are
1871 * no reclaimable pages under this hierarchy
1872 */
1873 if (!total)
1874 break;
1875 /*
1876 * We want to do more targeted reclaim.
1877 * excess >> 2 is not to excessive so as to
1878 * reclaim too much, nor too less that we keep
1879 * coming back to reclaim from this cgroup
1880 */
1881 if (total >= (excess >> 2) ||
1882 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1883 break;
1884 }
1885 continue;
1886 }
1887 if (!mem_cgroup_reclaimable(victim, false))
1888 continue;
1889 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1890 zone, &nr_scanned);
1891 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001892 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001893 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001894 }
Andrew Morton0608f432013-09-24 15:27:41 -07001895 mem_cgroup_iter_break(root_memcg, victim);
1896 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001897}
1898
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001899#ifdef CONFIG_LOCKDEP
1900static struct lockdep_map memcg_oom_lock_dep_map = {
1901 .name = "memcg_oom_lock",
1902};
1903#endif
1904
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001905static DEFINE_SPINLOCK(memcg_oom_lock);
1906
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001907/*
1908 * Check OOM-Killer is already running under our hierarchy.
1909 * If someone is running, return false.
1910 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001911static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001912{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001913 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001914
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001915 spin_lock(&memcg_oom_lock);
1916
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001917 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001918 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001919 /*
1920 * this subtree of our hierarchy is already locked
1921 * so we cannot give a lock.
1922 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001923 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001924 mem_cgroup_iter_break(memcg, iter);
1925 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001926 } else
1927 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001928 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001929
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001930 if (failed) {
1931 /*
1932 * OK, we failed to lock the whole subtree so we have
1933 * to clean up what we set up to the failing subtree
1934 */
1935 for_each_mem_cgroup_tree(iter, memcg) {
1936 if (iter == failed) {
1937 mem_cgroup_iter_break(memcg, iter);
1938 break;
1939 }
1940 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001941 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001942 } else
1943 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001944
1945 spin_unlock(&memcg_oom_lock);
1946
1947 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001948}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001949
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001950static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001951{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001952 struct mem_cgroup *iter;
1953
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001954 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001955 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001956 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001957 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001958 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001959}
1960
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001961static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001962{
1963 struct mem_cgroup *iter;
1964
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001965 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001966 atomic_inc(&iter->under_oom);
1967}
1968
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001969static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001970{
1971 struct mem_cgroup *iter;
1972
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001973 /*
1974 * When a new child is created while the hierarchy is under oom,
1975 * mem_cgroup_oom_lock() may not be called. We have to use
1976 * atomic_add_unless() here.
1977 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001978 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001979 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001980}
1981
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001982static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1983
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001984struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001985 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001986 wait_queue_t wait;
1987};
1988
1989static int memcg_oom_wake_function(wait_queue_t *wait,
1990 unsigned mode, int sync, void *arg)
1991{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001992 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1993 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001994 struct oom_wait_info *oom_wait_info;
1995
1996 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001997 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001998
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001999 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002000 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002001 * Then we can use css_is_ancestor without taking care of RCU.
2002 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002003 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2004 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002005 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002006 return autoremove_wake_function(wait, mode, sync, arg);
2007}
2008
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002009static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002010{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002011 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002012 /* for filtering, pass "memcg" as argument. */
2013 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002014}
2015
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002016static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002017{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018 if (memcg && atomic_read(&memcg->under_oom))
2019 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002020}
2021
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002022static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002023{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002024 if (!current->memcg_oom.may_oom)
2025 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002026 /*
Johannes Weiner49426422013-10-16 13:46:59 -07002027 * We are in the middle of the charge context here, so we
2028 * don't want to block when potentially sitting on a callstack
2029 * that holds all kinds of filesystem and mm locks.
2030 *
2031 * Also, the caller may handle a failed allocation gracefully
2032 * (like optional page cache readahead) and so an OOM killer
2033 * invocation might not even be necessary.
2034 *
2035 * That's why we don't do anything here except remember the
2036 * OOM context and then deal with it at the end of the page
2037 * fault when the stack is unwound, the locks are released,
2038 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002039 */
Johannes Weiner49426422013-10-16 13:46:59 -07002040 css_get(&memcg->css);
2041 current->memcg_oom.memcg = memcg;
2042 current->memcg_oom.gfp_mask = mask;
2043 current->memcg_oom.order = order;
2044}
2045
2046/**
2047 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2048 * @handle: actually kill/wait or just clean up the OOM state
2049 *
2050 * This has to be called at the end of a page fault if the memcg OOM
2051 * handler was enabled.
2052 *
2053 * Memcg supports userspace OOM handling where failed allocations must
2054 * sleep on a waitqueue until the userspace task resolves the
2055 * situation. Sleeping directly in the charge context with all kinds
2056 * of locks held is not a good idea, instead we remember an OOM state
2057 * in the task and mem_cgroup_oom_synchronize() has to be called at
2058 * the end of the page fault to complete the OOM handling.
2059 *
2060 * Returns %true if an ongoing memcg OOM situation was detected and
2061 * completed, %false otherwise.
2062 */
2063bool mem_cgroup_oom_synchronize(bool handle)
2064{
2065 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2066 struct oom_wait_info owait;
2067 bool locked;
2068
2069 /* OOM is global, do not handle */
2070 if (!memcg)
2071 return false;
2072
2073 if (!handle)
2074 goto cleanup;
2075
2076 owait.memcg = memcg;
2077 owait.wait.flags = 0;
2078 owait.wait.func = memcg_oom_wake_function;
2079 owait.wait.private = current;
2080 INIT_LIST_HEAD(&owait.wait.task_list);
2081
2082 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002083 mem_cgroup_mark_under_oom(memcg);
2084
2085 locked = mem_cgroup_oom_trylock(memcg);
2086
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002087 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002088 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002089
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002090 if (locked && !memcg->oom_kill_disable) {
2091 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07002092 finish_wait(&memcg_oom_waitq, &owait.wait);
2093 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2094 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002095 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002096 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07002097 mem_cgroup_unmark_under_oom(memcg);
2098 finish_wait(&memcg_oom_waitq, &owait.wait);
2099 }
2100
2101 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002102 mem_cgroup_oom_unlock(memcg);
2103 /*
2104 * There is no guarantee that an OOM-lock contender
2105 * sees the wakeups triggered by the OOM kill
2106 * uncharges. Wake any sleepers explicitely.
2107 */
2108 memcg_oom_recover(memcg);
2109 }
Johannes Weiner49426422013-10-16 13:46:59 -07002110cleanup:
2111 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002112 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002113 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002114}
2115
Johannes Weinerd7365e72014-10-29 14:50:48 -07002116/**
2117 * mem_cgroup_begin_page_stat - begin a page state statistics transaction
2118 * @page: page that is going to change accounted state
2119 * @locked: &memcg->move_lock slowpath was taken
2120 * @flags: IRQ-state flags for &memcg->move_lock
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002121 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07002122 * This function must mark the beginning of an accounted page state
2123 * change to prevent double accounting when the page is concurrently
2124 * being moved to another memcg:
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002125 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07002126 * memcg = mem_cgroup_begin_page_stat(page, &locked, &flags);
2127 * if (TestClearPageState(page))
2128 * mem_cgroup_update_page_stat(memcg, state, -1);
2129 * mem_cgroup_end_page_stat(memcg, locked, flags);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002130 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07002131 * The RCU lock is held throughout the transaction. The fast path can
2132 * get away without acquiring the memcg->move_lock (@locked is false)
2133 * because page moving starts with an RCU grace period.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002134 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07002135 * The RCU lock also protects the memcg from being freed when the page
2136 * state that is going to change is the only thing preventing the page
2137 * from being uncharged. E.g. end-writeback clearing PageWriteback(),
2138 * which allows migration to go ahead and uncharge the page before the
2139 * account transaction might be complete.
Balbir Singhd69b0422009-06-17 16:26:34 -07002140 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07002141struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page,
2142 bool *locked,
2143 unsigned long *flags)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002144{
2145 struct mem_cgroup *memcg;
2146 struct page_cgroup *pc;
2147
Johannes Weinerd7365e72014-10-29 14:50:48 -07002148 rcu_read_lock();
2149
2150 if (mem_cgroup_disabled())
2151 return NULL;
2152
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002153 pc = lookup_page_cgroup(page);
2154again:
2155 memcg = pc->mem_cgroup;
2156 if (unlikely(!memcg || !PageCgroupUsed(pc)))
Johannes Weinerd7365e72014-10-29 14:50:48 -07002157 return NULL;
2158
2159 *locked = false;
Qiang Huangbdcbb652014-06-04 16:08:21 -07002160 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weinerd7365e72014-10-29 14:50:48 -07002161 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002162
2163 move_lock_mem_cgroup(memcg, flags);
2164 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2165 move_unlock_mem_cgroup(memcg, flags);
2166 goto again;
2167 }
2168 *locked = true;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002169
2170 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002171}
2172
Johannes Weinerd7365e72014-10-29 14:50:48 -07002173/**
2174 * mem_cgroup_end_page_stat - finish a page state statistics transaction
2175 * @memcg: the memcg that was accounted against
2176 * @locked: value received from mem_cgroup_begin_page_stat()
2177 * @flags: value received from mem_cgroup_begin_page_stat()
2178 */
2179void mem_cgroup_end_page_stat(struct mem_cgroup *memcg, bool locked,
2180 unsigned long flags)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002181{
Johannes Weinerd7365e72014-10-29 14:50:48 -07002182 if (memcg && locked)
2183 move_unlock_mem_cgroup(memcg, &flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002184
Johannes Weinerd7365e72014-10-29 14:50:48 -07002185 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002186}
2187
Johannes Weinerd7365e72014-10-29 14:50:48 -07002188/**
2189 * mem_cgroup_update_page_stat - update page state statistics
2190 * @memcg: memcg to account against
2191 * @idx: page state item to account
2192 * @val: number of pages (positive or negative)
2193 *
2194 * See mem_cgroup_begin_page_stat() for locking requirements.
2195 */
2196void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
Sha Zhengju68b48762013-09-12 15:13:50 -07002197 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002198{
Sha Zhengju658b72c2013-09-12 15:13:52 -07002199 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002200
Johannes Weinerd7365e72014-10-29 14:50:48 -07002201 if (memcg)
2202 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002203}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002204
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002205/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206 * size of first charge trial. "32" comes from vmscan.c's magic value.
2207 * TODO: maybe necessary to use big numbers in big irons.
2208 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002209#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002210struct memcg_stock_pcp {
2211 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002212 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002213 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002214 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002215#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002216};
2217static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002218static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002219
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002220/**
2221 * consume_stock: Try to consume stocked charge on this cpu.
2222 * @memcg: memcg to consume from.
2223 * @nr_pages: how many pages to charge.
2224 *
2225 * The charges will only happen if @memcg matches the current cpu's memcg
2226 * stock, and at least @nr_pages are available in that stock. Failure to
2227 * service an allocation will refill the stock.
2228 *
2229 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002230 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002231static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002232{
2233 struct memcg_stock_pcp *stock;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002234 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002235
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002236 if (nr_pages > CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002237 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002238
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002239 stock = &get_cpu_var(memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002240 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002241 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002242 ret = true;
2243 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002244 put_cpu_var(memcg_stock);
2245 return ret;
2246}
2247
2248/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002249 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002250 */
2251static void drain_stock(struct memcg_stock_pcp *stock)
2252{
2253 struct mem_cgroup *old = stock->cached;
2254
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002255 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002256 page_counter_uncharge(&old->memory, stock->nr_pages);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002257 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002258 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002259 css_put_many(&old->css, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002260 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002261 }
2262 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002263}
2264
2265/*
2266 * This must be called under preempt disabled or must be called by
2267 * a thread which is pinned to local cpu.
2268 */
2269static void drain_local_stock(struct work_struct *dummy)
2270{
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002271 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002272 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002273 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002274}
2275
Michal Hockoe4777492013-02-22 16:35:40 -08002276static void __init memcg_stock_init(void)
2277{
2278 int cpu;
2279
2280 for_each_possible_cpu(cpu) {
2281 struct memcg_stock_pcp *stock =
2282 &per_cpu(memcg_stock, cpu);
2283 INIT_WORK(&stock->work, drain_local_stock);
2284 }
2285}
2286
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002287/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002288 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002289 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002290 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002291static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002292{
2293 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2294
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002295 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002296 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002297 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002298 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002299 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002300 put_cpu_var(memcg_stock);
2301}
2302
2303/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002304 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002305 * of the hierarchy under it. sync flag says whether we should block
2306 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002307 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002308static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002309{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002310 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002311
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002312 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002314 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002315 for_each_online_cpu(cpu) {
2316 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002317 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002318
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002319 memcg = stock->cached;
2320 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002321 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002322 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002323 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002324 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2325 if (cpu == curcpu)
2326 drain_local_stock(&stock->work);
2327 else
2328 schedule_work_on(cpu, &stock->work);
2329 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002330 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002331 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002332
2333 if (!sync)
2334 goto out;
2335
2336 for_each_online_cpu(cpu) {
2337 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002338 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002339 flush_work(&stock->work);
2340 }
2341out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002342 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002343}
2344
2345/*
2346 * Tries to drain stocked charges in other cpus. This function is asynchronous
2347 * and just put a work per cpu for draining localy on each cpu. Caller can
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002348 * expects some charges will be back later but cannot wait for it.
Michal Hockod38144b2011-07-26 16:08:28 -07002349 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002350static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002351{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002352 /*
2353 * If someone calls draining, avoid adding more kworker runs.
2354 */
2355 if (!mutex_trylock(&percpu_charge_mutex))
2356 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002357 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002358 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002359}
2360
2361/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002362static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002363{
2364 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002365 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002366 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002367 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002368}
2369
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002370/*
2371 * This function drains percpu counter value from DEAD cpu and
2372 * move it to local cpu. Note that this function can be preempted.
2373 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002375{
2376 int i;
2377
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002378 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002379 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002380 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002381
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002382 per_cpu(memcg->stat->count[i], cpu) = 0;
2383 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002384 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002385 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002387
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002388 per_cpu(memcg->stat->events[i], cpu) = 0;
2389 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002390 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002391 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002392}
2393
Paul Gortmaker0db06282013-06-19 14:53:51 -04002394static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002395 unsigned long action,
2396 void *hcpu)
2397{
2398 int cpu = (unsigned long)hcpu;
2399 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002400 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002401
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002402 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002403 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002404
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002405 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002406 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002407
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002408 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002409 mem_cgroup_drain_pcp_counter(iter, cpu);
2410
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002411 stock = &per_cpu(memcg_stock, cpu);
2412 drain_stock(stock);
2413 return NOTIFY_OK;
2414}
2415
Johannes Weiner00501b52014-08-08 14:19:20 -07002416static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2417 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002418{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002419 unsigned int batch = max(CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002420 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002421 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002422 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002423 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002424 bool may_swap = true;
2425 bool drained = false;
Johannes Weiner05b84302014-08-06 16:05:59 -07002426 int ret = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002427
Johannes Weinerce00a962014-09-05 08:43:57 -04002428 if (mem_cgroup_is_root(memcg))
2429 goto done;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002430retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002431 if (consume_stock(memcg, nr_pages))
2432 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002433
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002434 if (!do_swap_account ||
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002435 !page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2436 if (!page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002437 goto done_restock;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002438 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002439 page_counter_uncharge(&memcg->memsw, batch);
2440 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002441 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002442 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002443 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002444 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002445
Johannes Weiner6539cc02014-08-06 16:05:42 -07002446 if (batch > nr_pages) {
2447 batch = nr_pages;
2448 goto retry;
2449 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002450
Johannes Weiner06b078f2014-08-06 16:05:44 -07002451 /*
2452 * Unlike in global OOM situations, memcg is not in a physical
2453 * memory shortage. Allow dying and OOM-killed tasks to
2454 * bypass the last charges so that they can exit quickly and
2455 * free their memory.
2456 */
2457 if (unlikely(test_thread_flag(TIF_MEMDIE) ||
2458 fatal_signal_pending(current) ||
2459 current->flags & PF_EXITING))
2460 goto bypass;
2461
2462 if (unlikely(task_in_memcg_oom(current)))
2463 goto nomem;
2464
Johannes Weiner6539cc02014-08-06 16:05:42 -07002465 if (!(gfp_mask & __GFP_WAIT))
2466 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002467
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002468 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2469 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002470
Johannes Weiner61e02c72014-08-06 16:08:16 -07002471 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002472 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002473
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002474 if (!drained) {
2475 drain_all_stock_async(mem_over_limit);
2476 drained = true;
2477 goto retry;
2478 }
2479
Johannes Weiner28c34c22014-08-06 16:05:47 -07002480 if (gfp_mask & __GFP_NORETRY)
2481 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002482 /*
2483 * Even though the limit is exceeded at this point, reclaim
2484 * may have been able to free some pages. Retry the charge
2485 * before killing the task.
2486 *
2487 * Only for regular pages, though: huge pages are rather
2488 * unlikely to succeed so close to the limit, and we fall back
2489 * to regular pages anyway in case of failure.
2490 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002491 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002492 goto retry;
2493 /*
2494 * At task move, charge accounts can be doubly counted. So, it's
2495 * better to wait until the end of task_move if something is going on.
2496 */
2497 if (mem_cgroup_wait_acct_move(mem_over_limit))
2498 goto retry;
2499
Johannes Weiner9b130612014-08-06 16:05:51 -07002500 if (nr_retries--)
2501 goto retry;
2502
Johannes Weiner06b078f2014-08-06 16:05:44 -07002503 if (gfp_mask & __GFP_NOFAIL)
2504 goto bypass;
2505
Johannes Weiner6539cc02014-08-06 16:05:42 -07002506 if (fatal_signal_pending(current))
2507 goto bypass;
2508
Johannes Weiner61e02c72014-08-06 16:08:16 -07002509 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages));
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002510nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002511 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002512 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002513bypass:
Johannes Weinerce00a962014-09-05 08:43:57 -04002514 return -EINTR;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002515
2516done_restock:
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002517 css_get_many(&memcg->css, batch);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002518 if (batch > nr_pages)
2519 refill_stock(memcg, batch - nr_pages);
2520done:
Johannes Weiner05b84302014-08-06 16:05:59 -07002521 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002522}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002523
Johannes Weiner00501b52014-08-08 14:19:20 -07002524static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002525{
Johannes Weinerce00a962014-09-05 08:43:57 -04002526 if (mem_cgroup_is_root(memcg))
2527 return;
2528
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002529 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner05b84302014-08-06 16:05:59 -07002530 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002531 page_counter_uncharge(&memcg->memsw, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002532
2533 css_put_many(&memcg->css, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002534}
2535
2536/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002537 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoec903c02014-05-13 12:11:01 -04002538 * rcu_read_lock(). The caller is responsible for calling
2539 * css_tryget_online() if the mem_cgroup is used for charging. (dropping
2540 * refcnt from swap can be called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002541 */
2542static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2543{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002544 /* ID 0 is unused ID */
2545 if (!id)
2546 return NULL;
Li Zefan34c00c32013-09-23 16:56:01 +08002547 return mem_cgroup_from_id(id);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002548}
2549
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002550/*
2551 * try_get_mem_cgroup_from_page - look up page's memcg association
2552 * @page: the page
2553 *
2554 * Look up, get a css reference, and return the memcg that owns @page.
2555 *
2556 * The page must be locked to prevent racing with swap-in and page
2557 * cache charges. If coming from an unlocked page table, the caller
2558 * must ensure the page is on the LRU or this can race with charging.
2559 */
Wu Fengguange42d9d52009-12-16 12:19:59 +01002560struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002561{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002562 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002563 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002564 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002565 swp_entry_t ent;
2566
Sasha Levin309381fea2014-01-23 15:52:54 -08002567 VM_BUG_ON_PAGE(!PageLocked(page), page);
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002568
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002569 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002570 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 memcg = pc->mem_cgroup;
Tejun Heoec903c02014-05-13 12:11:01 -04002572 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002573 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002574 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002575 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002576 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002577 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002578 memcg = mem_cgroup_lookup(id);
Tejun Heoec903c02014-05-13 12:11:01 -04002579 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002580 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002581 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002582 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002583 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002584}
2585
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002586static void lock_page_lru(struct page *page, int *isolated)
2587{
2588 struct zone *zone = page_zone(page);
2589
2590 spin_lock_irq(&zone->lru_lock);
2591 if (PageLRU(page)) {
2592 struct lruvec *lruvec;
2593
2594 lruvec = mem_cgroup_page_lruvec(page, zone);
2595 ClearPageLRU(page);
2596 del_page_from_lru_list(page, lruvec, page_lru(page));
2597 *isolated = 1;
2598 } else
2599 *isolated = 0;
2600}
2601
2602static void unlock_page_lru(struct page *page, int isolated)
2603{
2604 struct zone *zone = page_zone(page);
2605
2606 if (isolated) {
2607 struct lruvec *lruvec;
2608
2609 lruvec = mem_cgroup_page_lruvec(page, zone);
2610 VM_BUG_ON_PAGE(PageLRU(page), page);
2611 SetPageLRU(page);
2612 add_page_to_lru_list(page, lruvec, page_lru(page));
2613 }
2614 spin_unlock_irq(&zone->lru_lock);
2615}
2616
Johannes Weiner00501b52014-08-08 14:19:20 -07002617static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002618 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002619{
Johannes Weinerce587e62012-04-24 20:22:33 +02002620 struct page_cgroup *pc = lookup_page_cgroup(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002621 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002622
Sasha Levin309381fea2014-01-23 15:52:54 -08002623 VM_BUG_ON_PAGE(PageCgroupUsed(pc), page);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002624 /*
2625 * we don't need page_cgroup_lock about tail pages, becase they are not
2626 * accessed by any other context at this point.
2627 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002628
2629 /*
2630 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2631 * may already be on some other mem_cgroup's LRU. Take care of it.
2632 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002633 if (lrucare)
2634 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002635
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002636 /*
2637 * Nobody should be changing or seriously looking at
2638 * pc->mem_cgroup and pc->flags at this point:
2639 *
2640 * - the page is uncharged
2641 *
2642 * - the page is off-LRU
2643 *
2644 * - an anonymous fault has exclusive page access, except for
2645 * a locked page table
2646 *
2647 * - a page cache insertion, a swapin fault, or a migration
2648 * have the page locked
2649 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002650 pc->mem_cgroup = memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002651 pc->flags = PCG_USED | PCG_MEM | (do_swap_account ? PCG_MEMSW : 0);
Hugh Dickins3be912772008-02-07 00:14:19 -08002652
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002653 if (lrucare)
2654 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002655}
2656
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002657#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovbd673142014-06-04 16:07:40 -07002658/*
2659 * The memcg_slab_mutex is held whenever a per memcg kmem cache is created or
2660 * destroyed. It protects memcg_caches arrays and memcg_slab_caches lists.
2661 */
2662static DEFINE_MUTEX(memcg_slab_mutex);
2663
Vladimir Davydovd6441632014-01-23 15:53:09 -08002664static DEFINE_MUTEX(activate_kmem_mutex);
2665
Glauber Costa1f458cb2012-12-18 14:22:50 -08002666/*
2667 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2668 * in the memcg_cache_params struct.
2669 */
2670static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2671{
2672 struct kmem_cache *cachep;
2673
2674 VM_BUG_ON(p->is_root_cache);
2675 cachep = p->root_cache;
Qiang Huang7a67d7a2013-11-12 15:08:24 -08002676 return cache_from_memcg_idx(cachep, memcg_cache_id(p->memcg));
Glauber Costa1f458cb2012-12-18 14:22:50 -08002677}
2678
Glauber Costa749c5412012-12-18 14:23:01 -08002679#ifdef CONFIG_SLABINFO
Tejun Heo2da8ca82013-12-05 12:28:04 -05002680static int mem_cgroup_slabinfo_read(struct seq_file *m, void *v)
Glauber Costa749c5412012-12-18 14:23:01 -08002681{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002682 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Glauber Costa749c5412012-12-18 14:23:01 -08002683 struct memcg_cache_params *params;
2684
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07002685 if (!memcg_kmem_is_active(memcg))
Glauber Costa749c5412012-12-18 14:23:01 -08002686 return -EIO;
2687
2688 print_slabinfo_header(m);
2689
Vladimir Davydovbd673142014-06-04 16:07:40 -07002690 mutex_lock(&memcg_slab_mutex);
Glauber Costa749c5412012-12-18 14:23:01 -08002691 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2692 cache_show(memcg_params_to_cache(params), m);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002693 mutex_unlock(&memcg_slab_mutex);
Glauber Costa749c5412012-12-18 14:23:01 -08002694
2695 return 0;
2696}
2697#endif
2698
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002699static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
2700 unsigned long nr_pages)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002701{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002702 struct page_counter *counter;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002703 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002704
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002705 ret = page_counter_try_charge(&memcg->kmem, nr_pages, &counter);
2706 if (ret < 0)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002707 return ret;
2708
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002709 ret = try_charge(memcg, gfp, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002710 if (ret == -EINTR) {
2711 /*
Johannes Weiner00501b52014-08-08 14:19:20 -07002712 * try_charge() chose to bypass to root due to OOM kill or
2713 * fatal signal. Since our only options are to either fail
2714 * the allocation or charge it to this cgroup, do it as a
2715 * temporary condition. But we can't fail. From a kmem/slab
2716 * perspective, the cache has already been selected, by
2717 * mem_cgroup_kmem_get_cache(), so it is too late to change
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002718 * our minds.
2719 *
2720 * This condition will only trigger if the task entered
Johannes Weiner00501b52014-08-08 14:19:20 -07002721 * memcg_charge_kmem in a sane state, but was OOM-killed
2722 * during try_charge() above. Tasks that were already dying
2723 * when the allocation triggers should have been already
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002724 * directed to the root cgroup in memcontrol.h
2725 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002726 page_counter_charge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002727 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002728 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002729 css_get_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002730 ret = 0;
2731 } else if (ret)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002732 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002733
2734 return ret;
2735}
2736
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002737static void memcg_uncharge_kmem(struct mem_cgroup *memcg,
2738 unsigned long nr_pages)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002739{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002740 page_counter_uncharge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002741 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002742 page_counter_uncharge(&memcg->memsw, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002743
Johannes Weiner64f21992014-12-10 15:42:45 -08002744 page_counter_uncharge(&memcg->kmem, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002745
2746 css_put_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002747}
2748
Glauber Costa2633d7a2012-12-18 14:22:34 -08002749/*
2750 * helper for acessing a memcg's index. It will be used as an index in the
2751 * child cache array in kmem_cache, and also to derive its name. This function
2752 * will return -1 when this is not a kmem-limited memcg.
2753 */
2754int memcg_cache_id(struct mem_cgroup *memcg)
2755{
2756 return memcg ? memcg->kmemcg_id : -1;
2757}
2758
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002759static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002760{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002761 int id, size;
2762 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002763
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002764 id = ida_simple_get(&kmem_limited_groups,
2765 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2766 if (id < 0)
2767 return id;
2768
2769 if (id < memcg_limited_groups_array_size)
2770 return id;
2771
2772 /*
2773 * There's no space for the new id in memcg_caches arrays,
2774 * so we have to grow them.
2775 */
2776
2777 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002778 if (size < MEMCG_CACHES_MIN_SIZE)
2779 size = MEMCG_CACHES_MIN_SIZE;
2780 else if (size > MEMCG_CACHES_MAX_SIZE)
2781 size = MEMCG_CACHES_MAX_SIZE;
2782
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002783 mutex_lock(&memcg_slab_mutex);
2784 err = memcg_update_all_caches(size);
2785 mutex_unlock(&memcg_slab_mutex);
2786
2787 if (err) {
2788 ida_simple_remove(&kmem_limited_groups, id);
2789 return err;
2790 }
2791 return id;
2792}
2793
2794static void memcg_free_cache_id(int id)
2795{
2796 ida_simple_remove(&kmem_limited_groups, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002797}
2798
2799/*
2800 * We should update the current array size iff all caches updates succeed. This
2801 * can only be done from the slab side. The slab mutex needs to be held when
2802 * calling this.
2803 */
2804void memcg_update_array_size(int num)
2805{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002806 memcg_limited_groups_array_size = num;
Glauber Costa55007d82012-12-18 14:22:38 -08002807}
2808
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002809static void memcg_register_cache(struct mem_cgroup *memcg,
2810 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08002811{
Vladimir Davydov93f39ee2014-06-04 16:08:24 -07002812 static char memcg_name_buf[NAME_MAX + 1]; /* protected by
2813 memcg_slab_mutex */
Vladimir Davydovbd673142014-06-04 16:07:40 -07002814 struct kmem_cache *cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002815 int id;
2816
Vladimir Davydovbd673142014-06-04 16:07:40 -07002817 lockdep_assert_held(&memcg_slab_mutex);
2818
2819 id = memcg_cache_id(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08002820
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002821 /*
Vladimir Davydovbd673142014-06-04 16:07:40 -07002822 * Since per-memcg caches are created asynchronously on first
2823 * allocation (see memcg_kmem_get_cache()), several threads can try to
2824 * create the same cache, but only one of them may succeed.
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002825 */
Vladimir Davydovbd673142014-06-04 16:07:40 -07002826 if (cache_from_memcg_idx(root_cache, id))
2827 return;
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002828
Vladimir Davydov073ee1c2014-06-04 16:08:23 -07002829 cgroup_name(memcg->css.cgroup, memcg_name_buf, NAME_MAX + 1);
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002830 cachep = memcg_create_kmem_cache(memcg, root_cache, memcg_name_buf);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002831 /*
2832 * If we could not create a memcg cache, do not complain, because
2833 * that's not critical at all as we can always proceed with the root
2834 * cache.
2835 */
2836 if (!cachep)
2837 return;
2838
Vladimir Davydov33a690c2014-10-09 15:28:43 -07002839 css_get(&memcg->css);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002840 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002841
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002842 /*
Vladimir Davydov959c8962014-01-23 15:52:59 -08002843 * Since readers won't lock (see cache_from_memcg_idx()), we need a
2844 * barrier here to ensure nobody will see the kmem_cache partially
2845 * initialized.
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002846 */
Vladimir Davydov959c8962014-01-23 15:52:59 -08002847 smp_wmb();
2848
Vladimir Davydovbd673142014-06-04 16:07:40 -07002849 BUG_ON(root_cache->memcg_params->memcg_caches[id]);
2850 root_cache->memcg_params->memcg_caches[id] = cachep;
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002851}
2852
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002853static void memcg_unregister_cache(struct kmem_cache *cachep)
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002854{
Vladimir Davydovbd673142014-06-04 16:07:40 -07002855 struct kmem_cache *root_cache;
Vladimir Davydov1aa13252014-01-23 15:52:58 -08002856 struct mem_cgroup *memcg;
2857 int id;
2858
Vladimir Davydovbd673142014-06-04 16:07:40 -07002859 lockdep_assert_held(&memcg_slab_mutex);
Glauber Costad7f25f82012-12-18 14:22:40 -08002860
Vladimir Davydovbd673142014-06-04 16:07:40 -07002861 BUG_ON(is_root_cache(cachep));
Vladimir Davydov2edefe12014-01-23 15:53:02 -08002862
Vladimir Davydovbd673142014-06-04 16:07:40 -07002863 root_cache = cachep->memcg_params->root_cache;
2864 memcg = cachep->memcg_params->memcg;
Vladimir Davydov96403da2014-01-23 15:53:01 -08002865 id = memcg_cache_id(memcg);
Glauber Costad7f25f82012-12-18 14:22:40 -08002866
Vladimir Davydovbd673142014-06-04 16:07:40 -07002867 BUG_ON(root_cache->memcg_params->memcg_caches[id] != cachep);
2868 root_cache->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08002869
Vladimir Davydovbd673142014-06-04 16:07:40 -07002870 list_del(&cachep->memcg_params->list);
2871
2872 kmem_cache_destroy(cachep);
Vladimir Davydov33a690c2014-10-09 15:28:43 -07002873
2874 /* drop the reference taken in memcg_register_cache */
2875 css_put(&memcg->css);
Glauber Costa2633d7a2012-12-18 14:22:34 -08002876}
2877
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002878/*
2879 * During the creation a new cache, we need to disable our accounting mechanism
2880 * altogether. This is true even if we are not creating, but rather just
2881 * enqueing new caches to be created.
2882 *
2883 * This is because that process will trigger allocations; some visible, like
2884 * explicit kmallocs to auxiliary data structures, name strings and internal
2885 * cache structures; some well concealed, like INIT_WORK() that can allocate
2886 * objects during debug.
2887 *
2888 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
2889 * to it. This may not be a bounded recursion: since the first cache creation
2890 * failed to complete (waiting on the allocation), we'll just try to create the
2891 * cache again, failing at the same point.
2892 *
2893 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
2894 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
2895 * inside the following two functions.
2896 */
2897static inline void memcg_stop_kmem_account(void)
2898{
2899 VM_BUG_ON(!current->mm);
2900 current->memcg_kmem_skip_account++;
2901}
2902
2903static inline void memcg_resume_kmem_account(void)
2904{
2905 VM_BUG_ON(!current->mm);
2906 current->memcg_kmem_skip_account--;
2907}
2908
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002909int __memcg_cleanup_cache_params(struct kmem_cache *s)
Glauber Costa7cf27982012-12-18 14:22:55 -08002910{
2911 struct kmem_cache *c;
Vladimir Davydovb8529902014-04-07 15:39:28 -07002912 int i, failed = 0;
Glauber Costa7cf27982012-12-18 14:22:55 -08002913
Vladimir Davydovbd673142014-06-04 16:07:40 -07002914 mutex_lock(&memcg_slab_mutex);
Qiang Huang7a67d7a2013-11-12 15:08:24 -08002915 for_each_memcg_cache_index(i) {
2916 c = cache_from_memcg_idx(s, i);
Glauber Costa7cf27982012-12-18 14:22:55 -08002917 if (!c)
2918 continue;
2919
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002920 memcg_unregister_cache(c);
Vladimir Davydovb8529902014-04-07 15:39:28 -07002921
2922 if (cache_from_memcg_idx(s, i))
2923 failed++;
Glauber Costa7cf27982012-12-18 14:22:55 -08002924 }
Vladimir Davydovbd673142014-06-04 16:07:40 -07002925 mutex_unlock(&memcg_slab_mutex);
Vladimir Davydovb8529902014-04-07 15:39:28 -07002926 return failed;
Glauber Costa7cf27982012-12-18 14:22:55 -08002927}
2928
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002929static void memcg_unregister_all_caches(struct mem_cgroup *memcg)
Glauber Costa1f458cb2012-12-18 14:22:50 -08002930{
2931 struct kmem_cache *cachep;
Vladimir Davydovbd673142014-06-04 16:07:40 -07002932 struct memcg_cache_params *params, *tmp;
Glauber Costa1f458cb2012-12-18 14:22:50 -08002933
2934 if (!memcg_kmem_is_active(memcg))
2935 return;
2936
Vladimir Davydovbd673142014-06-04 16:07:40 -07002937 mutex_lock(&memcg_slab_mutex);
2938 list_for_each_entry_safe(params, tmp, &memcg->memcg_slab_caches, list) {
Glauber Costa1f458cb2012-12-18 14:22:50 -08002939 cachep = memcg_params_to_cache(params);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002940 kmem_cache_shrink(cachep);
2941 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002942 memcg_unregister_cache(cachep);
Glauber Costa1f458cb2012-12-18 14:22:50 -08002943 }
Vladimir Davydovbd673142014-06-04 16:07:40 -07002944 mutex_unlock(&memcg_slab_mutex);
Glauber Costa1f458cb2012-12-18 14:22:50 -08002945}
2946
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002947struct memcg_register_cache_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07002948 struct mem_cgroup *memcg;
2949 struct kmem_cache *cachep;
2950 struct work_struct work;
2951};
2952
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002953static void memcg_register_cache_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08002954{
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002955 struct memcg_register_cache_work *cw =
2956 container_of(w, struct memcg_register_cache_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07002957 struct mem_cgroup *memcg = cw->memcg;
2958 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002959
Vladimir Davydovbd673142014-06-04 16:07:40 -07002960 mutex_lock(&memcg_slab_mutex);
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002961 memcg_register_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002962 mutex_unlock(&memcg_slab_mutex);
2963
Vladimir Davydov5722d092014-04-07 15:39:24 -07002964 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002965 kfree(cw);
2966}
2967
2968/*
2969 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002970 */
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002971static void __memcg_schedule_register_cache(struct mem_cgroup *memcg,
2972 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002973{
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002974 struct memcg_register_cache_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08002975
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002976 cw = kmalloc(sizeof(*cw), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07002977 if (cw == NULL) {
2978 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002979 return;
2980 }
2981
2982 cw->memcg = memcg;
2983 cw->cachep = cachep;
2984
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002985 INIT_WORK(&cw->work, memcg_register_cache_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08002986 schedule_work(&cw->work);
2987}
2988
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002989static void memcg_schedule_register_cache(struct mem_cgroup *memcg,
2990 struct kmem_cache *cachep)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002991{
2992 /*
2993 * We need to stop accounting when we kmalloc, because if the
2994 * corresponding kmalloc cache is not yet created, the first allocation
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002995 * in __memcg_schedule_register_cache will recurse.
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002996 *
2997 * However, it is better to enclose the whole function. Depending on
2998 * the debugging options enabled, INIT_WORK(), for instance, can
2999 * trigger an allocation. This too, will make us recurse. Because at
3000 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3001 * the safest choice is to do it like this, wrapping the whole function.
3002 */
3003 memcg_stop_kmem_account();
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003004 __memcg_schedule_register_cache(memcg, cachep);
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003005 memcg_resume_kmem_account();
3006}
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003007
3008int __memcg_charge_slab(struct kmem_cache *cachep, gfp_t gfp, int order)
3009{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003010 unsigned int nr_pages = 1 << order;
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003011 int res;
3012
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003013 res = memcg_charge_kmem(cachep->memcg_params->memcg, gfp, nr_pages);
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003014 if (!res)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003015 atomic_add(nr_pages, &cachep->memcg_params->nr_pages);
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003016 return res;
3017}
3018
3019void __memcg_uncharge_slab(struct kmem_cache *cachep, int order)
3020{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003021 unsigned int nr_pages = 1 << order;
3022
3023 memcg_uncharge_kmem(cachep->memcg_params->memcg, nr_pages);
3024 atomic_sub(nr_pages, &cachep->memcg_params->nr_pages);
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07003025}
3026
Glauber Costad7f25f82012-12-18 14:22:40 -08003027/*
3028 * Return the kmem_cache we're supposed to use for a slab allocation.
3029 * We try to use the current memcg's version of the cache.
3030 *
3031 * If the cache does not exist yet, if we are the first user of it,
3032 * we either create it immediately, if possible, or create it asynchronously
3033 * in a workqueue.
3034 * In the latter case, we will let the current allocation go through with
3035 * the original cache.
3036 *
3037 * Can't be called in interrupt context or from kernel threads.
3038 * This function needs to be called with rcu_read_lock() held.
3039 */
3040struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3041 gfp_t gfp)
3042{
3043 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08003044 struct kmem_cache *memcg_cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003045
3046 VM_BUG_ON(!cachep->memcg_params);
3047 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3048
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003049 if (!current->mm || current->memcg_kmem_skip_account)
3050 return cachep;
3051
Glauber Costad7f25f82012-12-18 14:22:40 -08003052 rcu_read_lock();
3053 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003054
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07003055 if (!memcg_kmem_is_active(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003056 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003057
Vladimir Davydov959c8962014-01-23 15:52:59 -08003058 memcg_cachep = cache_from_memcg_idx(cachep, memcg_cache_id(memcg));
3059 if (likely(memcg_cachep)) {
3060 cachep = memcg_cachep;
Li Zefanca0dde92013-04-29 15:08:57 -07003061 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003062 }
3063
Li Zefanca0dde92013-04-29 15:08:57 -07003064 /* The corresponding put will be done in the workqueue. */
Tejun Heoec903c02014-05-13 12:11:01 -04003065 if (!css_tryget_online(&memcg->css))
Li Zefanca0dde92013-04-29 15:08:57 -07003066 goto out;
3067 rcu_read_unlock();
3068
3069 /*
3070 * If we are in a safe context (can wait, and not in interrupt
3071 * context), we could be be predictable and return right away.
3072 * This would guarantee that the allocation being performed
3073 * already belongs in the new cache.
3074 *
3075 * However, there are some clashes that can arrive from locking.
3076 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003077 * memcg_create_kmem_cache, this means no further allocation
3078 * could happen with the slab_mutex held. So it's better to
3079 * defer everything.
Li Zefanca0dde92013-04-29 15:08:57 -07003080 */
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003081 memcg_schedule_register_cache(memcg, cachep);
Li Zefanca0dde92013-04-29 15:08:57 -07003082 return cachep;
3083out:
3084 rcu_read_unlock();
3085 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003086}
Glauber Costad7f25f82012-12-18 14:22:40 -08003087
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003088/*
3089 * We need to verify if the allocation against current->mm->owner's memcg is
3090 * possible for the given order. But the page is not allocated yet, so we'll
3091 * need a further commit step to do the final arrangements.
3092 *
3093 * It is possible for the task to switch cgroups in this mean time, so at
3094 * commit time, we can't rely on task conversion any longer. We'll then use
3095 * the handle argument to return to the caller which cgroup we should commit
3096 * against. We could also return the memcg directly and avoid the pointer
3097 * passing, but a boolean return value gives better semantics considering
3098 * the compiled-out case as well.
3099 *
3100 * Returning true means the allocation is possible.
3101 */
3102bool
3103__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3104{
3105 struct mem_cgroup *memcg;
3106 int ret;
3107
3108 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003109
3110 /*
3111 * Disabling accounting is only relevant for some specific memcg
3112 * internal allocations. Therefore we would initially not have such
Vladimir Davydov52383432014-06-04 16:06:39 -07003113 * check here, since direct calls to the page allocator that are
3114 * accounted to kmemcg (alloc_kmem_pages and friends) only happen
3115 * outside memcg core. We are mostly concerned with cache allocations,
3116 * and by having this test at memcg_kmem_get_cache, we are already able
3117 * to relay the allocation to the root cache and bypass the memcg cache
3118 * altogether.
Glauber Costa6d42c232013-07-08 16:00:00 -07003119 *
3120 * There is one exception, though: the SLUB allocator does not create
3121 * large order caches, but rather service large kmallocs directly from
3122 * the page allocator. Therefore, the following sequence when backed by
3123 * the SLUB allocator:
3124 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003125 * memcg_stop_kmem_account();
3126 * kmalloc(<large_number>)
3127 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003128 *
3129 * would effectively ignore the fact that we should skip accounting,
3130 * since it will drive us directly to this function without passing
3131 * through the cache selector memcg_kmem_get_cache. Such large
3132 * allocations are extremely rare but can happen, for instance, for the
3133 * cache arrays. We bring this test here.
3134 */
3135 if (!current->mm || current->memcg_kmem_skip_account)
3136 return true;
3137
Johannes Weinerdf381972014-04-07 15:37:43 -07003138 memcg = get_mem_cgroup_from_mm(current->mm);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003139
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07003140 if (!memcg_kmem_is_active(memcg)) {
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003141 css_put(&memcg->css);
3142 return true;
3143 }
3144
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003145 ret = memcg_charge_kmem(memcg, gfp, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003146 if (!ret)
3147 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003148
3149 css_put(&memcg->css);
3150 return (ret == 0);
3151}
3152
3153void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3154 int order)
3155{
3156 struct page_cgroup *pc;
3157
3158 VM_BUG_ON(mem_cgroup_is_root(memcg));
3159
3160 /* The page allocation failed. Revert */
3161 if (!page) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003162 memcg_uncharge_kmem(memcg, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003163 return;
3164 }
Johannes Weinera840cda2014-08-06 16:06:04 -07003165 /*
3166 * The page is freshly allocated and not visible to any
3167 * outside callers yet. Set up pc non-atomically.
3168 */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003169 pc = lookup_page_cgroup(page);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003170 pc->mem_cgroup = memcg;
Johannes Weinera840cda2014-08-06 16:06:04 -07003171 pc->flags = PCG_USED;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003172}
3173
3174void __memcg_kmem_uncharge_pages(struct page *page, int order)
3175{
3176 struct mem_cgroup *memcg = NULL;
3177 struct page_cgroup *pc;
3178
3179
3180 pc = lookup_page_cgroup(page);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003181 if (!PageCgroupUsed(pc))
3182 return;
3183
Johannes Weinera840cda2014-08-06 16:06:04 -07003184 memcg = pc->mem_cgroup;
3185 pc->flags = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003186
3187 /*
3188 * We trust that only if there is a memcg associated with the page, it
3189 * is a valid allocation
3190 */
3191 if (!memcg)
3192 return;
3193
Sasha Levin309381fea2014-01-23 15:52:54 -08003194 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003195 memcg_uncharge_kmem(memcg, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003196}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003197#else
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07003198static inline void memcg_unregister_all_caches(struct mem_cgroup *memcg)
Glauber Costa1f458cb2012-12-18 14:22:50 -08003199{
3200}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003201#endif /* CONFIG_MEMCG_KMEM */
3202
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003203#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3204
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003205/*
3206 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003207 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3208 * charge/uncharge will be never happen and move_account() is done under
3209 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003210 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003211void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003212{
3213 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003214 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003215 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003216 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003217
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003218 if (mem_cgroup_disabled())
3219 return;
David Rientjesb070e652013-05-07 16:18:09 -07003220
3221 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003222 for (i = 1; i < HPAGE_PMD_NR; i++) {
3223 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003224 pc->mem_cgroup = memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003225 pc->flags = head_pc->flags;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003226 }
David Rientjesb070e652013-05-07 16:18:09 -07003227 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3228 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003229}
Hugh Dickins12d27102012-01-12 17:19:52 -08003230#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003231
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003232/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003233 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003234 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003235 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003236 * @pc: page_cgroup of the page.
3237 * @from: mem_cgroup which the page is moved from.
3238 * @to: mem_cgroup which the page is moved to. @from != @to.
3239 *
3240 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003241 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003242 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003243 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003244 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3245 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003246 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003247static int mem_cgroup_move_account(struct page *page,
3248 unsigned int nr_pages,
3249 struct page_cgroup *pc,
3250 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003251 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003252{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003253 unsigned long flags;
3254 int ret;
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003255
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003256 VM_BUG_ON(from == to);
Sasha Levin309381fea2014-01-23 15:52:54 -08003257 VM_BUG_ON_PAGE(PageLRU(page), page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003258 /*
3259 * The page is isolated from LRU. So, collapse function
3260 * will not handle this page. But page splitting can happen.
3261 * Do this check under compound_page_lock(). The caller should
3262 * hold it.
3263 */
3264 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003265 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003266 goto out;
3267
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003268 /*
3269 * Prevent mem_cgroup_migrate() from looking at pc->mem_cgroup
3270 * of its source page while we change it: page migration takes
3271 * both pages off the LRU, but page cache replacement doesn't.
3272 */
3273 if (!trylock_page(page))
3274 goto out;
Johannes Weinerde3638d2011-03-23 16:42:28 -07003275
3276 ret = -EINVAL;
3277 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003278 goto out_unlock;
Johannes Weinerde3638d2011-03-23 16:42:28 -07003279
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003280 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003281
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003282 if (!PageAnon(page) && page_mapped(page)) {
Johannes Weiner59d1d252014-04-07 15:37:40 -07003283 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
3284 nr_pages);
3285 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
3286 nr_pages);
3287 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003288
Johannes Weiner59d1d252014-04-07 15:37:40 -07003289 if (PageWriteback(page)) {
3290 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK],
3291 nr_pages);
3292 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK],
3293 nr_pages);
3294 }
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003295
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003296 /*
3297 * It is safe to change pc->mem_cgroup here because the page
3298 * is referenced, charged, and isolated - we can't race with
3299 * uncharging, charging, migration, or LRU putback.
3300 */
Balbir Singhd69b0422009-06-17 16:26:34 -07003301
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003302 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003303 pc->mem_cgroup = to;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003304 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003305 ret = 0;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003306
3307 local_irq_disable();
3308 mem_cgroup_charge_statistics(to, page, nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07003309 memcg_check_events(to, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003310 mem_cgroup_charge_statistics(from, page, -nr_pages);
Johannes Weiner5564e882011-03-23 16:42:29 -07003311 memcg_check_events(from, page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003312 local_irq_enable();
3313out_unlock:
3314 unlock_page(page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003315out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003316 return ret;
3317}
3318
Andrew Mortonc255a452012-07-31 16:43:02 -07003319#ifdef CONFIG_MEMCG_SWAP
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003320static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
3321 bool charge)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003322{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003323 int val = (charge) ? 1 : -1;
3324 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003325}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003326
3327/**
3328 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3329 * @entry: swap entry to be moved
3330 * @from: mem_cgroup which the entry is moved from
3331 * @to: mem_cgroup which the entry is moved to
3332 *
3333 * It succeeds only when the swap_cgroup's record for this entry is the same
3334 * as the mem_cgroup's id of @from.
3335 *
3336 * Returns 0 on success, -EINVAL on failure.
3337 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003338 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08003339 * both res and memsw, and called css_get().
3340 */
3341static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003342 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003343{
3344 unsigned short old_id, new_id;
3345
Li Zefan34c00c32013-09-23 16:56:01 +08003346 old_id = mem_cgroup_id(from);
3347 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003348
3349 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003350 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003351 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003352 /*
3353 * This function is only called from task migration context now.
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003354 * It postpones page_counter and refcount handling till the end
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003355 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07003356 * improvement. But we cannot postpone css_get(to) because if
3357 * the process that has been moved to @to does swap-in, the
3358 * refcount of @to might be decreased to 0.
3359 *
3360 * We are in attach() phase, so the cgroup is guaranteed to be
3361 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003362 */
Li Zefan40503772013-07-08 16:00:34 -07003363 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003364 return 0;
3365 }
3366 return -EINVAL;
3367}
3368#else
3369static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003370 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003371{
3372 return -EINVAL;
3373}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003374#endif
3375
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003376#ifdef CONFIG_DEBUG_VM
3377static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3378{
3379 struct page_cgroup *pc;
3380
3381 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003382 /*
3383 * Can be NULL while feeding pages into the page allocator for
3384 * the first time, i.e. during boot or memory hotplug;
3385 * or when mem_cgroup_disabled().
3386 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003387 if (likely(pc) && PageCgroupUsed(pc))
3388 return pc;
3389 return NULL;
3390}
3391
3392bool mem_cgroup_bad_page_check(struct page *page)
3393{
3394 if (mem_cgroup_disabled())
3395 return false;
3396
3397 return lookup_page_cgroup_used(page) != NULL;
3398}
3399
3400void mem_cgroup_print_bad_page(struct page *page)
3401{
3402 struct page_cgroup *pc;
3403
3404 pc = lookup_page_cgroup_used(page);
3405 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08003406 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
3407 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003408 }
3409}
3410#endif
3411
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003412static DEFINE_MUTEX(memcg_limit_mutex);
3413
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003414static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003415 unsigned long limit)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003416{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003417 unsigned long curusage;
3418 unsigned long oldusage;
3419 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003420 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003421 int ret;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003422
3423 /*
3424 * For keeping hierarchical_reclaim simple, how long we should retry
3425 * is depends on callers. We set our retry-count to be function
3426 * of # of children which we should visit in this loop.
3427 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003428 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
3429 mem_cgroup_count_children(memcg);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003430
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003431 oldusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003432
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003433 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003434 if (signal_pending(current)) {
3435 ret = -EINTR;
3436 break;
3437 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003438
3439 mutex_lock(&memcg_limit_mutex);
3440 if (limit > memcg->memsw.limit) {
3441 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003442 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003443 break;
3444 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003445 if (limit > memcg->memory.limit)
3446 enlarge = true;
3447 ret = page_counter_limit(&memcg->memory, limit);
3448 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003449
3450 if (!ret)
3451 break;
3452
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003453 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, true);
3454
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003455 curusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003456 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003457 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003458 retry_count--;
3459 else
3460 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003461 } while (retry_count);
3462
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003463 if (!ret && enlarge)
3464 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003465
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003466 return ret;
3467}
3468
Li Zefan338c8432009-06-17 16:27:15 -07003469static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003470 unsigned long limit)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003471{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003472 unsigned long curusage;
3473 unsigned long oldusage;
3474 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003475 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003476 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003477
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003478 /* see mem_cgroup_resize_res_limit */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003479 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
3480 mem_cgroup_count_children(memcg);
3481
3482 oldusage = page_counter_read(&memcg->memsw);
3483
3484 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003485 if (signal_pending(current)) {
3486 ret = -EINTR;
3487 break;
3488 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003489
3490 mutex_lock(&memcg_limit_mutex);
3491 if (limit < memcg->memory.limit) {
3492 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003493 ret = -EINVAL;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003494 break;
3495 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003496 if (limit > memcg->memsw.limit)
3497 enlarge = true;
3498 ret = page_counter_limit(&memcg->memsw, limit);
3499 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003500
3501 if (!ret)
3502 break;
3503
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003504 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, false);
3505
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003506 curusage = page_counter_read(&memcg->memsw);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003507 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003508 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003509 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003510 else
3511 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003512 } while (retry_count);
3513
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003514 if (!ret && enlarge)
3515 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003516
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003517 return ret;
3518}
3519
Andrew Morton0608f432013-09-24 15:27:41 -07003520unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
3521 gfp_t gfp_mask,
3522 unsigned long *total_scanned)
3523{
3524 unsigned long nr_reclaimed = 0;
3525 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3526 unsigned long reclaimed;
3527 int loop = 0;
3528 struct mem_cgroup_tree_per_zone *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003529 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07003530 unsigned long nr_scanned;
3531
3532 if (order > 0)
3533 return 0;
3534
3535 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
3536 /*
3537 * This loop can run a while, specially if mem_cgroup's continuously
3538 * keep exceeding their soft limit and putting the system under
3539 * pressure
3540 */
3541 do {
3542 if (next_mz)
3543 mz = next_mz;
3544 else
3545 mz = mem_cgroup_largest_soft_limit_node(mctz);
3546 if (!mz)
3547 break;
3548
3549 nr_scanned = 0;
3550 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
3551 gfp_mask, &nr_scanned);
3552 nr_reclaimed += reclaimed;
3553 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003554 spin_lock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003555
3556 /*
3557 * If we failed to reclaim anything from this memory cgroup
3558 * it is time to move on to the next cgroup
3559 */
3560 next_mz = NULL;
3561 if (!reclaimed) {
3562 do {
3563 /*
3564 * Loop until we find yet another one.
3565 *
3566 * By the time we get the soft_limit lock
3567 * again, someone might have aded the
3568 * group back on the RB tree. Iterate to
3569 * make sure we get a different mem.
3570 * mem_cgroup_largest_soft_limit_node returns
3571 * NULL if no other cgroup is present on
3572 * the tree
3573 */
3574 next_mz =
3575 __mem_cgroup_largest_soft_limit_node(mctz);
3576 if (next_mz == mz)
3577 css_put(&next_mz->memcg->css);
3578 else /* next_mz == NULL or other memcg */
3579 break;
3580 } while (1);
3581 }
Johannes Weinercf2c8122014-06-06 14:38:21 -07003582 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003583 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07003584 /*
3585 * One school of thought says that we should not add
3586 * back the node to the tree if reclaim returns 0.
3587 * But our reclaim could return 0, simply because due
3588 * to priority we are exposing a smaller subset of
3589 * memory to reclaim from. Consider this as a longer
3590 * term TODO.
3591 */
3592 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003593 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003594 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003595 css_put(&mz->memcg->css);
3596 loop++;
3597 /*
3598 * Could not reclaim anything and there are no more
3599 * mem cgroups to try or we seem to be looping without
3600 * reclaiming anything.
3601 */
3602 if (!nr_reclaimed &&
3603 (next_mz == NULL ||
3604 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3605 break;
3606 } while (!nr_reclaimed);
3607 if (next_mz)
3608 css_put(&next_mz->memcg->css);
3609 return nr_reclaimed;
3610}
3611
Tejun Heoea280e72014-05-16 13:22:48 -04003612/*
3613 * Test whether @memcg has children, dead or alive. Note that this
3614 * function doesn't care whether @memcg has use_hierarchy enabled and
3615 * returns %true if there are child csses according to the cgroup
3616 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
3617 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003618static inline bool memcg_has_children(struct mem_cgroup *memcg)
3619{
Tejun Heoea280e72014-05-16 13:22:48 -04003620 bool ret;
3621
Johannes Weiner696ac172013-10-31 16:34:15 -07003622 /*
Tejun Heoea280e72014-05-16 13:22:48 -04003623 * The lock does not prevent addition or deletion of children, but
3624 * it prevents a new child from being initialized based on this
3625 * parent in css_online(), so it's enough to decide whether
3626 * hierarchically inherited attributes can still be changed or not.
Johannes Weiner696ac172013-10-31 16:34:15 -07003627 */
Tejun Heoea280e72014-05-16 13:22:48 -04003628 lockdep_assert_held(&memcg_create_mutex);
3629
3630 rcu_read_lock();
3631 ret = css_next_child(NULL, &memcg->css);
3632 rcu_read_unlock();
3633 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003634}
3635
3636/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003637 * Reclaims as many pages from the given memcg as possible and moves
3638 * the rest to the parent.
3639 *
3640 * Caller is responsible for holding css reference for memcg.
3641 */
3642static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3643{
3644 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003645
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003646 /* we call try-to-free pages for make this cgroup empty */
3647 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003648 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003649 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003650 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003651
Michal Hockoc26251f2012-10-26 13:37:28 +02003652 if (signal_pending(current))
3653 return -EINTR;
3654
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003655 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3656 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003657 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003658 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003659 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003660 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003661 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003662
3663 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003664
3665 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003666}
3667
Tejun Heo6770c642014-05-13 12:16:21 -04003668static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3669 char *buf, size_t nbytes,
3670 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003671{
Tejun Heo6770c642014-05-13 12:16:21 -04003672 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003673
Michal Hockod8423012012-10-26 13:37:29 +02003674 if (mem_cgroup_is_root(memcg))
3675 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003676 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003677}
3678
Tejun Heo182446d2013-08-08 20:11:24 -04003679static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3680 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003681{
Tejun Heo182446d2013-08-08 20:11:24 -04003682 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003683}
3684
Tejun Heo182446d2013-08-08 20:11:24 -04003685static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3686 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003687{
3688 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003689 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003690 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003691
Glauber Costa09998212013-02-22 16:34:55 -08003692 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07003693
3694 if (memcg->use_hierarchy == val)
3695 goto out;
3696
Balbir Singh18f59ea2009-01-07 18:08:07 -08003697 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003698 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003699 * in the child subtrees. If it is unset, then the change can
3700 * occur, provided the current cgroup has no children.
3701 *
3702 * For the root cgroup, parent_mem is NULL, we allow value to be
3703 * set if there are no children.
3704 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003705 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003706 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003707 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003708 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003709 else
3710 retval = -EBUSY;
3711 } else
3712 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003713
3714out:
Glauber Costa09998212013-02-22 16:34:55 -08003715 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003716
3717 return retval;
3718}
3719
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003720static unsigned long tree_stat(struct mem_cgroup *memcg,
3721 enum mem_cgroup_stat_index idx)
Johannes Weinerce00a962014-09-05 08:43:57 -04003722{
3723 struct mem_cgroup *iter;
3724 long val = 0;
3725
3726 /* Per-cpu values can be negative, use a signed accumulator */
3727 for_each_mem_cgroup_tree(iter, memcg)
3728 val += mem_cgroup_read_stat(iter, idx);
3729
3730 if (val < 0) /* race ? */
3731 val = 0;
3732 return val;
3733}
3734
3735static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3736{
3737 u64 val;
3738
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003739 if (mem_cgroup_is_root(memcg)) {
3740 val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE);
3741 val += tree_stat(memcg, MEM_CGROUP_STAT_RSS);
3742 if (swap)
3743 val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP);
3744 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003745 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003746 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003747 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003748 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003749 }
Johannes Weinerce00a962014-09-05 08:43:57 -04003750 return val << PAGE_SHIFT;
3751}
3752
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003753enum {
3754 RES_USAGE,
3755 RES_LIMIT,
3756 RES_MAX_USAGE,
3757 RES_FAILCNT,
3758 RES_SOFT_LIMIT,
3759};
Johannes Weinerce00a962014-09-05 08:43:57 -04003760
Tejun Heo791badb2013-12-05 12:28:02 -05003761static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003762 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003763{
Tejun Heo182446d2013-08-08 20:11:24 -04003764 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003765 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003766
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003767 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003768 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003769 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003770 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003771 case _MEMSWAP:
3772 counter = &memcg->memsw;
3773 break;
3774 case _KMEM:
3775 counter = &memcg->kmem;
3776 break;
3777 default:
3778 BUG();
3779 }
3780
3781 switch (MEMFILE_ATTR(cft->private)) {
3782 case RES_USAGE:
3783 if (counter == &memcg->memory)
3784 return mem_cgroup_usage(memcg, false);
3785 if (counter == &memcg->memsw)
3786 return mem_cgroup_usage(memcg, true);
3787 return (u64)page_counter_read(counter) * PAGE_SIZE;
3788 case RES_LIMIT:
3789 return (u64)counter->limit * PAGE_SIZE;
3790 case RES_MAX_USAGE:
3791 return (u64)counter->watermark * PAGE_SIZE;
3792 case RES_FAILCNT:
3793 return counter->failcnt;
3794 case RES_SOFT_LIMIT:
3795 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003796 default:
3797 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003798 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003799}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003800
Glauber Costa510fc4e2012-12-18 14:21:47 -08003801#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovd6441632014-01-23 15:53:09 -08003802/* should be called with activate_kmem_mutex held */
3803static int __memcg_activate_kmem(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003804 unsigned long nr_pages)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003805{
3806 int err = 0;
3807 int memcg_id;
3808
3809 if (memcg_kmem_is_active(memcg))
3810 return 0;
3811
3812 /*
3813 * We are going to allocate memory for data shared by all memory
3814 * cgroups so let's stop accounting here.
3815 */
3816 memcg_stop_kmem_account();
3817
Glauber Costa510fc4e2012-12-18 14:21:47 -08003818 /*
3819 * For simplicity, we won't allow this to be disabled. It also can't
3820 * be changed if the cgroup has children already, or if tasks had
3821 * already joined.
3822 *
3823 * If tasks join before we set the limit, a person looking at
3824 * kmem.usage_in_bytes will have no way to determine when it took
3825 * place, which makes the value quite meaningless.
3826 *
3827 * After it first became limited, changes in the value of the limit are
3828 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08003829 */
Glauber Costa09998212013-02-22 16:34:55 -08003830 mutex_lock(&memcg_create_mutex);
Tejun Heoea280e72014-05-16 13:22:48 -04003831 if (cgroup_has_tasks(memcg->css.cgroup) ||
3832 (memcg->use_hierarchy && memcg_has_children(memcg)))
Vladimir Davydovd6441632014-01-23 15:53:09 -08003833 err = -EBUSY;
Glauber Costa09998212013-02-22 16:34:55 -08003834 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003835 if (err)
3836 goto out;
3837
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003838 memcg_id = memcg_alloc_cache_id();
Vladimir Davydovd6441632014-01-23 15:53:09 -08003839 if (memcg_id < 0) {
3840 err = memcg_id;
3841 goto out;
3842 }
3843
Vladimir Davydovd6441632014-01-23 15:53:09 -08003844 memcg->kmemcg_id = memcg_id;
3845 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003846
3847 /*
3848 * We couldn't have accounted to this cgroup, because it hasn't got the
3849 * active bit set yet, so this should succeed.
3850 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003851 err = page_counter_limit(&memcg->kmem, nr_pages);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003852 VM_BUG_ON(err);
3853
3854 static_key_slow_inc(&memcg_kmem_enabled_key);
3855 /*
3856 * Setting the active bit after enabling static branching will
3857 * guarantee no one starts accounting before all call sites are
3858 * patched.
3859 */
3860 memcg_kmem_set_active(memcg);
3861out:
3862 memcg_resume_kmem_account();
3863 return err;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003864}
3865
3866static int memcg_activate_kmem(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003867 unsigned long nr_pages)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003868{
3869 int ret;
3870
3871 mutex_lock(&activate_kmem_mutex);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003872 ret = __memcg_activate_kmem(memcg, nr_pages);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003873 mutex_unlock(&activate_kmem_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08003874 return ret;
3875}
3876
Vladimir Davydovd6441632014-01-23 15:53:09 -08003877static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003878 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003879{
3880 int ret;
3881
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003882 mutex_lock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003883 if (!memcg_kmem_is_active(memcg))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003884 ret = memcg_activate_kmem(memcg, limit);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003885 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003886 ret = page_counter_limit(&memcg->kmem, limit);
3887 mutex_unlock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003888 return ret;
3889}
3890
Glauber Costa55007d82012-12-18 14:22:38 -08003891static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08003892{
Glauber Costa55007d82012-12-18 14:22:38 -08003893 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003894 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003895
Glauber Costa510fc4e2012-12-18 14:21:47 -08003896 if (!parent)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003897 return 0;
Glauber Costa55007d82012-12-18 14:22:38 -08003898
Vladimir Davydovd6441632014-01-23 15:53:09 -08003899 mutex_lock(&activate_kmem_mutex);
Glauber Costaa8964b92012-12-18 14:22:09 -08003900 /*
Vladimir Davydovd6441632014-01-23 15:53:09 -08003901 * If the parent cgroup is not kmem-active now, it cannot be activated
3902 * after this point, because it has at least one child already.
Glauber Costaa8964b92012-12-18 14:22:09 -08003903 */
Vladimir Davydovd6441632014-01-23 15:53:09 -08003904 if (memcg_kmem_is_active(parent))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003905 ret = __memcg_activate_kmem(memcg, PAGE_COUNTER_MAX);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003906 mutex_unlock(&activate_kmem_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08003907 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003908}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003909#else
3910static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003911 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003912{
3913 return -EINVAL;
3914}
Hugh Dickins6d0439902013-02-22 16:35:50 -08003915#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08003916
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003917/*
3918 * The user of this function is...
3919 * RES_LIMIT.
3920 */
Tejun Heo451af502014-05-13 12:16:21 -04003921static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3922 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003923{
Tejun Heo451af502014-05-13 12:16:21 -04003924 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003925 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003926 int ret;
3927
Tejun Heo451af502014-05-13 12:16:21 -04003928 buf = strstrip(buf);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003929 ret = page_counter_memparse(buf, &nr_pages);
3930 if (ret)
3931 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003932
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003933 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003934 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003935 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3936 ret = -EINVAL;
3937 break;
3938 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003939 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3940 case _MEM:
3941 ret = mem_cgroup_resize_limit(memcg, nr_pages);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003942 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003943 case _MEMSWAP:
3944 ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages);
3945 break;
3946 case _KMEM:
3947 ret = memcg_update_kmem_limit(memcg, nr_pages);
3948 break;
3949 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003950 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003951 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003952 memcg->soft_limit = nr_pages;
3953 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003954 break;
3955 }
Tejun Heo451af502014-05-13 12:16:21 -04003956 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003957}
3958
Tejun Heo6770c642014-05-13 12:16:21 -04003959static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3960 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003961{
Tejun Heo6770c642014-05-13 12:16:21 -04003962 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003963 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003964
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003965 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3966 case _MEM:
3967 counter = &memcg->memory;
3968 break;
3969 case _MEMSWAP:
3970 counter = &memcg->memsw;
3971 break;
3972 case _KMEM:
3973 counter = &memcg->kmem;
3974 break;
3975 default:
3976 BUG();
3977 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003978
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003979 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003980 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003981 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003982 break;
3983 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003984 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003985 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003986 default:
3987 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003988 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003989
Tejun Heo6770c642014-05-13 12:16:21 -04003990 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003991}
3992
Tejun Heo182446d2013-08-08 20:11:24 -04003993static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003994 struct cftype *cft)
3995{
Tejun Heo182446d2013-08-08 20:11:24 -04003996 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003997}
3998
Daisuke Nishimura02491442010-03-10 15:22:17 -08003999#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04004000static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004001 struct cftype *cft, u64 val)
4002{
Tejun Heo182446d2013-08-08 20:11:24 -04004003 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004004
4005 if (val >= (1 << NR_MOVE_TYPE))
4006 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004007
Glauber Costaee5e8472013-02-22 16:34:50 -08004008 /*
4009 * No kind of locking is needed in here, because ->can_attach() will
4010 * check this value once in the beginning of the process, and then carry
4011 * on with stale data. This means that changes to this value will only
4012 * affect task migrations starting after the change.
4013 */
4014 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004015 return 0;
4016}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004017#else
Tejun Heo182446d2013-08-08 20:11:24 -04004018static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004019 struct cftype *cft, u64 val)
4020{
4021 return -ENOSYS;
4022}
4023#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004024
Ying Han406eb0c2011-05-26 16:25:37 -07004025#ifdef CONFIG_NUMA
Tejun Heo2da8ca82013-12-05 12:28:04 -05004026static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07004027{
Greg Thelen25485de2013-11-12 15:07:40 -08004028 struct numa_stat {
4029 const char *name;
4030 unsigned int lru_mask;
4031 };
4032
4033 static const struct numa_stat stats[] = {
4034 { "total", LRU_ALL },
4035 { "file", LRU_ALL_FILE },
4036 { "anon", LRU_ALL_ANON },
4037 { "unevictable", BIT(LRU_UNEVICTABLE) },
4038 };
4039 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07004040 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08004041 unsigned long nr;
Tejun Heo2da8ca82013-12-05 12:28:04 -05004042 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Ying Han406eb0c2011-05-26 16:25:37 -07004043
Greg Thelen25485de2013-11-12 15:07:40 -08004044 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4045 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
4046 seq_printf(m, "%s=%lu", stat->name, nr);
4047 for_each_node_state(nid, N_MEMORY) {
4048 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
4049 stat->lru_mask);
4050 seq_printf(m, " N%d=%lu", nid, nr);
4051 }
4052 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07004053 }
Ying Han406eb0c2011-05-26 16:25:37 -07004054
Ying Han071aee12013-11-12 15:07:41 -08004055 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4056 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07004057
Ying Han071aee12013-11-12 15:07:41 -08004058 nr = 0;
4059 for_each_mem_cgroup_tree(iter, memcg)
4060 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
4061 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
4062 for_each_node_state(nid, N_MEMORY) {
4063 nr = 0;
4064 for_each_mem_cgroup_tree(iter, memcg)
4065 nr += mem_cgroup_node_nr_lru_pages(
4066 iter, nid, stat->lru_mask);
4067 seq_printf(m, " N%d=%lu", nid, nr);
4068 }
4069 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07004070 }
Ying Han406eb0c2011-05-26 16:25:37 -07004071
Ying Han406eb0c2011-05-26 16:25:37 -07004072 return 0;
4073}
4074#endif /* CONFIG_NUMA */
4075
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004076static inline void mem_cgroup_lru_names_not_uptodate(void)
4077{
4078 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4079}
4080
Tejun Heo2da8ca82013-12-05 12:28:04 -05004081static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004082{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004083 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004084 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004085 struct mem_cgroup *mi;
4086 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004087
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004088 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004089 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004090 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004091 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4092 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004093 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004094
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004095 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4096 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4097 mem_cgroup_read_events(memcg, i));
4098
4099 for (i = 0; i < NR_LRU_LISTS; i++)
4100 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4101 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4102
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004103 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004104 memory = memsw = PAGE_COUNTER_MAX;
4105 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
4106 memory = min(memory, mi->memory.limit);
4107 memsw = min(memsw, mi->memsw.limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004108 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004109 seq_printf(m, "hierarchical_memory_limit %llu\n",
4110 (u64)memory * PAGE_SIZE);
4111 if (do_swap_account)
4112 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4113 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004114
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004115 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4116 long long val = 0;
4117
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004118 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004119 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004120 for_each_mem_cgroup_tree(mi, memcg)
4121 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4122 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4123 }
4124
4125 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4126 unsigned long long val = 0;
4127
4128 for_each_mem_cgroup_tree(mi, memcg)
4129 val += mem_cgroup_read_events(mi, i);
4130 seq_printf(m, "total_%s %llu\n",
4131 mem_cgroup_events_names[i], val);
4132 }
4133
4134 for (i = 0; i < NR_LRU_LISTS; i++) {
4135 unsigned long long val = 0;
4136
4137 for_each_mem_cgroup_tree(mi, memcg)
4138 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4139 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004140 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004141
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004142#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004143 {
4144 int nid, zid;
4145 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004146 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004147 unsigned long recent_rotated[2] = {0, 0};
4148 unsigned long recent_scanned[2] = {0, 0};
4149
4150 for_each_online_node(nid)
4151 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Jianyu Zhane2318752014-06-06 14:38:20 -07004152 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
Hugh Dickins89abfab2012-05-29 15:06:53 -07004153 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004154
Hugh Dickins89abfab2012-05-29 15:06:53 -07004155 recent_rotated[0] += rstat->recent_rotated[0];
4156 recent_rotated[1] += rstat->recent_rotated[1];
4157 recent_scanned[0] += rstat->recent_scanned[0];
4158 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004159 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004160 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4161 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4162 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4163 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004164 }
4165#endif
4166
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004167 return 0;
4168}
4169
Tejun Heo182446d2013-08-08 20:11:24 -04004170static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4171 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004172{
Tejun Heo182446d2013-08-08 20:11:24 -04004173 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004174
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004175 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004176}
4177
Tejun Heo182446d2013-08-08 20:11:24 -04004178static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4179 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004180{
Tejun Heo182446d2013-08-08 20:11:24 -04004181 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08004182
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004183 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004184 return -EINVAL;
4185
Linus Torvalds14208b02014-06-09 15:03:33 -07004186 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004187 memcg->swappiness = val;
4188 else
4189 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08004190
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004191 return 0;
4192}
4193
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004194static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4195{
4196 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004197 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004198 int i;
4199
4200 rcu_read_lock();
4201 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004202 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004203 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004204 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004205
4206 if (!t)
4207 goto unlock;
4208
Johannes Weinerce00a962014-09-05 08:43:57 -04004209 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004210
4211 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004212 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004213 * If it's not true, a threshold was crossed after last
4214 * call of __mem_cgroup_threshold().
4215 */
Phil Carmody5407a562010-05-26 14:42:42 -07004216 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004217
4218 /*
4219 * Iterate backward over array of thresholds starting from
4220 * current_threshold and check if a threshold is crossed.
4221 * If none of thresholds below usage is crossed, we read
4222 * only one element of the array here.
4223 */
4224 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4225 eventfd_signal(t->entries[i].eventfd, 1);
4226
4227 /* i = current_threshold + 1 */
4228 i++;
4229
4230 /*
4231 * Iterate forward over array of thresholds starting from
4232 * current_threshold+1 and check if a threshold is crossed.
4233 * If none of thresholds above usage is crossed, we read
4234 * only one element of the array here.
4235 */
4236 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4237 eventfd_signal(t->entries[i].eventfd, 1);
4238
4239 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004240 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004241unlock:
4242 rcu_read_unlock();
4243}
4244
4245static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4246{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004247 while (memcg) {
4248 __mem_cgroup_threshold(memcg, false);
4249 if (do_swap_account)
4250 __mem_cgroup_threshold(memcg, true);
4251
4252 memcg = parent_mem_cgroup(memcg);
4253 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004254}
4255
4256static int compare_thresholds(const void *a, const void *b)
4257{
4258 const struct mem_cgroup_threshold *_a = a;
4259 const struct mem_cgroup_threshold *_b = b;
4260
Greg Thelen2bff24a2013-09-11 14:23:08 -07004261 if (_a->threshold > _b->threshold)
4262 return 1;
4263
4264 if (_a->threshold < _b->threshold)
4265 return -1;
4266
4267 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004268}
4269
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004270static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004271{
4272 struct mem_cgroup_eventfd_list *ev;
4273
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004274 spin_lock(&memcg_oom_lock);
4275
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004276 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004277 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07004278
4279 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004280 return 0;
4281}
4282
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004283static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004284{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004285 struct mem_cgroup *iter;
4286
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004287 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004288 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004289}
4290
Tejun Heo59b6f872013-11-22 18:20:43 -05004291static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004292 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004293{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004294 struct mem_cgroup_thresholds *thresholds;
4295 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004296 unsigned long threshold;
4297 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004298 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004299
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004300 ret = page_counter_memparse(args, &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004301 if (ret)
4302 return ret;
4303
4304 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004305
Johannes Weiner05b84302014-08-06 16:05:59 -07004306 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004307 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004308 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004309 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004310 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004311 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004312 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004313 BUG();
4314
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004315 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004316 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004317 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4318
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004319 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004320
4321 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004322 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004323 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004324 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004325 ret = -ENOMEM;
4326 goto unlock;
4327 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004328 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004329
4330 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004331 if (thresholds->primary) {
4332 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004333 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004334 }
4335
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004336 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004337 new->entries[size - 1].eventfd = eventfd;
4338 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004339
4340 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004341 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004342 compare_thresholds, NULL);
4343
4344 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004345 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004346 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004347 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004348 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004349 * new->current_threshold will not be used until
4350 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004351 * it here.
4352 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004353 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004354 } else
4355 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004356 }
4357
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004358 /* Free old spare buffer and save old primary buffer as spare */
4359 kfree(thresholds->spare);
4360 thresholds->spare = thresholds->primary;
4361
4362 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004363
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004364 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004365 synchronize_rcu();
4366
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004367unlock:
4368 mutex_unlock(&memcg->thresholds_lock);
4369
4370 return ret;
4371}
4372
Tejun Heo59b6f872013-11-22 18:20:43 -05004373static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004374 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004375{
Tejun Heo59b6f872013-11-22 18:20:43 -05004376 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004377}
4378
Tejun Heo59b6f872013-11-22 18:20:43 -05004379static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004380 struct eventfd_ctx *eventfd, const char *args)
4381{
Tejun Heo59b6f872013-11-22 18:20:43 -05004382 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004383}
4384
Tejun Heo59b6f872013-11-22 18:20:43 -05004385static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004386 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004387{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004388 struct mem_cgroup_thresholds *thresholds;
4389 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004390 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004391 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004392
4393 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07004394
4395 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004396 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004397 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07004398 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004399 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04004400 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07004401 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004402 BUG();
4403
Anton Vorontsov371528c2012-02-24 05:14:46 +04004404 if (!thresholds->primary)
4405 goto unlock;
4406
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004407 /* Check if a threshold crossed before removing */
4408 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4409
4410 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004411 size = 0;
4412 for (i = 0; i < thresholds->primary->size; i++) {
4413 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004414 size++;
4415 }
4416
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004417 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004418
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004419 /* Set thresholds array to NULL if we don't have thresholds */
4420 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004421 kfree(new);
4422 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004423 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424 }
4425
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004426 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004427
4428 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004429 new->current_threshold = -1;
4430 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4431 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004432 continue;
4433
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004434 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004435 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004437 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004438 * until rcu_assign_pointer(), so it's safe to increment
4439 * it here.
4440 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004441 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004442 }
4443 j++;
4444 }
4445
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004446swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004447 /* Swap primary and spare array */
4448 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004449 /* If all events are unregistered, free the spare array */
4450 if (!new) {
4451 kfree(thresholds->spare);
4452 thresholds->spare = NULL;
4453 }
4454
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004455 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004456
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004457 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004458 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004459unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004460 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004461}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004462
Tejun Heo59b6f872013-11-22 18:20:43 -05004463static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004464 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004465{
Tejun Heo59b6f872013-11-22 18:20:43 -05004466 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004467}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004468
Tejun Heo59b6f872013-11-22 18:20:43 -05004469static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004470 struct eventfd_ctx *eventfd)
4471{
Tejun Heo59b6f872013-11-22 18:20:43 -05004472 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004473}
4474
Tejun Heo59b6f872013-11-22 18:20:43 -05004475static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004476 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004477{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004478 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004479
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004480 event = kmalloc(sizeof(*event), GFP_KERNEL);
4481 if (!event)
4482 return -ENOMEM;
4483
Michal Hocko1af8efe2011-07-26 16:08:24 -07004484 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004485
4486 event->eventfd = eventfd;
4487 list_add(&event->list, &memcg->oom_notify);
4488
4489 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004490 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004491 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004492 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004493
4494 return 0;
4495}
4496
Tejun Heo59b6f872013-11-22 18:20:43 -05004497static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004498 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004499{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004500 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004501
Michal Hocko1af8efe2011-07-26 16:08:24 -07004502 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004503
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004504 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004505 if (ev->eventfd == eventfd) {
4506 list_del(&ev->list);
4507 kfree(ev);
4508 }
4509 }
4510
Michal Hocko1af8efe2011-07-26 16:08:24 -07004511 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004512}
4513
Tejun Heo2da8ca82013-12-05 12:28:04 -05004514static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004515{
Tejun Heo2da8ca82013-12-05 12:28:04 -05004516 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004517
Tejun Heo791badb2013-12-05 12:28:02 -05004518 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
4519 seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004520 return 0;
4521}
4522
Tejun Heo182446d2013-08-08 20:11:24 -04004523static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004524 struct cftype *cft, u64 val)
4525{
Tejun Heo182446d2013-08-08 20:11:24 -04004526 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004527
4528 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07004529 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004530 return -EINVAL;
4531
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004532 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004533 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004534 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004535
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004536 return 0;
4537}
4538
Andrew Mortonc255a452012-07-31 16:43:02 -07004539#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004540static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004541{
Glauber Costa55007d82012-12-18 14:22:38 -08004542 int ret;
4543
Glauber Costa2633d7a2012-12-18 14:22:34 -08004544 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08004545 ret = memcg_propagate_kmem(memcg);
4546 if (ret)
4547 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08004548
Glauber Costa1d62e432012-04-09 19:36:33 -03004549 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07004550}
Glauber Costae5671df2011-12-11 21:47:01 +00004551
Li Zefan10d5ebf2013-07-08 16:00:33 -07004552static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004553{
Glauber Costa1d62e432012-04-09 19:36:33 -03004554 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07004555}
Glauber Costae5671df2011-12-11 21:47:01 +00004556#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004557static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004558{
4559 return 0;
4560}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004561
Li Zefan10d5ebf2013-07-08 16:00:33 -07004562static void memcg_destroy_kmem(struct mem_cgroup *memcg)
4563{
4564}
Glauber Costae5671df2011-12-11 21:47:01 +00004565#endif
4566
Tejun Heo79bd9812013-11-22 18:20:42 -05004567/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004568 * DO NOT USE IN NEW FILES.
4569 *
4570 * "cgroup.event_control" implementation.
4571 *
4572 * This is way over-engineered. It tries to support fully configurable
4573 * events for each user. Such level of flexibility is completely
4574 * unnecessary especially in the light of the planned unified hierarchy.
4575 *
4576 * Please deprecate this and replace with something simpler if at all
4577 * possible.
4578 */
4579
4580/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004581 * Unregister event and free resources.
4582 *
4583 * Gets called from workqueue.
4584 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004585static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004586{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004587 struct mem_cgroup_event *event =
4588 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004589 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004590
4591 remove_wait_queue(event->wqh, &event->wait);
4592
Tejun Heo59b6f872013-11-22 18:20:43 -05004593 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004594
4595 /* Notify userspace the event is going away. */
4596 eventfd_signal(event->eventfd, 1);
4597
4598 eventfd_ctx_put(event->eventfd);
4599 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004600 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004601}
4602
4603/*
4604 * Gets called on POLLHUP on eventfd when user closes it.
4605 *
4606 * Called with wqh->lock held and interrupts disabled.
4607 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004608static int memcg_event_wake(wait_queue_t *wait, unsigned mode,
4609 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004610{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004611 struct mem_cgroup_event *event =
4612 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004613 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004614 unsigned long flags = (unsigned long)key;
4615
4616 if (flags & POLLHUP) {
4617 /*
4618 * If the event has been detached at cgroup removal, we
4619 * can simply return knowing the other side will cleanup
4620 * for us.
4621 *
4622 * We can't race against event freeing since the other
4623 * side will require wqh->lock via remove_wait_queue(),
4624 * which we hold.
4625 */
Tejun Heofba94802013-11-22 18:20:43 -05004626 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004627 if (!list_empty(&event->list)) {
4628 list_del_init(&event->list);
4629 /*
4630 * We are in atomic context, but cgroup_event_remove()
4631 * may sleep, so we have to call it in workqueue.
4632 */
4633 schedule_work(&event->remove);
4634 }
Tejun Heofba94802013-11-22 18:20:43 -05004635 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004636 }
4637
4638 return 0;
4639}
4640
Tejun Heo3bc942f2013-11-22 18:20:44 -05004641static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004642 wait_queue_head_t *wqh, poll_table *pt)
4643{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004644 struct mem_cgroup_event *event =
4645 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004646
4647 event->wqh = wqh;
4648 add_wait_queue(wqh, &event->wait);
4649}
4650
4651/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004652 * DO NOT USE IN NEW FILES.
4653 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004654 * Parse input and register new cgroup event handler.
4655 *
4656 * Input must be in format '<event_fd> <control_fd> <args>'.
4657 * Interpretation of args is defined by control file implementation.
4658 */
Tejun Heo451af502014-05-13 12:16:21 -04004659static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4660 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004661{
Tejun Heo451af502014-05-13 12:16:21 -04004662 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004663 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004664 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004665 struct cgroup_subsys_state *cfile_css;
4666 unsigned int efd, cfd;
4667 struct fd efile;
4668 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004669 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004670 char *endp;
4671 int ret;
4672
Tejun Heo451af502014-05-13 12:16:21 -04004673 buf = strstrip(buf);
4674
4675 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004676 if (*endp != ' ')
4677 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004678 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004679
Tejun Heo451af502014-05-13 12:16:21 -04004680 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004681 if ((*endp != ' ') && (*endp != '\0'))
4682 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004683 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004684
4685 event = kzalloc(sizeof(*event), GFP_KERNEL);
4686 if (!event)
4687 return -ENOMEM;
4688
Tejun Heo59b6f872013-11-22 18:20:43 -05004689 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004690 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004691 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4692 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4693 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004694
4695 efile = fdget(efd);
4696 if (!efile.file) {
4697 ret = -EBADF;
4698 goto out_kfree;
4699 }
4700
4701 event->eventfd = eventfd_ctx_fileget(efile.file);
4702 if (IS_ERR(event->eventfd)) {
4703 ret = PTR_ERR(event->eventfd);
4704 goto out_put_efile;
4705 }
4706
4707 cfile = fdget(cfd);
4708 if (!cfile.file) {
4709 ret = -EBADF;
4710 goto out_put_eventfd;
4711 }
4712
4713 /* the process need read permission on control file */
4714 /* AV: shouldn't we check that it's been opened for read instead? */
4715 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4716 if (ret < 0)
4717 goto out_put_cfile;
4718
Tejun Heo79bd9812013-11-22 18:20:42 -05004719 /*
Tejun Heofba94802013-11-22 18:20:43 -05004720 * Determine the event callbacks and set them in @event. This used
4721 * to be done via struct cftype but cgroup core no longer knows
4722 * about these events. The following is crude but the whole thing
4723 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004724 *
4725 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004726 */
4727 name = cfile.file->f_dentry->d_name.name;
4728
4729 if (!strcmp(name, "memory.usage_in_bytes")) {
4730 event->register_event = mem_cgroup_usage_register_event;
4731 event->unregister_event = mem_cgroup_usage_unregister_event;
4732 } else if (!strcmp(name, "memory.oom_control")) {
4733 event->register_event = mem_cgroup_oom_register_event;
4734 event->unregister_event = mem_cgroup_oom_unregister_event;
4735 } else if (!strcmp(name, "memory.pressure_level")) {
4736 event->register_event = vmpressure_register_event;
4737 event->unregister_event = vmpressure_unregister_event;
4738 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004739 event->register_event = memsw_cgroup_usage_register_event;
4740 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004741 } else {
4742 ret = -EINVAL;
4743 goto out_put_cfile;
4744 }
4745
4746 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004747 * Verify @cfile should belong to @css. Also, remaining events are
4748 * automatically removed on cgroup destruction but the removal is
4749 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004750 */
Tejun Heoec903c02014-05-13 12:11:01 -04004751 cfile_css = css_tryget_online_from_dir(cfile.file->f_dentry->d_parent,
4752 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004753 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004754 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004755 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004756 if (cfile_css != css) {
4757 css_put(cfile_css);
4758 goto out_put_cfile;
4759 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004760
Tejun Heo451af502014-05-13 12:16:21 -04004761 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004762 if (ret)
4763 goto out_put_css;
4764
4765 efile.file->f_op->poll(efile.file, &event->pt);
4766
Tejun Heofba94802013-11-22 18:20:43 -05004767 spin_lock(&memcg->event_list_lock);
4768 list_add(&event->list, &memcg->event_list);
4769 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004770
4771 fdput(cfile);
4772 fdput(efile);
4773
Tejun Heo451af502014-05-13 12:16:21 -04004774 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004775
4776out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004777 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004778out_put_cfile:
4779 fdput(cfile);
4780out_put_eventfd:
4781 eventfd_ctx_put(event->eventfd);
4782out_put_efile:
4783 fdput(efile);
4784out_kfree:
4785 kfree(event);
4786
4787 return ret;
4788}
4789
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004790static struct cftype mem_cgroup_files[] = {
4791 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004792 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004793 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004794 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004795 },
4796 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004797 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004798 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004799 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004800 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004801 },
4802 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004803 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004804 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004805 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004806 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004807 },
4808 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004809 .name = "soft_limit_in_bytes",
4810 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004811 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004812 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004813 },
4814 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004815 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004816 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004817 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004818 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004819 },
Balbir Singh8697d332008-02-07 00:13:59 -08004820 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004821 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004822 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004823 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004824 {
4825 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004826 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004827 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004828 {
4829 .name = "use_hierarchy",
4830 .write_u64 = mem_cgroup_hierarchy_write,
4831 .read_u64 = mem_cgroup_hierarchy_read,
4832 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004833 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004834 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004835 .write = memcg_write_event_control,
Tejun Heo79bd9812013-11-22 18:20:42 -05004836 .flags = CFTYPE_NO_PREFIX,
4837 .mode = S_IWUGO,
4838 },
4839 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004840 .name = "swappiness",
4841 .read_u64 = mem_cgroup_swappiness_read,
4842 .write_u64 = mem_cgroup_swappiness_write,
4843 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004844 {
4845 .name = "move_charge_at_immigrate",
4846 .read_u64 = mem_cgroup_move_charge_read,
4847 .write_u64 = mem_cgroup_move_charge_write,
4848 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004849 {
4850 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004851 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004852 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004853 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4854 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004855 {
4856 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004857 },
Ying Han406eb0c2011-05-26 16:25:37 -07004858#ifdef CONFIG_NUMA
4859 {
4860 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004861 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004862 },
4863#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004864#ifdef CONFIG_MEMCG_KMEM
4865 {
4866 .name = "kmem.limit_in_bytes",
4867 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004868 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004869 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004870 },
4871 {
4872 .name = "kmem.usage_in_bytes",
4873 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004874 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004875 },
4876 {
4877 .name = "kmem.failcnt",
4878 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004879 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004880 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004881 },
4882 {
4883 .name = "kmem.max_usage_in_bytes",
4884 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004885 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004886 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004887 },
Glauber Costa749c5412012-12-18 14:23:01 -08004888#ifdef CONFIG_SLABINFO
4889 {
4890 .name = "kmem.slabinfo",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004891 .seq_show = mem_cgroup_slabinfo_read,
Glauber Costa749c5412012-12-18 14:23:01 -08004892 },
4893#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004894#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004895 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004896};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004897
Michal Hocko2d110852013-02-22 16:34:43 -08004898#ifdef CONFIG_MEMCG_SWAP
4899static struct cftype memsw_cgroup_files[] = {
4900 {
4901 .name = "memsw.usage_in_bytes",
4902 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004903 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08004904 },
4905 {
4906 .name = "memsw.max_usage_in_bytes",
4907 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004908 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004909 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08004910 },
4911 {
4912 .name = "memsw.limit_in_bytes",
4913 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004914 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004915 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08004916 },
4917 {
4918 .name = "memsw.failcnt",
4919 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004920 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004921 .read_u64 = mem_cgroup_read_u64,
Michal Hocko2d110852013-02-22 16:34:43 -08004922 },
4923 { }, /* terminate */
4924};
4925#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004926static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004927{
4928 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004929 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004930 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004931 /*
4932 * This routine is called against possible nodes.
4933 * But it's BUG to call kmalloc() against offline node.
4934 *
4935 * TODO: this routine can waste much memory for nodes which will
4936 * never be onlined. It's better to use memory hotplug callback
4937 * function.
4938 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004939 if (!node_state(node, N_NORMAL_MEMORY))
4940 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004941 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004942 if (!pn)
4943 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004944
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004945 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4946 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004947 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004948 mz->usage_in_excess = 0;
4949 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004950 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004951 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004952 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004953 return 0;
4954}
4955
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004956static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004957{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004958 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004959}
4960
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004961static struct mem_cgroup *mem_cgroup_alloc(void)
4962{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004963 struct mem_cgroup *memcg;
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004964 size_t size;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004965
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004966 size = sizeof(struct mem_cgroup);
4967 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004968
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004969 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004970 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004971 return NULL;
4972
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004973 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4974 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004975 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004976 spin_lock_init(&memcg->pcp_counter_lock);
4977 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004978
4979out_free:
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004980 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004981 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004982}
4983
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004984/*
Glauber Costac8b2a362012-12-18 14:22:13 -08004985 * At destroying mem_cgroup, references from swap_cgroup can remain.
4986 * (scanning all at force_empty is too costly...)
4987 *
4988 * Instead of clearing all references at force_empty, we remember
4989 * the number of reference from swap_cgroup and free mem_cgroup when
4990 * it goes down to 0.
4991 *
4992 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07004993 */
Glauber Costac8b2a362012-12-18 14:22:13 -08004994
4995static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004996{
Glauber Costac8b2a362012-12-18 14:22:13 -08004997 int node;
Hugh Dickins59927fb2012-03-15 15:17:07 -07004998
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004999 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08005000
5001 for_each_node(node)
5002 free_mem_cgroup_per_zone_info(memcg, node);
5003
5004 free_percpu(memcg->stat);
5005
Glauber Costa3f134612012-05-29 15:07:11 -07005006 /*
5007 * We need to make sure that (at least for now), the jump label
5008 * destruction code runs outside of the cgroup lock. This is because
5009 * get_online_cpus(), which is called from the static_branch update,
5010 * can't be called inside the cgroup_lock. cpusets are the ones
5011 * enforcing this dependency, so if they ever change, we might as well.
5012 *
5013 * schedule_work() will guarantee this happens. Be careful if you need
5014 * to move this code around, and make sure it is outside
5015 * the cgroup_lock.
5016 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005017 disarm_static_keys(memcg);
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08005018 kfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005019}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005020
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005021/*
5022 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5023 */
Glauber Costae1aab162011-12-11 21:47:03 +00005024struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005025{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005026 if (!memcg->memory.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005027 return NULL;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005028 return mem_cgroup_from_counter(memcg->memory.parent, memory);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005029}
Glauber Costae1aab162011-12-11 21:47:03 +00005030EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005031
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07005032static void __init mem_cgroup_soft_limit_tree_init(void)
5033{
5034 struct mem_cgroup_tree_per_node *rtpn;
5035 struct mem_cgroup_tree_per_zone *rtpz;
5036 int tmp, node, zone;
5037
5038 for_each_node(node) {
5039 tmp = node;
5040 if (!node_state(node, N_NORMAL_MEMORY))
5041 tmp = -1;
5042 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5043 BUG_ON(!rtpn);
5044
5045 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5046
5047 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5048 rtpz = &rtpn->rb_tree_per_zone[zone];
5049 rtpz->rb_root = RB_ROOT;
5050 spin_lock_init(&rtpz->lock);
5051 }
5052 }
5053}
5054
Li Zefan0eb253e2009-01-15 13:51:25 -08005055static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04005056mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005057{
Glauber Costad142e3e2013-02-22 16:34:52 -08005058 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005059 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005060 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005061
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005062 memcg = mem_cgroup_alloc();
5063 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005064 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005065
Bob Liu3ed28fa2012-01-12 17:19:04 -08005066 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005067 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005068 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005069
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005070 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04005071 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08005072 root_mem_cgroup = memcg;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005073 page_counter_init(&memcg->memory, NULL);
5074 page_counter_init(&memcg->memsw, NULL);
5075 page_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005076 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005077
Glauber Costad142e3e2013-02-22 16:34:52 -08005078 memcg->last_scanned_node = MAX_NUMNODES;
5079 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005080 memcg->move_charge_at_immigrate = 0;
5081 mutex_init(&memcg->thresholds_lock);
5082 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005083 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05005084 INIT_LIST_HEAD(&memcg->event_list);
5085 spin_lock_init(&memcg->event_list_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08005086
5087 return &memcg->css;
5088
5089free_out:
5090 __mem_cgroup_free(memcg);
5091 return ERR_PTR(error);
5092}
5093
5094static int
Tejun Heoeb954192013-08-08 20:11:23 -04005095mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005096{
Tejun Heoeb954192013-08-08 20:11:23 -04005097 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04005098 struct mem_cgroup *parent = mem_cgroup_from_css(css->parent);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005099 int ret;
Glauber Costad142e3e2013-02-22 16:34:52 -08005100
Tejun Heo15a4c832014-05-04 15:09:14 -04005101 if (css->id > MEM_CGROUP_ID_MAX)
Li Zefan4219b2d2013-09-23 16:56:29 +08005102 return -ENOSPC;
5103
Tejun Heo63876982013-08-08 20:11:23 -04005104 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08005105 return 0;
5106
Glauber Costa09998212013-02-22 16:34:55 -08005107 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08005108
5109 memcg->use_hierarchy = parent->use_hierarchy;
5110 memcg->oom_kill_disable = parent->oom_kill_disable;
5111 memcg->swappiness = mem_cgroup_swappiness(parent);
5112
5113 if (parent->use_hierarchy) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005114 page_counter_init(&memcg->memory, &parent->memory);
5115 page_counter_init(&memcg->memsw, &parent->memsw);
5116 page_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08005117
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005118 /*
Li Zefan8d76a972013-07-08 16:00:36 -07005119 * No need to take a reference to the parent because cgroup
5120 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005121 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08005122 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005123 page_counter_init(&memcg->memory, NULL);
5124 page_counter_init(&memcg->memsw, NULL);
5125 page_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005126 /*
5127 * Deeper hierachy with use_hierarchy == false doesn't make
5128 * much sense so let cgroup subsystem know about this
5129 * unfortunate state in our controller.
5130 */
Glauber Costad142e3e2013-02-22 16:34:52 -08005131 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05005132 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005133 }
Glauber Costa09998212013-02-22 16:34:55 -08005134 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08005135
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07005136 ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
5137 if (ret)
5138 return ret;
5139
5140 /*
5141 * Make sure the memcg is initialized: mem_cgroup_iter()
5142 * orders reading memcg->initialized against its callers
5143 * reading the memcg members.
5144 */
5145 smp_store_release(&memcg->initialized, 1);
5146
5147 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005148}
5149
Tejun Heoeb954192013-08-08 20:11:23 -04005150static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005151{
Tejun Heoeb954192013-08-08 20:11:23 -04005152 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05005153 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05005154
5155 /*
5156 * Unregister events and notify userspace.
5157 * Notify userspace about cgroup removing only after rmdir of cgroup
5158 * directory to avoid race between userspace and kernelspace.
5159 */
Tejun Heofba94802013-11-22 18:20:43 -05005160 spin_lock(&memcg->event_list_lock);
5161 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05005162 list_del_init(&event->list);
5163 schedule_work(&event->remove);
5164 }
Tejun Heofba94802013-11-22 18:20:43 -05005165 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005166
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07005167 memcg_unregister_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07005168 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005169}
5170
Tejun Heoeb954192013-08-08 20:11:23 -04005171static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005172{
Tejun Heoeb954192013-08-08 20:11:23 -04005173 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005174
Li Zefan10d5ebf2013-07-08 16:00:33 -07005175 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07005176 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005177}
5178
Tejun Heo1ced9532014-07-08 18:02:57 -04005179/**
5180 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5181 * @css: the target css
5182 *
5183 * Reset the states of the mem_cgroup associated with @css. This is
5184 * invoked when the userland requests disabling on the default hierarchy
5185 * but the memcg is pinned through dependency. The memcg should stop
5186 * applying policies and should revert to the vanilla state as it may be
5187 * made visible again.
5188 *
5189 * The current implementation only resets the essential configurations.
5190 * This needs to be expanded to cover all the visible parts.
5191 */
5192static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5193{
5194 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5195
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005196 mem_cgroup_resize_limit(memcg, PAGE_COUNTER_MAX);
5197 mem_cgroup_resize_memsw_limit(memcg, PAGE_COUNTER_MAX);
5198 memcg_update_kmem_limit(memcg, PAGE_COUNTER_MAX);
5199 memcg->soft_limit = 0;
Tejun Heo1ced9532014-07-08 18:02:57 -04005200}
5201
Daisuke Nishimura02491442010-03-10 15:22:17 -08005202#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005203/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005204static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005205{
Johannes Weiner05b84302014-08-06 16:05:59 -07005206 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07005207
5208 /* Try a single bulk charge without reclaim first */
Johannes Weiner00501b52014-08-08 14:19:20 -07005209 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07005210 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005211 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005212 return ret;
5213 }
Johannes Weiner692e7c42014-08-06 16:05:57 -07005214 if (ret == -EINTR) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005215 cancel_charge(root_mem_cgroup, count);
Johannes Weiner692e7c42014-08-06 16:05:57 -07005216 return ret;
5217 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005218
5219 /* Try charges one by one with reclaim */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005220 while (count--) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005221 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1);
Johannes Weiner9476db92014-08-06 16:05:55 -07005222 /*
5223 * In case of failure, any residual charges against
5224 * mc.to will be dropped by mem_cgroup_clear_mc()
Johannes Weiner692e7c42014-08-06 16:05:57 -07005225 * later on. However, cancel any charges that are
5226 * bypassed to root right away or they'll be lost.
Johannes Weiner9476db92014-08-06 16:05:55 -07005227 */
Johannes Weiner692e7c42014-08-06 16:05:57 -07005228 if (ret == -EINTR)
Johannes Weiner00501b52014-08-08 14:19:20 -07005229 cancel_charge(root_mem_cgroup, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005230 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005231 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005232 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07005233 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005234 }
Johannes Weiner9476db92014-08-06 16:05:55 -07005235 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005236}
5237
5238/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005239 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005240 * @vma: the vma the pte to be checked belongs
5241 * @addr: the address corresponding to the pte to be checked
5242 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005243 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005244 *
5245 * Returns
5246 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5247 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5248 * move charge. if @target is not NULL, the page is stored in target->page
5249 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005250 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5251 * target for charge migration. if @target is not NULL, the entry is stored
5252 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005253 *
5254 * Called with pte lock held.
5255 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005256union mc_target {
5257 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005258 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005259};
5260
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005261enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005262 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005263 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005264 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005265};
5266
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005267static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5268 unsigned long addr, pte_t ptent)
5269{
5270 struct page *page = vm_normal_page(vma, addr, ptent);
5271
5272 if (!page || !page_mapped(page))
5273 return NULL;
5274 if (PageAnon(page)) {
5275 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005276 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005277 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005278 } else if (!move_file())
5279 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005280 return NULL;
5281 if (!get_page_unless_zero(page))
5282 return NULL;
5283
5284 return page;
5285}
5286
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005287#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005288static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5289 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5290{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005291 struct page *page = NULL;
5292 swp_entry_t ent = pte_to_swp_entry(ptent);
5293
5294 if (!move_anon() || non_swap_entry(ent))
5295 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005296 /*
5297 * Because lookup_swap_cache() updates some statistics counter,
5298 * we call find_get_page() with swapper_space directly.
5299 */
Shaohua Li33806f02013-02-22 16:34:37 -08005300 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005301 if (do_swap_account)
5302 entry->val = ent.val;
5303
5304 return page;
5305}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005306#else
5307static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5308 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5309{
5310 return NULL;
5311}
5312#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005313
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005314static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5315 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5316{
5317 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005318 struct address_space *mapping;
5319 pgoff_t pgoff;
5320
5321 if (!vma->vm_file) /* anonymous vma */
5322 return NULL;
5323 if (!move_file())
5324 return NULL;
5325
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005326 mapping = vma->vm_file->f_mapping;
5327 if (pte_none(ptent))
5328 pgoff = linear_page_index(vma, addr);
5329 else /* pte_file(ptent) is true */
5330 pgoff = pte_to_pgoff(ptent);
5331
5332 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005333#ifdef CONFIG_SWAP
5334 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07005335 if (shmem_mapping(mapping)) {
5336 page = find_get_entry(mapping, pgoff);
5337 if (radix_tree_exceptional_entry(page)) {
5338 swp_entry_t swp = radix_to_swp_entry(page);
5339 if (do_swap_account)
5340 *entry = swp;
5341 page = find_get_page(swap_address_space(swp), swp.val);
5342 }
5343 } else
5344 page = find_get_page(mapping, pgoff);
5345#else
5346 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005347#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005348 return page;
5349}
5350
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005351static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005352 unsigned long addr, pte_t ptent, union mc_target *target)
5353{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005354 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005355 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005356 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005357 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005358
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005359 if (pte_present(ptent))
5360 page = mc_handle_present_pte(vma, addr, ptent);
5361 else if (is_swap_pte(ptent))
5362 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005363 else if (pte_none(ptent) || pte_file(ptent))
5364 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005365
5366 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005367 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005368 if (page) {
5369 pc = lookup_page_cgroup(page);
5370 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005371 * Do only loose check w/o serialization.
5372 * mem_cgroup_move_account() checks the pc is valid or
5373 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08005374 */
5375 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5376 ret = MC_TARGET_PAGE;
5377 if (target)
5378 target->page = page;
5379 }
5380 if (!ret || !target)
5381 put_page(page);
5382 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005383 /* There is a swap entry and a page doesn't exist or isn't charged */
5384 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08005385 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005386 ret = MC_TARGET_SWAP;
5387 if (target)
5388 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005389 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005390 return ret;
5391}
5392
Naoya Horiguchi12724852012-03-21 16:34:28 -07005393#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5394/*
5395 * We don't consider swapping or file mapped pages because THP does not
5396 * support them for now.
5397 * Caller should make sure that pmd_trans_huge(pmd) is true.
5398 */
5399static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5400 unsigned long addr, pmd_t pmd, union mc_target *target)
5401{
5402 struct page *page = NULL;
5403 struct page_cgroup *pc;
5404 enum mc_target_type ret = MC_TARGET_NONE;
5405
5406 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08005407 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005408 if (!move_anon())
5409 return ret;
5410 pc = lookup_page_cgroup(page);
5411 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5412 ret = MC_TARGET_PAGE;
5413 if (target) {
5414 get_page(page);
5415 target->page = page;
5416 }
5417 }
5418 return ret;
5419}
5420#else
5421static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5422 unsigned long addr, pmd_t pmd, union mc_target *target)
5423{
5424 return MC_TARGET_NONE;
5425}
5426#endif
5427
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005428static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5429 unsigned long addr, unsigned long end,
5430 struct mm_walk *walk)
5431{
5432 struct vm_area_struct *vma = walk->private;
5433 pte_t *pte;
5434 spinlock_t *ptl;
5435
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005436 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005437 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5438 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005439 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005440 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005441 }
Dave Hansen03319322011-03-22 16:32:56 -07005442
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005443 if (pmd_trans_unstable(pmd))
5444 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005445 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5446 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005447 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005448 mc.precharge++; /* increment precharge temporarily */
5449 pte_unmap_unlock(pte - 1, ptl);
5450 cond_resched();
5451
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005452 return 0;
5453}
5454
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005455static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5456{
5457 unsigned long precharge;
5458 struct vm_area_struct *vma;
5459
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005460 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005461 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5462 struct mm_walk mem_cgroup_count_precharge_walk = {
5463 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5464 .mm = mm,
5465 .private = vma,
5466 };
5467 if (is_vm_hugetlb_page(vma))
5468 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005469 walk_page_range(vma->vm_start, vma->vm_end,
5470 &mem_cgroup_count_precharge_walk);
5471 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005472 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005473
5474 precharge = mc.precharge;
5475 mc.precharge = 0;
5476
5477 return precharge;
5478}
5479
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005480static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5481{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005482 unsigned long precharge = mem_cgroup_count_precharge(mm);
5483
5484 VM_BUG_ON(mc.moving_task);
5485 mc.moving_task = current;
5486 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005487}
5488
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005489/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5490static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005491{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005492 struct mem_cgroup *from = mc.from;
5493 struct mem_cgroup *to = mc.to;
5494
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005495 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005496 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005497 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005498 mc.precharge = 0;
5499 }
5500 /*
5501 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5502 * we must uncharge here.
5503 */
5504 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005505 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005506 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005507 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005508 /* we must fixup refcnts and charges */
5509 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005510 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005511 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005512 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
5513
5514 /*
5515 * we charged both to->memory and to->memsw, so we
5516 * should uncharge to->memory.
5517 */
5518 if (!mem_cgroup_is_root(mc.to))
5519 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07005520
Johannes Weinere8ea14c2014-12-10 15:42:42 -08005521 css_put_many(&mc.from->css, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005522
Li Zefan40503772013-07-08 16:00:34 -07005523 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005524 mc.moved_swap = 0;
5525 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005526 memcg_oom_recover(from);
5527 memcg_oom_recover(to);
5528 wake_up_all(&mc.waitq);
5529}
5530
5531static void mem_cgroup_clear_mc(void)
5532{
5533 struct mem_cgroup *from = mc.from;
5534
5535 /*
5536 * we must clear moving_task before waking up waiters at the end of
5537 * task migration.
5538 */
5539 mc.moving_task = NULL;
5540 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005541 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005542 mc.from = NULL;
5543 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005544 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005545 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005546}
5547
Tejun Heoeb954192013-08-08 20:11:23 -04005548static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005549 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005550{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005551 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005552 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04005553 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08005554 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005555
Glauber Costaee5e8472013-02-22 16:34:50 -08005556 /*
5557 * We are now commited to this value whatever it is. Changes in this
5558 * tunable will only affect upcoming migrations, not the current one.
5559 * So we need to save it, and keep it going.
5560 */
5561 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
5562 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005563 struct mm_struct *mm;
5564 struct mem_cgroup *from = mem_cgroup_from_task(p);
5565
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005566 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005567
5568 mm = get_task_mm(p);
5569 if (!mm)
5570 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005571 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005572 if (mm->owner == p) {
5573 VM_BUG_ON(mc.from);
5574 VM_BUG_ON(mc.to);
5575 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005576 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005577 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005578 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005579 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005580 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005581 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08005582 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005583 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005584 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005585
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005586 ret = mem_cgroup_precharge_mc(mm);
5587 if (ret)
5588 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005589 }
5590 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005591 }
5592 return ret;
5593}
5594
Tejun Heoeb954192013-08-08 20:11:23 -04005595static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005596 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005597{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005598 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005599}
5600
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005601static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5602 unsigned long addr, unsigned long end,
5603 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005604{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005605 int ret = 0;
5606 struct vm_area_struct *vma = walk->private;
5607 pte_t *pte;
5608 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005609 enum mc_target_type target_type;
5610 union mc_target target;
5611 struct page *page;
5612 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005613
Naoya Horiguchi12724852012-03-21 16:34:28 -07005614 /*
5615 * We don't take compound_lock() here but no race with splitting thp
5616 * happens because:
5617 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5618 * under splitting, which means there's no concurrent thp split,
5619 * - if another thread runs into split_huge_page() just after we
5620 * entered this if-block, the thread must wait for page table lock
5621 * to be unlocked in __split_huge_page_splitting(), where the main
5622 * part of thp split is not executed yet.
5623 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005624 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005625 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005626 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005627 return 0;
5628 }
5629 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5630 if (target_type == MC_TARGET_PAGE) {
5631 page = target.page;
5632 if (!isolate_lru_page(page)) {
5633 pc = lookup_page_cgroup(page);
5634 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005635 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005636 mc.precharge -= HPAGE_PMD_NR;
5637 mc.moved_charge += HPAGE_PMD_NR;
5638 }
5639 putback_lru_page(page);
5640 }
5641 put_page(page);
5642 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005643 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005644 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005645 }
5646
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005647 if (pmd_trans_unstable(pmd))
5648 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005649retry:
5650 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5651 for (; addr != end; addr += PAGE_SIZE) {
5652 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005653 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005654
5655 if (!mc.precharge)
5656 break;
5657
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005658 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005659 case MC_TARGET_PAGE:
5660 page = target.page;
5661 if (isolate_lru_page(page))
5662 goto put;
5663 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005664 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005665 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005666 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005667 /* we uncharge from mc.from later. */
5668 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005669 }
5670 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005671put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005672 put_page(page);
5673 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005674 case MC_TARGET_SWAP:
5675 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005676 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005677 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005678 /* we fixup refcnts and charges later. */
5679 mc.moved_swap++;
5680 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005681 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005682 default:
5683 break;
5684 }
5685 }
5686 pte_unmap_unlock(pte - 1, ptl);
5687 cond_resched();
5688
5689 if (addr != end) {
5690 /*
5691 * We have consumed all precharges we got in can_attach().
5692 * We try charge one by one, but don't do any additional
5693 * charges to mc.to if we have failed in charge once in attach()
5694 * phase.
5695 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005696 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005697 if (!ret)
5698 goto retry;
5699 }
5700
5701 return ret;
5702}
5703
5704static void mem_cgroup_move_charge(struct mm_struct *mm)
5705{
5706 struct vm_area_struct *vma;
5707
5708 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005709retry:
5710 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5711 /*
5712 * Someone who are holding the mmap_sem might be waiting in
5713 * waitq. So we cancel all extra charges, wake up all waiters,
5714 * and retry. Because we cancel precharges, we might not be able
5715 * to move enough charges, but moving charge is a best-effort
5716 * feature anyway, so it wouldn't be a big problem.
5717 */
5718 __mem_cgroup_clear_mc();
5719 cond_resched();
5720 goto retry;
5721 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005722 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5723 int ret;
5724 struct mm_walk mem_cgroup_move_charge_walk = {
5725 .pmd_entry = mem_cgroup_move_charge_pte_range,
5726 .mm = mm,
5727 .private = vma,
5728 };
5729 if (is_vm_hugetlb_page(vma))
5730 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005731 ret = walk_page_range(vma->vm_start, vma->vm_end,
5732 &mem_cgroup_move_charge_walk);
5733 if (ret)
5734 /*
5735 * means we have consumed all precharges and failed in
5736 * doing additional charge. Just abandon here.
5737 */
5738 break;
5739 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005740 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005741}
5742
Tejun Heoeb954192013-08-08 20:11:23 -04005743static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005744 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005745{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005746 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005747 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005748
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005749 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005750 if (mc.to)
5751 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005752 mmput(mm);
5753 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005754 if (mc.to)
5755 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005756}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005757#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04005758static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005759 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005760{
5761 return 0;
5762}
Tejun Heoeb954192013-08-08 20:11:23 -04005763static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005764 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005765{
5766}
Tejun Heoeb954192013-08-08 20:11:23 -04005767static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef52012-01-31 13:47:36 +08005768 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005769{
5770}
5771#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005772
Tejun Heof00baae2013-04-15 13:41:15 -07005773/*
5774 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04005775 * to verify whether we're attached to the default hierarchy on each mount
5776 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07005777 */
Tejun Heoeb954192013-08-08 20:11:23 -04005778static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07005779{
5780 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04005781 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07005782 * guarantees that @root doesn't have any children, so turning it
5783 * on for the root memcg is enough.
5784 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04005785 if (cgroup_on_dfl(root_css->cgroup))
Tejun Heoeb954192013-08-08 20:11:23 -04005786 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07005787}
5788
Tejun Heo073219e2014-02-08 10:36:58 -05005789struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005790 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08005791 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08005792 .css_offline = mem_cgroup_css_offline,
5793 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04005794 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005795 .can_attach = mem_cgroup_can_attach,
5796 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005797 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07005798 .bind = mem_cgroup_bind,
Tejun Heo55779642014-07-15 11:05:09 -04005799 .legacy_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005800 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005801};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005802
Andrew Mortonc255a452012-07-31 16:43:02 -07005803#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005804static int __init enable_swap_account(char *s)
5805{
Michal Hockoa2c89902011-05-24 17:12:50 -07005806 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005807 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005808 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005809 really_do_swap_account = 0;
5810 return 1;
5811}
Michal Hockoa2c89902011-05-24 17:12:50 -07005812__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005813
Michal Hocko2d110852013-02-22 16:34:43 -08005814static void __init memsw_file_init(void)
5815{
Tejun Heo2cf669a2014-07-15 11:05:09 -04005816 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
5817 memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08005818}
Michal Hocko6acc8b02013-02-22 16:34:45 -08005819
5820static void __init enable_swap_cgroup(void)
5821{
5822 if (!mem_cgroup_disabled() && really_do_swap_account) {
5823 do_swap_account = 1;
5824 memsw_file_init();
5825 }
5826}
5827
Michal Hocko2d110852013-02-22 16:34:43 -08005828#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08005829static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08005830{
5831}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005832#endif
Michal Hocko2d110852013-02-22 16:34:43 -08005833
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005834#ifdef CONFIG_MEMCG_SWAP
5835/**
5836 * mem_cgroup_swapout - transfer a memsw charge to swap
5837 * @page: page whose memsw charge to transfer
5838 * @entry: swap entry to move the charge to
5839 *
5840 * Transfer the memsw charge of @page to @entry.
5841 */
5842void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
5843{
5844 struct page_cgroup *pc;
5845 unsigned short oldid;
5846
5847 VM_BUG_ON_PAGE(PageLRU(page), page);
5848 VM_BUG_ON_PAGE(page_count(page), page);
5849
5850 if (!do_swap_account)
5851 return;
5852
5853 pc = lookup_page_cgroup(page);
5854
5855 /* Readahead page, never charged */
5856 if (!PageCgroupUsed(pc))
5857 return;
5858
5859 VM_BUG_ON_PAGE(!(pc->flags & PCG_MEMSW), page);
5860
5861 oldid = swap_cgroup_record(entry, mem_cgroup_id(pc->mem_cgroup));
5862 VM_BUG_ON_PAGE(oldid, page);
5863
5864 pc->flags &= ~PCG_MEMSW;
5865 css_get(&pc->mem_cgroup->css);
5866 mem_cgroup_swap_statistics(pc->mem_cgroup, true);
5867}
5868
5869/**
5870 * mem_cgroup_uncharge_swap - uncharge a swap entry
5871 * @entry: swap entry to uncharge
5872 *
5873 * Drop the memsw charge associated with @entry.
5874 */
5875void mem_cgroup_uncharge_swap(swp_entry_t entry)
5876{
5877 struct mem_cgroup *memcg;
5878 unsigned short id;
5879
5880 if (!do_swap_account)
5881 return;
5882
5883 id = swap_cgroup_record(entry, 0);
5884 rcu_read_lock();
5885 memcg = mem_cgroup_lookup(id);
5886 if (memcg) {
Johannes Weinerce00a962014-09-05 08:43:57 -04005887 if (!mem_cgroup_is_root(memcg))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005888 page_counter_uncharge(&memcg->memsw, 1);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005889 mem_cgroup_swap_statistics(memcg, false);
5890 css_put(&memcg->css);
5891 }
5892 rcu_read_unlock();
5893}
5894#endif
5895
Johannes Weiner00501b52014-08-08 14:19:20 -07005896/**
5897 * mem_cgroup_try_charge - try charging a page
5898 * @page: page to charge
5899 * @mm: mm context of the victim
5900 * @gfp_mask: reclaim mode
5901 * @memcgp: charged memcg return
5902 *
5903 * Try to charge @page to the memcg that @mm belongs to, reclaiming
5904 * pages according to @gfp_mask if necessary.
5905 *
5906 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
5907 * Otherwise, an error code is returned.
5908 *
5909 * After page->mapping has been set up, the caller must finalize the
5910 * charge with mem_cgroup_commit_charge(). Or abort the transaction
5911 * with mem_cgroup_cancel_charge() in case page instantiation fails.
5912 */
5913int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
5914 gfp_t gfp_mask, struct mem_cgroup **memcgp)
5915{
5916 struct mem_cgroup *memcg = NULL;
5917 unsigned int nr_pages = 1;
5918 int ret = 0;
5919
5920 if (mem_cgroup_disabled())
5921 goto out;
5922
5923 if (PageSwapCache(page)) {
5924 struct page_cgroup *pc = lookup_page_cgroup(page);
5925 /*
5926 * Every swap fault against a single page tries to charge the
5927 * page, bail as early as possible. shmem_unuse() encounters
5928 * already charged pages, too. The USED bit is protected by
5929 * the page lock, which serializes swap cache removal, which
5930 * in turn serializes uncharging.
5931 */
5932 if (PageCgroupUsed(pc))
5933 goto out;
5934 }
5935
5936 if (PageTransHuge(page)) {
5937 nr_pages <<= compound_order(page);
5938 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5939 }
5940
5941 if (do_swap_account && PageSwapCache(page))
5942 memcg = try_get_mem_cgroup_from_page(page);
5943 if (!memcg)
5944 memcg = get_mem_cgroup_from_mm(mm);
5945
5946 ret = try_charge(memcg, gfp_mask, nr_pages);
5947
5948 css_put(&memcg->css);
5949
5950 if (ret == -EINTR) {
5951 memcg = root_mem_cgroup;
5952 ret = 0;
5953 }
5954out:
5955 *memcgp = memcg;
5956 return ret;
5957}
5958
5959/**
5960 * mem_cgroup_commit_charge - commit a page charge
5961 * @page: page to charge
5962 * @memcg: memcg to charge the page to
5963 * @lrucare: page might be on LRU already
5964 *
5965 * Finalize a charge transaction started by mem_cgroup_try_charge(),
5966 * after page->mapping has been set up. This must happen atomically
5967 * as part of the page instantiation, i.e. under the page table lock
5968 * for anonymous pages, under the page lock for page and swap cache.
5969 *
5970 * In addition, the page must not be on the LRU during the commit, to
5971 * prevent racing with task migration. If it might be, use @lrucare.
5972 *
5973 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
5974 */
5975void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
5976 bool lrucare)
5977{
5978 unsigned int nr_pages = 1;
5979
5980 VM_BUG_ON_PAGE(!page->mapping, page);
5981 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
5982
5983 if (mem_cgroup_disabled())
5984 return;
5985 /*
5986 * Swap faults will attempt to charge the same page multiple
5987 * times. But reuse_swap_page() might have removed the page
5988 * from swapcache already, so we can't check PageSwapCache().
5989 */
5990 if (!memcg)
5991 return;
5992
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005993 commit_charge(page, memcg, lrucare);
5994
Johannes Weiner00501b52014-08-08 14:19:20 -07005995 if (PageTransHuge(page)) {
5996 nr_pages <<= compound_order(page);
5997 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5998 }
5999
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006000 local_irq_disable();
6001 mem_cgroup_charge_statistics(memcg, page, nr_pages);
6002 memcg_check_events(memcg, page);
6003 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07006004
6005 if (do_swap_account && PageSwapCache(page)) {
6006 swp_entry_t entry = { .val = page_private(page) };
6007 /*
6008 * The swap entry might not get freed for a long time,
6009 * let's not wait for it. The page already received a
6010 * memory+swap charge, drop the swap entry duplicate.
6011 */
6012 mem_cgroup_uncharge_swap(entry);
6013 }
6014}
6015
6016/**
6017 * mem_cgroup_cancel_charge - cancel a page charge
6018 * @page: page to charge
6019 * @memcg: memcg to charge the page to
6020 *
6021 * Cancel a charge transaction started by mem_cgroup_try_charge().
6022 */
6023void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg)
6024{
6025 unsigned int nr_pages = 1;
6026
6027 if (mem_cgroup_disabled())
6028 return;
6029 /*
6030 * Swap faults will attempt to charge the same page multiple
6031 * times. But reuse_swap_page() might have removed the page
6032 * from swapcache already, so we can't check PageSwapCache().
6033 */
6034 if (!memcg)
6035 return;
6036
6037 if (PageTransHuge(page)) {
6038 nr_pages <<= compound_order(page);
6039 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6040 }
6041
6042 cancel_charge(memcg, nr_pages);
6043}
6044
Johannes Weiner747db952014-08-08 14:19:24 -07006045static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
6046 unsigned long nr_mem, unsigned long nr_memsw,
6047 unsigned long nr_anon, unsigned long nr_file,
6048 unsigned long nr_huge, struct page *dummy_page)
6049{
6050 unsigned long flags;
6051
Johannes Weinerce00a962014-09-05 08:43:57 -04006052 if (!mem_cgroup_is_root(memcg)) {
6053 if (nr_mem)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08006054 page_counter_uncharge(&memcg->memory, nr_mem);
Johannes Weinerce00a962014-09-05 08:43:57 -04006055 if (nr_memsw)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08006056 page_counter_uncharge(&memcg->memsw, nr_memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04006057 memcg_oom_recover(memcg);
6058 }
Johannes Weiner747db952014-08-08 14:19:24 -07006059
6060 local_irq_save(flags);
6061 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
6062 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
6063 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
6064 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
6065 __this_cpu_add(memcg->stat->nr_page_events, nr_anon + nr_file);
6066 memcg_check_events(memcg, dummy_page);
6067 local_irq_restore(flags);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08006068
6069 if (!mem_cgroup_is_root(memcg))
6070 css_put_many(&memcg->css, max(nr_mem, nr_memsw));
Johannes Weiner747db952014-08-08 14:19:24 -07006071}
6072
6073static void uncharge_list(struct list_head *page_list)
6074{
6075 struct mem_cgroup *memcg = NULL;
6076 unsigned long nr_memsw = 0;
6077 unsigned long nr_anon = 0;
6078 unsigned long nr_file = 0;
6079 unsigned long nr_huge = 0;
6080 unsigned long pgpgout = 0;
6081 unsigned long nr_mem = 0;
6082 struct list_head *next;
6083 struct page *page;
6084
6085 next = page_list->next;
6086 do {
6087 unsigned int nr_pages = 1;
6088 struct page_cgroup *pc;
6089
6090 page = list_entry(next, struct page, lru);
6091 next = page->lru.next;
6092
6093 VM_BUG_ON_PAGE(PageLRU(page), page);
6094 VM_BUG_ON_PAGE(page_count(page), page);
6095
6096 pc = lookup_page_cgroup(page);
6097 if (!PageCgroupUsed(pc))
6098 continue;
6099
6100 /*
6101 * Nobody should be changing or seriously looking at
6102 * pc->mem_cgroup and pc->flags at this point, we have
6103 * fully exclusive access to the page.
6104 */
6105
6106 if (memcg != pc->mem_cgroup) {
6107 if (memcg) {
6108 uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw,
6109 nr_anon, nr_file, nr_huge, page);
6110 pgpgout = nr_mem = nr_memsw = 0;
6111 nr_anon = nr_file = nr_huge = 0;
6112 }
6113 memcg = pc->mem_cgroup;
6114 }
6115
6116 if (PageTransHuge(page)) {
6117 nr_pages <<= compound_order(page);
6118 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
6119 nr_huge += nr_pages;
6120 }
6121
6122 if (PageAnon(page))
6123 nr_anon += nr_pages;
6124 else
6125 nr_file += nr_pages;
6126
6127 if (pc->flags & PCG_MEM)
6128 nr_mem += nr_pages;
6129 if (pc->flags & PCG_MEMSW)
6130 nr_memsw += nr_pages;
6131 pc->flags = 0;
6132
6133 pgpgout++;
6134 } while (next != page_list);
6135
6136 if (memcg)
6137 uncharge_batch(memcg, pgpgout, nr_mem, nr_memsw,
6138 nr_anon, nr_file, nr_huge, page);
6139}
6140
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006141/**
6142 * mem_cgroup_uncharge - uncharge a page
6143 * @page: page to uncharge
6144 *
6145 * Uncharge a page previously charged with mem_cgroup_try_charge() and
6146 * mem_cgroup_commit_charge().
6147 */
6148void mem_cgroup_uncharge(struct page *page)
6149{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006150 struct page_cgroup *pc;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006151
6152 if (mem_cgroup_disabled())
6153 return;
6154
Johannes Weiner747db952014-08-08 14:19:24 -07006155 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006156 pc = lookup_page_cgroup(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006157 if (!PageCgroupUsed(pc))
6158 return;
6159
Johannes Weiner747db952014-08-08 14:19:24 -07006160 INIT_LIST_HEAD(&page->lru);
6161 uncharge_list(&page->lru);
6162}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006163
Johannes Weiner747db952014-08-08 14:19:24 -07006164/**
6165 * mem_cgroup_uncharge_list - uncharge a list of page
6166 * @page_list: list of pages to uncharge
6167 *
6168 * Uncharge a list of pages previously charged with
6169 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
6170 */
6171void mem_cgroup_uncharge_list(struct list_head *page_list)
6172{
6173 if (mem_cgroup_disabled())
6174 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006175
Johannes Weiner747db952014-08-08 14:19:24 -07006176 if (!list_empty(page_list))
6177 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006178}
6179
6180/**
6181 * mem_cgroup_migrate - migrate a charge to another page
6182 * @oldpage: currently charged page
6183 * @newpage: page to transfer the charge to
6184 * @lrucare: both pages might be on the LRU already
6185 *
6186 * Migrate the charge from @oldpage to @newpage.
6187 *
6188 * Both pages must be locked, @newpage->mapping must be set up.
6189 */
6190void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
6191 bool lrucare)
6192{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006193 struct page_cgroup *pc;
6194 int isolated;
6195
6196 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
6197 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
6198 VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage);
6199 VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage);
6200 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006201 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
6202 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006203
6204 if (mem_cgroup_disabled())
6205 return;
6206
6207 /* Page cache replacement: new page already charged? */
6208 pc = lookup_page_cgroup(newpage);
6209 if (PageCgroupUsed(pc))
6210 return;
6211
6212 /* Re-entrant migration: old page already uncharged? */
6213 pc = lookup_page_cgroup(oldpage);
6214 if (!PageCgroupUsed(pc))
6215 return;
6216
6217 VM_BUG_ON_PAGE(!(pc->flags & PCG_MEM), oldpage);
6218 VM_BUG_ON_PAGE(do_swap_account && !(pc->flags & PCG_MEMSW), oldpage);
6219
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006220 if (lrucare)
6221 lock_page_lru(oldpage, &isolated);
6222
6223 pc->flags = 0;
6224
6225 if (lrucare)
6226 unlock_page_lru(oldpage, isolated);
6227
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006228 commit_charge(newpage, pc->mem_cgroup, lrucare);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006229}
6230
Michal Hocko2d110852013-02-22 16:34:43 -08006231/*
Michal Hocko10813122013-02-22 16:35:41 -08006232 * subsys_initcall() for memory controller.
6233 *
6234 * Some parts like hotcpu_notifier() have to be initialized from this context
6235 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6236 * everything that doesn't depend on a specific mem_cgroup structure should
6237 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006238 */
6239static int __init mem_cgroup_init(void)
6240{
6241 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006242 enable_swap_cgroup();
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07006243 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08006244 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006245 return 0;
6246}
6247subsys_initcall(mem_cgroup_init);