blob: c23c1a3e8e16af1adad1c453dadb9931fd1d93d3 [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 *
Johannes Weiner1575e682015-04-14 15:44:51 -070017 * Native page reclaim
18 * Charge lifetime sanitation
19 * Lockless page tracking & accounting
20 * Unified hierarchy configuration model
21 * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner
22 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080023 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 */
33
Johannes Weiner3e32cb22014-12-10 15:42:31 -080034#include <linux/page_counter.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080035#include <linux/memcontrol.h>
36#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080037#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080038#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080039#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080040#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080041#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080042#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080043#include <linux/bit_spinlock.h>
44#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070045#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040046#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080047#include <linux/mutex.h>
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -070048#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070049#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080050#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080051#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080052#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080053#include <linux/eventfd.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050054#include <linux/poll.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080055#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080056#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080057#include <linux/seq_file.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070058#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070059#include <linux/mm_inline.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -080060#include <linux/swap_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080061#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070062#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070063#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050064#include <linux/file.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080065#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000066#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070067#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000068#include <net/tcp_memcontrol.h>
Qiang Huangf35c3a82013-11-12 15:08:22 -080069#include "slab.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080070
Balbir Singh8697d332008-02-07 00:13:59 -080071#include <asm/uaccess.h>
72
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070073#include <trace/events/vmscan.h>
74
Tejun Heo073219e2014-02-08 10:36:58 -050075struct cgroup_subsys memory_cgrp_subsys __read_mostly;
76EXPORT_SYMBOL(memory_cgrp_subsys);
David Rientjes68ae5642012-12-12 13:51:57 -080077
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070078#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070079static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080080
Johannes Weiner21afa382015-02-11 15:26:36 -080081/* Whether the swap controller is active */
Andrew Mortonc255a452012-07-31 16:43:02 -070082#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080083int do_swap_account __read_mostly;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080084#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070085#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080086#endif
87
Johannes Weineraf7c4b02012-05-29 15:07:08 -070088static const char * const mem_cgroup_stat_names[] = {
89 "cache",
90 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070091 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070092 "mapped_file",
Greg Thelenc4843a72015-05-22 17:13:16 -040093 "dirty",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070094 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070095 "swap",
96};
97
Johannes Weineraf7c4b02012-05-29 15:07:08 -070098static const char * const mem_cgroup_events_names[] = {
99 "pgpgin",
100 "pgpgout",
101 "pgfault",
102 "pgmajfault",
103};
104
Sha Zhengju58cf1882013-02-22 16:32:05 -0800105static const char * const mem_cgroup_lru_names[] = {
106 "inactive_anon",
107 "active_anon",
108 "inactive_file",
109 "active_file",
110 "unevictable",
111};
112
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700113/*
114 * Per memcg event counter is incremented at every pagein/pageout. With THP,
115 * it will be incremated by the number of pages. This counter is used for
116 * for trigger some periodic events. This is straightforward and better
117 * than using jiffies etc. to handle periodic memcg event.
118 */
119enum mem_cgroup_events_target {
120 MEM_CGROUP_TARGET_THRESH,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700121 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700122 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123 MEM_CGROUP_NTARGETS,
124};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700125#define THRESHOLDS_EVENTS_TARGET 128
126#define SOFTLIMIT_EVENTS_TARGET 1024
127#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700128
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800129struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700130 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weiner241994e2015-02-11 15:26:06 -0800131 unsigned long events[MEMCG_NR_EVENTS];
Johannes Weiner13114712012-05-29 15:07:07 -0700132 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800134};
135
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800136struct reclaim_iter {
137 struct mem_cgroup *position;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800138 /* scan generation, increased every round-trip */
139 unsigned int generation;
140};
141
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800142/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800143 * per-zone information in memory controller.
144 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800145struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800146 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700147 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800148
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800149 struct reclaim_iter iter[DEF_PRIORITY + 1];
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800150
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700151 struct rb_node tree_node; /* RB tree node */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800152 unsigned long usage_in_excess;/* Set to the value by which */
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700153 /* the soft limit is exceeded*/
154 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700155 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700156 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800157};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800158
159struct mem_cgroup_per_node {
160 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
161};
162
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700163/*
164 * Cgroups above their limits are maintained in a RB-Tree, independent of
165 * their hierarchy representation
166 */
167
168struct mem_cgroup_tree_per_zone {
169 struct rb_root rb_root;
170 spinlock_t lock;
171};
172
173struct mem_cgroup_tree_per_node {
174 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
175};
176
177struct mem_cgroup_tree {
178 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
179};
180
181static struct mem_cgroup_tree soft_limit_tree __read_mostly;
182
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800183struct mem_cgroup_threshold {
184 struct eventfd_ctx *eventfd;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800185 unsigned long threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800186};
187
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700188/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800189struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700190 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700191 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800192 /* Size of entries[] */
193 unsigned int size;
194 /* Array of thresholds */
195 struct mem_cgroup_threshold entries[0];
196};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700197
198struct mem_cgroup_thresholds {
199 /* Primary thresholds array */
200 struct mem_cgroup_threshold_ary *primary;
201 /*
202 * Spare threshold array.
203 * This is needed to make mem_cgroup_unregister_event() "never fail".
204 * It must be able to store at least primary->size - 1 entries.
205 */
206 struct mem_cgroup_threshold_ary *spare;
207};
208
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700209/* for OOM */
210struct mem_cgroup_eventfd_list {
211 struct list_head list;
212 struct eventfd_ctx *eventfd;
213};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800214
Tejun Heo79bd9812013-11-22 18:20:42 -0500215/*
216 * cgroup_event represents events which userspace want to receive.
217 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500218struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500219 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500220 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500221 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500222 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500223 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500224 * eventfd to signal userspace about the event.
225 */
226 struct eventfd_ctx *eventfd;
227 /*
228 * Each of these stored in a list by the cgroup.
229 */
230 struct list_head list;
231 /*
Tejun Heofba94802013-11-22 18:20:43 -0500232 * register_event() callback will be used to add new userspace
233 * waiter for changes related to this event. Use eventfd_signal()
234 * on eventfd to send notification to userspace.
235 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500236 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500237 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500238 /*
239 * unregister_event() callback will be called when userspace closes
240 * the eventfd or on cgroup removing. This callback must be set,
241 * if you want provide notification functionality.
242 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500243 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500244 struct eventfd_ctx *eventfd);
245 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500246 * All fields below needed to unregister event when
247 * userspace closes eventfd.
248 */
249 poll_table pt;
250 wait_queue_head_t *wqh;
251 wait_queue_t wait;
252 struct work_struct remove;
253};
254
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700255static void mem_cgroup_threshold(struct mem_cgroup *memcg);
256static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800257
Balbir Singhf64c3f52009-09-23 15:56:37 -0700258/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800259 * The memory controller data structure. The memory controller controls both
260 * page cache and RSS per cgroup. We would eventually like to provide
261 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
262 * to help the administrator determine what knobs to tune.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800263 */
264struct mem_cgroup {
265 struct cgroup_subsys_state css;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800266
267 /* Accounted resources */
268 struct page_counter memory;
269 struct page_counter memsw;
270 struct page_counter kmem;
271
Johannes Weiner241994e2015-02-11 15:26:06 -0800272 /* Normal memory consumption range */
273 unsigned long low;
274 unsigned long high;
275
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800276 unsigned long soft_limit;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700277
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700278 /* vmpressure notifications */
279 struct vmpressure vmpressure;
280
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -0700281 /* css_online() has been completed */
282 int initialized;
283
Li Zefan465939a2013-07-08 16:00:38 -0700284 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800285 * Should the accounting and control be hierarchical, per subtree?
286 */
287 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700288
289 bool oom_lock;
290 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700291 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700292
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700293 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700294 /* OOM-Killer disable */
295 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800296
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800297 /* protect arrays of thresholds */
298 struct mutex thresholds_lock;
299
300 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700301 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700302
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800303 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700304 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700305
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700306 /* For oom notifier event fd */
307 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700308
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800309 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800310 * Should we move charges of a task when a task is moved into this
311 * mem_cgroup ? And what type of charges should we move ?
312 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700313 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800314 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700315 * set > 0 if pages under this cgroup are moving to other cgroup.
316 */
Johannes Weiner6de22612015-02-11 15:25:01 -0800317 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700318 /* taken only while moving_account > 0 */
Johannes Weiner6de22612015-02-11 15:25:01 -0800319 spinlock_t move_lock;
320 struct task_struct *move_lock_task;
321 unsigned long move_lock_flags;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700322 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800323 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800324 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700325 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700326 /*
327 * used when a cpu is offlined or other synchronizations
328 * See mem_cgroup_read_stat().
329 */
330 struct mem_cgroup_stat_cpu nocpu_base;
331 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000332
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700333#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700334 struct cg_proto tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000335#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800336#if defined(CONFIG_MEMCG_KMEM)
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800337 /* Index in the kmem_cache->memcg_params.memcg_caches array */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800338 int kmemcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800339 bool kmem_acct_activated;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -0800340 bool kmem_acct_active;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800341#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800342
343 int last_scanned_node;
344#if MAX_NUMNODES > 1
345 nodemask_t scan_nodes;
346 atomic_t numainfo_events;
347 atomic_t numainfo_updating;
348#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700349
Tejun Heofba94802013-11-22 18:20:43 -0500350 /* List of events which userspace want to receive */
351 struct list_head event_list;
352 spinlock_t event_list_lock;
353
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700354 struct mem_cgroup_per_node *nodeinfo[0];
355 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800356};
357
Glauber Costa510fc4e2012-12-18 14:21:47 -0800358#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovcb731d62015-02-12 14:58:54 -0800359bool memcg_kmem_is_active(struct mem_cgroup *memcg)
Glauber Costa7de37682012-12-18 14:22:07 -0800360{
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -0800361 return memcg->kmem_acct_active;
Glauber Costa7de37682012-12-18 14:22:07 -0800362}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800363#endif
364
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800365/* Stuffs for move charges at task migration. */
366/*
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800367 * Types of charges to be moved.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800368 */
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800369#define MOVE_ANON 0x1U
370#define MOVE_FILE 0x2U
371#define MOVE_MASK (MOVE_ANON | MOVE_FILE)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800372
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800373/* "mc" and its members are protected by cgroup_mutex */
374static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800375 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800376 struct mem_cgroup *from;
377 struct mem_cgroup *to;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800378 unsigned long flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800379 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800380 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800381 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800382 struct task_struct *moving_task; /* a task moving charges */
383 wait_queue_head_t waitq; /* a waitq for other context */
384} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700385 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800386 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
387};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800388
Balbir Singh4e416952009-09-23 15:56:39 -0700389/*
390 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
391 * limit reclaim to prevent infinite loops, if they ever occur.
392 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700393#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700394#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700395
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800396enum charge_type {
397 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700398 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800399 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700400 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700401 NR_CHARGE_TYPE,
402};
403
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800404/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800405enum res_type {
406 _MEM,
407 _MEMSWAP,
408 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800409 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800410};
411
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700412#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
413#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800414#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700415/* Used for OOM nofiier */
416#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800417
Balbir Singh75822b42009-09-23 15:56:38 -0700418/*
Glauber Costa09998212013-02-22 16:34:55 -0800419 * The memcg_create_mutex will be held whenever a new cgroup is created.
420 * As a consequence, any change that needs to protect against new child cgroups
421 * appearing has to hold it as well.
422 */
423static DEFINE_MUTEX(memcg_create_mutex);
424
Wanpeng Lib2145142012-07-31 16:46:01 -0700425struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
426{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400427 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700428}
429
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700430/* Some nice accessors for the vmpressure. */
431struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
432{
433 if (!memcg)
434 memcg = root_mem_cgroup;
435 return &memcg->vmpressure;
436}
437
438struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
439{
440 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
441}
442
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700443static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
444{
445 return (memcg == root_mem_cgroup);
446}
447
Li Zefan4219b2d2013-09-23 16:56:29 +0800448/*
449 * We restrict the id in the range of [1, 65535], so it can fit into
450 * an unsigned short.
451 */
452#define MEM_CGROUP_ID_MAX USHRT_MAX
453
Li Zefan34c00c32013-09-23 16:56:01 +0800454static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
455{
Tejun Heo15a4c832014-05-04 15:09:14 -0400456 return memcg->css.id;
Li Zefan34c00c32013-09-23 16:56:01 +0800457}
458
Vladimir Davydovadbe4272015-04-15 16:13:00 -0700459/*
460 * A helper function to get mem_cgroup from ID. must be called under
461 * rcu_read_lock(). The caller is responsible for calling
462 * css_tryget_online() if the mem_cgroup is used for charging. (dropping
463 * refcnt from swap can be called against removed memcg.)
464 */
Li Zefan34c00c32013-09-23 16:56:01 +0800465static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
466{
467 struct cgroup_subsys_state *css;
468
Tejun Heo7d699dd2014-05-04 15:09:13 -0400469 css = css_from_id(id, &memory_cgrp_subsys);
Li Zefan34c00c32013-09-23 16:56:01 +0800470 return mem_cgroup_from_css(css);
471}
472
Glauber Costae1aab162011-12-11 21:47:03 +0000473/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700474#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000475
Glauber Costae1aab162011-12-11 21:47:03 +0000476void sock_update_memcg(struct sock *sk)
477{
Glauber Costa376be5f2012-01-20 04:57:14 +0000478 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000479 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700480 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000481
482 BUG_ON(!sk->sk_prot->proto_cgroup);
483
Glauber Costaf3f511e2012-01-05 20:16:39 +0000484 /* Socket cloning can throw us here with sk_cgrp already
485 * filled. It won't however, necessarily happen from
486 * process context. So the test for root memcg given
487 * the current task's memcg won't help us in this case.
488 *
489 * Respecting the original socket's memcg is a better
490 * decision in this case.
491 */
492 if (sk->sk_cgrp) {
493 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700494 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000495 return;
496 }
497
Glauber Costae1aab162011-12-11 21:47:03 +0000498 rcu_read_lock();
499 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700500 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700501 if (!mem_cgroup_is_root(memcg) &&
Tejun Heoec903c02014-05-13 12:11:01 -0400502 memcg_proto_active(cg_proto) &&
503 css_tryget_online(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700504 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000505 }
506 rcu_read_unlock();
507 }
508}
509EXPORT_SYMBOL(sock_update_memcg);
510
511void sock_release_memcg(struct sock *sk)
512{
Glauber Costa376be5f2012-01-20 04:57:14 +0000513 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000514 struct mem_cgroup *memcg;
515 WARN_ON(!sk->sk_cgrp->memcg);
516 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700517 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000518 }
519}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000520
521struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
522{
523 if (!memcg || mem_cgroup_is_root(memcg))
524 return NULL;
525
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700526 return &memcg->tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000527}
528EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000529
Glauber Costa3f134612012-05-29 15:07:11 -0700530#endif
531
Glauber Costaa8964b92012-12-18 14:22:09 -0800532#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800533/*
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800534 * This will be the memcg's index in each cache's ->memcg_params.memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800535 * The main reason for not using cgroup id for this:
536 * this works better in sparse environments, where we have a lot of memcgs,
537 * but only a few kmem-limited. Or also, if we have, for instance, 200
538 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
539 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800540 *
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800541 * The current size of the caches array is stored in memcg_nr_cache_ids. It
542 * will double each time we have to increase it.
Glauber Costa55007d82012-12-18 14:22:38 -0800543 */
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800544static DEFINE_IDA(memcg_cache_ida);
545int memcg_nr_cache_ids;
Glauber Costa749c5412012-12-18 14:23:01 -0800546
Vladimir Davydov05257a12015-02-12 14:59:01 -0800547/* Protects memcg_nr_cache_ids */
548static DECLARE_RWSEM(memcg_cache_ids_sem);
549
550void memcg_get_cache_ids(void)
551{
552 down_read(&memcg_cache_ids_sem);
553}
554
555void memcg_put_cache_ids(void)
556{
557 up_read(&memcg_cache_ids_sem);
558}
559
Glauber Costa55007d82012-12-18 14:22:38 -0800560/*
561 * MIN_SIZE is different than 1, because we would like to avoid going through
562 * the alloc/free process all the time. In a small machine, 4 kmem-limited
563 * cgroups is a reasonable guess. In the future, it could be a parameter or
564 * tunable, but that is strictly not necessary.
565 *
Li Zefanb8627832013-09-23 16:56:47 +0800566 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800567 * this constant directly from cgroup, but it is understandable that this is
568 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800569 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800570 * increase ours as well if it increases.
571 */
572#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800573#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800574
Glauber Costad7f25f82012-12-18 14:22:40 -0800575/*
576 * A lot of the calls to the cache allocation functions are expected to be
577 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
578 * conditional to this static branch, we'll have to allow modules that does
579 * kmem_cache_alloc and the such to see this symbol as well
580 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800581struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800582EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800583
Glauber Costaa8964b92012-12-18 14:22:09 -0800584#endif /* CONFIG_MEMCG_KMEM */
585
Balbir Singhf64c3f52009-09-23 15:56:37 -0700586static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700587mem_cgroup_zone_zoneinfo(struct mem_cgroup *memcg, struct zone *zone)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700588{
Jianyu Zhane2318752014-06-06 14:38:20 -0700589 int nid = zone_to_nid(zone);
590 int zid = zone_idx(zone);
591
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700592 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700593}
594
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700595struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100596{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700597 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100598}
599
Balbir Singhf64c3f52009-09-23 15:56:37 -0700600static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700601mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700602{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700603 int nid = page_to_nid(page);
604 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700605
Jianyu Zhane2318752014-06-06 14:38:20 -0700606 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700607}
608
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700609static struct mem_cgroup_tree_per_zone *
610soft_limit_tree_node_zone(int nid, int zid)
611{
612 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
613}
614
615static struct mem_cgroup_tree_per_zone *
616soft_limit_tree_from_page(struct page *page)
617{
618 int nid = page_to_nid(page);
619 int zid = page_zonenum(page);
620
621 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
622}
623
Johannes Weinercf2c8122014-06-06 14:38:21 -0700624static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz,
625 struct mem_cgroup_tree_per_zone *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800626 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700627{
628 struct rb_node **p = &mctz->rb_root.rb_node;
629 struct rb_node *parent = NULL;
630 struct mem_cgroup_per_zone *mz_node;
631
632 if (mz->on_tree)
633 return;
634
635 mz->usage_in_excess = new_usage_in_excess;
636 if (!mz->usage_in_excess)
637 return;
638 while (*p) {
639 parent = *p;
640 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
641 tree_node);
642 if (mz->usage_in_excess < mz_node->usage_in_excess)
643 p = &(*p)->rb_left;
644 /*
645 * We can't avoid mem cgroups that are over their soft
646 * limit by the same amount
647 */
648 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
649 p = &(*p)->rb_right;
650 }
651 rb_link_node(&mz->tree_node, parent, p);
652 rb_insert_color(&mz->tree_node, &mctz->rb_root);
653 mz->on_tree = true;
654}
655
Johannes Weinercf2c8122014-06-06 14:38:21 -0700656static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
657 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700658{
659 if (!mz->on_tree)
660 return;
661 rb_erase(&mz->tree_node, &mctz->rb_root);
662 mz->on_tree = false;
663}
664
Johannes Weinercf2c8122014-06-06 14:38:21 -0700665static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
666 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700667{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700668 unsigned long flags;
669
670 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700671 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700672 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700673}
674
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800675static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
676{
677 unsigned long nr_pages = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -0700678 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800679 unsigned long excess = 0;
680
681 if (nr_pages > soft_limit)
682 excess = nr_pages - soft_limit;
683
684 return excess;
685}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700686
687static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
688{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800689 unsigned long excess;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700690 struct mem_cgroup_per_zone *mz;
691 struct mem_cgroup_tree_per_zone *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700692
Jianyu Zhane2318752014-06-06 14:38:20 -0700693 mctz = soft_limit_tree_from_page(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700694 /*
695 * Necessary to update all ancestors when hierarchy is used.
696 * because their event counter is not touched.
697 */
698 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Jianyu Zhane2318752014-06-06 14:38:20 -0700699 mz = mem_cgroup_page_zoneinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800700 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700701 /*
702 * We have to update the tree if mz is on RB-tree or
703 * mem is over its softlimit.
704 */
705 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700706 unsigned long flags;
707
708 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700709 /* if on-tree, remove it */
710 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700711 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700712 /*
713 * Insert again. mz->usage_in_excess will be updated.
714 * If excess is 0, no tree ops.
715 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700716 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700717 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700718 }
719 }
720}
721
722static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
723{
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700724 struct mem_cgroup_tree_per_zone *mctz;
Jianyu Zhane2318752014-06-06 14:38:20 -0700725 struct mem_cgroup_per_zone *mz;
726 int nid, zid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700727
Jianyu Zhane2318752014-06-06 14:38:20 -0700728 for_each_node(nid) {
729 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
730 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
731 mctz = soft_limit_tree_node_zone(nid, zid);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700732 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700733 }
734 }
735}
736
737static struct mem_cgroup_per_zone *
738__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
739{
740 struct rb_node *rightmost = NULL;
741 struct mem_cgroup_per_zone *mz;
742
743retry:
744 mz = NULL;
745 rightmost = rb_last(&mctz->rb_root);
746 if (!rightmost)
747 goto done; /* Nothing to reclaim from */
748
749 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
750 /*
751 * Remove the node now but someone else can add it back,
752 * we will to add it back at the end of reclaim to its correct
753 * position in the tree.
754 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700755 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800756 if (!soft_limit_excess(mz->memcg) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400757 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700758 goto retry;
759done:
760 return mz;
761}
762
763static struct mem_cgroup_per_zone *
764mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
765{
766 struct mem_cgroup_per_zone *mz;
767
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700768 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700769 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700770 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700771 return mz;
772}
773
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700774/*
775 * Implementation Note: reading percpu statistics for memcg.
776 *
777 * Both of vmstat[] and percpu_counter has threshold and do periodic
778 * synchronization to implement "quick" read. There are trade-off between
779 * reading cost and precision of value. Then, we may have a chance to implement
780 * a periodic synchronizion of counter in memcg's counter.
781 *
782 * But this _read() function is used for user interface now. The user accounts
783 * memory usage by memory cgroup and he _always_ requires exact value because
784 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
785 * have to visit all online cpus and make sum. So, for now, unnecessary
786 * synchronization is not implemented. (just implemented for cpu hotplug)
787 *
788 * If there are kernel internal actions which can make use of some not-exact
789 * value, and reading all cpu value can be performance bottleneck in some
790 * common workload, threashold and synchonization as vmstat[] should be
791 * implemented.
792 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700794 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800795{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700796 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800797 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800798
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700799 get_online_cpus();
800 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700801 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700802#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700803 spin_lock(&memcg->pcp_counter_lock);
804 val += memcg->nocpu_base.count[idx];
805 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700806#endif
807 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800808 return val;
809}
810
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700811static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700812 enum mem_cgroup_events_index idx)
813{
814 unsigned long val = 0;
815 int cpu;
816
David Rientjes9c567512013-10-16 13:46:43 -0700817 get_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700818 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700819 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700820#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700821 spin_lock(&memcg->pcp_counter_lock);
822 val += memcg->nocpu_base.events[idx];
823 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700824#endif
David Rientjes9c567512013-10-16 13:46:43 -0700825 put_online_cpus();
Johannes Weinere9f89742011-03-23 16:42:37 -0700826 return val;
827}
828
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700829static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700830 struct page *page,
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700831 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800832{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700833 /*
834 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
835 * counted as CACHE even if it's on ANON LRU.
836 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700837 if (PageAnon(page))
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700838 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700839 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800840 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700841 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700843
David Rientjesb070e652013-05-07 16:18:09 -0700844 if (PageTransHuge(page))
845 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
846 nr_pages);
847
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800848 /* pagein of a big page is an event. So, ignore page size */
849 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700850 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800851 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700852 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800853 nr_pages = -nr_pages; /* for event */
854 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800855
Johannes Weiner13114712012-05-29 15:07:07 -0700856 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800857}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800858
Jianyu Zhane2318752014-06-06 14:38:20 -0700859unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700860{
861 struct mem_cgroup_per_zone *mz;
862
863 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
864 return mz->lru_size[lru];
865}
866
Jianyu Zhane2318752014-06-06 14:38:20 -0700867static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
868 int nid,
869 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700870{
Jianyu Zhane2318752014-06-06 14:38:20 -0700871 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700872 int zid;
873
Jianyu Zhane2318752014-06-06 14:38:20 -0700874 VM_BUG_ON((unsigned)nid >= nr_node_ids);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700875
Jianyu Zhane2318752014-06-06 14:38:20 -0700876 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
877 struct mem_cgroup_per_zone *mz;
878 enum lru_list lru;
879
880 for_each_lru(lru) {
881 if (!(BIT(lru) & lru_mask))
882 continue;
883 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
884 nr += mz->lru_size[lru];
885 }
886 }
887 return nr;
Ying Han889976d2011-05-26 16:25:33 -0700888}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700889
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700890static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700891 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800892{
Jianyu Zhane2318752014-06-06 14:38:20 -0700893 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700894 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800895
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800896 for_each_node_state(nid, N_MEMORY)
Jianyu Zhane2318752014-06-06 14:38:20 -0700897 nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
898 return nr;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800899}
900
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800901static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
902 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800903{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700904 unsigned long val, next;
905
Johannes Weiner13114712012-05-29 15:07:07 -0700906 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700907 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700908 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800909 if ((long)next - (long)val < 0) {
910 switch (target) {
911 case MEM_CGROUP_TARGET_THRESH:
912 next = val + THRESHOLDS_EVENTS_TARGET;
913 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700914 case MEM_CGROUP_TARGET_SOFTLIMIT:
915 next = val + SOFTLIMIT_EVENTS_TARGET;
916 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800917 case MEM_CGROUP_TARGET_NUMAINFO:
918 next = val + NUMAINFO_EVENTS_TARGET;
919 break;
920 default:
921 break;
922 }
923 __this_cpu_write(memcg->stat->targets[target], next);
924 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700925 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800926 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800927}
928
929/*
930 * Check events in order.
931 *
932 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700933static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800934{
935 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800936 if (unlikely(mem_cgroup_event_ratelimit(memcg,
937 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700938 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800939 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800940
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700941 do_softlimit = mem_cgroup_event_ratelimit(memcg,
942 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700943#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800944 do_numainfo = mem_cgroup_event_ratelimit(memcg,
945 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700946#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800947 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700948 if (unlikely(do_softlimit))
949 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800950#if MAX_NUMNODES > 1
951 if (unlikely(do_numainfo))
952 atomic_inc(&memcg->numainfo_events);
953#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700954 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800955}
956
Balbir Singhcf475ad2008-04-29 01:00:16 -0700957struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800958{
Balbir Singh31a78f22008-09-28 23:09:31 +0100959 /*
960 * mm_update_next_owner() may clear mm->owner to NULL
961 * if it races with swapoff, page migration, etc.
962 * So this can be called with p == NULL.
963 */
964 if (unlikely(!p))
965 return NULL;
966
Tejun Heo073219e2014-02-08 10:36:58 -0500967 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800968}
969
Johannes Weinerdf381972014-04-07 15:37:43 -0700970static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800971{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700972 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700973
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800974 rcu_read_lock();
975 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -0700976 /*
977 * Page cache insertions can happen withou an
978 * actual mm context, e.g. during disk probing
979 * on boot, loopback IO, acct() writes etc.
980 */
981 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -0700982 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -0700983 else {
984 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
985 if (unlikely(!memcg))
986 memcg = root_mem_cgroup;
987 }
Tejun Heoec903c02014-05-13 12:11:01 -0400988 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800989 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700990 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800991}
992
Johannes Weiner56600482012-01-12 17:17:59 -0800993/**
994 * mem_cgroup_iter - iterate over memory cgroup hierarchy
995 * @root: hierarchy root
996 * @prev: previously returned memcg, NULL on first invocation
997 * @reclaim: cookie for shared reclaim walks, NULL for full walks
998 *
999 * Returns references to children of the hierarchy below @root, or
1000 * @root itself, or %NULL after a full round-trip.
1001 *
1002 * Caller must pass the return value in @prev on subsequent
1003 * invocations for reference counting, or use mem_cgroup_iter_break()
1004 * to cancel a hierarchy walk before the round-trip is complete.
1005 *
1006 * Reclaimers can specify a zone and a priority level in @reclaim to
1007 * divide up the memcgs in the hierarchy among all concurrent
1008 * reclaimers operating on the same zone and priority.
1009 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001010struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001011 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001012 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001013{
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001014 struct reclaim_iter *uninitialized_var(iter);
1015 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001016 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001017 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001018
Andrew Morton694fbc02013-09-24 15:27:37 -07001019 if (mem_cgroup_disabled())
1020 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001021
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001022 if (!root)
1023 root = root_mem_cgroup;
1024
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001025 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001026 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001027
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001028 if (!root->use_hierarchy && root != root_mem_cgroup) {
1029 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001030 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001031 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001032 }
1033
Michal Hocko542f85f2013-04-29 15:07:15 -07001034 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001035
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001036 if (reclaim) {
1037 struct mem_cgroup_per_zone *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001038
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001039 mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone);
1040 iter = &mz->iter[reclaim->priority];
Michal Hocko5f578162013-04-29 15:07:17 -07001041
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001042 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001043 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001044
1045 do {
Jason Low4db0c3c2015-04-15 16:14:08 -07001046 pos = READ_ONCE(iter->position);
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001047 /*
1048 * A racing update may change the position and
1049 * put the last reference, hence css_tryget(),
1050 * or retry to see the updated position.
1051 */
1052 } while (pos && !css_tryget(&pos->css));
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001053 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001054
1055 if (pos)
1056 css = &pos->css;
1057
1058 for (;;) {
1059 css = css_next_descendant_pre(css, &root->css);
1060 if (!css) {
1061 /*
1062 * Reclaimers share the hierarchy walk, and a
1063 * new one might jump in right at the end of
1064 * the hierarchy - make sure they see at least
1065 * one group and restart from the beginning.
1066 */
1067 if (!prev)
1068 continue;
1069 break;
1070 }
1071
1072 /*
1073 * Verify the css and acquire a reference. The root
1074 * is provided by the caller, so we know it's alive
1075 * and kicking, and don't take an extra reference.
1076 */
1077 memcg = mem_cgroup_from_css(css);
1078
1079 if (css == &root->css)
1080 break;
1081
Johannes Weinerb2052562014-12-10 15:42:48 -08001082 if (css_tryget(css)) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001083 /*
1084 * Make sure the memcg is initialized:
1085 * mem_cgroup_css_online() orders the the
1086 * initialization against setting the flag.
1087 */
1088 if (smp_load_acquire(&memcg->initialized))
1089 break;
1090
1091 css_put(css);
1092 }
1093
1094 memcg = NULL;
1095 }
1096
1097 if (reclaim) {
1098 if (cmpxchg(&iter->position, pos, memcg) == pos) {
1099 if (memcg)
1100 css_get(&memcg->css);
1101 if (pos)
1102 css_put(&pos->css);
1103 }
1104
1105 /*
1106 * pairs with css_tryget when dereferencing iter->position
1107 * above.
1108 */
1109 if (pos)
1110 css_put(&pos->css);
1111
1112 if (!memcg)
1113 iter->generation++;
1114 else if (!prev)
1115 reclaim->generation = iter->generation;
1116 }
1117
Michal Hocko542f85f2013-04-29 15:07:15 -07001118out_unlock:
1119 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001120out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001121 if (prev && prev != root)
1122 css_put(&prev->css);
1123
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001124 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001125}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126
Johannes Weiner56600482012-01-12 17:17:59 -08001127/**
1128 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1129 * @root: hierarchy root
1130 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1131 */
1132void mem_cgroup_iter_break(struct mem_cgroup *root,
1133 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001134{
1135 if (!root)
1136 root = root_mem_cgroup;
1137 if (prev && prev != root)
1138 css_put(&prev->css);
1139}
1140
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001141/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001142 * Iteration constructs for visiting all cgroups (under a tree). If
1143 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1144 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001145 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001146#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001147 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001148 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001149 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001150
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001151#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001152 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001153 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001154 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001155
David Rientjes68ae5642012-12-12 13:51:57 -08001156void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001157{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001158 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001159
Ying Han456f9982011-05-26 16:25:38 -07001160 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001161 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1162 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001163 goto out;
1164
1165 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001166 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001167 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1168 break;
1169 case PGMAJFAULT:
1170 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001171 break;
1172 default:
1173 BUG();
1174 }
1175out:
1176 rcu_read_unlock();
1177}
David Rientjes68ae5642012-12-12 13:51:57 -08001178EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001179
Johannes Weiner925b7672012-01-12 17:18:15 -08001180/**
1181 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1182 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001183 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001184 *
1185 * Returns the lru list vector holding pages for the given @zone and
1186 * @mem. This can be the global zone lruvec, if the memory controller
1187 * is disabled.
1188 */
1189struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1190 struct mem_cgroup *memcg)
1191{
1192 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001193 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001194
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001195 if (mem_cgroup_disabled()) {
1196 lruvec = &zone->lruvec;
1197 goto out;
1198 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001199
Jianyu Zhane2318752014-06-06 14:38:20 -07001200 mz = mem_cgroup_zone_zoneinfo(memcg, zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001201 lruvec = &mz->lruvec;
1202out:
1203 /*
1204 * Since a node can be onlined after the mem_cgroup was created,
1205 * we have to be prepared to initialize lruvec->zone here;
1206 * and if offlined then reonlined, we need to reinitialize it.
1207 */
1208 if (unlikely(lruvec->zone != zone))
1209 lruvec->zone = zone;
1210 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001211}
1212
Johannes Weiner925b7672012-01-12 17:18:15 -08001213/**
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001214 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
Johannes Weiner925b7672012-01-12 17:18:15 -08001215 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001216 * @zone: zone of the page
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001217 *
1218 * This function is only safe when following the LRU page isolation
1219 * and putback protocol: the LRU lock must be held, and the page must
1220 * either be PageLRU() or the caller must have isolated/allocated it.
Minchan Kim3f58a822011-03-22 16:32:53 -07001221 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001222struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001223{
1224 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001225 struct mem_cgroup *memcg;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001226 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001227
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001228 if (mem_cgroup_disabled()) {
1229 lruvec = &zone->lruvec;
1230 goto out;
1231 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001232
Johannes Weiner1306a852014-12-10 15:44:52 -08001233 memcg = page->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001234 /*
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001235 * Swapcache readahead pages are added to the LRU - and
Johannes Weiner29833312014-12-10 15:44:02 -08001236 * possibly migrated - before they are charged.
Hugh Dickins75121022012-03-05 14:59:18 -08001237 */
Johannes Weiner29833312014-12-10 15:44:02 -08001238 if (!memcg)
1239 memcg = root_mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001240
Jianyu Zhane2318752014-06-06 14:38:20 -07001241 mz = mem_cgroup_page_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001242 lruvec = &mz->lruvec;
1243out:
1244 /*
1245 * Since a node can be onlined after the mem_cgroup was created,
1246 * we have to be prepared to initialize lruvec->zone here;
1247 * and if offlined then reonlined, we need to reinitialize it.
1248 */
1249 if (unlikely(lruvec->zone != zone))
1250 lruvec->zone = zone;
1251 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001252}
1253
1254/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001255 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1256 * @lruvec: mem_cgroup per zone lru vector
1257 * @lru: index of lru list the page is sitting on
1258 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001259 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001260 * This function must be called when a page is added to or removed from an
1261 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001262 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001263void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1264 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001265{
1266 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001267 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001268
1269 if (mem_cgroup_disabled())
1270 return;
1271
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001272 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1273 lru_size = mz->lru_size + lru;
1274 *lru_size += nr_pages;
1275 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001276}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001277
Johannes Weiner2314b422014-12-10 15:44:33 -08001278bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, struct mem_cgroup *root)
Michal Hocko3e920412011-07-26 16:08:29 -07001279{
Johannes Weiner2314b422014-12-10 15:44:33 -08001280 if (root == memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001281 return true;
Johannes Weiner2314b422014-12-10 15:44:33 -08001282 if (!root->use_hierarchy)
Johannes Weiner91c637342012-05-29 15:06:24 -07001283 return false;
Johannes Weiner2314b422014-12-10 15:44:33 -08001284 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001285}
1286
Johannes Weiner2314b422014-12-10 15:44:33 -08001287bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg)
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001288{
Johannes Weiner2314b422014-12-10 15:44:33 -08001289 struct mem_cgroup *task_memcg;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001290 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001291 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001292
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001293 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001294 if (p) {
Johannes Weiner2314b422014-12-10 15:44:33 -08001295 task_memcg = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001296 task_unlock(p);
1297 } else {
1298 /*
1299 * All threads may have already detached their mm's, but the oom
1300 * killer still needs to detect if they have already been oom
1301 * killed to prevent needlessly killing additional tasks.
1302 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001303 rcu_read_lock();
Johannes Weiner2314b422014-12-10 15:44:33 -08001304 task_memcg = mem_cgroup_from_task(task);
1305 css_get(&task_memcg->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001306 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001307 }
Johannes Weiner2314b422014-12-10 15:44:33 -08001308 ret = mem_cgroup_is_descendant(task_memcg, memcg);
1309 css_put(&task_memcg->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001310 return ret;
1311}
1312
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001313int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001314{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001315 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001316 unsigned long inactive;
1317 unsigned long active;
1318 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001319
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001320 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1321 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001322
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001323 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1324 if (gb)
1325 inactive_ratio = int_sqrt(10 * gb);
1326 else
1327 inactive_ratio = 1;
1328
Johannes Weiner9b272972011-11-02 13:38:23 -07001329 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001330}
1331
Vladimir Davydov90cbc252015-02-11 15:25:55 -08001332bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
1333{
1334 struct mem_cgroup_per_zone *mz;
1335 struct mem_cgroup *memcg;
1336
1337 if (mem_cgroup_disabled())
1338 return true;
1339
1340 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1341 memcg = mz->memcg;
1342
1343 return !!(memcg->css.flags & CSS_ONLINE);
1344}
1345
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001346#define mem_cgroup_from_counter(counter, member) \
Balbir Singh6d61ef42009-01-07 18:08:06 -08001347 container_of(counter, struct mem_cgroup, member)
1348
Johannes Weiner19942822011-02-01 15:52:43 -08001349/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001350 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001351 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001352 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001353 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001354 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001355 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001356static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001357{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001358 unsigned long margin = 0;
1359 unsigned long count;
1360 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001361
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001362 count = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -07001363 limit = READ_ONCE(memcg->memory.limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001364 if (count < limit)
1365 margin = limit - count;
1366
1367 if (do_swap_account) {
1368 count = page_counter_read(&memcg->memsw);
Jason Low4db0c3c2015-04-15 16:14:08 -07001369 limit = READ_ONCE(memcg->memsw.limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001370 if (count <= limit)
1371 margin = min(margin, limit - count);
1372 }
1373
1374 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001375}
1376
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001377int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001378{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001379 /* root ? */
Linus Torvalds14208b02014-06-09 15:03:33 -07001380 if (mem_cgroup_disabled() || !memcg->css.parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001381 return vm_swappiness;
1382
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001383 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001384}
1385
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001386/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001387 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001388 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001389 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1390 * moving cgroups. This is for waiting at high-memory pressure
1391 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001392 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001393static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001394{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001395 struct mem_cgroup *from;
1396 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001397 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001398 /*
1399 * Unlike task_move routines, we access mc.to, mc.from not under
1400 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1401 */
1402 spin_lock(&mc.lock);
1403 from = mc.from;
1404 to = mc.to;
1405 if (!from)
1406 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001407
Johannes Weiner2314b422014-12-10 15:44:33 -08001408 ret = mem_cgroup_is_descendant(from, memcg) ||
1409 mem_cgroup_is_descendant(to, memcg);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001410unlock:
1411 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001412 return ret;
1413}
1414
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001415static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001416{
1417 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001418 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001419 DEFINE_WAIT(wait);
1420 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1421 /* moving charge context might have finished. */
1422 if (mc.moving_task)
1423 schedule();
1424 finish_wait(&mc.waitq, &wait);
1425 return true;
1426 }
1427 }
1428 return false;
1429}
1430
Sha Zhengju58cf1882013-02-22 16:32:05 -08001431#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001432/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001433 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001434 * @memcg: The memory cgroup that went over limit
1435 * @p: Task that is going to be killed
1436 *
1437 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1438 * enabled
1439 */
1440void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1441{
Tejun Heoe61734c2014-02-12 09:29:50 -05001442 /* oom_info_lock ensures that parallel ooms do not interleave */
Michal Hocko08088cb2014-02-25 15:01:44 -08001443 static DEFINE_MUTEX(oom_info_lock);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001444 struct mem_cgroup *iter;
1445 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001446
Michal Hocko08088cb2014-02-25 15:01:44 -08001447 mutex_lock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001448 rcu_read_lock();
1449
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -07001450 if (p) {
1451 pr_info("Task in ");
1452 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1453 pr_cont(" killed as a result of limit of ");
1454 } else {
1455 pr_info("Memory limit reached of cgroup ");
1456 }
1457
Tejun Heoe61734c2014-02-12 09:29:50 -05001458 pr_cont_cgroup_path(memcg->css.cgroup);
Greg Thelen0346dad2015-01-26 12:58:38 -08001459 pr_cont("\n");
Balbir Singhe2224322009-04-02 16:57:39 -07001460
Balbir Singhe2224322009-04-02 16:57:39 -07001461 rcu_read_unlock();
1462
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001463 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1464 K((u64)page_counter_read(&memcg->memory)),
1465 K((u64)memcg->memory.limit), memcg->memory.failcnt);
1466 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1467 K((u64)page_counter_read(&memcg->memsw)),
1468 K((u64)memcg->memsw.limit), memcg->memsw.failcnt);
1469 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1470 K((u64)page_counter_read(&memcg->kmem)),
1471 K((u64)memcg->kmem.limit), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001472
1473 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heoe61734c2014-02-12 09:29:50 -05001474 pr_info("Memory cgroup stats for ");
1475 pr_cont_cgroup_path(iter->css.cgroup);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001476 pr_cont(":");
1477
1478 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1479 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1480 continue;
1481 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1482 K(mem_cgroup_read_stat(iter, i)));
1483 }
1484
1485 for (i = 0; i < NR_LRU_LISTS; i++)
1486 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1487 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1488
1489 pr_cont("\n");
1490 }
Michal Hocko08088cb2014-02-25 15:01:44 -08001491 mutex_unlock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001492}
1493
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001494/*
1495 * This function returns the number of memcg under hierarchy tree. Returns
1496 * 1(self count) if no children.
1497 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001498static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001499{
1500 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001501 struct mem_cgroup *iter;
1502
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001503 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001504 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001505 return num;
1506}
1507
Balbir Singh6d61ef42009-01-07 18:08:06 -08001508/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001509 * Return the memory (and swap, if configured) limit for a memcg.
1510 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001511static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001512{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001513 unsigned long limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001514
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001515 limit = memcg->memory.limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001516 if (mem_cgroup_swappiness(memcg)) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001517 unsigned long memsw_limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001518
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001519 memsw_limit = memcg->memsw.limit;
1520 limit = min(limit + total_swap_pages, memsw_limit);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001521 }
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001522 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001523}
1524
David Rientjes19965462012-12-11 16:00:26 -08001525static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1526 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001527{
1528 struct mem_cgroup *iter;
1529 unsigned long chosen_points = 0;
1530 unsigned long totalpages;
1531 unsigned int points = 0;
1532 struct task_struct *chosen = NULL;
1533
David Rientjes876aafb2012-07-31 16:43:48 -07001534 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001535 * If current has a pending SIGKILL or is exiting, then automatically
1536 * select it. The goal is to allow it to allocate so that it may
1537 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001538 */
Oleg Nesterovd003f372014-12-12 16:56:24 -08001539 if (fatal_signal_pending(current) || task_will_free_mem(current)) {
Michal Hocko49550b62015-02-11 15:26:12 -08001540 mark_tsk_oom_victim(current);
David Rientjes876aafb2012-07-31 16:43:48 -07001541 return;
1542 }
1543
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -07001544 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL, memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001545 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001546 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001547 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001548 struct task_struct *task;
1549
Tejun Heo72ec7022013-08-08 20:11:26 -04001550 css_task_iter_start(&iter->css, &it);
1551 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001552 switch (oom_scan_process_thread(task, totalpages, NULL,
1553 false)) {
1554 case OOM_SCAN_SELECT:
1555 if (chosen)
1556 put_task_struct(chosen);
1557 chosen = task;
1558 chosen_points = ULONG_MAX;
1559 get_task_struct(chosen);
1560 /* fall through */
1561 case OOM_SCAN_CONTINUE:
1562 continue;
1563 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001564 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001565 mem_cgroup_iter_break(memcg, iter);
1566 if (chosen)
1567 put_task_struct(chosen);
1568 return;
1569 case OOM_SCAN_OK:
1570 break;
1571 };
1572 points = oom_badness(task, memcg, NULL, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -08001573 if (!points || points < chosen_points)
1574 continue;
1575 /* Prefer thread group leaders for display purposes */
1576 if (points == chosen_points &&
1577 thread_group_leader(chosen))
1578 continue;
1579
1580 if (chosen)
1581 put_task_struct(chosen);
1582 chosen = task;
1583 chosen_points = points;
1584 get_task_struct(chosen);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001585 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001586 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001587 }
1588
1589 if (!chosen)
1590 return;
1591 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001592 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1593 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001594}
1595
Michele Curtiae6e71d2014-12-12 16:56:35 -08001596#if MAX_NUMNODES > 1
1597
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001598/**
1599 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001600 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001601 * @nid: the node ID to be checked.
1602 * @noswap : specify true here if the user wants flle only information.
1603 *
1604 * This function returns whether the specified memcg contains any
1605 * reclaimable pages on a node. Returns true if there are any reclaimable
1606 * pages in the node.
1607 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001608static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001609 int nid, bool noswap)
1610{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001611 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001612 return true;
1613 if (noswap || !total_swap_pages)
1614 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001615 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001616 return true;
1617 return false;
1618
1619}
Ying Han889976d2011-05-26 16:25:33 -07001620
1621/*
1622 * Always updating the nodemask is not very good - even if we have an empty
1623 * list or the wrong list here, we can start from some node and traverse all
1624 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1625 *
1626 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001627static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001628{
1629 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001630 /*
1631 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1632 * pagein/pageout changes since the last update.
1633 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001634 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001635 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001636 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001637 return;
1638
Ying Han889976d2011-05-26 16:25:33 -07001639 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001640 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001641
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001642 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001643
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001644 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1645 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001646 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001648 atomic_set(&memcg->numainfo_events, 0);
1649 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001650}
1651
1652/*
1653 * Selecting a node where we start reclaim from. Because what we need is just
1654 * reducing usage counter, start from anywhere is O,K. Considering
1655 * memory reclaim from current node, there are pros. and cons.
1656 *
1657 * Freeing memory from current node means freeing memory from a node which
1658 * we'll use or we've used. So, it may make LRU bad. And if several threads
1659 * hit limits, it will see a contention on a node. But freeing from remote
1660 * node means more costs for memory reclaim because of memory latency.
1661 *
1662 * Now, we use round-robin. Better algorithm is welcomed.
1663 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001664int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001665{
1666 int node;
1667
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001668 mem_cgroup_may_update_nodemask(memcg);
1669 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001670
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001671 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001672 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001674 /*
1675 * We call this when we hit limit, not when pages are added to LRU.
1676 * No LRU may hold pages because all pages are UNEVICTABLE or
1677 * memcg is too small and all pages are not on LRU. In that case,
1678 * we use curret node.
1679 */
1680 if (unlikely(node == MAX_NUMNODES))
1681 node = numa_node_id();
1682
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001683 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001684 return node;
1685}
Ying Han889976d2011-05-26 16:25:33 -07001686#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001687int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001688{
1689 return 0;
1690}
1691#endif
1692
Andrew Morton0608f432013-09-24 15:27:41 -07001693static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1694 struct zone *zone,
1695 gfp_t gfp_mask,
1696 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001697{
Andrew Morton0608f432013-09-24 15:27:41 -07001698 struct mem_cgroup *victim = NULL;
1699 int total = 0;
1700 int loop = 0;
1701 unsigned long excess;
1702 unsigned long nr_scanned;
1703 struct mem_cgroup_reclaim_cookie reclaim = {
1704 .zone = zone,
1705 .priority = 0,
1706 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001707
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001708 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001709
Andrew Morton0608f432013-09-24 15:27:41 -07001710 while (1) {
1711 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1712 if (!victim) {
1713 loop++;
1714 if (loop >= 2) {
1715 /*
1716 * If we have not been able to reclaim
1717 * anything, it might because there are
1718 * no reclaimable pages under this hierarchy
1719 */
1720 if (!total)
1721 break;
1722 /*
1723 * We want to do more targeted reclaim.
1724 * excess >> 2 is not to excessive so as to
1725 * reclaim too much, nor too less that we keep
1726 * coming back to reclaim from this cgroup
1727 */
1728 if (total >= (excess >> 2) ||
1729 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1730 break;
1731 }
1732 continue;
1733 }
Andrew Morton0608f432013-09-24 15:27:41 -07001734 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1735 zone, &nr_scanned);
1736 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001737 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001738 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001739 }
Andrew Morton0608f432013-09-24 15:27:41 -07001740 mem_cgroup_iter_break(root_memcg, victim);
1741 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001742}
1743
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001744#ifdef CONFIG_LOCKDEP
1745static struct lockdep_map memcg_oom_lock_dep_map = {
1746 .name = "memcg_oom_lock",
1747};
1748#endif
1749
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001750static DEFINE_SPINLOCK(memcg_oom_lock);
1751
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001752/*
1753 * Check OOM-Killer is already running under our hierarchy.
1754 * If someone is running, return false.
1755 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001756static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001757{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001758 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001759
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001760 spin_lock(&memcg_oom_lock);
1761
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001762 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001763 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001764 /*
1765 * this subtree of our hierarchy is already locked
1766 * so we cannot give a lock.
1767 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001768 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001769 mem_cgroup_iter_break(memcg, iter);
1770 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001771 } else
1772 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001773 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001774
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001775 if (failed) {
1776 /*
1777 * OK, we failed to lock the whole subtree so we have
1778 * to clean up what we set up to the failing subtree
1779 */
1780 for_each_mem_cgroup_tree(iter, memcg) {
1781 if (iter == failed) {
1782 mem_cgroup_iter_break(memcg, iter);
1783 break;
1784 }
1785 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001786 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001787 } else
1788 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001789
1790 spin_unlock(&memcg_oom_lock);
1791
1792 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001793}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001794
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001795static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001796{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001797 struct mem_cgroup *iter;
1798
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001799 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001800 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001801 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001802 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001803 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001804}
1805
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001806static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001807{
1808 struct mem_cgroup *iter;
1809
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001810 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001811 atomic_inc(&iter->under_oom);
1812}
1813
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001814static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001815{
1816 struct mem_cgroup *iter;
1817
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001818 /*
1819 * When a new child is created while the hierarchy is under oom,
1820 * mem_cgroup_oom_lock() may not be called. We have to use
1821 * atomic_add_unless() here.
1822 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001823 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001824 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001825}
1826
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001827static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1828
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001830 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001831 wait_queue_t wait;
1832};
1833
1834static int memcg_oom_wake_function(wait_queue_t *wait,
1835 unsigned mode, int sync, void *arg)
1836{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001837 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1838 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001839 struct oom_wait_info *oom_wait_info;
1840
1841 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001842 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001843
Johannes Weiner2314b422014-12-10 15:44:33 -08001844 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1845 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001846 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001847 return autoremove_wake_function(wait, mode, sync, arg);
1848}
1849
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001850static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001851{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001852 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853 /* for filtering, pass "memcg" as argument. */
1854 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001855}
1856
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001857static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001858{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859 if (memcg && atomic_read(&memcg->under_oom))
1860 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001861}
1862
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001863static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001864{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001865 if (!current->memcg_oom.may_oom)
1866 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001867 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001868 * We are in the middle of the charge context here, so we
1869 * don't want to block when potentially sitting on a callstack
1870 * that holds all kinds of filesystem and mm locks.
1871 *
1872 * Also, the caller may handle a failed allocation gracefully
1873 * (like optional page cache readahead) and so an OOM killer
1874 * invocation might not even be necessary.
1875 *
1876 * That's why we don't do anything here except remember the
1877 * OOM context and then deal with it at the end of the page
1878 * fault when the stack is unwound, the locks are released,
1879 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001880 */
Johannes Weiner49426422013-10-16 13:46:59 -07001881 css_get(&memcg->css);
1882 current->memcg_oom.memcg = memcg;
1883 current->memcg_oom.gfp_mask = mask;
1884 current->memcg_oom.order = order;
1885}
1886
1887/**
1888 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1889 * @handle: actually kill/wait or just clean up the OOM state
1890 *
1891 * This has to be called at the end of a page fault if the memcg OOM
1892 * handler was enabled.
1893 *
1894 * Memcg supports userspace OOM handling where failed allocations must
1895 * sleep on a waitqueue until the userspace task resolves the
1896 * situation. Sleeping directly in the charge context with all kinds
1897 * of locks held is not a good idea, instead we remember an OOM state
1898 * in the task and mem_cgroup_oom_synchronize() has to be called at
1899 * the end of the page fault to complete the OOM handling.
1900 *
1901 * Returns %true if an ongoing memcg OOM situation was detected and
1902 * completed, %false otherwise.
1903 */
1904bool mem_cgroup_oom_synchronize(bool handle)
1905{
1906 struct mem_cgroup *memcg = current->memcg_oom.memcg;
1907 struct oom_wait_info owait;
1908 bool locked;
1909
1910 /* OOM is global, do not handle */
1911 if (!memcg)
1912 return false;
1913
Michal Hockoc32b3cb2015-02-11 15:26:24 -08001914 if (!handle || oom_killer_disabled)
Johannes Weiner49426422013-10-16 13:46:59 -07001915 goto cleanup;
1916
1917 owait.memcg = memcg;
1918 owait.wait.flags = 0;
1919 owait.wait.func = memcg_oom_wake_function;
1920 owait.wait.private = current;
1921 INIT_LIST_HEAD(&owait.wait.task_list);
1922
1923 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001924 mem_cgroup_mark_under_oom(memcg);
1925
1926 locked = mem_cgroup_oom_trylock(memcg);
1927
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001928 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001929 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001930
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001931 if (locked && !memcg->oom_kill_disable) {
1932 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07001933 finish_wait(&memcg_oom_waitq, &owait.wait);
1934 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
1935 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001936 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001937 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07001938 mem_cgroup_unmark_under_oom(memcg);
1939 finish_wait(&memcg_oom_waitq, &owait.wait);
1940 }
1941
1942 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001943 mem_cgroup_oom_unlock(memcg);
1944 /*
1945 * There is no guarantee that an OOM-lock contender
1946 * sees the wakeups triggered by the OOM kill
1947 * uncharges. Wake any sleepers explicitely.
1948 */
1949 memcg_oom_recover(memcg);
1950 }
Johannes Weiner49426422013-10-16 13:46:59 -07001951cleanup:
1952 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001953 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001954 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001955}
1956
Johannes Weinerd7365e72014-10-29 14:50:48 -07001957/**
1958 * mem_cgroup_begin_page_stat - begin a page state statistics transaction
1959 * @page: page that is going to change accounted state
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001960 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07001961 * This function must mark the beginning of an accounted page state
1962 * change to prevent double accounting when the page is concurrently
1963 * being moved to another memcg:
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001964 *
Johannes Weiner6de22612015-02-11 15:25:01 -08001965 * memcg = mem_cgroup_begin_page_stat(page);
Johannes Weinerd7365e72014-10-29 14:50:48 -07001966 * if (TestClearPageState(page))
1967 * mem_cgroup_update_page_stat(memcg, state, -1);
Johannes Weiner6de22612015-02-11 15:25:01 -08001968 * mem_cgroup_end_page_stat(memcg);
Balbir Singhd69b0422009-06-17 16:26:34 -07001969 */
Johannes Weiner6de22612015-02-11 15:25:01 -08001970struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001971{
1972 struct mem_cgroup *memcg;
Johannes Weiner6de22612015-02-11 15:25:01 -08001973 unsigned long flags;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001974
Johannes Weiner6de22612015-02-11 15:25:01 -08001975 /*
1976 * The RCU lock is held throughout the transaction. The fast
1977 * path can get away without acquiring the memcg->move_lock
1978 * because page moving starts with an RCU grace period.
1979 *
1980 * The RCU lock also protects the memcg from being freed when
1981 * the page state that is going to change is the only thing
1982 * preventing the page from being uncharged.
1983 * E.g. end-writeback clearing PageWriteback(), which allows
1984 * migration to go ahead and uncharge the page before the
1985 * account transaction might be complete.
1986 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07001987 rcu_read_lock();
1988
1989 if (mem_cgroup_disabled())
1990 return NULL;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001991again:
Johannes Weiner1306a852014-12-10 15:44:52 -08001992 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08001993 if (unlikely(!memcg))
Johannes Weinerd7365e72014-10-29 14:50:48 -07001994 return NULL;
1995
Qiang Huangbdcbb652014-06-04 16:08:21 -07001996 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weinerd7365e72014-10-29 14:50:48 -07001997 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001998
Johannes Weiner6de22612015-02-11 15:25:01 -08001999 spin_lock_irqsave(&memcg->move_lock, flags);
Johannes Weiner1306a852014-12-10 15:44:52 -08002000 if (memcg != page->mem_cgroup) {
Johannes Weiner6de22612015-02-11 15:25:01 -08002001 spin_unlock_irqrestore(&memcg->move_lock, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002002 goto again;
2003 }
Johannes Weiner6de22612015-02-11 15:25:01 -08002004
2005 /*
2006 * When charge migration first begins, we can have locked and
2007 * unlocked page stat updates happening concurrently. Track
2008 * the task who has the lock for mem_cgroup_end_page_stat().
2009 */
2010 memcg->move_lock_task = current;
2011 memcg->move_lock_flags = flags;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002012
2013 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002014}
Greg Thelenc4843a72015-05-22 17:13:16 -04002015EXPORT_SYMBOL(mem_cgroup_begin_page_stat);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002016
Johannes Weinerd7365e72014-10-29 14:50:48 -07002017/**
2018 * mem_cgroup_end_page_stat - finish a page state statistics transaction
2019 * @memcg: the memcg that was accounted against
Johannes Weinerd7365e72014-10-29 14:50:48 -07002020 */
Johannes Weiner6de22612015-02-11 15:25:01 -08002021void mem_cgroup_end_page_stat(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002022{
Johannes Weiner6de22612015-02-11 15:25:01 -08002023 if (memcg && memcg->move_lock_task == current) {
2024 unsigned long flags = memcg->move_lock_flags;
2025
2026 memcg->move_lock_task = NULL;
2027 memcg->move_lock_flags = 0;
2028
2029 spin_unlock_irqrestore(&memcg->move_lock, flags);
2030 }
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002031
Johannes Weinerd7365e72014-10-29 14:50:48 -07002032 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002033}
Greg Thelenc4843a72015-05-22 17:13:16 -04002034EXPORT_SYMBOL(mem_cgroup_end_page_stat);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002035
Johannes Weinerd7365e72014-10-29 14:50:48 -07002036/**
2037 * mem_cgroup_update_page_stat - update page state statistics
2038 * @memcg: memcg to account against
2039 * @idx: page state item to account
2040 * @val: number of pages (positive or negative)
2041 *
2042 * See mem_cgroup_begin_page_stat() for locking requirements.
2043 */
2044void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
Sha Zhengju68b48762013-09-12 15:13:50 -07002045 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002046{
Sha Zhengju658b72c2013-09-12 15:13:52 -07002047 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002048
Johannes Weinerd7365e72014-10-29 14:50:48 -07002049 if (memcg)
2050 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002051}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002052
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002053/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002054 * size of first charge trial. "32" comes from vmscan.c's magic value.
2055 * TODO: maybe necessary to use big numbers in big irons.
2056 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002057#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002058struct memcg_stock_pcp {
2059 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002060 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002061 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002062 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002063#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002064};
2065static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002066static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002067
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002068/**
2069 * consume_stock: Try to consume stocked charge on this cpu.
2070 * @memcg: memcg to consume from.
2071 * @nr_pages: how many pages to charge.
2072 *
2073 * The charges will only happen if @memcg matches the current cpu's memcg
2074 * stock, and at least @nr_pages are available in that stock. Failure to
2075 * service an allocation will refill the stock.
2076 *
2077 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002078 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002079static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080{
2081 struct memcg_stock_pcp *stock;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002082 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002083
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002084 if (nr_pages > CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002085 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002086
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002087 stock = &get_cpu_var(memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002088 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002089 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002090 ret = true;
2091 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092 put_cpu_var(memcg_stock);
2093 return ret;
2094}
2095
2096/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002097 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002098 */
2099static void drain_stock(struct memcg_stock_pcp *stock)
2100{
2101 struct mem_cgroup *old = stock->cached;
2102
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002103 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002104 page_counter_uncharge(&old->memory, stock->nr_pages);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002105 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002106 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002107 css_put_many(&old->css, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002108 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002109 }
2110 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002111}
2112
2113/*
2114 * This must be called under preempt disabled or must be called by
2115 * a thread which is pinned to local cpu.
2116 */
2117static void drain_local_stock(struct work_struct *dummy)
2118{
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002119 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002120 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002121 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002122}
2123
2124/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002125 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002126 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002127 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002128static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002129{
2130 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2131
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002132 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002134 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002135 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002136 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002137 put_cpu_var(memcg_stock);
2138}
2139
2140/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002141 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002142 * of the hierarchy under it.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002143 */
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002144static void drain_all_stock(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002145{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002146 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002147
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002148 /* If someone's already draining, avoid adding running more workers. */
2149 if (!mutex_trylock(&percpu_charge_mutex))
2150 return;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002151 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002152 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002153 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002154 for_each_online_cpu(cpu) {
2155 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002156 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002157
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002158 memcg = stock->cached;
2159 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002160 continue;
Johannes Weiner2314b422014-12-10 15:44:33 -08002161 if (!mem_cgroup_is_descendant(memcg, root_memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002162 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002163 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2164 if (cpu == curcpu)
2165 drain_local_stock(&stock->work);
2166 else
2167 schedule_work_on(cpu, &stock->work);
2168 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002169 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002170 put_cpu();
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002171 put_online_cpus();
Michal Hocko9f50fad2011-08-09 11:56:26 +02002172 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002173}
2174
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002175/*
2176 * This function drains percpu counter value from DEAD cpu and
2177 * move it to local cpu. Note that this function can be preempted.
2178 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002179static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002180{
2181 int i;
2182
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002183 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002184 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002185 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002186
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002187 per_cpu(memcg->stat->count[i], cpu) = 0;
2188 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002189 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002190 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002191 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002192
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002193 per_cpu(memcg->stat->events[i], cpu) = 0;
2194 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002195 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002196 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002197}
2198
Paul Gortmaker0db06282013-06-19 14:53:51 -04002199static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200 unsigned long action,
2201 void *hcpu)
2202{
2203 int cpu = (unsigned long)hcpu;
2204 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002205 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002206
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002207 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002208 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002209
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002210 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002211 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002212
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002213 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002214 mem_cgroup_drain_pcp_counter(iter, cpu);
2215
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002216 stock = &per_cpu(memcg_stock, cpu);
2217 drain_stock(stock);
2218 return NOTIFY_OK;
2219}
2220
Johannes Weiner00501b52014-08-08 14:19:20 -07002221static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2222 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002223{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002224 unsigned int batch = max(CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002225 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002226 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002227 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002228 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002229 bool may_swap = true;
2230 bool drained = false;
Johannes Weiner05b84302014-08-06 16:05:59 -07002231 int ret = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002232
Johannes Weinerce00a962014-09-05 08:43:57 -04002233 if (mem_cgroup_is_root(memcg))
2234 goto done;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002235retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002236 if (consume_stock(memcg, nr_pages))
2237 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002238
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002239 if (!do_swap_account ||
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002240 !page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2241 if (!page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002242 goto done_restock;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002243 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002244 page_counter_uncharge(&memcg->memsw, batch);
2245 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002246 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002247 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002248 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002249 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002250
Johannes Weiner6539cc02014-08-06 16:05:42 -07002251 if (batch > nr_pages) {
2252 batch = nr_pages;
2253 goto retry;
2254 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002255
Johannes Weiner06b078f2014-08-06 16:05:44 -07002256 /*
2257 * Unlike in global OOM situations, memcg is not in a physical
2258 * memory shortage. Allow dying and OOM-killed tasks to
2259 * bypass the last charges so that they can exit quickly and
2260 * free their memory.
2261 */
2262 if (unlikely(test_thread_flag(TIF_MEMDIE) ||
2263 fatal_signal_pending(current) ||
2264 current->flags & PF_EXITING))
2265 goto bypass;
2266
2267 if (unlikely(task_in_memcg_oom(current)))
2268 goto nomem;
2269
Johannes Weiner6539cc02014-08-06 16:05:42 -07002270 if (!(gfp_mask & __GFP_WAIT))
2271 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002272
Johannes Weiner241994e2015-02-11 15:26:06 -08002273 mem_cgroup_events(mem_over_limit, MEMCG_MAX, 1);
2274
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002275 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2276 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002277
Johannes Weiner61e02c72014-08-06 16:08:16 -07002278 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002279 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002280
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002281 if (!drained) {
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002282 drain_all_stock(mem_over_limit);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002283 drained = true;
2284 goto retry;
2285 }
2286
Johannes Weiner28c34c22014-08-06 16:05:47 -07002287 if (gfp_mask & __GFP_NORETRY)
2288 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002289 /*
2290 * Even though the limit is exceeded at this point, reclaim
2291 * may have been able to free some pages. Retry the charge
2292 * before killing the task.
2293 *
2294 * Only for regular pages, though: huge pages are rather
2295 * unlikely to succeed so close to the limit, and we fall back
2296 * to regular pages anyway in case of failure.
2297 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002298 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002299 goto retry;
2300 /*
2301 * At task move, charge accounts can be doubly counted. So, it's
2302 * better to wait until the end of task_move if something is going on.
2303 */
2304 if (mem_cgroup_wait_acct_move(mem_over_limit))
2305 goto retry;
2306
Johannes Weiner9b130612014-08-06 16:05:51 -07002307 if (nr_retries--)
2308 goto retry;
2309
Johannes Weiner06b078f2014-08-06 16:05:44 -07002310 if (gfp_mask & __GFP_NOFAIL)
2311 goto bypass;
2312
Johannes Weiner6539cc02014-08-06 16:05:42 -07002313 if (fatal_signal_pending(current))
2314 goto bypass;
2315
Johannes Weiner241994e2015-02-11 15:26:06 -08002316 mem_cgroup_events(mem_over_limit, MEMCG_OOM, 1);
2317
Johannes Weiner61e02c72014-08-06 16:08:16 -07002318 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages));
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002319nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002320 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002321 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002322bypass:
Johannes Weinerce00a962014-09-05 08:43:57 -04002323 return -EINTR;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002324
2325done_restock:
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002326 css_get_many(&memcg->css, batch);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002327 if (batch > nr_pages)
2328 refill_stock(memcg, batch - nr_pages);
Johannes Weiner241994e2015-02-11 15:26:06 -08002329 /*
2330 * If the hierarchy is above the normal consumption range,
2331 * make the charging task trim their excess contribution.
2332 */
2333 do {
2334 if (page_counter_read(&memcg->memory) <= memcg->high)
2335 continue;
2336 mem_cgroup_events(memcg, MEMCG_HIGH, 1);
2337 try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true);
2338 } while ((memcg = parent_mem_cgroup(memcg)));
Johannes Weiner6539cc02014-08-06 16:05:42 -07002339done:
Johannes Weiner05b84302014-08-06 16:05:59 -07002340 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002341}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002342
Johannes Weiner00501b52014-08-08 14:19:20 -07002343static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002344{
Johannes Weinerce00a962014-09-05 08:43:57 -04002345 if (mem_cgroup_is_root(memcg))
2346 return;
2347
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002348 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner05b84302014-08-06 16:05:59 -07002349 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002350 page_counter_uncharge(&memcg->memsw, nr_pages);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002351
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002352 css_put_many(&memcg->css, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002353}
2354
2355/*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002356 * try_get_mem_cgroup_from_page - look up page's memcg association
2357 * @page: the page
2358 *
2359 * Look up, get a css reference, and return the memcg that owns @page.
2360 *
2361 * The page must be locked to prevent racing with swap-in and page
2362 * cache charges. If coming from an unlocked page table, the caller
2363 * must ensure the page is on the LRU or this can race with charging.
2364 */
Wu Fengguange42d9d52009-12-16 12:19:59 +01002365struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002366{
Johannes Weiner29833312014-12-10 15:44:02 -08002367 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002368 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002369 swp_entry_t ent;
2370
Sasha Levin309381fea2014-01-23 15:52:54 -08002371 VM_BUG_ON_PAGE(!PageLocked(page), page);
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002372
Johannes Weiner1306a852014-12-10 15:44:52 -08002373 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002374 if (memcg) {
2375 if (!css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002376 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002377 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002378 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002379 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002380 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07002381 memcg = mem_cgroup_from_id(id);
Tejun Heoec903c02014-05-13 12:11:01 -04002382 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002383 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002384 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002385 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002386 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002387}
2388
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002389static void lock_page_lru(struct page *page, int *isolated)
2390{
2391 struct zone *zone = page_zone(page);
2392
2393 spin_lock_irq(&zone->lru_lock);
2394 if (PageLRU(page)) {
2395 struct lruvec *lruvec;
2396
2397 lruvec = mem_cgroup_page_lruvec(page, zone);
2398 ClearPageLRU(page);
2399 del_page_from_lru_list(page, lruvec, page_lru(page));
2400 *isolated = 1;
2401 } else
2402 *isolated = 0;
2403}
2404
2405static void unlock_page_lru(struct page *page, int isolated)
2406{
2407 struct zone *zone = page_zone(page);
2408
2409 if (isolated) {
2410 struct lruvec *lruvec;
2411
2412 lruvec = mem_cgroup_page_lruvec(page, zone);
2413 VM_BUG_ON_PAGE(PageLRU(page), page);
2414 SetPageLRU(page);
2415 add_page_to_lru_list(page, lruvec, page_lru(page));
2416 }
2417 spin_unlock_irq(&zone->lru_lock);
2418}
2419
Johannes Weiner00501b52014-08-08 14:19:20 -07002420static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002421 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002422{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002423 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002424
Johannes Weiner1306a852014-12-10 15:44:52 -08002425 VM_BUG_ON_PAGE(page->mem_cgroup, page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002426
2427 /*
2428 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2429 * may already be on some other mem_cgroup's LRU. Take care of it.
2430 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002431 if (lrucare)
2432 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002433
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002434 /*
2435 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08002436 * page->mem_cgroup at this point:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002437 *
2438 * - the page is uncharged
2439 *
2440 * - the page is off-LRU
2441 *
2442 * - an anonymous fault has exclusive page access, except for
2443 * a locked page table
2444 *
2445 * - a page cache insertion, a swapin fault, or a migration
2446 * have the page locked
2447 */
Johannes Weiner1306a852014-12-10 15:44:52 -08002448 page->mem_cgroup = memcg;
Hugh Dickins3be91272008-02-07 00:14:19 -08002449
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002450 if (lrucare)
2451 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002452}
2453
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002454#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002455int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
2456 unsigned long nr_pages)
Glauber Costa749c5412012-12-18 14:23:01 -08002457{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002458 struct page_counter *counter;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002459 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002460
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002461 ret = page_counter_try_charge(&memcg->kmem, nr_pages, &counter);
2462 if (ret < 0)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002463 return ret;
2464
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002465 ret = try_charge(memcg, gfp, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002466 if (ret == -EINTR) {
2467 /*
Johannes Weiner00501b52014-08-08 14:19:20 -07002468 * try_charge() chose to bypass to root due to OOM kill or
2469 * fatal signal. Since our only options are to either fail
2470 * the allocation or charge it to this cgroup, do it as a
2471 * temporary condition. But we can't fail. From a kmem/slab
2472 * perspective, the cache has already been selected, by
2473 * mem_cgroup_kmem_get_cache(), so it is too late to change
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002474 * our minds.
2475 *
2476 * This condition will only trigger if the task entered
Johannes Weiner00501b52014-08-08 14:19:20 -07002477 * memcg_charge_kmem in a sane state, but was OOM-killed
2478 * during try_charge() above. Tasks that were already dying
2479 * when the allocation triggers should have been already
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002480 * directed to the root cgroup in memcontrol.h
2481 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002482 page_counter_charge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002483 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002484 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002485 css_get_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002486 ret = 0;
2487 } else if (ret)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002488 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002489
2490 return ret;
2491}
2492
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002493void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002494{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002495 page_counter_uncharge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002496 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002497 page_counter_uncharge(&memcg->memsw, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002498
Johannes Weiner64f21992014-12-10 15:42:45 -08002499 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002500
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002501 css_put_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002502}
2503
Glauber Costa2633d7a2012-12-18 14:22:34 -08002504/*
2505 * helper for acessing a memcg's index. It will be used as an index in the
2506 * child cache array in kmem_cache, and also to derive its name. This function
2507 * will return -1 when this is not a kmem-limited memcg.
2508 */
2509int memcg_cache_id(struct mem_cgroup *memcg)
2510{
2511 return memcg ? memcg->kmemcg_id : -1;
2512}
2513
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002514static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002515{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002516 int id, size;
2517 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002518
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002519 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002520 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2521 if (id < 0)
2522 return id;
2523
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002524 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002525 return id;
2526
2527 /*
2528 * There's no space for the new id in memcg_caches arrays,
2529 * so we have to grow them.
2530 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002531 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002532
2533 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002534 if (size < MEMCG_CACHES_MIN_SIZE)
2535 size = MEMCG_CACHES_MIN_SIZE;
2536 else if (size > MEMCG_CACHES_MAX_SIZE)
2537 size = MEMCG_CACHES_MAX_SIZE;
2538
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002539 err = memcg_update_all_caches(size);
Vladimir Davydov05257a12015-02-12 14:59:01 -08002540 if (!err)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002541 err = memcg_update_all_list_lrus(size);
2542 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002543 memcg_nr_cache_ids = size;
2544
2545 up_write(&memcg_cache_ids_sem);
2546
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002547 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002548 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002549 return err;
2550 }
2551 return id;
2552}
2553
2554static void memcg_free_cache_id(int id)
2555{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002556 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002557}
2558
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002559struct memcg_kmem_cache_create_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07002560 struct mem_cgroup *memcg;
2561 struct kmem_cache *cachep;
2562 struct work_struct work;
2563};
2564
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002565static void memcg_kmem_cache_create_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08002566{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002567 struct memcg_kmem_cache_create_work *cw =
2568 container_of(w, struct memcg_kmem_cache_create_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07002569 struct mem_cgroup *memcg = cw->memcg;
2570 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002571
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002572 memcg_create_kmem_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002573
Vladimir Davydov5722d092014-04-07 15:39:24 -07002574 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002575 kfree(cw);
2576}
2577
2578/*
2579 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002580 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002581static void __memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2582 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002583{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002584 struct memcg_kmem_cache_create_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08002585
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002586 cw = kmalloc(sizeof(*cw), GFP_NOWAIT);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002587 if (!cw)
Glauber Costad7f25f82012-12-18 14:22:40 -08002588 return;
Vladimir Davydov8135be52014-12-12 16:56:38 -08002589
2590 css_get(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002591
2592 cw->memcg = memcg;
2593 cw->cachep = cachep;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002594 INIT_WORK(&cw->work, memcg_kmem_cache_create_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08002595
Glauber Costad7f25f82012-12-18 14:22:40 -08002596 schedule_work(&cw->work);
2597}
2598
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002599static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2600 struct kmem_cache *cachep)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002601{
2602 /*
2603 * We need to stop accounting when we kmalloc, because if the
2604 * corresponding kmalloc cache is not yet created, the first allocation
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002605 * in __memcg_schedule_kmem_cache_create will recurse.
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002606 *
2607 * However, it is better to enclose the whole function. Depending on
2608 * the debugging options enabled, INIT_WORK(), for instance, can
2609 * trigger an allocation. This too, will make us recurse. Because at
2610 * this point we can't allow ourselves back into memcg_kmem_get_cache,
2611 * the safest choice is to do it like this, wrapping the whole function.
2612 */
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002613 current->memcg_kmem_skip_account = 1;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002614 __memcg_schedule_kmem_cache_create(memcg, cachep);
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002615 current->memcg_kmem_skip_account = 0;
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002616}
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07002617
Glauber Costad7f25f82012-12-18 14:22:40 -08002618/*
2619 * Return the kmem_cache we're supposed to use for a slab allocation.
2620 * We try to use the current memcg's version of the cache.
2621 *
2622 * If the cache does not exist yet, if we are the first user of it,
2623 * we either create it immediately, if possible, or create it asynchronously
2624 * in a workqueue.
2625 * In the latter case, we will let the current allocation go through with
2626 * the original cache.
2627 *
2628 * Can't be called in interrupt context or from kernel threads.
2629 * This function needs to be called with rcu_read_lock() held.
2630 */
Zhang Zhen056b7cc2014-12-12 16:55:38 -08002631struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002632{
2633 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08002634 struct kmem_cache *memcg_cachep;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002635 int kmemcg_id;
Glauber Costad7f25f82012-12-18 14:22:40 -08002636
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002637 VM_BUG_ON(!is_root_cache(cachep));
Glauber Costad7f25f82012-12-18 14:22:40 -08002638
Vladimir Davydov9d100c52014-12-12 16:54:53 -08002639 if (current->memcg_kmem_skip_account)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002640 return cachep;
2641
Vladimir Davydov8135be52014-12-12 16:56:38 -08002642 memcg = get_mem_cgroup_from_mm(current->mm);
Jason Low4db0c3c2015-04-15 16:14:08 -07002643 kmemcg_id = READ_ONCE(memcg->kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002644 if (kmemcg_id < 0)
Li Zefanca0dde92013-04-29 15:08:57 -07002645 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08002646
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002647 memcg_cachep = cache_from_memcg_idx(cachep, kmemcg_id);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002648 if (likely(memcg_cachep))
2649 return memcg_cachep;
Li Zefanca0dde92013-04-29 15:08:57 -07002650
2651 /*
2652 * If we are in a safe context (can wait, and not in interrupt
2653 * context), we could be be predictable and return right away.
2654 * This would guarantee that the allocation being performed
2655 * already belongs in the new cache.
2656 *
2657 * However, there are some clashes that can arrive from locking.
2658 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002659 * memcg_create_kmem_cache, this means no further allocation
2660 * could happen with the slab_mutex held. So it's better to
2661 * defer everything.
Li Zefanca0dde92013-04-29 15:08:57 -07002662 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002663 memcg_schedule_kmem_cache_create(memcg, cachep);
Li Zefanca0dde92013-04-29 15:08:57 -07002664out:
Vladimir Davydov8135be52014-12-12 16:56:38 -08002665 css_put(&memcg->css);
Li Zefanca0dde92013-04-29 15:08:57 -07002666 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002667}
Glauber Costad7f25f82012-12-18 14:22:40 -08002668
Vladimir Davydov8135be52014-12-12 16:56:38 -08002669void __memcg_kmem_put_cache(struct kmem_cache *cachep)
2670{
2671 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002672 css_put(&cachep->memcg_params.memcg->css);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002673}
2674
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002675/*
2676 * We need to verify if the allocation against current->mm->owner's memcg is
2677 * possible for the given order. But the page is not allocated yet, so we'll
2678 * need a further commit step to do the final arrangements.
2679 *
2680 * It is possible for the task to switch cgroups in this mean time, so at
2681 * commit time, we can't rely on task conversion any longer. We'll then use
2682 * the handle argument to return to the caller which cgroup we should commit
2683 * against. We could also return the memcg directly and avoid the pointer
2684 * passing, but a boolean return value gives better semantics considering
2685 * the compiled-out case as well.
2686 *
2687 * Returning true means the allocation is possible.
2688 */
2689bool
2690__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2691{
2692 struct mem_cgroup *memcg;
2693 int ret;
2694
2695 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07002696
Johannes Weinerdf381972014-04-07 15:37:43 -07002697 memcg = get_mem_cgroup_from_mm(current->mm);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002698
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07002699 if (!memcg_kmem_is_active(memcg)) {
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002700 css_put(&memcg->css);
2701 return true;
2702 }
2703
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002704 ret = memcg_charge_kmem(memcg, gfp, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002705 if (!ret)
2706 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002707
2708 css_put(&memcg->css);
2709 return (ret == 0);
2710}
2711
2712void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2713 int order)
2714{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002715 VM_BUG_ON(mem_cgroup_is_root(memcg));
2716
2717 /* The page allocation failed. Revert */
2718 if (!page) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002719 memcg_uncharge_kmem(memcg, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002720 return;
2721 }
Johannes Weiner1306a852014-12-10 15:44:52 -08002722 page->mem_cgroup = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002723}
2724
2725void __memcg_kmem_uncharge_pages(struct page *page, int order)
2726{
Johannes Weiner1306a852014-12-10 15:44:52 -08002727 struct mem_cgroup *memcg = page->mem_cgroup;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002728
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002729 if (!memcg)
2730 return;
2731
Sasha Levin309381fea2014-01-23 15:52:54 -08002732 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Johannes Weiner29833312014-12-10 15:44:02 -08002733
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002734 memcg_uncharge_kmem(memcg, 1 << order);
Johannes Weiner1306a852014-12-10 15:44:52 -08002735 page->mem_cgroup = NULL;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002736}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002737
2738struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr)
2739{
2740 struct mem_cgroup *memcg = NULL;
2741 struct kmem_cache *cachep;
2742 struct page *page;
2743
2744 page = virt_to_head_page(ptr);
2745 if (PageSlab(page)) {
2746 cachep = page->slab_cache;
2747 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002748 memcg = cachep->memcg_params.memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002749 } else
2750 /* page allocated by alloc_kmem_pages */
2751 memcg = page->mem_cgroup;
2752
2753 return memcg;
2754}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002755#endif /* CONFIG_MEMCG_KMEM */
2756
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002757#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2758
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002759/*
2760 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002761 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2762 * charge/uncharge will be never happen and move_account() is done under
2763 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002764 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002765void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002766{
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002767 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002768
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002769 if (mem_cgroup_disabled())
2770 return;
David Rientjesb070e652013-05-07 16:18:09 -07002771
Johannes Weiner29833312014-12-10 15:44:02 -08002772 for (i = 1; i < HPAGE_PMD_NR; i++)
Johannes Weiner1306a852014-12-10 15:44:52 -08002773 head[i].mem_cgroup = head->mem_cgroup;
Michal Hockob9982f82014-12-10 15:43:51 -08002774
Johannes Weiner1306a852014-12-10 15:44:52 -08002775 __this_cpu_sub(head->mem_cgroup->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
David Rientjesb070e652013-05-07 16:18:09 -07002776 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002777}
Hugh Dickins12d27102012-01-12 17:19:52 -08002778#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002779
Andrew Mortonc255a452012-07-31 16:43:02 -07002780#ifdef CONFIG_MEMCG_SWAP
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002781static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
2782 bool charge)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002783{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002784 int val = (charge) ? 1 : -1;
2785 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002786}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002787
2788/**
2789 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2790 * @entry: swap entry to be moved
2791 * @from: mem_cgroup which the entry is moved from
2792 * @to: mem_cgroup which the entry is moved to
2793 *
2794 * It succeeds only when the swap_cgroup's record for this entry is the same
2795 * as the mem_cgroup's id of @from.
2796 *
2797 * Returns 0 on success, -EINVAL on failure.
2798 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002799 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08002800 * both res and memsw, and called css_get().
2801 */
2802static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002803 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002804{
2805 unsigned short old_id, new_id;
2806
Li Zefan34c00c32013-09-23 16:56:01 +08002807 old_id = mem_cgroup_id(from);
2808 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002809
2810 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002811 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002812 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002813 return 0;
2814 }
2815 return -EINVAL;
2816}
2817#else
2818static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002819 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002820{
2821 return -EINVAL;
2822}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002823#endif
2824
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002825static DEFINE_MUTEX(memcg_limit_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07002826
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002827static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002828 unsigned long limit)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002829{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002830 unsigned long curusage;
2831 unsigned long oldusage;
2832 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002833 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002834 int ret;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002835
2836 /*
2837 * For keeping hierarchical_reclaim simple, how long we should retry
2838 * is depends on callers. We set our retry-count to be function
2839 * of # of children which we should visit in this loop.
2840 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002841 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2842 mem_cgroup_count_children(memcg);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002843
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002844 oldusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002845
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002846 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002847 if (signal_pending(current)) {
2848 ret = -EINTR;
2849 break;
2850 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002851
2852 mutex_lock(&memcg_limit_mutex);
2853 if (limit > memcg->memsw.limit) {
2854 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002855 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002856 break;
2857 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002858 if (limit > memcg->memory.limit)
2859 enlarge = true;
2860 ret = page_counter_limit(&memcg->memory, limit);
2861 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002862
2863 if (!ret)
2864 break;
2865
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002866 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, true);
2867
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002868 curusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002869 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002870 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002871 retry_count--;
2872 else
2873 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002874 } while (retry_count);
2875
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002876 if (!ret && enlarge)
2877 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002878
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002879 return ret;
2880}
2881
Li Zefan338c8432009-06-17 16:27:15 -07002882static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002883 unsigned long limit)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002884{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002885 unsigned long curusage;
2886 unsigned long oldusage;
2887 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002888 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002889 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002890
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002891 /* see mem_cgroup_resize_res_limit */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002892 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2893 mem_cgroup_count_children(memcg);
2894
2895 oldusage = page_counter_read(&memcg->memsw);
2896
2897 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002898 if (signal_pending(current)) {
2899 ret = -EINTR;
2900 break;
2901 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002902
2903 mutex_lock(&memcg_limit_mutex);
2904 if (limit < memcg->memory.limit) {
2905 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002906 ret = -EINVAL;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002907 break;
2908 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002909 if (limit > memcg->memsw.limit)
2910 enlarge = true;
2911 ret = page_counter_limit(&memcg->memsw, limit);
2912 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002913
2914 if (!ret)
2915 break;
2916
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002917 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, false);
2918
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002919 curusage = page_counter_read(&memcg->memsw);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002920 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002921 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002922 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002923 else
2924 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002925 } while (retry_count);
2926
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002927 if (!ret && enlarge)
2928 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002929
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002930 return ret;
2931}
2932
Andrew Morton0608f432013-09-24 15:27:41 -07002933unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2934 gfp_t gfp_mask,
2935 unsigned long *total_scanned)
2936{
2937 unsigned long nr_reclaimed = 0;
2938 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2939 unsigned long reclaimed;
2940 int loop = 0;
2941 struct mem_cgroup_tree_per_zone *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002942 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07002943 unsigned long nr_scanned;
2944
2945 if (order > 0)
2946 return 0;
2947
2948 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
2949 /*
2950 * This loop can run a while, specially if mem_cgroup's continuously
2951 * keep exceeding their soft limit and putting the system under
2952 * pressure
2953 */
2954 do {
2955 if (next_mz)
2956 mz = next_mz;
2957 else
2958 mz = mem_cgroup_largest_soft_limit_node(mctz);
2959 if (!mz)
2960 break;
2961
2962 nr_scanned = 0;
2963 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
2964 gfp_mask, &nr_scanned);
2965 nr_reclaimed += reclaimed;
2966 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002967 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08002968 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07002969
2970 /*
2971 * If we failed to reclaim anything from this memory cgroup
2972 * it is time to move on to the next cgroup
2973 */
2974 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08002975 if (!reclaimed)
2976 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
2977
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002978 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07002979 /*
2980 * One school of thought says that we should not add
2981 * back the node to the tree if reclaim returns 0.
2982 * But our reclaim could return 0, simply because due
2983 * to priority we are exposing a smaller subset of
2984 * memory to reclaim from. Consider this as a longer
2985 * term TODO.
2986 */
2987 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07002988 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002989 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07002990 css_put(&mz->memcg->css);
2991 loop++;
2992 /*
2993 * Could not reclaim anything and there are no more
2994 * mem cgroups to try or we seem to be looping without
2995 * reclaiming anything.
2996 */
2997 if (!nr_reclaimed &&
2998 (next_mz == NULL ||
2999 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3000 break;
3001 } while (!nr_reclaimed);
3002 if (next_mz)
3003 css_put(&next_mz->memcg->css);
3004 return nr_reclaimed;
3005}
3006
Tejun Heoea280e72014-05-16 13:22:48 -04003007/*
3008 * Test whether @memcg has children, dead or alive. Note that this
3009 * function doesn't care whether @memcg has use_hierarchy enabled and
3010 * returns %true if there are child csses according to the cgroup
3011 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
3012 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003013static inline bool memcg_has_children(struct mem_cgroup *memcg)
3014{
Tejun Heoea280e72014-05-16 13:22:48 -04003015 bool ret;
3016
Johannes Weiner696ac172013-10-31 16:34:15 -07003017 /*
Tejun Heoea280e72014-05-16 13:22:48 -04003018 * The lock does not prevent addition or deletion of children, but
3019 * it prevents a new child from being initialized based on this
3020 * parent in css_online(), so it's enough to decide whether
3021 * hierarchically inherited attributes can still be changed or not.
Johannes Weiner696ac172013-10-31 16:34:15 -07003022 */
Tejun Heoea280e72014-05-16 13:22:48 -04003023 lockdep_assert_held(&memcg_create_mutex);
3024
3025 rcu_read_lock();
3026 ret = css_next_child(NULL, &memcg->css);
3027 rcu_read_unlock();
3028 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003029}
3030
3031/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003032 * Reclaims as many pages from the given memcg as possible and moves
3033 * the rest to the parent.
3034 *
3035 * Caller is responsible for holding css reference for memcg.
3036 */
3037static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3038{
3039 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003040
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003041 /* we call try-to-free pages for make this cgroup empty */
3042 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003043 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003044 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003045 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003046
Michal Hockoc26251f2012-10-26 13:37:28 +02003047 if (signal_pending(current))
3048 return -EINTR;
3049
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003050 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3051 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003052 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003053 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003054 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003055 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003056 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003057
3058 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003059
3060 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003061}
3062
Tejun Heo6770c642014-05-13 12:16:21 -04003063static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3064 char *buf, size_t nbytes,
3065 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003066{
Tejun Heo6770c642014-05-13 12:16:21 -04003067 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003068
Michal Hockod8423012012-10-26 13:37:29 +02003069 if (mem_cgroup_is_root(memcg))
3070 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003071 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003072}
3073
Tejun Heo182446d2013-08-08 20:11:24 -04003074static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3075 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003076{
Tejun Heo182446d2013-08-08 20:11:24 -04003077 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003078}
3079
Tejun Heo182446d2013-08-08 20:11:24 -04003080static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3081 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003082{
3083 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003084 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003085 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003086
Glauber Costa09998212013-02-22 16:34:55 -08003087 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07003088
3089 if (memcg->use_hierarchy == val)
3090 goto out;
3091
Balbir Singh18f59ea2009-01-07 18:08:07 -08003092 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003093 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003094 * in the child subtrees. If it is unset, then the change can
3095 * occur, provided the current cgroup has no children.
3096 *
3097 * For the root cgroup, parent_mem is NULL, we allow value to be
3098 * set if there are no children.
3099 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003100 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003101 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003102 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003103 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003104 else
3105 retval = -EBUSY;
3106 } else
3107 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003108
3109out:
Glauber Costa09998212013-02-22 16:34:55 -08003110 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003111
3112 return retval;
3113}
3114
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003115static unsigned long tree_stat(struct mem_cgroup *memcg,
3116 enum mem_cgroup_stat_index idx)
Johannes Weinerce00a962014-09-05 08:43:57 -04003117{
3118 struct mem_cgroup *iter;
3119 long val = 0;
3120
3121 /* Per-cpu values can be negative, use a signed accumulator */
3122 for_each_mem_cgroup_tree(iter, memcg)
3123 val += mem_cgroup_read_stat(iter, idx);
3124
3125 if (val < 0) /* race ? */
3126 val = 0;
3127 return val;
3128}
3129
3130static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3131{
3132 u64 val;
3133
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003134 if (mem_cgroup_is_root(memcg)) {
3135 val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE);
3136 val += tree_stat(memcg, MEM_CGROUP_STAT_RSS);
3137 if (swap)
3138 val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP);
3139 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003140 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003141 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003142 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003143 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003144 }
Johannes Weinerce00a962014-09-05 08:43:57 -04003145 return val << PAGE_SHIFT;
3146}
3147
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003148enum {
3149 RES_USAGE,
3150 RES_LIMIT,
3151 RES_MAX_USAGE,
3152 RES_FAILCNT,
3153 RES_SOFT_LIMIT,
3154};
Johannes Weinerce00a962014-09-05 08:43:57 -04003155
Tejun Heo791badb2013-12-05 12:28:02 -05003156static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003157 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003158{
Tejun Heo182446d2013-08-08 20:11:24 -04003159 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003160 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003161
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003162 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003163 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003164 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003165 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003166 case _MEMSWAP:
3167 counter = &memcg->memsw;
3168 break;
3169 case _KMEM:
3170 counter = &memcg->kmem;
3171 break;
3172 default:
3173 BUG();
3174 }
3175
3176 switch (MEMFILE_ATTR(cft->private)) {
3177 case RES_USAGE:
3178 if (counter == &memcg->memory)
3179 return mem_cgroup_usage(memcg, false);
3180 if (counter == &memcg->memsw)
3181 return mem_cgroup_usage(memcg, true);
3182 return (u64)page_counter_read(counter) * PAGE_SIZE;
3183 case RES_LIMIT:
3184 return (u64)counter->limit * PAGE_SIZE;
3185 case RES_MAX_USAGE:
3186 return (u64)counter->watermark * PAGE_SIZE;
3187 case RES_FAILCNT:
3188 return counter->failcnt;
3189 case RES_SOFT_LIMIT:
3190 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003191 default:
3192 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003193 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003194}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003195
Glauber Costa510fc4e2012-12-18 14:21:47 -08003196#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003197static int memcg_activate_kmem(struct mem_cgroup *memcg,
3198 unsigned long nr_pages)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003199{
3200 int err = 0;
3201 int memcg_id;
3202
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003203 BUG_ON(memcg->kmemcg_id >= 0);
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003204 BUG_ON(memcg->kmem_acct_activated);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003205 BUG_ON(memcg->kmem_acct_active);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003206
3207 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -08003208 * For simplicity, we won't allow this to be disabled. It also can't
3209 * be changed if the cgroup has children already, or if tasks had
3210 * already joined.
3211 *
3212 * If tasks join before we set the limit, a person looking at
3213 * kmem.usage_in_bytes will have no way to determine when it took
3214 * place, which makes the value quite meaningless.
3215 *
3216 * After it first became limited, changes in the value of the limit are
3217 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08003218 */
Glauber Costa09998212013-02-22 16:34:55 -08003219 mutex_lock(&memcg_create_mutex);
Tejun Heoea280e72014-05-16 13:22:48 -04003220 if (cgroup_has_tasks(memcg->css.cgroup) ||
3221 (memcg->use_hierarchy && memcg_has_children(memcg)))
Vladimir Davydovd6441632014-01-23 15:53:09 -08003222 err = -EBUSY;
Glauber Costa09998212013-02-22 16:34:55 -08003223 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003224 if (err)
3225 goto out;
3226
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003227 memcg_id = memcg_alloc_cache_id();
Vladimir Davydovd6441632014-01-23 15:53:09 -08003228 if (memcg_id < 0) {
3229 err = memcg_id;
3230 goto out;
3231 }
3232
Vladimir Davydovd6441632014-01-23 15:53:09 -08003233 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003234 * We couldn't have accounted to this cgroup, because it hasn't got
3235 * activated yet, so this should succeed.
Vladimir Davydovd6441632014-01-23 15:53:09 -08003236 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003237 err = page_counter_limit(&memcg->kmem, nr_pages);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003238 VM_BUG_ON(err);
3239
3240 static_key_slow_inc(&memcg_kmem_enabled_key);
3241 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003242 * A memory cgroup is considered kmem-active as soon as it gets
3243 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003244 * guarantee no one starts accounting before all call sites are
3245 * patched.
3246 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003247 memcg->kmemcg_id = memcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003248 memcg->kmem_acct_activated = true;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003249 memcg->kmem_acct_active = true;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003250out:
Vladimir Davydovd6441632014-01-23 15:53:09 -08003251 return err;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003252}
3253
Vladimir Davydovd6441632014-01-23 15:53:09 -08003254static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003255 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003256{
3257 int ret;
3258
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003259 mutex_lock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003260 if (!memcg_kmem_is_active(memcg))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003261 ret = memcg_activate_kmem(memcg, limit);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003262 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003263 ret = page_counter_limit(&memcg->kmem, limit);
3264 mutex_unlock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003265 return ret;
3266}
3267
Glauber Costa55007d82012-12-18 14:22:38 -08003268static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08003269{
Glauber Costa55007d82012-12-18 14:22:38 -08003270 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003271 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003272
Glauber Costa510fc4e2012-12-18 14:21:47 -08003273 if (!parent)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003274 return 0;
Glauber Costa55007d82012-12-18 14:22:38 -08003275
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003276 mutex_lock(&memcg_limit_mutex);
Glauber Costaa8964b92012-12-18 14:22:09 -08003277 /*
Vladimir Davydovd6441632014-01-23 15:53:09 -08003278 * If the parent cgroup is not kmem-active now, it cannot be activated
3279 * after this point, because it has at least one child already.
Glauber Costaa8964b92012-12-18 14:22:09 -08003280 */
Vladimir Davydovd6441632014-01-23 15:53:09 -08003281 if (memcg_kmem_is_active(parent))
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003282 ret = memcg_activate_kmem(memcg, PAGE_COUNTER_MAX);
3283 mutex_unlock(&memcg_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08003284 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003285}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003286#else
3287static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003288 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003289{
3290 return -EINVAL;
3291}
Hugh Dickins6d0439902013-02-22 16:35:50 -08003292#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08003293
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003294/*
3295 * The user of this function is...
3296 * RES_LIMIT.
3297 */
Tejun Heo451af502014-05-13 12:16:21 -04003298static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3299 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003300{
Tejun Heo451af502014-05-13 12:16:21 -04003301 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003302 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003303 int ret;
3304
Tejun Heo451af502014-05-13 12:16:21 -04003305 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003306 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003307 if (ret)
3308 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003309
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003310 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003311 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003312 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3313 ret = -EINVAL;
3314 break;
3315 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003316 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3317 case _MEM:
3318 ret = mem_cgroup_resize_limit(memcg, nr_pages);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003319 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003320 case _MEMSWAP:
3321 ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages);
3322 break;
3323 case _KMEM:
3324 ret = memcg_update_kmem_limit(memcg, nr_pages);
3325 break;
3326 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003327 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003328 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003329 memcg->soft_limit = nr_pages;
3330 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003331 break;
3332 }
Tejun Heo451af502014-05-13 12:16:21 -04003333 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003334}
3335
Tejun Heo6770c642014-05-13 12:16:21 -04003336static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3337 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003338{
Tejun Heo6770c642014-05-13 12:16:21 -04003339 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003340 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003341
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003342 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3343 case _MEM:
3344 counter = &memcg->memory;
3345 break;
3346 case _MEMSWAP:
3347 counter = &memcg->memsw;
3348 break;
3349 case _KMEM:
3350 counter = &memcg->kmem;
3351 break;
3352 default:
3353 BUG();
3354 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003355
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003356 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003357 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003358 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003359 break;
3360 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003361 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003362 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003363 default:
3364 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003365 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003366
Tejun Heo6770c642014-05-13 12:16:21 -04003367 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003368}
3369
Tejun Heo182446d2013-08-08 20:11:24 -04003370static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003371 struct cftype *cft)
3372{
Tejun Heo182446d2013-08-08 20:11:24 -04003373 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003374}
3375
Daisuke Nishimura02491442010-03-10 15:22:17 -08003376#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003377static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003378 struct cftype *cft, u64 val)
3379{
Tejun Heo182446d2013-08-08 20:11:24 -04003380 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003381
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003382 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003383 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003384
Glauber Costaee5e8472013-02-22 16:34:50 -08003385 /*
3386 * No kind of locking is needed in here, because ->can_attach() will
3387 * check this value once in the beginning of the process, and then carry
3388 * on with stale data. This means that changes to this value will only
3389 * affect task migrations starting after the change.
3390 */
3391 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003392 return 0;
3393}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003394#else
Tejun Heo182446d2013-08-08 20:11:24 -04003395static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003396 struct cftype *cft, u64 val)
3397{
3398 return -ENOSYS;
3399}
3400#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003401
Ying Han406eb0c2011-05-26 16:25:37 -07003402#ifdef CONFIG_NUMA
Tejun Heo2da8ca82013-12-05 12:28:04 -05003403static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003404{
Greg Thelen25485de2013-11-12 15:07:40 -08003405 struct numa_stat {
3406 const char *name;
3407 unsigned int lru_mask;
3408 };
3409
3410 static const struct numa_stat stats[] = {
3411 { "total", LRU_ALL },
3412 { "file", LRU_ALL_FILE },
3413 { "anon", LRU_ALL_ANON },
3414 { "unevictable", BIT(LRU_UNEVICTABLE) },
3415 };
3416 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003417 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08003418 unsigned long nr;
Tejun Heo2da8ca82013-12-05 12:28:04 -05003419 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Ying Han406eb0c2011-05-26 16:25:37 -07003420
Greg Thelen25485de2013-11-12 15:07:40 -08003421 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3422 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
3423 seq_printf(m, "%s=%lu", stat->name, nr);
3424 for_each_node_state(nid, N_MEMORY) {
3425 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
3426 stat->lru_mask);
3427 seq_printf(m, " N%d=%lu", nid, nr);
3428 }
3429 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003430 }
Ying Han406eb0c2011-05-26 16:25:37 -07003431
Ying Han071aee12013-11-12 15:07:41 -08003432 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3433 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07003434
Ying Han071aee12013-11-12 15:07:41 -08003435 nr = 0;
3436 for_each_mem_cgroup_tree(iter, memcg)
3437 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
3438 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
3439 for_each_node_state(nid, N_MEMORY) {
3440 nr = 0;
3441 for_each_mem_cgroup_tree(iter, memcg)
3442 nr += mem_cgroup_node_nr_lru_pages(
3443 iter, nid, stat->lru_mask);
3444 seq_printf(m, " N%d=%lu", nid, nr);
3445 }
3446 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003447 }
Ying Han406eb0c2011-05-26 16:25:37 -07003448
Ying Han406eb0c2011-05-26 16:25:37 -07003449 return 0;
3450}
3451#endif /* CONFIG_NUMA */
3452
Tejun Heo2da8ca82013-12-05 12:28:04 -05003453static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003454{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003455 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003456 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003457 struct mem_cgroup *mi;
3458 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003459
Greg Thelen0ca44b12015-02-11 15:25:58 -08003460 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_stat_names) !=
3461 MEM_CGROUP_STAT_NSTATS);
3462 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_events_names) !=
3463 MEM_CGROUP_EVENTS_NSTATS);
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003464 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
3465
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003466 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003467 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003468 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003469 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
3470 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003471 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003472
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003473 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
3474 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
3475 mem_cgroup_read_events(memcg, i));
3476
3477 for (i = 0; i < NR_LRU_LISTS; i++)
3478 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
3479 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
3480
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003481 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003482 memory = memsw = PAGE_COUNTER_MAX;
3483 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
3484 memory = min(memory, mi->memory.limit);
3485 memsw = min(memsw, mi->memsw.limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003486 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003487 seq_printf(m, "hierarchical_memory_limit %llu\n",
3488 (u64)memory * PAGE_SIZE);
3489 if (do_swap_account)
3490 seq_printf(m, "hierarchical_memsw_limit %llu\n",
3491 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003492
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003493 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
3494 long long val = 0;
3495
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003496 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003497 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003498 for_each_mem_cgroup_tree(mi, memcg)
3499 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
3500 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
3501 }
3502
3503 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
3504 unsigned long long val = 0;
3505
3506 for_each_mem_cgroup_tree(mi, memcg)
3507 val += mem_cgroup_read_events(mi, i);
3508 seq_printf(m, "total_%s %llu\n",
3509 mem_cgroup_events_names[i], val);
3510 }
3511
3512 for (i = 0; i < NR_LRU_LISTS; i++) {
3513 unsigned long long val = 0;
3514
3515 for_each_mem_cgroup_tree(mi, memcg)
3516 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
3517 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003518 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003519
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003520#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003521 {
3522 int nid, zid;
3523 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07003524 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003525 unsigned long recent_rotated[2] = {0, 0};
3526 unsigned long recent_scanned[2] = {0, 0};
3527
3528 for_each_online_node(nid)
3529 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Jianyu Zhane2318752014-06-06 14:38:20 -07003530 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
Hugh Dickins89abfab2012-05-29 15:06:53 -07003531 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003532
Hugh Dickins89abfab2012-05-29 15:06:53 -07003533 recent_rotated[0] += rstat->recent_rotated[0];
3534 recent_rotated[1] += rstat->recent_rotated[1];
3535 recent_scanned[0] += rstat->recent_scanned[0];
3536 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003537 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07003538 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
3539 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
3540 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
3541 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003542 }
3543#endif
3544
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003545 return 0;
3546}
3547
Tejun Heo182446d2013-08-08 20:11:24 -04003548static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
3549 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003550{
Tejun Heo182446d2013-08-08 20:11:24 -04003551 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003552
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07003553 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003554}
3555
Tejun Heo182446d2013-08-08 20:11:24 -04003556static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
3557 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003558{
Tejun Heo182446d2013-08-08 20:11:24 -04003559 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08003560
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003561 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003562 return -EINVAL;
3563
Linus Torvalds14208b02014-06-09 15:03:33 -07003564 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003565 memcg->swappiness = val;
3566 else
3567 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08003568
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003569 return 0;
3570}
3571
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003572static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3573{
3574 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003575 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003576 int i;
3577
3578 rcu_read_lock();
3579 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003580 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003581 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003582 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003583
3584 if (!t)
3585 goto unlock;
3586
Johannes Weinerce00a962014-09-05 08:43:57 -04003587 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003588
3589 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07003590 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003591 * If it's not true, a threshold was crossed after last
3592 * call of __mem_cgroup_threshold().
3593 */
Phil Carmody5407a562010-05-26 14:42:42 -07003594 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003595
3596 /*
3597 * Iterate backward over array of thresholds starting from
3598 * current_threshold and check if a threshold is crossed.
3599 * If none of thresholds below usage is crossed, we read
3600 * only one element of the array here.
3601 */
3602 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3603 eventfd_signal(t->entries[i].eventfd, 1);
3604
3605 /* i = current_threshold + 1 */
3606 i++;
3607
3608 /*
3609 * Iterate forward over array of thresholds starting from
3610 * current_threshold+1 and check if a threshold is crossed.
3611 * If none of thresholds above usage is crossed, we read
3612 * only one element of the array here.
3613 */
3614 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3615 eventfd_signal(t->entries[i].eventfd, 1);
3616
3617 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003618 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003619unlock:
3620 rcu_read_unlock();
3621}
3622
3623static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3624{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003625 while (memcg) {
3626 __mem_cgroup_threshold(memcg, false);
3627 if (do_swap_account)
3628 __mem_cgroup_threshold(memcg, true);
3629
3630 memcg = parent_mem_cgroup(memcg);
3631 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003632}
3633
3634static int compare_thresholds(const void *a, const void *b)
3635{
3636 const struct mem_cgroup_threshold *_a = a;
3637 const struct mem_cgroup_threshold *_b = b;
3638
Greg Thelen2bff24a2013-09-11 14:23:08 -07003639 if (_a->threshold > _b->threshold)
3640 return 1;
3641
3642 if (_a->threshold < _b->threshold)
3643 return -1;
3644
3645 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003646}
3647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003648static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003649{
3650 struct mem_cgroup_eventfd_list *ev;
3651
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003652 spin_lock(&memcg_oom_lock);
3653
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003654 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003655 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003656
3657 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003658 return 0;
3659}
3660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003661static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003662{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003663 struct mem_cgroup *iter;
3664
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003665 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003666 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003667}
3668
Tejun Heo59b6f872013-11-22 18:20:43 -05003669static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003670 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003671{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003672 struct mem_cgroup_thresholds *thresholds;
3673 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003674 unsigned long threshold;
3675 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003676 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003677
Johannes Weiner650c5e52015-02-11 15:26:03 -08003678 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003679 if (ret)
3680 return ret;
3681
3682 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003683
Johannes Weiner05b84302014-08-06 16:05:59 -07003684 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003685 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003686 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003687 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003688 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003689 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003690 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003691 BUG();
3692
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003693 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003694 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003695 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3696
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003697 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003698
3699 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003700 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003701 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003702 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003703 ret = -ENOMEM;
3704 goto unlock;
3705 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003706 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003707
3708 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003709 if (thresholds->primary) {
3710 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003711 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003712 }
3713
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003714 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003715 new->entries[size - 1].eventfd = eventfd;
3716 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003717
3718 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003719 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003720 compare_thresholds, NULL);
3721
3722 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003723 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003724 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07003725 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003726 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003727 * new->current_threshold will not be used until
3728 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003729 * it here.
3730 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003731 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07003732 } else
3733 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003734 }
3735
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003736 /* Free old spare buffer and save old primary buffer as spare */
3737 kfree(thresholds->spare);
3738 thresholds->spare = thresholds->primary;
3739
3740 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003741
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003742 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003743 synchronize_rcu();
3744
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003745unlock:
3746 mutex_unlock(&memcg->thresholds_lock);
3747
3748 return ret;
3749}
3750
Tejun Heo59b6f872013-11-22 18:20:43 -05003751static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003752 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003753{
Tejun Heo59b6f872013-11-22 18:20:43 -05003754 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003755}
3756
Tejun Heo59b6f872013-11-22 18:20:43 -05003757static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003758 struct eventfd_ctx *eventfd, const char *args)
3759{
Tejun Heo59b6f872013-11-22 18:20:43 -05003760 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003761}
3762
Tejun Heo59b6f872013-11-22 18:20:43 -05003763static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003764 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003765{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003766 struct mem_cgroup_thresholds *thresholds;
3767 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003768 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003769 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003770
3771 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07003772
3773 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003774 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003775 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003776 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003777 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003778 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003779 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003780 BUG();
3781
Anton Vorontsov371528c2012-02-24 05:14:46 +04003782 if (!thresholds->primary)
3783 goto unlock;
3784
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003785 /* Check if a threshold crossed before removing */
3786 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3787
3788 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003789 size = 0;
3790 for (i = 0; i < thresholds->primary->size; i++) {
3791 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003792 size++;
3793 }
3794
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003795 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003796
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003797 /* Set thresholds array to NULL if we don't have thresholds */
3798 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003799 kfree(new);
3800 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003801 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003802 }
3803
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003804 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003805
3806 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003807 new->current_threshold = -1;
3808 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3809 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003810 continue;
3811
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003812 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07003813 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003814 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003815 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003816 * until rcu_assign_pointer(), so it's safe to increment
3817 * it here.
3818 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003819 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003820 }
3821 j++;
3822 }
3823
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003824swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003825 /* Swap primary and spare array */
3826 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07003827 /* If all events are unregistered, free the spare array */
3828 if (!new) {
3829 kfree(thresholds->spare);
3830 thresholds->spare = NULL;
3831 }
3832
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003833 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003834
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003835 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003836 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04003837unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003838 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003839}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003840
Tejun Heo59b6f872013-11-22 18:20:43 -05003841static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003842 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003843{
Tejun Heo59b6f872013-11-22 18:20:43 -05003844 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003845}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003846
Tejun Heo59b6f872013-11-22 18:20:43 -05003847static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003848 struct eventfd_ctx *eventfd)
3849{
Tejun Heo59b6f872013-11-22 18:20:43 -05003850 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003851}
3852
Tejun Heo59b6f872013-11-22 18:20:43 -05003853static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003854 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003855{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003856 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003857
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003858 event = kmalloc(sizeof(*event), GFP_KERNEL);
3859 if (!event)
3860 return -ENOMEM;
3861
Michal Hocko1af8efe2011-07-26 16:08:24 -07003862 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003863
3864 event->eventfd = eventfd;
3865 list_add(&event->list, &memcg->oom_notify);
3866
3867 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07003868 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003869 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07003870 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003871
3872 return 0;
3873}
3874
Tejun Heo59b6f872013-11-22 18:20:43 -05003875static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003876 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003877{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003878 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003879
Michal Hocko1af8efe2011-07-26 16:08:24 -07003880 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003882 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003883 if (ev->eventfd == eventfd) {
3884 list_del(&ev->list);
3885 kfree(ev);
3886 }
3887 }
3888
Michal Hocko1af8efe2011-07-26 16:08:24 -07003889 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003890}
3891
Tejun Heo2da8ca82013-12-05 12:28:04 -05003892static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003893{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003894 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003895
Tejun Heo791badb2013-12-05 12:28:02 -05003896 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
3897 seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003898 return 0;
3899}
3900
Tejun Heo182446d2013-08-08 20:11:24 -04003901static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003902 struct cftype *cft, u64 val)
3903{
Tejun Heo182446d2013-08-08 20:11:24 -04003904 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003905
3906 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07003907 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003908 return -EINVAL;
3909
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003910 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003911 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003912 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003913
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003914 return 0;
3915}
3916
Andrew Mortonc255a452012-07-31 16:43:02 -07003917#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03003918static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00003919{
Glauber Costa55007d82012-12-18 14:22:38 -08003920 int ret;
3921
Glauber Costa55007d82012-12-18 14:22:38 -08003922 ret = memcg_propagate_kmem(memcg);
3923 if (ret)
3924 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003925
Glauber Costa1d62e432012-04-09 19:36:33 -03003926 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07003927}
Glauber Costae5671df2011-12-11 21:47:01 +00003928
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003929static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
3930{
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003931 struct cgroup_subsys_state *css;
3932 struct mem_cgroup *parent, *child;
3933 int kmemcg_id;
3934
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003935 if (!memcg->kmem_acct_active)
3936 return;
3937
3938 /*
3939 * Clear the 'active' flag before clearing memcg_caches arrays entries.
3940 * Since we take the slab_mutex in memcg_deactivate_kmem_caches(), it
3941 * guarantees no cache will be created for this cgroup after we are
3942 * done (see memcg_create_kmem_cache()).
3943 */
3944 memcg->kmem_acct_active = false;
3945
3946 memcg_deactivate_kmem_caches(memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -08003947
3948 kmemcg_id = memcg->kmemcg_id;
3949 BUG_ON(kmemcg_id < 0);
3950
3951 parent = parent_mem_cgroup(memcg);
3952 if (!parent)
3953 parent = root_mem_cgroup;
3954
3955 /*
3956 * Change kmemcg_id of this cgroup and all its descendants to the
3957 * parent's id, and then move all entries from this cgroup's list_lrus
3958 * to ones of the parent. After we have finished, all list_lrus
3959 * corresponding to this cgroup are guaranteed to remain empty. The
3960 * ordering is imposed by list_lru_node->lock taken by
3961 * memcg_drain_all_list_lrus().
3962 */
3963 css_for_each_descendant_pre(css, &memcg->css) {
3964 child = mem_cgroup_from_css(css);
3965 BUG_ON(child->kmemcg_id != kmemcg_id);
3966 child->kmemcg_id = parent->kmemcg_id;
3967 if (!memcg->use_hierarchy)
3968 break;
3969 }
3970 memcg_drain_all_list_lrus(kmemcg_id, parent->kmemcg_id);
3971
3972 memcg_free_cache_id(kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003973}
3974
Li Zefan10d5ebf2013-07-08 16:00:33 -07003975static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00003976{
Vladimir Davydovf48b80a2015-02-12 14:59:56 -08003977 if (memcg->kmem_acct_activated) {
3978 memcg_destroy_kmem_caches(memcg);
3979 static_key_slow_dec(&memcg_kmem_enabled_key);
3980 WARN_ON(page_counter_read(&memcg->kmem));
3981 }
Glauber Costa1d62e432012-04-09 19:36:33 -03003982 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07003983}
Glauber Costae5671df2011-12-11 21:47:01 +00003984#else
Glauber Costacbe128e32012-04-09 19:36:34 -03003985static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00003986{
3987 return 0;
3988}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00003989
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003990static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
3991{
3992}
3993
Li Zefan10d5ebf2013-07-08 16:00:33 -07003994static void memcg_destroy_kmem(struct mem_cgroup *memcg)
3995{
3996}
Glauber Costae5671df2011-12-11 21:47:01 +00003997#endif
3998
Tejun Heo79bd9812013-11-22 18:20:42 -05003999/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004000 * DO NOT USE IN NEW FILES.
4001 *
4002 * "cgroup.event_control" implementation.
4003 *
4004 * This is way over-engineered. It tries to support fully configurable
4005 * events for each user. Such level of flexibility is completely
4006 * unnecessary especially in the light of the planned unified hierarchy.
4007 *
4008 * Please deprecate this and replace with something simpler if at all
4009 * possible.
4010 */
4011
4012/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004013 * Unregister event and free resources.
4014 *
4015 * Gets called from workqueue.
4016 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004017static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004018{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004019 struct mem_cgroup_event *event =
4020 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004021 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004022
4023 remove_wait_queue(event->wqh, &event->wait);
4024
Tejun Heo59b6f872013-11-22 18:20:43 -05004025 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004026
4027 /* Notify userspace the event is going away. */
4028 eventfd_signal(event->eventfd, 1);
4029
4030 eventfd_ctx_put(event->eventfd);
4031 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004032 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004033}
4034
4035/*
4036 * Gets called on POLLHUP on eventfd when user closes it.
4037 *
4038 * Called with wqh->lock held and interrupts disabled.
4039 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004040static int memcg_event_wake(wait_queue_t *wait, unsigned mode,
4041 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004042{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004043 struct mem_cgroup_event *event =
4044 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004045 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004046 unsigned long flags = (unsigned long)key;
4047
4048 if (flags & POLLHUP) {
4049 /*
4050 * If the event has been detached at cgroup removal, we
4051 * can simply return knowing the other side will cleanup
4052 * for us.
4053 *
4054 * We can't race against event freeing since the other
4055 * side will require wqh->lock via remove_wait_queue(),
4056 * which we hold.
4057 */
Tejun Heofba94802013-11-22 18:20:43 -05004058 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004059 if (!list_empty(&event->list)) {
4060 list_del_init(&event->list);
4061 /*
4062 * We are in atomic context, but cgroup_event_remove()
4063 * may sleep, so we have to call it in workqueue.
4064 */
4065 schedule_work(&event->remove);
4066 }
Tejun Heofba94802013-11-22 18:20:43 -05004067 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004068 }
4069
4070 return 0;
4071}
4072
Tejun Heo3bc942f2013-11-22 18:20:44 -05004073static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004074 wait_queue_head_t *wqh, poll_table *pt)
4075{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004076 struct mem_cgroup_event *event =
4077 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004078
4079 event->wqh = wqh;
4080 add_wait_queue(wqh, &event->wait);
4081}
4082
4083/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004084 * DO NOT USE IN NEW FILES.
4085 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004086 * Parse input and register new cgroup event handler.
4087 *
4088 * Input must be in format '<event_fd> <control_fd> <args>'.
4089 * Interpretation of args is defined by control file implementation.
4090 */
Tejun Heo451af502014-05-13 12:16:21 -04004091static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4092 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004093{
Tejun Heo451af502014-05-13 12:16:21 -04004094 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004095 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004096 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004097 struct cgroup_subsys_state *cfile_css;
4098 unsigned int efd, cfd;
4099 struct fd efile;
4100 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004101 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004102 char *endp;
4103 int ret;
4104
Tejun Heo451af502014-05-13 12:16:21 -04004105 buf = strstrip(buf);
4106
4107 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004108 if (*endp != ' ')
4109 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004110 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004111
Tejun Heo451af502014-05-13 12:16:21 -04004112 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004113 if ((*endp != ' ') && (*endp != '\0'))
4114 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004115 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004116
4117 event = kzalloc(sizeof(*event), GFP_KERNEL);
4118 if (!event)
4119 return -ENOMEM;
4120
Tejun Heo59b6f872013-11-22 18:20:43 -05004121 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004122 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004123 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4124 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4125 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004126
4127 efile = fdget(efd);
4128 if (!efile.file) {
4129 ret = -EBADF;
4130 goto out_kfree;
4131 }
4132
4133 event->eventfd = eventfd_ctx_fileget(efile.file);
4134 if (IS_ERR(event->eventfd)) {
4135 ret = PTR_ERR(event->eventfd);
4136 goto out_put_efile;
4137 }
4138
4139 cfile = fdget(cfd);
4140 if (!cfile.file) {
4141 ret = -EBADF;
4142 goto out_put_eventfd;
4143 }
4144
4145 /* the process need read permission on control file */
4146 /* AV: shouldn't we check that it's been opened for read instead? */
4147 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4148 if (ret < 0)
4149 goto out_put_cfile;
4150
Tejun Heo79bd9812013-11-22 18:20:42 -05004151 /*
Tejun Heofba94802013-11-22 18:20:43 -05004152 * Determine the event callbacks and set them in @event. This used
4153 * to be done via struct cftype but cgroup core no longer knows
4154 * about these events. The following is crude but the whole thing
4155 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004156 *
4157 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004158 */
Al Virob5830432014-10-31 01:22:04 -04004159 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004160
4161 if (!strcmp(name, "memory.usage_in_bytes")) {
4162 event->register_event = mem_cgroup_usage_register_event;
4163 event->unregister_event = mem_cgroup_usage_unregister_event;
4164 } else if (!strcmp(name, "memory.oom_control")) {
4165 event->register_event = mem_cgroup_oom_register_event;
4166 event->unregister_event = mem_cgroup_oom_unregister_event;
4167 } else if (!strcmp(name, "memory.pressure_level")) {
4168 event->register_event = vmpressure_register_event;
4169 event->unregister_event = vmpressure_unregister_event;
4170 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004171 event->register_event = memsw_cgroup_usage_register_event;
4172 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004173 } else {
4174 ret = -EINVAL;
4175 goto out_put_cfile;
4176 }
4177
4178 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004179 * Verify @cfile should belong to @css. Also, remaining events are
4180 * automatically removed on cgroup destruction but the removal is
4181 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004182 */
Al Virob5830432014-10-31 01:22:04 -04004183 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004184 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004185 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004186 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004187 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004188 if (cfile_css != css) {
4189 css_put(cfile_css);
4190 goto out_put_cfile;
4191 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004192
Tejun Heo451af502014-05-13 12:16:21 -04004193 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004194 if (ret)
4195 goto out_put_css;
4196
4197 efile.file->f_op->poll(efile.file, &event->pt);
4198
Tejun Heofba94802013-11-22 18:20:43 -05004199 spin_lock(&memcg->event_list_lock);
4200 list_add(&event->list, &memcg->event_list);
4201 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004202
4203 fdput(cfile);
4204 fdput(efile);
4205
Tejun Heo451af502014-05-13 12:16:21 -04004206 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004207
4208out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004209 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004210out_put_cfile:
4211 fdput(cfile);
4212out_put_eventfd:
4213 eventfd_ctx_put(event->eventfd);
4214out_put_efile:
4215 fdput(efile);
4216out_kfree:
4217 kfree(event);
4218
4219 return ret;
4220}
4221
Johannes Weiner241994e2015-02-11 15:26:06 -08004222static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004223 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004224 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004225 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004226 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004227 },
4228 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004229 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004230 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004231 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004232 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004233 },
4234 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004235 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004236 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004237 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004238 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004239 },
4240 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004241 .name = "soft_limit_in_bytes",
4242 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004243 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004244 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004245 },
4246 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004247 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004248 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004249 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004250 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004251 },
Balbir Singh8697d332008-02-07 00:13:59 -08004252 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004253 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004254 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004255 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004256 {
4257 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004258 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004259 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004260 {
4261 .name = "use_hierarchy",
4262 .write_u64 = mem_cgroup_hierarchy_write,
4263 .read_u64 = mem_cgroup_hierarchy_read,
4264 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004265 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004266 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004267 .write = memcg_write_event_control,
Tejun Heo79bd9812013-11-22 18:20:42 -05004268 .flags = CFTYPE_NO_PREFIX,
4269 .mode = S_IWUGO,
4270 },
4271 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004272 .name = "swappiness",
4273 .read_u64 = mem_cgroup_swappiness_read,
4274 .write_u64 = mem_cgroup_swappiness_write,
4275 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004276 {
4277 .name = "move_charge_at_immigrate",
4278 .read_u64 = mem_cgroup_move_charge_read,
4279 .write_u64 = mem_cgroup_move_charge_write,
4280 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004281 {
4282 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004283 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004284 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004285 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4286 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004287 {
4288 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004289 },
Ying Han406eb0c2011-05-26 16:25:37 -07004290#ifdef CONFIG_NUMA
4291 {
4292 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004293 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004294 },
4295#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004296#ifdef CONFIG_MEMCG_KMEM
4297 {
4298 .name = "kmem.limit_in_bytes",
4299 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004300 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004301 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004302 },
4303 {
4304 .name = "kmem.usage_in_bytes",
4305 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004306 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004307 },
4308 {
4309 .name = "kmem.failcnt",
4310 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004311 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004312 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004313 },
4314 {
4315 .name = "kmem.max_usage_in_bytes",
4316 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004317 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004318 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004319 },
Glauber Costa749c5412012-12-18 14:23:01 -08004320#ifdef CONFIG_SLABINFO
4321 {
4322 .name = "kmem.slabinfo",
Vladimir Davydovb0475012014-12-10 15:44:19 -08004323 .seq_start = slab_start,
4324 .seq_next = slab_next,
4325 .seq_stop = slab_stop,
4326 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004327 },
4328#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004329#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004330 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004331};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004332
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004333static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004334{
4335 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004336 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004337 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004338 /*
4339 * This routine is called against possible nodes.
4340 * But it's BUG to call kmalloc() against offline node.
4341 *
4342 * TODO: this routine can waste much memory for nodes which will
4343 * never be onlined. It's better to use memory hotplug callback
4344 * function.
4345 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004346 if (!node_state(node, N_NORMAL_MEMORY))
4347 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004348 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004349 if (!pn)
4350 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004351
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004352 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4353 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004354 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004355 mz->usage_in_excess = 0;
4356 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004357 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004358 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004359 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004360 return 0;
4361}
4362
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004363static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004364{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004365 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004366}
4367
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004368static struct mem_cgroup *mem_cgroup_alloc(void)
4369{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004370 struct mem_cgroup *memcg;
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004371 size_t size;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004372
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004373 size = sizeof(struct mem_cgroup);
4374 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004375
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004376 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004377 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004378 return NULL;
4379
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004380 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4381 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004382 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004383 spin_lock_init(&memcg->pcp_counter_lock);
4384 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004385
4386out_free:
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004387 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004388 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004389}
4390
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004391/*
Glauber Costac8b2a362012-12-18 14:22:13 -08004392 * At destroying mem_cgroup, references from swap_cgroup can remain.
4393 * (scanning all at force_empty is too costly...)
4394 *
4395 * Instead of clearing all references at force_empty, we remember
4396 * the number of reference from swap_cgroup and free mem_cgroup when
4397 * it goes down to 0.
4398 *
4399 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07004400 */
Glauber Costac8b2a362012-12-18 14:22:13 -08004401
4402static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004403{
Glauber Costac8b2a362012-12-18 14:22:13 -08004404 int node;
Hugh Dickins59927fb2012-03-15 15:17:07 -07004405
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004406 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08004407
4408 for_each_node(node)
4409 free_mem_cgroup_per_zone_info(memcg, node);
4410
4411 free_percpu(memcg->stat);
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004412 kfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004413}
Glauber Costa3afe36b2012-05-29 15:07:10 -07004414
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004415/*
4416 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4417 */
Glauber Costae1aab162011-12-11 21:47:03 +00004418struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004419{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004420 if (!memcg->memory.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004421 return NULL;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004422 return mem_cgroup_from_counter(memcg->memory.parent, memory);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004423}
Glauber Costae1aab162011-12-11 21:47:03 +00004424EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004425
Li Zefan0eb253e2009-01-15 13:51:25 -08004426static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04004427mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004428{
Glauber Costad142e3e2013-02-22 16:34:52 -08004429 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004430 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004431 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004432
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004433 memcg = mem_cgroup_alloc();
4434 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004435 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004436
Bob Liu3ed28fa2012-01-12 17:19:04 -08004437 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004438 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004439 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004440
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004441 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04004442 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08004443 root_mem_cgroup = memcg;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004444 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994e2015-02-11 15:26:06 -08004445 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004446 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004447 page_counter_init(&memcg->memsw, NULL);
4448 page_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004449 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004450
Glauber Costad142e3e2013-02-22 16:34:52 -08004451 memcg->last_scanned_node = MAX_NUMNODES;
4452 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08004453 memcg->move_charge_at_immigrate = 0;
4454 mutex_init(&memcg->thresholds_lock);
4455 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004456 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05004457 INIT_LIST_HEAD(&memcg->event_list);
4458 spin_lock_init(&memcg->event_list_lock);
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004459#ifdef CONFIG_MEMCG_KMEM
4460 memcg->kmemcg_id = -1;
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004461#endif
Glauber Costad142e3e2013-02-22 16:34:52 -08004462
4463 return &memcg->css;
4464
4465free_out:
4466 __mem_cgroup_free(memcg);
4467 return ERR_PTR(error);
4468}
4469
4470static int
Tejun Heoeb954192013-08-08 20:11:23 -04004471mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08004472{
Tejun Heoeb954192013-08-08 20:11:23 -04004473 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04004474 struct mem_cgroup *parent = mem_cgroup_from_css(css->parent);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004475 int ret;
Glauber Costad142e3e2013-02-22 16:34:52 -08004476
Tejun Heo15a4c832014-05-04 15:09:14 -04004477 if (css->id > MEM_CGROUP_ID_MAX)
Li Zefan4219b2d2013-09-23 16:56:29 +08004478 return -ENOSPC;
4479
Tejun Heo63876982013-08-08 20:11:23 -04004480 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08004481 return 0;
4482
Glauber Costa09998212013-02-22 16:34:55 -08004483 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08004484
4485 memcg->use_hierarchy = parent->use_hierarchy;
4486 memcg->oom_kill_disable = parent->oom_kill_disable;
4487 memcg->swappiness = mem_cgroup_swappiness(parent);
4488
4489 if (parent->use_hierarchy) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004490 page_counter_init(&memcg->memory, &parent->memory);
Johannes Weiner241994e2015-02-11 15:26:06 -08004491 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004492 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004493 page_counter_init(&memcg->memsw, &parent->memsw);
4494 page_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08004495
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004496 /*
Li Zefan8d76a972013-07-08 16:00:36 -07004497 * No need to take a reference to the parent because cgroup
4498 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004499 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08004500 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004501 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994e2015-02-11 15:26:06 -08004502 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004503 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004504 page_counter_init(&memcg->memsw, NULL);
4505 page_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004506 /*
4507 * Deeper hierachy with use_hierarchy == false doesn't make
4508 * much sense so let cgroup subsystem know about this
4509 * unfortunate state in our controller.
4510 */
Glauber Costad142e3e2013-02-22 16:34:52 -08004511 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05004512 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004513 }
Glauber Costa09998212013-02-22 16:34:55 -08004514 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004515
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004516 ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
4517 if (ret)
4518 return ret;
4519
4520 /*
4521 * Make sure the memcg is initialized: mem_cgroup_iter()
4522 * orders reading memcg->initialized against its callers
4523 * reading the memcg members.
4524 */
4525 smp_store_release(&memcg->initialized, 1);
4526
4527 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004528}
4529
Tejun Heoeb954192013-08-08 20:11:23 -04004530static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004531{
Tejun Heoeb954192013-08-08 20:11:23 -04004532 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004533 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05004534
4535 /*
4536 * Unregister events and notify userspace.
4537 * Notify userspace about cgroup removing only after rmdir of cgroup
4538 * directory to avoid race between userspace and kernelspace.
4539 */
Tejun Heofba94802013-11-22 18:20:43 -05004540 spin_lock(&memcg->event_list_lock);
4541 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004542 list_del_init(&event->list);
4543 schedule_work(&event->remove);
4544 }
Tejun Heofba94802013-11-22 18:20:43 -05004545 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004546
Michal Hocko33cb8762013-07-31 13:53:51 -07004547 vmpressure_cleanup(&memcg->vmpressure);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004548
4549 memcg_deactivate_kmem(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004550}
4551
Tejun Heoeb954192013-08-08 20:11:23 -04004552static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004553{
Tejun Heoeb954192013-08-08 20:11:23 -04004554 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004555
Li Zefan10d5ebf2013-07-08 16:00:33 -07004556 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07004557 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004558}
4559
Tejun Heo1ced9532014-07-08 18:02:57 -04004560/**
4561 * mem_cgroup_css_reset - reset the states of a mem_cgroup
4562 * @css: the target css
4563 *
4564 * Reset the states of the mem_cgroup associated with @css. This is
4565 * invoked when the userland requests disabling on the default hierarchy
4566 * but the memcg is pinned through dependency. The memcg should stop
4567 * applying policies and should revert to the vanilla state as it may be
4568 * made visible again.
4569 *
4570 * The current implementation only resets the essential configurations.
4571 * This needs to be expanded to cover all the visible parts.
4572 */
4573static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
4574{
4575 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4576
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004577 mem_cgroup_resize_limit(memcg, PAGE_COUNTER_MAX);
4578 mem_cgroup_resize_memsw_limit(memcg, PAGE_COUNTER_MAX);
4579 memcg_update_kmem_limit(memcg, PAGE_COUNTER_MAX);
Johannes Weiner241994e2015-02-11 15:26:06 -08004580 memcg->low = 0;
4581 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004582 memcg->soft_limit = PAGE_COUNTER_MAX;
Tejun Heo1ced9532014-07-08 18:02:57 -04004583}
4584
Daisuke Nishimura02491442010-03-10 15:22:17 -08004585#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004586/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004587static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004588{
Johannes Weiner05b84302014-08-06 16:05:59 -07004589 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07004590
4591 /* Try a single bulk charge without reclaim first */
Johannes Weiner00501b52014-08-08 14:19:20 -07004592 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07004593 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004594 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004595 return ret;
4596 }
Johannes Weiner692e7c42014-08-06 16:05:57 -07004597 if (ret == -EINTR) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004598 cancel_charge(root_mem_cgroup, count);
Johannes Weiner692e7c42014-08-06 16:05:57 -07004599 return ret;
4600 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004601
4602 /* Try charges one by one with reclaim */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004603 while (count--) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004604 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1);
Johannes Weiner9476db92014-08-06 16:05:55 -07004605 /*
4606 * In case of failure, any residual charges against
4607 * mc.to will be dropped by mem_cgroup_clear_mc()
Johannes Weiner692e7c42014-08-06 16:05:57 -07004608 * later on. However, cancel any charges that are
4609 * bypassed to root right away or they'll be lost.
Johannes Weiner9476db92014-08-06 16:05:55 -07004610 */
Johannes Weiner692e7c42014-08-06 16:05:57 -07004611 if (ret == -EINTR)
Johannes Weiner00501b52014-08-08 14:19:20 -07004612 cancel_charge(root_mem_cgroup, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004613 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004614 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004615 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07004616 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004617 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004618 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004619}
4620
4621/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004622 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004623 * @vma: the vma the pte to be checked belongs
4624 * @addr: the address corresponding to the pte to be checked
4625 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004626 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004627 *
4628 * Returns
4629 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4630 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4631 * move charge. if @target is not NULL, the page is stored in target->page
4632 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004633 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4634 * target for charge migration. if @target is not NULL, the entry is stored
4635 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004636 *
4637 * Called with pte lock held.
4638 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004639union mc_target {
4640 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004641 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004642};
4643
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004644enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004645 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004646 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004647 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004648};
4649
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004650static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4651 unsigned long addr, pte_t ptent)
4652{
4653 struct page *page = vm_normal_page(vma, addr, ptent);
4654
4655 if (!page || !page_mapped(page))
4656 return NULL;
4657 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004658 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004659 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004660 } else {
4661 if (!(mc.flags & MOVE_FILE))
4662 return NULL;
4663 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004664 if (!get_page_unless_zero(page))
4665 return NULL;
4666
4667 return page;
4668}
4669
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004670#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004671static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4672 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4673{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004674 struct page *page = NULL;
4675 swp_entry_t ent = pte_to_swp_entry(ptent);
4676
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004677 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004678 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004679 /*
4680 * Because lookup_swap_cache() updates some statistics counter,
4681 * we call find_get_page() with swapper_space directly.
4682 */
Shaohua Li33806f02013-02-22 16:34:37 -08004683 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004684 if (do_swap_account)
4685 entry->val = ent.val;
4686
4687 return page;
4688}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004689#else
4690static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4691 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4692{
4693 return NULL;
4694}
4695#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004696
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004697static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4698 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4699{
4700 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004701 struct address_space *mapping;
4702 pgoff_t pgoff;
4703
4704 if (!vma->vm_file) /* anonymous vma */
4705 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004706 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004707 return NULL;
4708
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004709 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004710 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004711
4712 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004713#ifdef CONFIG_SWAP
4714 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07004715 if (shmem_mapping(mapping)) {
4716 page = find_get_entry(mapping, pgoff);
4717 if (radix_tree_exceptional_entry(page)) {
4718 swp_entry_t swp = radix_to_swp_entry(page);
4719 if (do_swap_account)
4720 *entry = swp;
4721 page = find_get_page(swap_address_space(swp), swp.val);
4722 }
4723 } else
4724 page = find_get_page(mapping, pgoff);
4725#else
4726 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004727#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004728 return page;
4729}
4730
Chen Gangb1b0dea2015-04-14 15:47:35 -07004731/**
4732 * mem_cgroup_move_account - move account of the page
4733 * @page: the page
4734 * @nr_pages: number of regular pages (>1 for huge pages)
4735 * @from: mem_cgroup which the page is moved from.
4736 * @to: mem_cgroup which the page is moved to. @from != @to.
4737 *
4738 * The caller must confirm following.
4739 * - page is not on LRU (isolate_page() is useful.)
4740 * - compound_lock is held when nr_pages > 1
4741 *
4742 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
4743 * from old cgroup.
4744 */
4745static int mem_cgroup_move_account(struct page *page,
4746 unsigned int nr_pages,
4747 struct mem_cgroup *from,
4748 struct mem_cgroup *to)
4749{
4750 unsigned long flags;
4751 int ret;
Greg Thelenc4843a72015-05-22 17:13:16 -04004752 bool anon;
Chen Gangb1b0dea2015-04-14 15:47:35 -07004753
4754 VM_BUG_ON(from == to);
4755 VM_BUG_ON_PAGE(PageLRU(page), page);
4756 /*
4757 * The page is isolated from LRU. So, collapse function
4758 * will not handle this page. But page splitting can happen.
4759 * Do this check under compound_page_lock(). The caller should
4760 * hold it.
4761 */
4762 ret = -EBUSY;
4763 if (nr_pages > 1 && !PageTransHuge(page))
4764 goto out;
4765
4766 /*
4767 * Prevent mem_cgroup_migrate() from looking at page->mem_cgroup
4768 * of its source page while we change it: page migration takes
4769 * both pages off the LRU, but page cache replacement doesn't.
4770 */
4771 if (!trylock_page(page))
4772 goto out;
4773
4774 ret = -EINVAL;
4775 if (page->mem_cgroup != from)
4776 goto out_unlock;
4777
Greg Thelenc4843a72015-05-22 17:13:16 -04004778 anon = PageAnon(page);
4779
Chen Gangb1b0dea2015-04-14 15:47:35 -07004780 spin_lock_irqsave(&from->move_lock, flags);
4781
Greg Thelenc4843a72015-05-22 17:13:16 -04004782 if (!anon && page_mapped(page)) {
Chen Gangb1b0dea2015-04-14 15:47:35 -07004783 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
4784 nr_pages);
4785 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
4786 nr_pages);
4787 }
4788
Greg Thelenc4843a72015-05-22 17:13:16 -04004789 /*
4790 * move_lock grabbed above and caller set from->moving_account, so
4791 * mem_cgroup_update_page_stat() will serialize updates to PageDirty.
4792 * So mapping should be stable for dirty pages.
4793 */
4794 if (!anon && PageDirty(page)) {
4795 struct address_space *mapping = page_mapping(page);
4796
4797 if (mapping_cap_account_dirty(mapping)) {
4798 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_DIRTY],
4799 nr_pages);
4800 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_DIRTY],
4801 nr_pages);
4802 }
4803 }
4804
Chen Gangb1b0dea2015-04-14 15:47:35 -07004805 if (PageWriteback(page)) {
4806 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK],
4807 nr_pages);
4808 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK],
4809 nr_pages);
4810 }
4811
4812 /*
4813 * It is safe to change page->mem_cgroup here because the page
4814 * is referenced, charged, and isolated - we can't race with
4815 * uncharging, charging, migration, or LRU putback.
4816 */
4817
4818 /* caller should have done css_get */
4819 page->mem_cgroup = to;
4820 spin_unlock_irqrestore(&from->move_lock, flags);
4821
4822 ret = 0;
4823
4824 local_irq_disable();
4825 mem_cgroup_charge_statistics(to, page, nr_pages);
4826 memcg_check_events(to, page);
4827 mem_cgroup_charge_statistics(from, page, -nr_pages);
4828 memcg_check_events(from, page);
4829 local_irq_enable();
4830out_unlock:
4831 unlock_page(page);
4832out:
4833 return ret;
4834}
4835
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004836static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004837 unsigned long addr, pte_t ptent, union mc_target *target)
4838{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004839 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004840 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004841 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004842
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004843 if (pte_present(ptent))
4844 page = mc_handle_present_pte(vma, addr, ptent);
4845 else if (is_swap_pte(ptent))
4846 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004847 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004848 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004849
4850 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004851 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004852 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004853 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004854 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08004855 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004856 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08004857 */
Johannes Weiner1306a852014-12-10 15:44:52 -08004858 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004859 ret = MC_TARGET_PAGE;
4860 if (target)
4861 target->page = page;
4862 }
4863 if (!ret || !target)
4864 put_page(page);
4865 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004866 /* There is a swap entry and a page doesn't exist or isn't charged */
4867 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08004868 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004869 ret = MC_TARGET_SWAP;
4870 if (target)
4871 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004872 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004873 return ret;
4874}
4875
Naoya Horiguchi12724852012-03-21 16:34:28 -07004876#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4877/*
4878 * We don't consider swapping or file mapped pages because THP does not
4879 * support them for now.
4880 * Caller should make sure that pmd_trans_huge(pmd) is true.
4881 */
4882static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4883 unsigned long addr, pmd_t pmd, union mc_target *target)
4884{
4885 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07004886 enum mc_target_type ret = MC_TARGET_NONE;
4887
4888 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08004889 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004890 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07004891 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08004892 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07004893 ret = MC_TARGET_PAGE;
4894 if (target) {
4895 get_page(page);
4896 target->page = page;
4897 }
4898 }
4899 return ret;
4900}
4901#else
4902static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4903 unsigned long addr, pmd_t pmd, union mc_target *target)
4904{
4905 return MC_TARGET_NONE;
4906}
4907#endif
4908
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004909static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4910 unsigned long addr, unsigned long end,
4911 struct mm_walk *walk)
4912{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004913 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004914 pte_t *pte;
4915 spinlock_t *ptl;
4916
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08004917 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07004918 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
4919 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08004920 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07004921 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07004922 }
Dave Hansen03319322011-03-22 16:32:56 -07004923
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07004924 if (pmd_trans_unstable(pmd))
4925 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004926 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4927 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004928 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004929 mc.precharge++; /* increment precharge temporarily */
4930 pte_unmap_unlock(pte - 1, ptl);
4931 cond_resched();
4932
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004933 return 0;
4934}
4935
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004936static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4937{
4938 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004939
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004940 struct mm_walk mem_cgroup_count_precharge_walk = {
4941 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4942 .mm = mm,
4943 };
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004944 down_read(&mm->mmap_sem);
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08004945 walk_page_range(0, ~0UL, &mem_cgroup_count_precharge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004946 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004947
4948 precharge = mc.precharge;
4949 mc.precharge = 0;
4950
4951 return precharge;
4952}
4953
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004954static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4955{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004956 unsigned long precharge = mem_cgroup_count_precharge(mm);
4957
4958 VM_BUG_ON(mc.moving_task);
4959 mc.moving_task = current;
4960 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004961}
4962
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08004963/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
4964static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004965{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07004966 struct mem_cgroup *from = mc.from;
4967 struct mem_cgroup *to = mc.to;
4968
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004969 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004970 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004971 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004972 mc.precharge = 0;
4973 }
4974 /*
4975 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4976 * we must uncharge here.
4977 */
4978 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004979 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004980 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004981 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004982 /* we must fixup refcnts and charges */
4983 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004984 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04004985 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004986 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004987
Johannes Weiner05b84302014-08-06 16:05:59 -07004988 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004989 * we charged both to->memory and to->memsw, so we
4990 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07004991 */
Johannes Weinerce00a962014-09-05 08:43:57 -04004992 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004993 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004994
Johannes Weinere8ea14c2014-12-10 15:42:42 -08004995 css_put_many(&mc.from->css, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004996
Li Zefan40503772013-07-08 16:00:34 -07004997 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004998 mc.moved_swap = 0;
4999 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005000 memcg_oom_recover(from);
5001 memcg_oom_recover(to);
5002 wake_up_all(&mc.waitq);
5003}
5004
5005static void mem_cgroup_clear_mc(void)
5006{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005007 /*
5008 * we must clear moving_task before waking up waiters at the end of
5009 * task migration.
5010 */
5011 mc.moving_task = NULL;
5012 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005013 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005014 mc.from = NULL;
5015 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005016 spin_unlock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005017}
5018
Tejun Heoeb954192013-08-08 20:11:23 -04005019static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005020 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005021{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005022 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005023 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04005024 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005025 unsigned long move_flags;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005026
Glauber Costaee5e8472013-02-22 16:34:50 -08005027 /*
5028 * We are now commited to this value whatever it is. Changes in this
5029 * tunable will only affect upcoming migrations, not the current one.
5030 * So we need to save it, and keep it going.
5031 */
Jason Low4db0c3c2015-04-15 16:14:08 -07005032 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005033 if (move_flags) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005034 struct mm_struct *mm;
5035 struct mem_cgroup *from = mem_cgroup_from_task(p);
5036
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005037 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005038
5039 mm = get_task_mm(p);
5040 if (!mm)
5041 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005042 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005043 if (mm->owner == p) {
5044 VM_BUG_ON(mc.from);
5045 VM_BUG_ON(mc.to);
5046 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005047 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005048 VM_BUG_ON(mc.moved_swap);
Johannes Weiner247b1442014-12-10 15:44:11 -08005049
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005050 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005051 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005052 mc.to = memcg;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005053 mc.flags = move_flags;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005054 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005055 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005056
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005057 ret = mem_cgroup_precharge_mc(mm);
5058 if (ret)
5059 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005060 }
5061 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005062 }
5063 return ret;
5064}
5065
Tejun Heoeb954192013-08-08 20:11:23 -04005066static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005067 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005068{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005069 if (mc.to)
5070 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005071}
5072
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005073static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5074 unsigned long addr, unsigned long end,
5075 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005076{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005077 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005078 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005079 pte_t *pte;
5080 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005081 enum mc_target_type target_type;
5082 union mc_target target;
5083 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005084
Naoya Horiguchi12724852012-03-21 16:34:28 -07005085 /*
5086 * We don't take compound_lock() here but no race with splitting thp
5087 * happens because:
5088 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5089 * under splitting, which means there's no concurrent thp split,
5090 * - if another thread runs into split_huge_page() just after we
5091 * entered this if-block, the thread must wait for page table lock
5092 * to be unlocked in __split_huge_page_splitting(), where the main
5093 * part of thp split is not executed yet.
5094 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005095 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005096 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005097 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005098 return 0;
5099 }
5100 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5101 if (target_type == MC_TARGET_PAGE) {
5102 page = target.page;
5103 if (!isolate_lru_page(page)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005104 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
Johannes Weiner1306a852014-12-10 15:44:52 -08005105 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005106 mc.precharge -= HPAGE_PMD_NR;
5107 mc.moved_charge += HPAGE_PMD_NR;
5108 }
5109 putback_lru_page(page);
5110 }
5111 put_page(page);
5112 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005113 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005114 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005115 }
5116
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005117 if (pmd_trans_unstable(pmd))
5118 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005119retry:
5120 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5121 for (; addr != end; addr += PAGE_SIZE) {
5122 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005123 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005124
5125 if (!mc.precharge)
5126 break;
5127
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005128 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005129 case MC_TARGET_PAGE:
5130 page = target.page;
5131 if (isolate_lru_page(page))
5132 goto put;
Johannes Weiner1306a852014-12-10 15:44:52 -08005133 if (!mem_cgroup_move_account(page, 1, mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005134 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005135 /* we uncharge from mc.from later. */
5136 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005137 }
5138 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005139put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005140 put_page(page);
5141 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005142 case MC_TARGET_SWAP:
5143 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005144 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005145 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005146 /* we fixup refcnts and charges later. */
5147 mc.moved_swap++;
5148 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005149 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005150 default:
5151 break;
5152 }
5153 }
5154 pte_unmap_unlock(pte - 1, ptl);
5155 cond_resched();
5156
5157 if (addr != end) {
5158 /*
5159 * We have consumed all precharges we got in can_attach().
5160 * We try charge one by one, but don't do any additional
5161 * charges to mc.to if we have failed in charge once in attach()
5162 * phase.
5163 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005164 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005165 if (!ret)
5166 goto retry;
5167 }
5168
5169 return ret;
5170}
5171
5172static void mem_cgroup_move_charge(struct mm_struct *mm)
5173{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005174 struct mm_walk mem_cgroup_move_charge_walk = {
5175 .pmd_entry = mem_cgroup_move_charge_pte_range,
5176 .mm = mm,
5177 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005178
5179 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08005180 /*
5181 * Signal mem_cgroup_begin_page_stat() to take the memcg's
5182 * move_lock while we're moving its pages to another memcg.
5183 * Then wait for already started RCU-only updates to finish.
5184 */
5185 atomic_inc(&mc.from->moving_account);
5186 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005187retry:
5188 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5189 /*
5190 * Someone who are holding the mmap_sem might be waiting in
5191 * waitq. So we cancel all extra charges, wake up all waiters,
5192 * and retry. Because we cancel precharges, we might not be able
5193 * to move enough charges, but moving charge is a best-effort
5194 * feature anyway, so it wouldn't be a big problem.
5195 */
5196 __mem_cgroup_clear_mc();
5197 cond_resched();
5198 goto retry;
5199 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005200 /*
5201 * When we have consumed all precharges and failed in doing
5202 * additional charge, the page walk just aborts.
5203 */
5204 walk_page_range(0, ~0UL, &mem_cgroup_move_charge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005205 up_read(&mm->mmap_sem);
Johannes Weiner312722c2014-12-10 15:44:25 -08005206 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005207}
5208
Tejun Heoeb954192013-08-08 20:11:23 -04005209static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005210 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005211{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005212 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005213 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005214
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005215 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005216 if (mc.to)
5217 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005218 mmput(mm);
5219 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005220 if (mc.to)
5221 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005222}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005223#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04005224static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005225 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005226{
5227 return 0;
5228}
Tejun Heoeb954192013-08-08 20:11:23 -04005229static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005230 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005231{
5232}
Tejun Heoeb954192013-08-08 20:11:23 -04005233static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005234 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005235{
5236}
5237#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005238
Tejun Heof00baae2013-04-15 13:41:15 -07005239/*
5240 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04005241 * to verify whether we're attached to the default hierarchy on each mount
5242 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07005243 */
Tejun Heoeb954192013-08-08 20:11:23 -04005244static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07005245{
5246 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04005247 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07005248 * guarantees that @root doesn't have any children, so turning it
5249 * on for the root memcg is enough.
5250 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04005251 if (cgroup_on_dfl(root_css->cgroup))
Vladimir Davydov7feee592015-03-12 16:26:19 -07005252 root_mem_cgroup->use_hierarchy = true;
5253 else
5254 root_mem_cgroup->use_hierarchy = false;
Tejun Heof00baae2013-04-15 13:41:15 -07005255}
5256
Johannes Weiner241994e2015-02-11 15:26:06 -08005257static u64 memory_current_read(struct cgroup_subsys_state *css,
5258 struct cftype *cft)
5259{
5260 return mem_cgroup_usage(mem_cgroup_from_css(css), false);
5261}
5262
5263static int memory_low_show(struct seq_file *m, void *v)
5264{
5265 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005266 unsigned long low = READ_ONCE(memcg->low);
Johannes Weiner241994e2015-02-11 15:26:06 -08005267
5268 if (low == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005269 seq_puts(m, "max\n");
Johannes Weiner241994e2015-02-11 15:26:06 -08005270 else
5271 seq_printf(m, "%llu\n", (u64)low * PAGE_SIZE);
5272
5273 return 0;
5274}
5275
5276static ssize_t memory_low_write(struct kernfs_open_file *of,
5277 char *buf, size_t nbytes, loff_t off)
5278{
5279 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5280 unsigned long low;
5281 int err;
5282
5283 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005284 err = page_counter_memparse(buf, "max", &low);
Johannes Weiner241994e2015-02-11 15:26:06 -08005285 if (err)
5286 return err;
5287
5288 memcg->low = low;
5289
5290 return nbytes;
5291}
5292
5293static int memory_high_show(struct seq_file *m, void *v)
5294{
5295 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005296 unsigned long high = READ_ONCE(memcg->high);
Johannes Weiner241994e2015-02-11 15:26:06 -08005297
5298 if (high == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005299 seq_puts(m, "max\n");
Johannes Weiner241994e2015-02-11 15:26:06 -08005300 else
5301 seq_printf(m, "%llu\n", (u64)high * PAGE_SIZE);
5302
5303 return 0;
5304}
5305
5306static ssize_t memory_high_write(struct kernfs_open_file *of,
5307 char *buf, size_t nbytes, loff_t off)
5308{
5309 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5310 unsigned long high;
5311 int err;
5312
5313 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005314 err = page_counter_memparse(buf, "max", &high);
Johannes Weiner241994e2015-02-11 15:26:06 -08005315 if (err)
5316 return err;
5317
5318 memcg->high = high;
5319
5320 return nbytes;
5321}
5322
5323static int memory_max_show(struct seq_file *m, void *v)
5324{
5325 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005326 unsigned long max = READ_ONCE(memcg->memory.limit);
Johannes Weiner241994e2015-02-11 15:26:06 -08005327
5328 if (max == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005329 seq_puts(m, "max\n");
Johannes Weiner241994e2015-02-11 15:26:06 -08005330 else
5331 seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE);
5332
5333 return 0;
5334}
5335
5336static ssize_t memory_max_write(struct kernfs_open_file *of,
5337 char *buf, size_t nbytes, loff_t off)
5338{
5339 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5340 unsigned long max;
5341 int err;
5342
5343 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005344 err = page_counter_memparse(buf, "max", &max);
Johannes Weiner241994e2015-02-11 15:26:06 -08005345 if (err)
5346 return err;
5347
5348 err = mem_cgroup_resize_limit(memcg, max);
5349 if (err)
5350 return err;
5351
5352 return nbytes;
5353}
5354
5355static int memory_events_show(struct seq_file *m, void *v)
5356{
5357 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5358
5359 seq_printf(m, "low %lu\n", mem_cgroup_read_events(memcg, MEMCG_LOW));
5360 seq_printf(m, "high %lu\n", mem_cgroup_read_events(memcg, MEMCG_HIGH));
5361 seq_printf(m, "max %lu\n", mem_cgroup_read_events(memcg, MEMCG_MAX));
5362 seq_printf(m, "oom %lu\n", mem_cgroup_read_events(memcg, MEMCG_OOM));
5363
5364 return 0;
5365}
5366
5367static struct cftype memory_files[] = {
5368 {
5369 .name = "current",
5370 .read_u64 = memory_current_read,
5371 },
5372 {
5373 .name = "low",
5374 .flags = CFTYPE_NOT_ON_ROOT,
5375 .seq_show = memory_low_show,
5376 .write = memory_low_write,
5377 },
5378 {
5379 .name = "high",
5380 .flags = CFTYPE_NOT_ON_ROOT,
5381 .seq_show = memory_high_show,
5382 .write = memory_high_write,
5383 },
5384 {
5385 .name = "max",
5386 .flags = CFTYPE_NOT_ON_ROOT,
5387 .seq_show = memory_max_show,
5388 .write = memory_max_write,
5389 },
5390 {
5391 .name = "events",
5392 .flags = CFTYPE_NOT_ON_ROOT,
5393 .seq_show = memory_events_show,
5394 },
5395 { } /* terminate */
5396};
5397
Tejun Heo073219e2014-02-08 10:36:58 -05005398struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005399 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08005400 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08005401 .css_offline = mem_cgroup_css_offline,
5402 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04005403 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005404 .can_attach = mem_cgroup_can_attach,
5405 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005406 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07005407 .bind = mem_cgroup_bind,
Johannes Weiner241994e2015-02-11 15:26:06 -08005408 .dfl_cftypes = memory_files,
5409 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005410 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005411};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005412
Johannes Weiner241994e2015-02-11 15:26:06 -08005413/**
5414 * mem_cgroup_events - count memory events against a cgroup
5415 * @memcg: the memory cgroup
5416 * @idx: the event index
5417 * @nr: the number of events to account for
5418 */
5419void mem_cgroup_events(struct mem_cgroup *memcg,
5420 enum mem_cgroup_events_index idx,
5421 unsigned int nr)
5422{
5423 this_cpu_add(memcg->stat->events[idx], nr);
5424}
5425
5426/**
5427 * mem_cgroup_low - check if memory consumption is below the normal range
5428 * @root: the highest ancestor to consider
5429 * @memcg: the memory cgroup to check
5430 *
5431 * Returns %true if memory consumption of @memcg, and that of all
5432 * configurable ancestors up to @root, is below the normal range.
5433 */
5434bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg)
5435{
5436 if (mem_cgroup_disabled())
5437 return false;
5438
5439 /*
5440 * The toplevel group doesn't have a configurable range, so
5441 * it's never low when looked at directly, and it is not
5442 * considered an ancestor when assessing the hierarchy.
5443 */
5444
5445 if (memcg == root_mem_cgroup)
5446 return false;
5447
Michal Hocko4e54ded2015-02-27 15:51:46 -08005448 if (page_counter_read(&memcg->memory) >= memcg->low)
Johannes Weiner241994e2015-02-11 15:26:06 -08005449 return false;
5450
5451 while (memcg != root) {
5452 memcg = parent_mem_cgroup(memcg);
5453
5454 if (memcg == root_mem_cgroup)
5455 break;
5456
Michal Hocko4e54ded2015-02-27 15:51:46 -08005457 if (page_counter_read(&memcg->memory) >= memcg->low)
Johannes Weiner241994e2015-02-11 15:26:06 -08005458 return false;
5459 }
5460 return true;
5461}
5462
Johannes Weiner00501b52014-08-08 14:19:20 -07005463/**
5464 * mem_cgroup_try_charge - try charging a page
5465 * @page: page to charge
5466 * @mm: mm context of the victim
5467 * @gfp_mask: reclaim mode
5468 * @memcgp: charged memcg return
5469 *
5470 * Try to charge @page to the memcg that @mm belongs to, reclaiming
5471 * pages according to @gfp_mask if necessary.
5472 *
5473 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
5474 * Otherwise, an error code is returned.
5475 *
5476 * After page->mapping has been set up, the caller must finalize the
5477 * charge with mem_cgroup_commit_charge(). Or abort the transaction
5478 * with mem_cgroup_cancel_charge() in case page instantiation fails.
5479 */
5480int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
5481 gfp_t gfp_mask, struct mem_cgroup **memcgp)
5482{
5483 struct mem_cgroup *memcg = NULL;
5484 unsigned int nr_pages = 1;
5485 int ret = 0;
5486
5487 if (mem_cgroup_disabled())
5488 goto out;
5489
5490 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005491 /*
5492 * Every swap fault against a single page tries to charge the
5493 * page, bail as early as possible. shmem_unuse() encounters
5494 * already charged pages, too. The USED bit is protected by
5495 * the page lock, which serializes swap cache removal, which
5496 * in turn serializes uncharging.
5497 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005498 if (page->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07005499 goto out;
5500 }
5501
5502 if (PageTransHuge(page)) {
5503 nr_pages <<= compound_order(page);
5504 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5505 }
5506
5507 if (do_swap_account && PageSwapCache(page))
5508 memcg = try_get_mem_cgroup_from_page(page);
5509 if (!memcg)
5510 memcg = get_mem_cgroup_from_mm(mm);
5511
5512 ret = try_charge(memcg, gfp_mask, nr_pages);
5513
5514 css_put(&memcg->css);
5515
5516 if (ret == -EINTR) {
5517 memcg = root_mem_cgroup;
5518 ret = 0;
5519 }
5520out:
5521 *memcgp = memcg;
5522 return ret;
5523}
5524
5525/**
5526 * mem_cgroup_commit_charge - commit a page charge
5527 * @page: page to charge
5528 * @memcg: memcg to charge the page to
5529 * @lrucare: page might be on LRU already
5530 *
5531 * Finalize a charge transaction started by mem_cgroup_try_charge(),
5532 * after page->mapping has been set up. This must happen atomically
5533 * as part of the page instantiation, i.e. under the page table lock
5534 * for anonymous pages, under the page lock for page and swap cache.
5535 *
5536 * In addition, the page must not be on the LRU during the commit, to
5537 * prevent racing with task migration. If it might be, use @lrucare.
5538 *
5539 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
5540 */
5541void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
5542 bool lrucare)
5543{
5544 unsigned int nr_pages = 1;
5545
5546 VM_BUG_ON_PAGE(!page->mapping, page);
5547 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
5548
5549 if (mem_cgroup_disabled())
5550 return;
5551 /*
5552 * Swap faults will attempt to charge the same page multiple
5553 * times. But reuse_swap_page() might have removed the page
5554 * from swapcache already, so we can't check PageSwapCache().
5555 */
5556 if (!memcg)
5557 return;
5558
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005559 commit_charge(page, memcg, lrucare);
5560
Johannes Weiner00501b52014-08-08 14:19:20 -07005561 if (PageTransHuge(page)) {
5562 nr_pages <<= compound_order(page);
5563 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5564 }
5565
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005566 local_irq_disable();
5567 mem_cgroup_charge_statistics(memcg, page, nr_pages);
5568 memcg_check_events(memcg, page);
5569 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07005570
5571 if (do_swap_account && PageSwapCache(page)) {
5572 swp_entry_t entry = { .val = page_private(page) };
5573 /*
5574 * The swap entry might not get freed for a long time,
5575 * let's not wait for it. The page already received a
5576 * memory+swap charge, drop the swap entry duplicate.
5577 */
5578 mem_cgroup_uncharge_swap(entry);
5579 }
5580}
5581
5582/**
5583 * mem_cgroup_cancel_charge - cancel a page charge
5584 * @page: page to charge
5585 * @memcg: memcg to charge the page to
5586 *
5587 * Cancel a charge transaction started by mem_cgroup_try_charge().
5588 */
5589void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg)
5590{
5591 unsigned int nr_pages = 1;
5592
5593 if (mem_cgroup_disabled())
5594 return;
5595 /*
5596 * Swap faults will attempt to charge the same page multiple
5597 * times. But reuse_swap_page() might have removed the page
5598 * from swapcache already, so we can't check PageSwapCache().
5599 */
5600 if (!memcg)
5601 return;
5602
5603 if (PageTransHuge(page)) {
5604 nr_pages <<= compound_order(page);
5605 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5606 }
5607
5608 cancel_charge(memcg, nr_pages);
5609}
5610
Johannes Weiner747db952014-08-08 14:19:24 -07005611static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
Johannes Weiner747db952014-08-08 14:19:24 -07005612 unsigned long nr_anon, unsigned long nr_file,
5613 unsigned long nr_huge, struct page *dummy_page)
5614{
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005615 unsigned long nr_pages = nr_anon + nr_file;
Johannes Weiner747db952014-08-08 14:19:24 -07005616 unsigned long flags;
5617
Johannes Weinerce00a962014-09-05 08:43:57 -04005618 if (!mem_cgroup_is_root(memcg)) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005619 page_counter_uncharge(&memcg->memory, nr_pages);
5620 if (do_swap_account)
5621 page_counter_uncharge(&memcg->memsw, nr_pages);
Johannes Weinerce00a962014-09-05 08:43:57 -04005622 memcg_oom_recover(memcg);
5623 }
Johannes Weiner747db952014-08-08 14:19:24 -07005624
5625 local_irq_save(flags);
5626 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
5627 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
5628 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
5629 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005630 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005631 memcg_check_events(memcg, dummy_page);
5632 local_irq_restore(flags);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08005633
5634 if (!mem_cgroup_is_root(memcg))
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005635 css_put_many(&memcg->css, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005636}
5637
5638static void uncharge_list(struct list_head *page_list)
5639{
5640 struct mem_cgroup *memcg = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005641 unsigned long nr_anon = 0;
5642 unsigned long nr_file = 0;
5643 unsigned long nr_huge = 0;
5644 unsigned long pgpgout = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005645 struct list_head *next;
5646 struct page *page;
5647
5648 next = page_list->next;
5649 do {
5650 unsigned int nr_pages = 1;
Johannes Weiner747db952014-08-08 14:19:24 -07005651
5652 page = list_entry(next, struct page, lru);
5653 next = page->lru.next;
5654
5655 VM_BUG_ON_PAGE(PageLRU(page), page);
5656 VM_BUG_ON_PAGE(page_count(page), page);
5657
Johannes Weiner1306a852014-12-10 15:44:52 -08005658 if (!page->mem_cgroup)
Johannes Weiner747db952014-08-08 14:19:24 -07005659 continue;
5660
5661 /*
5662 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08005663 * page->mem_cgroup at this point, we have fully
Johannes Weiner29833312014-12-10 15:44:02 -08005664 * exclusive access to the page.
Johannes Weiner747db952014-08-08 14:19:24 -07005665 */
5666
Johannes Weiner1306a852014-12-10 15:44:52 -08005667 if (memcg != page->mem_cgroup) {
Johannes Weiner747db952014-08-08 14:19:24 -07005668 if (memcg) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005669 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5670 nr_huge, page);
5671 pgpgout = nr_anon = nr_file = nr_huge = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005672 }
Johannes Weiner1306a852014-12-10 15:44:52 -08005673 memcg = page->mem_cgroup;
Johannes Weiner747db952014-08-08 14:19:24 -07005674 }
5675
5676 if (PageTransHuge(page)) {
5677 nr_pages <<= compound_order(page);
5678 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5679 nr_huge += nr_pages;
5680 }
5681
5682 if (PageAnon(page))
5683 nr_anon += nr_pages;
5684 else
5685 nr_file += nr_pages;
5686
Johannes Weiner1306a852014-12-10 15:44:52 -08005687 page->mem_cgroup = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005688
5689 pgpgout++;
5690 } while (next != page_list);
5691
5692 if (memcg)
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005693 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5694 nr_huge, page);
Johannes Weiner747db952014-08-08 14:19:24 -07005695}
5696
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005697/**
5698 * mem_cgroup_uncharge - uncharge a page
5699 * @page: page to uncharge
5700 *
5701 * Uncharge a page previously charged with mem_cgroup_try_charge() and
5702 * mem_cgroup_commit_charge().
5703 */
5704void mem_cgroup_uncharge(struct page *page)
5705{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005706 if (mem_cgroup_disabled())
5707 return;
5708
Johannes Weiner747db952014-08-08 14:19:24 -07005709 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08005710 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005711 return;
5712
Johannes Weiner747db952014-08-08 14:19:24 -07005713 INIT_LIST_HEAD(&page->lru);
5714 uncharge_list(&page->lru);
5715}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005716
Johannes Weiner747db952014-08-08 14:19:24 -07005717/**
5718 * mem_cgroup_uncharge_list - uncharge a list of page
5719 * @page_list: list of pages to uncharge
5720 *
5721 * Uncharge a list of pages previously charged with
5722 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
5723 */
5724void mem_cgroup_uncharge_list(struct list_head *page_list)
5725{
5726 if (mem_cgroup_disabled())
5727 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005728
Johannes Weiner747db952014-08-08 14:19:24 -07005729 if (!list_empty(page_list))
5730 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005731}
5732
5733/**
5734 * mem_cgroup_migrate - migrate a charge to another page
5735 * @oldpage: currently charged page
5736 * @newpage: page to transfer the charge to
Michal Hockof5e03a42015-02-05 12:25:14 -08005737 * @lrucare: either or both pages might be on the LRU already
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005738 *
5739 * Migrate the charge from @oldpage to @newpage.
5740 *
5741 * Both pages must be locked, @newpage->mapping must be set up.
5742 */
5743void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
5744 bool lrucare)
5745{
Johannes Weiner29833312014-12-10 15:44:02 -08005746 struct mem_cgroup *memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005747 int isolated;
5748
5749 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
5750 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
5751 VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage);
5752 VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage);
5753 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005754 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
5755 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005756
5757 if (mem_cgroup_disabled())
5758 return;
5759
5760 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08005761 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005762 return;
5763
Johannes Weiner7d5e3242014-12-10 15:43:46 -08005764 /*
5765 * Swapcache readahead pages can get migrated before being
5766 * charged, and migration from compaction can happen to an
5767 * uncharged page when the PFN walker finds a page that
5768 * reclaim just put back on the LRU but has not released yet.
5769 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005770 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08005771 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005772 return;
5773
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005774 if (lrucare)
5775 lock_page_lru(oldpage, &isolated);
5776
Johannes Weiner1306a852014-12-10 15:44:52 -08005777 oldpage->mem_cgroup = NULL;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005778
5779 if (lrucare)
5780 unlock_page_lru(oldpage, isolated);
5781
Johannes Weiner29833312014-12-10 15:44:02 -08005782 commit_charge(newpage, memcg, lrucare);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005783}
5784
Michal Hocko2d110852013-02-22 16:34:43 -08005785/*
Michal Hocko10813122013-02-22 16:35:41 -08005786 * subsys_initcall() for memory controller.
5787 *
5788 * Some parts like hotcpu_notifier() have to be initialized from this context
5789 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
5790 * everything that doesn't depend on a specific mem_cgroup structure should
5791 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08005792 */
5793static int __init mem_cgroup_init(void)
5794{
Johannes Weiner95a045f2015-02-11 15:26:33 -08005795 int cpu, node;
5796
Michal Hocko2d110852013-02-22 16:34:43 -08005797 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Johannes Weiner95a045f2015-02-11 15:26:33 -08005798
5799 for_each_possible_cpu(cpu)
5800 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
5801 drain_local_stock);
5802
5803 for_each_node(node) {
5804 struct mem_cgroup_tree_per_node *rtpn;
5805 int zone;
5806
5807 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
5808 node_online(node) ? node : NUMA_NO_NODE);
5809
5810 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5811 struct mem_cgroup_tree_per_zone *rtpz;
5812
5813 rtpz = &rtpn->rb_tree_per_zone[zone];
5814 rtpz->rb_root = RB_ROOT;
5815 spin_lock_init(&rtpz->lock);
5816 }
5817 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5818 }
5819
Michal Hocko2d110852013-02-22 16:34:43 -08005820 return 0;
5821}
5822subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08005823
5824#ifdef CONFIG_MEMCG_SWAP
5825/**
5826 * mem_cgroup_swapout - transfer a memsw charge to swap
5827 * @page: page whose memsw charge to transfer
5828 * @entry: swap entry to move the charge to
5829 *
5830 * Transfer the memsw charge of @page to @entry.
5831 */
5832void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
5833{
5834 struct mem_cgroup *memcg;
5835 unsigned short oldid;
5836
5837 VM_BUG_ON_PAGE(PageLRU(page), page);
5838 VM_BUG_ON_PAGE(page_count(page), page);
5839
5840 if (!do_swap_account)
5841 return;
5842
5843 memcg = page->mem_cgroup;
5844
5845 /* Readahead page, never charged */
5846 if (!memcg)
5847 return;
5848
5849 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg));
5850 VM_BUG_ON_PAGE(oldid, page);
5851 mem_cgroup_swap_statistics(memcg, true);
5852
5853 page->mem_cgroup = NULL;
5854
5855 if (!mem_cgroup_is_root(memcg))
5856 page_counter_uncharge(&memcg->memory, 1);
5857
5858 /* XXX: caller holds IRQ-safe mapping->tree_lock */
5859 VM_BUG_ON(!irqs_disabled());
5860
5861 mem_cgroup_charge_statistics(memcg, page, -1);
5862 memcg_check_events(memcg, page);
5863}
5864
5865/**
5866 * mem_cgroup_uncharge_swap - uncharge a swap entry
5867 * @entry: swap entry to uncharge
5868 *
5869 * Drop the memsw charge associated with @entry.
5870 */
5871void mem_cgroup_uncharge_swap(swp_entry_t entry)
5872{
5873 struct mem_cgroup *memcg;
5874 unsigned short id;
5875
5876 if (!do_swap_account)
5877 return;
5878
5879 id = swap_cgroup_record(entry, 0);
5880 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005881 memcg = mem_cgroup_from_id(id);
Johannes Weiner21afa382015-02-11 15:26:36 -08005882 if (memcg) {
5883 if (!mem_cgroup_is_root(memcg))
5884 page_counter_uncharge(&memcg->memsw, 1);
5885 mem_cgroup_swap_statistics(memcg, false);
5886 css_put(&memcg->css);
5887 }
5888 rcu_read_unlock();
5889}
5890
5891/* for remember boot option*/
5892#ifdef CONFIG_MEMCG_SWAP_ENABLED
5893static int really_do_swap_account __initdata = 1;
5894#else
5895static int really_do_swap_account __initdata;
5896#endif
5897
5898static int __init enable_swap_account(char *s)
5899{
5900 if (!strcmp(s, "1"))
5901 really_do_swap_account = 1;
5902 else if (!strcmp(s, "0"))
5903 really_do_swap_account = 0;
5904 return 1;
5905}
5906__setup("swapaccount=", enable_swap_account);
5907
5908static struct cftype memsw_cgroup_files[] = {
5909 {
5910 .name = "memsw.usage_in_bytes",
5911 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5912 .read_u64 = mem_cgroup_read_u64,
5913 },
5914 {
5915 .name = "memsw.max_usage_in_bytes",
5916 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5917 .write = mem_cgroup_reset,
5918 .read_u64 = mem_cgroup_read_u64,
5919 },
5920 {
5921 .name = "memsw.limit_in_bytes",
5922 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5923 .write = mem_cgroup_write,
5924 .read_u64 = mem_cgroup_read_u64,
5925 },
5926 {
5927 .name = "memsw.failcnt",
5928 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5929 .write = mem_cgroup_reset,
5930 .read_u64 = mem_cgroup_read_u64,
5931 },
5932 { }, /* terminate */
5933};
5934
5935static int __init mem_cgroup_swap_init(void)
5936{
5937 if (!mem_cgroup_disabled() && really_do_swap_account) {
5938 do_swap_account = 1;
5939 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
5940 memsw_cgroup_files));
5941 }
5942 return 0;
5943}
5944subsys_initcall(mem_cgroup_swap_init);
5945
5946#endif /* CONFIG_MEMCG_SWAP */