blob: f48ff9aae320452884a5b65ee826d246c54e4fc2 [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;
Tejun Heodbee2272015-05-22 17:13:20 -040080struct cgroup_subsys_state *mem_cgroup_root_css __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080081
Johannes Weiner21afa382015-02-11 15:26:36 -080082/* Whether the swap controller is active */
Andrew Mortonc255a452012-07-31 16:43:02 -070083#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080084int do_swap_account __read_mostly;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080085#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070086#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080087#endif
88
Johannes Weineraf7c4b02012-05-29 15:07:08 -070089static const char * const mem_cgroup_stat_names[] = {
90 "cache",
91 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070092 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070093 "mapped_file",
Greg Thelen7c9d3ff2015-05-22 17:13:16 -040094 "dirty",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070095 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070096 "swap",
97};
98
Johannes Weineraf7c4b02012-05-29 15:07:08 -070099static const char * const mem_cgroup_events_names[] = {
100 "pgpgin",
101 "pgpgout",
102 "pgfault",
103 "pgmajfault",
104};
105
Sha Zhengju58cf1882013-02-22 16:32:05 -0800106static const char * const mem_cgroup_lru_names[] = {
107 "inactive_anon",
108 "active_anon",
109 "inactive_file",
110 "active_file",
111 "unevictable",
112};
113
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700114/*
115 * Per memcg event counter is incremented at every pagein/pageout. With THP,
116 * it will be incremated by the number of pages. This counter is used for
117 * for trigger some periodic events. This is straightforward and better
118 * than using jiffies etc. to handle periodic memcg event.
119 */
120enum mem_cgroup_events_target {
121 MEM_CGROUP_TARGET_THRESH,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700122 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700123 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700124 MEM_CGROUP_NTARGETS,
125};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700126#define THRESHOLDS_EVENTS_TARGET 128
127#define SOFTLIMIT_EVENTS_TARGET 1024
128#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700129
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800130struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700131 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weiner241994ed2015-02-11 15:26:06 -0800132 unsigned long events[MEMCG_NR_EVENTS];
Johannes Weiner13114712012-05-29 15:07:07 -0700133 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700134 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800135};
136
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800137struct reclaim_iter {
138 struct mem_cgroup *position;
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800139 /* scan generation, increased every round-trip */
140 unsigned int generation;
141};
142
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800143/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800144 * per-zone information in memory controller.
145 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800146struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800147 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700148 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800149
Johannes Weiner5ac8fb32014-12-10 15:42:39 -0800150 struct reclaim_iter iter[DEF_PRIORITY + 1];
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800151
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700152 struct rb_node tree_node; /* RB tree node */
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800153 unsigned long usage_in_excess;/* Set to the value by which */
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700154 /* the soft limit is exceeded*/
155 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700156 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700157 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800158};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800159
160struct mem_cgroup_per_node {
161 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
162};
163
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700164/*
165 * Cgroups above their limits are maintained in a RB-Tree, independent of
166 * their hierarchy representation
167 */
168
169struct mem_cgroup_tree_per_zone {
170 struct rb_root rb_root;
171 spinlock_t lock;
172};
173
174struct mem_cgroup_tree_per_node {
175 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
176};
177
178struct mem_cgroup_tree {
179 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
180};
181
182static struct mem_cgroup_tree soft_limit_tree __read_mostly;
183
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800184struct mem_cgroup_threshold {
185 struct eventfd_ctx *eventfd;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800186 unsigned long threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800187};
188
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700189/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800190struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700191 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700192 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800193 /* Size of entries[] */
194 unsigned int size;
195 /* Array of thresholds */
196 struct mem_cgroup_threshold entries[0];
197};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700198
199struct mem_cgroup_thresholds {
200 /* Primary thresholds array */
201 struct mem_cgroup_threshold_ary *primary;
202 /*
203 * Spare threshold array.
204 * This is needed to make mem_cgroup_unregister_event() "never fail".
205 * It must be able to store at least primary->size - 1 entries.
206 */
207 struct mem_cgroup_threshold_ary *spare;
208};
209
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700210/* for OOM */
211struct mem_cgroup_eventfd_list {
212 struct list_head list;
213 struct eventfd_ctx *eventfd;
214};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215
Tejun Heo79bd9812013-11-22 18:20:42 -0500216/*
217 * cgroup_event represents events which userspace want to receive.
218 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500219struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500220 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500221 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500222 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500223 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500224 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500225 * eventfd to signal userspace about the event.
226 */
227 struct eventfd_ctx *eventfd;
228 /*
229 * Each of these stored in a list by the cgroup.
230 */
231 struct list_head list;
232 /*
Tejun Heofba94802013-11-22 18:20:43 -0500233 * register_event() callback will be used to add new userspace
234 * waiter for changes related to this event. Use eventfd_signal()
235 * on eventfd to send notification to userspace.
236 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500237 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500238 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500239 /*
240 * unregister_event() callback will be called when userspace closes
241 * the eventfd or on cgroup removing. This callback must be set,
242 * if you want provide notification functionality.
243 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500244 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500245 struct eventfd_ctx *eventfd);
246 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500247 * All fields below needed to unregister event when
248 * userspace closes eventfd.
249 */
250 poll_table pt;
251 wait_queue_head_t *wqh;
252 wait_queue_t wait;
253 struct work_struct remove;
254};
255
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700256static void mem_cgroup_threshold(struct mem_cgroup *memcg);
257static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800258
Balbir Singhf64c3f52009-09-23 15:56:37 -0700259/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800260 * The memory controller data structure. The memory controller controls both
261 * page cache and RSS per cgroup. We would eventually like to provide
262 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
263 * to help the administrator determine what knobs to tune.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800264 */
265struct mem_cgroup {
266 struct cgroup_subsys_state css;
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800267
268 /* Accounted resources */
269 struct page_counter memory;
270 struct page_counter memsw;
271 struct page_counter kmem;
272
Johannes Weiner241994ed2015-02-11 15:26:06 -0800273 /* Normal memory consumption range */
274 unsigned long low;
275 unsigned long high;
276
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800277 unsigned long soft_limit;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700278
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700279 /* vmpressure notifications */
280 struct vmpressure vmpressure;
281
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -0700282 /* css_online() has been completed */
283 int initialized;
284
Li Zefan465939a2013-07-08 16:00:38 -0700285 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800286 * Should the accounting and control be hierarchical, per subtree?
287 */
288 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700289
290 bool oom_lock;
291 atomic_t under_oom;
292
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 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000327
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700328#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Eric W. Biederman2e685ca2013-10-19 16:26:19 -0700329 struct cg_proto tcp_mem;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000330#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800331#if defined(CONFIG_MEMCG_KMEM)
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800332 /* Index in the kmem_cache->memcg_params.memcg_caches array */
Glauber Costa2633d7a2012-12-18 14:22:34 -0800333 int kmemcg_id;
Vladimir Davydov2788cf0c2015-02-12 14:59:38 -0800334 bool kmem_acct_activated;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -0800335 bool kmem_acct_active;
Glauber Costa2633d7a2012-12-18 14:22:34 -0800336#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800337
338 int last_scanned_node;
339#if MAX_NUMNODES > 1
340 nodemask_t scan_nodes;
341 atomic_t numainfo_events;
342 atomic_t numainfo_updating;
343#endif
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700344
Tejun Heo6b385782015-05-22 17:13:37 -0400345#ifdef CONFIG_CGROUP_WRITEBACK
346 struct list_head cgwb_list;
Tejun Heo8c93c2f2015-05-22 18:23:33 -0400347 struct wb_domain cgwb_domain;
Tejun Heo6b385782015-05-22 17:13:37 -0400348#endif
349
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
Tejun Heoe4487942015-05-27 20:00:02 -0400600/**
601 * mem_cgroup_css_from_page - css of the memcg associated with a page
602 * @page: page of interest
603 *
604 * If memcg is bound to the default hierarchy, css of the memcg associated
605 * with @page is returned. The returned css remains associated with @page
606 * until it is released.
607 *
608 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
609 * is returned.
610 *
611 * XXX: The above description of behavior on the default hierarchy isn't
612 * strictly true yet as replace_page_cache_page() can modify the
613 * association before @page is released even on the default hierarchy;
614 * however, the current and planned usages don't mix the the two functions
615 * and replace_page_cache_page() will soon be updated to make the invariant
616 * actually true.
617 */
618struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page)
619{
620 struct mem_cgroup *memcg;
621
622 rcu_read_lock();
623
624 memcg = page->mem_cgroup;
625
626 if (!memcg || !cgroup_on_dfl(memcg->css.cgroup))
627 memcg = root_mem_cgroup;
628
629 rcu_read_unlock();
630 return &memcg->css;
631}
632
Balbir Singhf64c3f52009-09-23 15:56:37 -0700633static struct mem_cgroup_per_zone *
Jianyu Zhane2318752014-06-06 14:38:20 -0700634mem_cgroup_page_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700635{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700636 int nid = page_to_nid(page);
637 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700638
Jianyu Zhane2318752014-06-06 14:38:20 -0700639 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700640}
641
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700642static struct mem_cgroup_tree_per_zone *
643soft_limit_tree_node_zone(int nid, int zid)
644{
645 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
646}
647
648static struct mem_cgroup_tree_per_zone *
649soft_limit_tree_from_page(struct page *page)
650{
651 int nid = page_to_nid(page);
652 int zid = page_zonenum(page);
653
654 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
655}
656
Johannes Weinercf2c8122014-06-06 14:38:21 -0700657static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_zone *mz,
658 struct mem_cgroup_tree_per_zone *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800659 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700660{
661 struct rb_node **p = &mctz->rb_root.rb_node;
662 struct rb_node *parent = NULL;
663 struct mem_cgroup_per_zone *mz_node;
664
665 if (mz->on_tree)
666 return;
667
668 mz->usage_in_excess = new_usage_in_excess;
669 if (!mz->usage_in_excess)
670 return;
671 while (*p) {
672 parent = *p;
673 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
674 tree_node);
675 if (mz->usage_in_excess < mz_node->usage_in_excess)
676 p = &(*p)->rb_left;
677 /*
678 * We can't avoid mem cgroups that are over their soft
679 * limit by the same amount
680 */
681 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
682 p = &(*p)->rb_right;
683 }
684 rb_link_node(&mz->tree_node, parent, p);
685 rb_insert_color(&mz->tree_node, &mctz->rb_root);
686 mz->on_tree = true;
687}
688
Johannes Weinercf2c8122014-06-06 14:38:21 -0700689static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
690 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700691{
692 if (!mz->on_tree)
693 return;
694 rb_erase(&mz->tree_node, &mctz->rb_root);
695 mz->on_tree = false;
696}
697
Johannes Weinercf2c8122014-06-06 14:38:21 -0700698static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_zone *mz,
699 struct mem_cgroup_tree_per_zone *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700700{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700701 unsigned long flags;
702
703 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700704 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700705 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700706}
707
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800708static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
709{
710 unsigned long nr_pages = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -0700711 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800712 unsigned long excess = 0;
713
714 if (nr_pages > soft_limit)
715 excess = nr_pages - soft_limit;
716
717 return excess;
718}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700719
720static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
721{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800722 unsigned long excess;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700723 struct mem_cgroup_per_zone *mz;
724 struct mem_cgroup_tree_per_zone *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700725
Jianyu Zhane2318752014-06-06 14:38:20 -0700726 mctz = soft_limit_tree_from_page(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700727 /*
728 * Necessary to update all ancestors when hierarchy is used.
729 * because their event counter is not touched.
730 */
731 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Jianyu Zhane2318752014-06-06 14:38:20 -0700732 mz = mem_cgroup_page_zoneinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800733 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700734 /*
735 * We have to update the tree if mz is on RB-tree or
736 * mem is over its softlimit.
737 */
738 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700739 unsigned long flags;
740
741 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700742 /* if on-tree, remove it */
743 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700744 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700745 /*
746 * Insert again. mz->usage_in_excess will be updated.
747 * If excess is 0, no tree ops.
748 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700749 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700750 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700751 }
752 }
753}
754
755static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
756{
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700757 struct mem_cgroup_tree_per_zone *mctz;
Jianyu Zhane2318752014-06-06 14:38:20 -0700758 struct mem_cgroup_per_zone *mz;
759 int nid, zid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700760
Jianyu Zhane2318752014-06-06 14:38:20 -0700761 for_each_node(nid) {
762 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
763 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
764 mctz = soft_limit_tree_node_zone(nid, zid);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700765 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700766 }
767 }
768}
769
770static struct mem_cgroup_per_zone *
771__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
772{
773 struct rb_node *rightmost = NULL;
774 struct mem_cgroup_per_zone *mz;
775
776retry:
777 mz = NULL;
778 rightmost = rb_last(&mctz->rb_root);
779 if (!rightmost)
780 goto done; /* Nothing to reclaim from */
781
782 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
783 /*
784 * Remove the node now but someone else can add it back,
785 * we will to add it back at the end of reclaim to its correct
786 * position in the tree.
787 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700788 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800789 if (!soft_limit_excess(mz->memcg) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400790 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700791 goto retry;
792done:
793 return mz;
794}
795
796static struct mem_cgroup_per_zone *
797mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
798{
799 struct mem_cgroup_per_zone *mz;
800
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700801 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700802 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700803 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700804 return mz;
805}
806
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700807/*
808 * Implementation Note: reading percpu statistics for memcg.
809 *
810 * Both of vmstat[] and percpu_counter has threshold and do periodic
811 * synchronization to implement "quick" read. There are trade-off between
812 * reading cost and precision of value. Then, we may have a chance to implement
813 * a periodic synchronizion of counter in memcg's counter.
814 *
815 * But this _read() function is used for user interface now. The user accounts
816 * memory usage by memory cgroup and he _always_ requires exact value because
817 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
818 * have to visit all online cpus and make sum. So, for now, unnecessary
819 * synchronization is not implemented. (just implemented for cpu hotplug)
820 *
821 * If there are kernel internal actions which can make use of some not-exact
822 * value, and reading all cpu value can be performance bottleneck in some
823 * common workload, threashold and synchonization as vmstat[] should be
824 * implemented.
825 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700826static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700827 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800828{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700829 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800830 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800831
Tejun Heo11aa0bf2015-05-22 18:23:18 -0400832 for_each_possible_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700833 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800834 return val;
835}
836
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700837static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700838 enum mem_cgroup_events_index idx)
839{
840 unsigned long val = 0;
841 int cpu;
842
Tejun Heo11aa0bf2015-05-22 18:23:18 -0400843 for_each_possible_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700844 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700845 return val;
846}
847
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700848static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700849 struct page *page,
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700850 int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800851{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700852 /*
853 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
854 * counted as CACHE even if it's on ANON LRU.
855 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700856 if (PageAnon(page))
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700857 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700858 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800859 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700860 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700861 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700862
David Rientjesb070e652013-05-07 16:18:09 -0700863 if (PageTransHuge(page))
864 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
865 nr_pages);
866
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800867 /* pagein of a big page is an event. So, ignore page size */
868 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700869 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800870 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700871 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800872 nr_pages = -nr_pages; /* for event */
873 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800874
Johannes Weiner13114712012-05-29 15:07:07 -0700875 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800876}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800877
Jianyu Zhane2318752014-06-06 14:38:20 -0700878unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700879{
880 struct mem_cgroup_per_zone *mz;
881
882 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
883 return mz->lru_size[lru];
884}
885
Jianyu Zhane2318752014-06-06 14:38:20 -0700886static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
887 int nid,
888 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700889{
Jianyu Zhane2318752014-06-06 14:38:20 -0700890 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700891 int zid;
892
Jianyu Zhane2318752014-06-06 14:38:20 -0700893 VM_BUG_ON((unsigned)nid >= nr_node_ids);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700894
Jianyu Zhane2318752014-06-06 14:38:20 -0700895 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
896 struct mem_cgroup_per_zone *mz;
897 enum lru_list lru;
898
899 for_each_lru(lru) {
900 if (!(BIT(lru) & lru_mask))
901 continue;
902 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
903 nr += mz->lru_size[lru];
904 }
905 }
906 return nr;
Ying Han889976d2011-05-26 16:25:33 -0700907}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700908
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700909static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700910 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800911{
Jianyu Zhane2318752014-06-06 14:38:20 -0700912 unsigned long nr = 0;
Ying Han889976d2011-05-26 16:25:33 -0700913 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800914
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800915 for_each_node_state(nid, N_MEMORY)
Jianyu Zhane2318752014-06-06 14:38:20 -0700916 nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
917 return nr;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800918}
919
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800920static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
921 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800922{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700923 unsigned long val, next;
924
Johannes Weiner13114712012-05-29 15:07:07 -0700925 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700926 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700927 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800928 if ((long)next - (long)val < 0) {
929 switch (target) {
930 case MEM_CGROUP_TARGET_THRESH:
931 next = val + THRESHOLDS_EVENTS_TARGET;
932 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700933 case MEM_CGROUP_TARGET_SOFTLIMIT:
934 next = val + SOFTLIMIT_EVENTS_TARGET;
935 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800936 case MEM_CGROUP_TARGET_NUMAINFO:
937 next = val + NUMAINFO_EVENTS_TARGET;
938 break;
939 default:
940 break;
941 }
942 __this_cpu_write(memcg->stat->targets[target], next);
943 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700944 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800945 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800946}
947
948/*
949 * Check events in order.
950 *
951 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700952static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800953{
954 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800955 if (unlikely(mem_cgroup_event_ratelimit(memcg,
956 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700957 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800958 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800959
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700960 do_softlimit = mem_cgroup_event_ratelimit(memcg,
961 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700962#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800963 do_numainfo = mem_cgroup_event_ratelimit(memcg,
964 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700965#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800966 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700967 if (unlikely(do_softlimit))
968 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800969#if MAX_NUMNODES > 1
970 if (unlikely(do_numainfo))
971 atomic_inc(&memcg->numainfo_events);
972#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700973 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800974}
975
Balbir Singhcf475ad2008-04-29 01:00:16 -0700976struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800977{
Balbir Singh31a78f22008-09-28 23:09:31 +0100978 /*
979 * mm_update_next_owner() may clear mm->owner to NULL
980 * if it races with swapoff, page migration, etc.
981 * So this can be called with p == NULL.
982 */
983 if (unlikely(!p))
984 return NULL;
985
Tejun Heo073219e2014-02-08 10:36:58 -0500986 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800987}
988
Johannes Weinerdf381972014-04-07 15:37:43 -0700989static struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800990{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700991 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700992
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800993 rcu_read_lock();
994 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -0700995 /*
996 * Page cache insertions can happen withou an
997 * actual mm context, e.g. during disk probing
998 * on boot, loopback IO, acct() writes etc.
999 */
1000 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -07001001 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -07001002 else {
1003 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1004 if (unlikely(!memcg))
1005 memcg = root_mem_cgroup;
1006 }
Tejun Heoec903c02014-05-13 12:11:01 -04001007 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001008 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001009 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001010}
1011
Johannes Weiner56600482012-01-12 17:17:59 -08001012/**
1013 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1014 * @root: hierarchy root
1015 * @prev: previously returned memcg, NULL on first invocation
1016 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1017 *
1018 * Returns references to children of the hierarchy below @root, or
1019 * @root itself, or %NULL after a full round-trip.
1020 *
1021 * Caller must pass the return value in @prev on subsequent
1022 * invocations for reference counting, or use mem_cgroup_iter_break()
1023 * to cancel a hierarchy walk before the round-trip is complete.
1024 *
1025 * Reclaimers can specify a zone and a priority level in @reclaim to
1026 * divide up the memcgs in the hierarchy among all concurrent
1027 * reclaimers operating on the same zone and priority.
1028 */
Andrew Morton694fbc02013-09-24 15:27:37 -07001029struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001030 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001031 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001032{
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001033 struct reclaim_iter *uninitialized_var(iter);
1034 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001035 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001036 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001037
Andrew Morton694fbc02013-09-24 15:27:37 -07001038 if (mem_cgroup_disabled())
1039 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001040
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001041 if (!root)
1042 root = root_mem_cgroup;
1043
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001044 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001045 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001046
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001047 if (!root->use_hierarchy && root != root_mem_cgroup) {
1048 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001049 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001050 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001051 }
1052
Michal Hocko542f85f2013-04-29 15:07:15 -07001053 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001054
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001055 if (reclaim) {
1056 struct mem_cgroup_per_zone *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001057
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001058 mz = mem_cgroup_zone_zoneinfo(root, reclaim->zone);
1059 iter = &mz->iter[reclaim->priority];
Michal Hocko5f578162013-04-29 15:07:17 -07001060
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001061 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001062 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001063
1064 do {
Jason Low4db0c3c2015-04-15 16:14:08 -07001065 pos = READ_ONCE(iter->position);
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001066 /*
1067 * A racing update may change the position and
1068 * put the last reference, hence css_tryget(),
1069 * or retry to see the updated position.
1070 */
1071 } while (pos && !css_tryget(&pos->css));
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001072 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001073
1074 if (pos)
1075 css = &pos->css;
1076
1077 for (;;) {
1078 css = css_next_descendant_pre(css, &root->css);
1079 if (!css) {
1080 /*
1081 * Reclaimers share the hierarchy walk, and a
1082 * new one might jump in right at the end of
1083 * the hierarchy - make sure they see at least
1084 * one group and restart from the beginning.
1085 */
1086 if (!prev)
1087 continue;
1088 break;
1089 }
1090
1091 /*
1092 * Verify the css and acquire a reference. The root
1093 * is provided by the caller, so we know it's alive
1094 * and kicking, and don't take an extra reference.
1095 */
1096 memcg = mem_cgroup_from_css(css);
1097
1098 if (css == &root->css)
1099 break;
1100
Johannes Weinerb2052562014-12-10 15:42:48 -08001101 if (css_tryget(css)) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001102 /*
1103 * Make sure the memcg is initialized:
1104 * mem_cgroup_css_online() orders the the
1105 * initialization against setting the flag.
1106 */
1107 if (smp_load_acquire(&memcg->initialized))
1108 break;
1109
1110 css_put(css);
1111 }
1112
1113 memcg = NULL;
1114 }
1115
1116 if (reclaim) {
1117 if (cmpxchg(&iter->position, pos, memcg) == pos) {
1118 if (memcg)
1119 css_get(&memcg->css);
1120 if (pos)
1121 css_put(&pos->css);
1122 }
1123
1124 /*
1125 * pairs with css_tryget when dereferencing iter->position
1126 * above.
1127 */
1128 if (pos)
1129 css_put(&pos->css);
1130
1131 if (!memcg)
1132 iter->generation++;
1133 else if (!prev)
1134 reclaim->generation = iter->generation;
1135 }
1136
Michal Hocko542f85f2013-04-29 15:07:15 -07001137out_unlock:
1138 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001139out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001140 if (prev && prev != root)
1141 css_put(&prev->css);
1142
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001143 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001144}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001145
Johannes Weiner56600482012-01-12 17:17:59 -08001146/**
1147 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1148 * @root: hierarchy root
1149 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1150 */
1151void mem_cgroup_iter_break(struct mem_cgroup *root,
1152 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001153{
1154 if (!root)
1155 root = root_mem_cgroup;
1156 if (prev && prev != root)
1157 css_put(&prev->css);
1158}
1159
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001160/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001161 * Iteration constructs for visiting all cgroups (under a tree). If
1162 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1163 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001164 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001165#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001166 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001167 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001168 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001169
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001170#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001171 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001172 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001173 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001174
David Rientjes68ae5642012-12-12 13:51:57 -08001175void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001176{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001177 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001178
Ying Han456f9982011-05-26 16:25:38 -07001179 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001180 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1181 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001182 goto out;
1183
1184 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001185 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001186 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1187 break;
1188 case PGMAJFAULT:
1189 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001190 break;
1191 default:
1192 BUG();
1193 }
1194out:
1195 rcu_read_unlock();
1196}
David Rientjes68ae5642012-12-12 13:51:57 -08001197EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001198
Johannes Weiner925b7672012-01-12 17:18:15 -08001199/**
1200 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1201 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001202 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001203 *
1204 * Returns the lru list vector holding pages for the given @zone and
1205 * @mem. This can be the global zone lruvec, if the memory controller
1206 * is disabled.
1207 */
1208struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1209 struct mem_cgroup *memcg)
1210{
1211 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001212 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001213
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001214 if (mem_cgroup_disabled()) {
1215 lruvec = &zone->lruvec;
1216 goto out;
1217 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001218
Jianyu Zhane2318752014-06-06 14:38:20 -07001219 mz = mem_cgroup_zone_zoneinfo(memcg, zone);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001220 lruvec = &mz->lruvec;
1221out:
1222 /*
1223 * Since a node can be onlined after the mem_cgroup was created,
1224 * we have to be prepared to initialize lruvec->zone here;
1225 * and if offlined then reonlined, we need to reinitialize it.
1226 */
1227 if (unlikely(lruvec->zone != zone))
1228 lruvec->zone = zone;
1229 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001230}
1231
Johannes Weiner925b7672012-01-12 17:18:15 -08001232/**
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001233 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
Johannes Weiner925b7672012-01-12 17:18:15 -08001234 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001235 * @zone: zone of the page
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001236 *
1237 * This function is only safe when following the LRU page isolation
1238 * and putback protocol: the LRU lock must be held, and the page must
1239 * either be PageLRU() or the caller must have isolated/allocated it.
Minchan Kim3f58a822011-03-22 16:32:53 -07001240 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001241struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001242{
1243 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001244 struct mem_cgroup *memcg;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001245 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001246
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001247 if (mem_cgroup_disabled()) {
1248 lruvec = &zone->lruvec;
1249 goto out;
1250 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001251
Johannes Weiner1306a852014-12-10 15:44:52 -08001252 memcg = page->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001253 /*
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001254 * Swapcache readahead pages are added to the LRU - and
Johannes Weiner29833312014-12-10 15:44:02 -08001255 * possibly migrated - before they are charged.
Hugh Dickins75121022012-03-05 14:59:18 -08001256 */
Johannes Weiner29833312014-12-10 15:44:02 -08001257 if (!memcg)
1258 memcg = root_mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001259
Jianyu Zhane2318752014-06-06 14:38:20 -07001260 mz = mem_cgroup_page_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001261 lruvec = &mz->lruvec;
1262out:
1263 /*
1264 * Since a node can be onlined after the mem_cgroup was created,
1265 * we have to be prepared to initialize lruvec->zone here;
1266 * and if offlined then reonlined, we need to reinitialize it.
1267 */
1268 if (unlikely(lruvec->zone != zone))
1269 lruvec->zone = zone;
1270 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001271}
1272
1273/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001274 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1275 * @lruvec: mem_cgroup per zone lru vector
1276 * @lru: index of lru list the page is sitting on
1277 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001278 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001279 * This function must be called when a page is added to or removed from an
1280 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001281 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001282void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1283 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001284{
1285 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001286 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001287
1288 if (mem_cgroup_disabled())
1289 return;
1290
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001291 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1292 lru_size = mz->lru_size + lru;
1293 *lru_size += nr_pages;
1294 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001295}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001296
Johannes Weiner2314b422014-12-10 15:44:33 -08001297bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, struct mem_cgroup *root)
Michal Hocko3e920412011-07-26 16:08:29 -07001298{
Johannes Weiner2314b422014-12-10 15:44:33 -08001299 if (root == memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001300 return true;
Johannes Weiner2314b422014-12-10 15:44:33 -08001301 if (!root->use_hierarchy)
Johannes Weiner91c637342012-05-29 15:06:24 -07001302 return false;
Johannes Weiner2314b422014-12-10 15:44:33 -08001303 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001304}
1305
Johannes Weiner2314b422014-12-10 15:44:33 -08001306bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg)
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001307{
Johannes Weiner2314b422014-12-10 15:44:33 -08001308 struct mem_cgroup *task_memcg;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001309 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001310 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001311
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001312 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001313 if (p) {
Johannes Weiner2314b422014-12-10 15:44:33 -08001314 task_memcg = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001315 task_unlock(p);
1316 } else {
1317 /*
1318 * All threads may have already detached their mm's, but the oom
1319 * killer still needs to detect if they have already been oom
1320 * killed to prevent needlessly killing additional tasks.
1321 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001322 rcu_read_lock();
Johannes Weiner2314b422014-12-10 15:44:33 -08001323 task_memcg = mem_cgroup_from_task(task);
1324 css_get(&task_memcg->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001325 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001326 }
Johannes Weiner2314b422014-12-10 15:44:33 -08001327 ret = mem_cgroup_is_descendant(task_memcg, memcg);
1328 css_put(&task_memcg->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001329 return ret;
1330}
1331
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001332int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001333{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001334 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001335 unsigned long inactive;
1336 unsigned long active;
1337 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001338
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001339 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1340 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001341
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001342 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1343 if (gb)
1344 inactive_ratio = int_sqrt(10 * gb);
1345 else
1346 inactive_ratio = 1;
1347
Johannes Weiner9b272972011-11-02 13:38:23 -07001348 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001349}
1350
Vladimir Davydov90cbc252015-02-11 15:25:55 -08001351bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
1352{
1353 struct mem_cgroup_per_zone *mz;
1354 struct mem_cgroup *memcg;
1355
1356 if (mem_cgroup_disabled())
1357 return true;
1358
1359 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1360 memcg = mz->memcg;
1361
1362 return !!(memcg->css.flags & CSS_ONLINE);
1363}
1364
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001365#define mem_cgroup_from_counter(counter, member) \
Balbir Singh6d61ef42009-01-07 18:08:06 -08001366 container_of(counter, struct mem_cgroup, member)
1367
Johannes Weiner19942822011-02-01 15:52:43 -08001368/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001369 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001370 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001371 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001372 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001373 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001374 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001375static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001376{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001377 unsigned long margin = 0;
1378 unsigned long count;
1379 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001380
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001381 count = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -07001382 limit = READ_ONCE(memcg->memory.limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001383 if (count < limit)
1384 margin = limit - count;
1385
1386 if (do_swap_account) {
1387 count = page_counter_read(&memcg->memsw);
Jason Low4db0c3c2015-04-15 16:14:08 -07001388 limit = READ_ONCE(memcg->memsw.limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001389 if (count <= limit)
1390 margin = min(margin, limit - count);
1391 }
1392
1393 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001394}
1395
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001396int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001397{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001398 /* root ? */
Linus Torvalds14208b02014-06-09 15:03:33 -07001399 if (mem_cgroup_disabled() || !memcg->css.parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001400 return vm_swappiness;
1401
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001402 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001403}
1404
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001405/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001406 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001407 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001408 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1409 * moving cgroups. This is for waiting at high-memory pressure
1410 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001411 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001412static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001413{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001414 struct mem_cgroup *from;
1415 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001416 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001417 /*
1418 * Unlike task_move routines, we access mc.to, mc.from not under
1419 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1420 */
1421 spin_lock(&mc.lock);
1422 from = mc.from;
1423 to = mc.to;
1424 if (!from)
1425 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001426
Johannes Weiner2314b422014-12-10 15:44:33 -08001427 ret = mem_cgroup_is_descendant(from, memcg) ||
1428 mem_cgroup_is_descendant(to, memcg);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001429unlock:
1430 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001431 return ret;
1432}
1433
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001434static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001435{
1436 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001437 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001438 DEFINE_WAIT(wait);
1439 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1440 /* moving charge context might have finished. */
1441 if (mc.moving_task)
1442 schedule();
1443 finish_wait(&mc.waitq, &wait);
1444 return true;
1445 }
1446 }
1447 return false;
1448}
1449
Sha Zhengju58cf1882013-02-22 16:32:05 -08001450#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001451/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001452 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001453 * @memcg: The memory cgroup that went over limit
1454 * @p: Task that is going to be killed
1455 *
1456 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1457 * enabled
1458 */
1459void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1460{
Tejun Heoe61734c2014-02-12 09:29:50 -05001461 /* oom_info_lock ensures that parallel ooms do not interleave */
Michal Hocko08088cb2014-02-25 15:01:44 -08001462 static DEFINE_MUTEX(oom_info_lock);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001463 struct mem_cgroup *iter;
1464 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001465
Michal Hocko08088cb2014-02-25 15:01:44 -08001466 mutex_lock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001467 rcu_read_lock();
1468
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -07001469 if (p) {
1470 pr_info("Task in ");
1471 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1472 pr_cont(" killed as a result of limit of ");
1473 } else {
1474 pr_info("Memory limit reached of cgroup ");
1475 }
1476
Tejun Heoe61734c2014-02-12 09:29:50 -05001477 pr_cont_cgroup_path(memcg->css.cgroup);
Greg Thelen0346dad2015-01-26 12:58:38 -08001478 pr_cont("\n");
Balbir Singhe2224322009-04-02 16:57:39 -07001479
Balbir Singhe2224322009-04-02 16:57:39 -07001480 rcu_read_unlock();
1481
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001482 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1483 K((u64)page_counter_read(&memcg->memory)),
1484 K((u64)memcg->memory.limit), memcg->memory.failcnt);
1485 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1486 K((u64)page_counter_read(&memcg->memsw)),
1487 K((u64)memcg->memsw.limit), memcg->memsw.failcnt);
1488 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1489 K((u64)page_counter_read(&memcg->kmem)),
1490 K((u64)memcg->kmem.limit), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001491
1492 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heoe61734c2014-02-12 09:29:50 -05001493 pr_info("Memory cgroup stats for ");
1494 pr_cont_cgroup_path(iter->css.cgroup);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001495 pr_cont(":");
1496
1497 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1498 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1499 continue;
1500 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1501 K(mem_cgroup_read_stat(iter, i)));
1502 }
1503
1504 for (i = 0; i < NR_LRU_LISTS; i++)
1505 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1506 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1507
1508 pr_cont("\n");
1509 }
Michal Hocko08088cb2014-02-25 15:01:44 -08001510 mutex_unlock(&oom_info_lock);
Balbir Singhe2224322009-04-02 16:57:39 -07001511}
1512
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001513/*
1514 * This function returns the number of memcg under hierarchy tree. Returns
1515 * 1(self count) if no children.
1516 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001517static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001518{
1519 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001520 struct mem_cgroup *iter;
1521
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001522 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001523 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001524 return num;
1525}
1526
Balbir Singh6d61ef42009-01-07 18:08:06 -08001527/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001528 * Return the memory (and swap, if configured) limit for a memcg.
1529 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001530static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001531{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001532 unsigned long limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001533
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001534 limit = memcg->memory.limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001535 if (mem_cgroup_swappiness(memcg)) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001536 unsigned long memsw_limit;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001537
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001538 memsw_limit = memcg->memsw.limit;
1539 limit = min(limit + total_swap_pages, memsw_limit);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001540 }
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001541 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001542}
1543
David Rientjes19965462012-12-11 16:00:26 -08001544static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1545 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001546{
1547 struct mem_cgroup *iter;
1548 unsigned long chosen_points = 0;
1549 unsigned long totalpages;
1550 unsigned int points = 0;
1551 struct task_struct *chosen = NULL;
1552
David Rientjes876aafb2012-07-31 16:43:48 -07001553 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001554 * If current has a pending SIGKILL or is exiting, then automatically
1555 * select it. The goal is to allow it to allocate so that it may
1556 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001557 */
Oleg Nesterovd003f372014-12-12 16:56:24 -08001558 if (fatal_signal_pending(current) || task_will_free_mem(current)) {
Michal Hocko49550b62015-02-11 15:26:12 -08001559 mark_tsk_oom_victim(current);
David Rientjes876aafb2012-07-31 16:43:48 -07001560 return;
1561 }
1562
Balasubramani Vivekanandan2415b9f2015-04-14 15:48:18 -07001563 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL, memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001564 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001565 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001566 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001567 struct task_struct *task;
1568
Tejun Heo72ec7022013-08-08 20:11:26 -04001569 css_task_iter_start(&iter->css, &it);
1570 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001571 switch (oom_scan_process_thread(task, totalpages, NULL,
1572 false)) {
1573 case OOM_SCAN_SELECT:
1574 if (chosen)
1575 put_task_struct(chosen);
1576 chosen = task;
1577 chosen_points = ULONG_MAX;
1578 get_task_struct(chosen);
1579 /* fall through */
1580 case OOM_SCAN_CONTINUE:
1581 continue;
1582 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001583 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001584 mem_cgroup_iter_break(memcg, iter);
1585 if (chosen)
1586 put_task_struct(chosen);
1587 return;
1588 case OOM_SCAN_OK:
1589 break;
1590 };
1591 points = oom_badness(task, memcg, NULL, totalpages);
David Rientjesd49ad932014-01-23 15:53:34 -08001592 if (!points || points < chosen_points)
1593 continue;
1594 /* Prefer thread group leaders for display purposes */
1595 if (points == chosen_points &&
1596 thread_group_leader(chosen))
1597 continue;
1598
1599 if (chosen)
1600 put_task_struct(chosen);
1601 chosen = task;
1602 chosen_points = points;
1603 get_task_struct(chosen);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001604 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001605 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001606 }
1607
1608 if (!chosen)
1609 return;
1610 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001611 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1612 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001613}
1614
Michele Curtiae6e71d2014-12-12 16:56:35 -08001615#if MAX_NUMNODES > 1
1616
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001617/**
1618 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001619 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001620 * @nid: the node ID to be checked.
1621 * @noswap : specify true here if the user wants flle only information.
1622 *
1623 * This function returns whether the specified memcg contains any
1624 * reclaimable pages on a node. Returns true if there are any reclaimable
1625 * pages in the node.
1626 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001627static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001628 int nid, bool noswap)
1629{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001630 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001631 return true;
1632 if (noswap || !total_swap_pages)
1633 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001634 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001635 return true;
1636 return false;
1637
1638}
Ying Han889976d2011-05-26 16:25:33 -07001639
1640/*
1641 * Always updating the nodemask is not very good - even if we have an empty
1642 * list or the wrong list here, we can start from some node and traverse all
1643 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1644 *
1645 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001646static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001647{
1648 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001649 /*
1650 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1651 * pagein/pageout changes since the last update.
1652 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001653 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001654 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001655 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001656 return;
1657
Ying Han889976d2011-05-26 16:25:33 -07001658 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001659 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001660
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001661 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001663 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1664 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001665 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001667 atomic_set(&memcg->numainfo_events, 0);
1668 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001669}
1670
1671/*
1672 * Selecting a node where we start reclaim from. Because what we need is just
1673 * reducing usage counter, start from anywhere is O,K. Considering
1674 * memory reclaim from current node, there are pros. and cons.
1675 *
1676 * Freeing memory from current node means freeing memory from a node which
1677 * we'll use or we've used. So, it may make LRU bad. And if several threads
1678 * hit limits, it will see a contention on a node. But freeing from remote
1679 * node means more costs for memory reclaim because of memory latency.
1680 *
1681 * Now, we use round-robin. Better algorithm is welcomed.
1682 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001683int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001684{
1685 int node;
1686
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001687 mem_cgroup_may_update_nodemask(memcg);
1688 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001689
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001690 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001691 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001692 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001693 /*
1694 * We call this when we hit limit, not when pages are added to LRU.
1695 * No LRU may hold pages because all pages are UNEVICTABLE or
1696 * memcg is too small and all pages are not on LRU. In that case,
1697 * we use curret node.
1698 */
1699 if (unlikely(node == MAX_NUMNODES))
1700 node = numa_node_id();
1701
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001702 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001703 return node;
1704}
Ying Han889976d2011-05-26 16:25:33 -07001705#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001706int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001707{
1708 return 0;
1709}
1710#endif
1711
Andrew Morton0608f432013-09-24 15:27:41 -07001712static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1713 struct zone *zone,
1714 gfp_t gfp_mask,
1715 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001716{
Andrew Morton0608f432013-09-24 15:27:41 -07001717 struct mem_cgroup *victim = NULL;
1718 int total = 0;
1719 int loop = 0;
1720 unsigned long excess;
1721 unsigned long nr_scanned;
1722 struct mem_cgroup_reclaim_cookie reclaim = {
1723 .zone = zone,
1724 .priority = 0,
1725 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001726
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001727 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001728
Andrew Morton0608f432013-09-24 15:27:41 -07001729 while (1) {
1730 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1731 if (!victim) {
1732 loop++;
1733 if (loop >= 2) {
1734 /*
1735 * If we have not been able to reclaim
1736 * anything, it might because there are
1737 * no reclaimable pages under this hierarchy
1738 */
1739 if (!total)
1740 break;
1741 /*
1742 * We want to do more targeted reclaim.
1743 * excess >> 2 is not to excessive so as to
1744 * reclaim too much, nor too less that we keep
1745 * coming back to reclaim from this cgroup
1746 */
1747 if (total >= (excess >> 2) ||
1748 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1749 break;
1750 }
1751 continue;
1752 }
Andrew Morton0608f432013-09-24 15:27:41 -07001753 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1754 zone, &nr_scanned);
1755 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001756 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001757 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001758 }
Andrew Morton0608f432013-09-24 15:27:41 -07001759 mem_cgroup_iter_break(root_memcg, victim);
1760 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001761}
1762
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001763#ifdef CONFIG_LOCKDEP
1764static struct lockdep_map memcg_oom_lock_dep_map = {
1765 .name = "memcg_oom_lock",
1766};
1767#endif
1768
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001769static DEFINE_SPINLOCK(memcg_oom_lock);
1770
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001771/*
1772 * Check OOM-Killer is already running under our hierarchy.
1773 * If someone is running, return false.
1774 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001775static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001776{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001777 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001778
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001779 spin_lock(&memcg_oom_lock);
1780
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001781 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001782 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001783 /*
1784 * this subtree of our hierarchy is already locked
1785 * so we cannot give a lock.
1786 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001787 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001788 mem_cgroup_iter_break(memcg, iter);
1789 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001790 } else
1791 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001792 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001793
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001794 if (failed) {
1795 /*
1796 * OK, we failed to lock the whole subtree so we have
1797 * to clean up what we set up to the failing subtree
1798 */
1799 for_each_mem_cgroup_tree(iter, memcg) {
1800 if (iter == failed) {
1801 mem_cgroup_iter_break(memcg, iter);
1802 break;
1803 }
1804 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001805 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001806 } else
1807 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001808
1809 spin_unlock(&memcg_oom_lock);
1810
1811 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001812}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001813
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001814static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001815{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001816 struct mem_cgroup *iter;
1817
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001818 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001819 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001820 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001821 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001822 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001823}
1824
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001825static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001826{
1827 struct mem_cgroup *iter;
1828
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001829 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001830 atomic_inc(&iter->under_oom);
1831}
1832
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001833static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001834{
1835 struct mem_cgroup *iter;
1836
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001837 /*
1838 * When a new child is created while the hierarchy is under oom,
1839 * mem_cgroup_oom_lock() may not be called. We have to use
1840 * atomic_add_unless() here.
1841 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001842 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001843 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001844}
1845
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001846static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1847
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001848struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001849 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001850 wait_queue_t wait;
1851};
1852
1853static int memcg_oom_wake_function(wait_queue_t *wait,
1854 unsigned mode, int sync, void *arg)
1855{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001856 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1857 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001858 struct oom_wait_info *oom_wait_info;
1859
1860 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001861 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001862
Johannes Weiner2314b422014-12-10 15:44:33 -08001863 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1864 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001865 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001866 return autoremove_wake_function(wait, mode, sync, arg);
1867}
1868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001869static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001870{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001871 if (memcg && atomic_read(&memcg->under_oom))
Tejun Heo55c3f9b2015-06-24 16:58:21 -07001872 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001873}
1874
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001875static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001876{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001877 if (!current->memcg_oom.may_oom)
1878 return;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001879 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001880 * We are in the middle of the charge context here, so we
1881 * don't want to block when potentially sitting on a callstack
1882 * that holds all kinds of filesystem and mm locks.
1883 *
1884 * Also, the caller may handle a failed allocation gracefully
1885 * (like optional page cache readahead) and so an OOM killer
1886 * invocation might not even be necessary.
1887 *
1888 * That's why we don't do anything here except remember the
1889 * OOM context and then deal with it at the end of the page
1890 * fault when the stack is unwound, the locks are released,
1891 * and when we know whether the fault was overall successful.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001892 */
Johannes Weiner49426422013-10-16 13:46:59 -07001893 css_get(&memcg->css);
1894 current->memcg_oom.memcg = memcg;
1895 current->memcg_oom.gfp_mask = mask;
1896 current->memcg_oom.order = order;
1897}
1898
1899/**
1900 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1901 * @handle: actually kill/wait or just clean up the OOM state
1902 *
1903 * This has to be called at the end of a page fault if the memcg OOM
1904 * handler was enabled.
1905 *
1906 * Memcg supports userspace OOM handling where failed allocations must
1907 * sleep on a waitqueue until the userspace task resolves the
1908 * situation. Sleeping directly in the charge context with all kinds
1909 * of locks held is not a good idea, instead we remember an OOM state
1910 * in the task and mem_cgroup_oom_synchronize() has to be called at
1911 * the end of the page fault to complete the OOM handling.
1912 *
1913 * Returns %true if an ongoing memcg OOM situation was detected and
1914 * completed, %false otherwise.
1915 */
1916bool mem_cgroup_oom_synchronize(bool handle)
1917{
1918 struct mem_cgroup *memcg = current->memcg_oom.memcg;
1919 struct oom_wait_info owait;
1920 bool locked;
1921
1922 /* OOM is global, do not handle */
1923 if (!memcg)
1924 return false;
1925
Michal Hockoc32b3cb2015-02-11 15:26:24 -08001926 if (!handle || oom_killer_disabled)
Johannes Weiner49426422013-10-16 13:46:59 -07001927 goto cleanup;
1928
1929 owait.memcg = memcg;
1930 owait.wait.flags = 0;
1931 owait.wait.func = memcg_oom_wake_function;
1932 owait.wait.private = current;
1933 INIT_LIST_HEAD(&owait.wait.task_list);
1934
1935 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001936 mem_cgroup_mark_under_oom(memcg);
1937
1938 locked = mem_cgroup_oom_trylock(memcg);
1939
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001940 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001941 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001942
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001943 if (locked && !memcg->oom_kill_disable) {
1944 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07001945 finish_wait(&memcg_oom_waitq, &owait.wait);
1946 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
1947 current->memcg_oom.order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001948 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001949 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07001950 mem_cgroup_unmark_under_oom(memcg);
1951 finish_wait(&memcg_oom_waitq, &owait.wait);
1952 }
1953
1954 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001955 mem_cgroup_oom_unlock(memcg);
1956 /*
1957 * There is no guarantee that an OOM-lock contender
1958 * sees the wakeups triggered by the OOM kill
1959 * uncharges. Wake any sleepers explicitely.
1960 */
1961 memcg_oom_recover(memcg);
1962 }
Johannes Weiner49426422013-10-16 13:46:59 -07001963cleanup:
1964 current->memcg_oom.memcg = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001965 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001966 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001967}
1968
Johannes Weinerd7365e72014-10-29 14:50:48 -07001969/**
1970 * mem_cgroup_begin_page_stat - begin a page state statistics transaction
1971 * @page: page that is going to change accounted state
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001972 *
Johannes Weinerd7365e72014-10-29 14:50:48 -07001973 * This function must mark the beginning of an accounted page state
1974 * change to prevent double accounting when the page is concurrently
1975 * being moved to another memcg:
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001976 *
Johannes Weiner6de22612015-02-11 15:25:01 -08001977 * memcg = mem_cgroup_begin_page_stat(page);
Johannes Weinerd7365e72014-10-29 14:50:48 -07001978 * if (TestClearPageState(page))
1979 * mem_cgroup_update_page_stat(memcg, state, -1);
Johannes Weiner6de22612015-02-11 15:25:01 -08001980 * mem_cgroup_end_page_stat(memcg);
Balbir Singhd69b0422009-06-17 16:26:34 -07001981 */
Johannes Weiner6de22612015-02-11 15:25:01 -08001982struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001983{
1984 struct mem_cgroup *memcg;
Johannes Weiner6de22612015-02-11 15:25:01 -08001985 unsigned long flags;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001986
Johannes Weiner6de22612015-02-11 15:25:01 -08001987 /*
1988 * The RCU lock is held throughout the transaction. The fast
1989 * path can get away without acquiring the memcg->move_lock
1990 * because page moving starts with an RCU grace period.
1991 *
1992 * The RCU lock also protects the memcg from being freed when
1993 * the page state that is going to change is the only thing
1994 * preventing the page from being uncharged.
1995 * E.g. end-writeback clearing PageWriteback(), which allows
1996 * migration to go ahead and uncharge the page before the
1997 * account transaction might be complete.
1998 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07001999 rcu_read_lock();
2000
2001 if (mem_cgroup_disabled())
2002 return NULL;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002003again:
Johannes Weiner1306a852014-12-10 15:44:52 -08002004 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002005 if (unlikely(!memcg))
Johannes Weinerd7365e72014-10-29 14:50:48 -07002006 return NULL;
2007
Qiang Huangbdcbb652014-06-04 16:08:21 -07002008 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weinerd7365e72014-10-29 14:50:48 -07002009 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002010
Johannes Weiner6de22612015-02-11 15:25:01 -08002011 spin_lock_irqsave(&memcg->move_lock, flags);
Johannes Weiner1306a852014-12-10 15:44:52 -08002012 if (memcg != page->mem_cgroup) {
Johannes Weiner6de22612015-02-11 15:25:01 -08002013 spin_unlock_irqrestore(&memcg->move_lock, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002014 goto again;
2015 }
Johannes Weiner6de22612015-02-11 15:25:01 -08002016
2017 /*
2018 * When charge migration first begins, we can have locked and
2019 * unlocked page stat updates happening concurrently. Track
2020 * the task who has the lock for mem_cgroup_end_page_stat().
2021 */
2022 memcg->move_lock_task = current;
2023 memcg->move_lock_flags = flags;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002024
2025 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002026}
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04002027EXPORT_SYMBOL(mem_cgroup_begin_page_stat);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002028
Johannes Weinerd7365e72014-10-29 14:50:48 -07002029/**
2030 * mem_cgroup_end_page_stat - finish a page state statistics transaction
2031 * @memcg: the memcg that was accounted against
Johannes Weinerd7365e72014-10-29 14:50:48 -07002032 */
Johannes Weiner6de22612015-02-11 15:25:01 -08002033void mem_cgroup_end_page_stat(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002034{
Johannes Weiner6de22612015-02-11 15:25:01 -08002035 if (memcg && memcg->move_lock_task == current) {
2036 unsigned long flags = memcg->move_lock_flags;
2037
2038 memcg->move_lock_task = NULL;
2039 memcg->move_lock_flags = 0;
2040
2041 spin_unlock_irqrestore(&memcg->move_lock, flags);
2042 }
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002043
Johannes Weinerd7365e72014-10-29 14:50:48 -07002044 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002045}
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04002046EXPORT_SYMBOL(mem_cgroup_end_page_stat);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002047
Johannes Weinerd7365e72014-10-29 14:50:48 -07002048/**
2049 * mem_cgroup_update_page_stat - update page state statistics
2050 * @memcg: memcg to account against
2051 * @idx: page state item to account
2052 * @val: number of pages (positive or negative)
2053 *
2054 * See mem_cgroup_begin_page_stat() for locking requirements.
2055 */
2056void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
Sha Zhengju68b48762013-09-12 15:13:50 -07002057 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002058{
Sha Zhengju658b72c2013-09-12 15:13:52 -07002059 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002060
Johannes Weinerd7365e72014-10-29 14:50:48 -07002061 if (memcg)
2062 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002063}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002064
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002065/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002066 * size of first charge trial. "32" comes from vmscan.c's magic value.
2067 * TODO: maybe necessary to use big numbers in big irons.
2068 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002069#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002070struct memcg_stock_pcp {
2071 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002072 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002073 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002074 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002075#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002076};
2077static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002078static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002079
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002080/**
2081 * consume_stock: Try to consume stocked charge on this cpu.
2082 * @memcg: memcg to consume from.
2083 * @nr_pages: how many pages to charge.
2084 *
2085 * The charges will only happen if @memcg matches the current cpu's memcg
2086 * stock, and at least @nr_pages are available in that stock. Failure to
2087 * service an allocation will refill the stock.
2088 *
2089 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002090 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002091static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002092{
2093 struct memcg_stock_pcp *stock;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002094 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002095
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002096 if (nr_pages > CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002097 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002098
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002099 stock = &get_cpu_var(memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002100 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002101 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002102 ret = true;
2103 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002104 put_cpu_var(memcg_stock);
2105 return ret;
2106}
2107
2108/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002109 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002110 */
2111static void drain_stock(struct memcg_stock_pcp *stock)
2112{
2113 struct mem_cgroup *old = stock->cached;
2114
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002115 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002116 page_counter_uncharge(&old->memory, stock->nr_pages);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002117 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002118 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002119 css_put_many(&old->css, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002120 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002121 }
2122 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002123}
2124
2125/*
2126 * This must be called under preempt disabled or must be called by
2127 * a thread which is pinned to local cpu.
2128 */
2129static void drain_local_stock(struct work_struct *dummy)
2130{
Christoph Lameter7c8e0182014-06-04 16:07:56 -07002131 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002132 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002133 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002134}
2135
2136/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002137 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002138 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002139 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002140static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002141{
2142 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2143
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002144 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002145 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002146 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002147 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002148 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002149 put_cpu_var(memcg_stock);
2150}
2151
2152/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002153 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002154 * of the hierarchy under it.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002155 */
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002156static void drain_all_stock(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002157{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002158 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002159
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002160 /* If someone's already draining, avoid adding running more workers. */
2161 if (!mutex_trylock(&percpu_charge_mutex))
2162 return;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002163 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002165 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002166 for_each_online_cpu(cpu) {
2167 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002168 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002169
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002170 memcg = stock->cached;
2171 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002172 continue;
Johannes Weiner2314b422014-12-10 15:44:33 -08002173 if (!mem_cgroup_is_descendant(memcg, root_memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002174 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002175 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2176 if (cpu == curcpu)
2177 drain_local_stock(&stock->work);
2178 else
2179 schedule_work_on(cpu, &stock->work);
2180 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002181 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002182 put_cpu();
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002183 put_online_cpus();
Michal Hocko9f50fad2011-08-09 11:56:26 +02002184 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002185}
2186
Paul Gortmaker0db06282013-06-19 14:53:51 -04002187static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002188 unsigned long action,
2189 void *hcpu)
2190{
2191 int cpu = (unsigned long)hcpu;
2192 struct memcg_stock_pcp *stock;
2193
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002194 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002195 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002196
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002197 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002198 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002199
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200 stock = &per_cpu(memcg_stock, cpu);
2201 drain_stock(stock);
2202 return NOTIFY_OK;
2203}
2204
Johannes Weiner00501b52014-08-08 14:19:20 -07002205static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2206 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002207{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002208 unsigned int batch = max(CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002209 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002210 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002211 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002212 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002213 bool may_swap = true;
2214 bool drained = false;
Johannes Weiner05b84302014-08-06 16:05:59 -07002215 int ret = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002216
Johannes Weinerce00a962014-09-05 08:43:57 -04002217 if (mem_cgroup_is_root(memcg))
2218 goto done;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002219retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002220 if (consume_stock(memcg, nr_pages))
2221 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002222
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002223 if (!do_swap_account ||
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002224 !page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2225 if (!page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002226 goto done_restock;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002227 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002228 page_counter_uncharge(&memcg->memsw, batch);
2229 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002230 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002231 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002232 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002233 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002234
Johannes Weiner6539cc02014-08-06 16:05:42 -07002235 if (batch > nr_pages) {
2236 batch = nr_pages;
2237 goto retry;
2238 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002239
Johannes Weiner06b078f2014-08-06 16:05:44 -07002240 /*
2241 * Unlike in global OOM situations, memcg is not in a physical
2242 * memory shortage. Allow dying and OOM-killed tasks to
2243 * bypass the last charges so that they can exit quickly and
2244 * free their memory.
2245 */
2246 if (unlikely(test_thread_flag(TIF_MEMDIE) ||
2247 fatal_signal_pending(current) ||
2248 current->flags & PF_EXITING))
2249 goto bypass;
2250
2251 if (unlikely(task_in_memcg_oom(current)))
2252 goto nomem;
2253
Johannes Weiner6539cc02014-08-06 16:05:42 -07002254 if (!(gfp_mask & __GFP_WAIT))
2255 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002256
Johannes Weiner241994ed2015-02-11 15:26:06 -08002257 mem_cgroup_events(mem_over_limit, MEMCG_MAX, 1);
2258
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002259 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2260 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002261
Johannes Weiner61e02c72014-08-06 16:08:16 -07002262 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002263 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002264
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002265 if (!drained) {
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002266 drain_all_stock(mem_over_limit);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002267 drained = true;
2268 goto retry;
2269 }
2270
Johannes Weiner28c34c22014-08-06 16:05:47 -07002271 if (gfp_mask & __GFP_NORETRY)
2272 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002273 /*
2274 * Even though the limit is exceeded at this point, reclaim
2275 * may have been able to free some pages. Retry the charge
2276 * before killing the task.
2277 *
2278 * Only for regular pages, though: huge pages are rather
2279 * unlikely to succeed so close to the limit, and we fall back
2280 * to regular pages anyway in case of failure.
2281 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002282 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002283 goto retry;
2284 /*
2285 * At task move, charge accounts can be doubly counted. So, it's
2286 * better to wait until the end of task_move if something is going on.
2287 */
2288 if (mem_cgroup_wait_acct_move(mem_over_limit))
2289 goto retry;
2290
Johannes Weiner9b130612014-08-06 16:05:51 -07002291 if (nr_retries--)
2292 goto retry;
2293
Johannes Weiner06b078f2014-08-06 16:05:44 -07002294 if (gfp_mask & __GFP_NOFAIL)
2295 goto bypass;
2296
Johannes Weiner6539cc02014-08-06 16:05:42 -07002297 if (fatal_signal_pending(current))
2298 goto bypass;
2299
Johannes Weiner241994ed2015-02-11 15:26:06 -08002300 mem_cgroup_events(mem_over_limit, MEMCG_OOM, 1);
2301
Johannes Weiner61e02c72014-08-06 16:08:16 -07002302 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(nr_pages));
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002303nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002304 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002305 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002306bypass:
Johannes Weinerce00a962014-09-05 08:43:57 -04002307 return -EINTR;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002308
2309done_restock:
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002310 css_get_many(&memcg->css, batch);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002311 if (batch > nr_pages)
2312 refill_stock(memcg, batch - nr_pages);
Vladimir Davydov7d638092015-06-10 11:14:46 -07002313 if (!(gfp_mask & __GFP_WAIT))
2314 goto done;
Johannes Weiner241994ed2015-02-11 15:26:06 -08002315 /*
2316 * If the hierarchy is above the normal consumption range,
2317 * make the charging task trim their excess contribution.
2318 */
2319 do {
2320 if (page_counter_read(&memcg->memory) <= memcg->high)
2321 continue;
2322 mem_cgroup_events(memcg, MEMCG_HIGH, 1);
2323 try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true);
2324 } while ((memcg = parent_mem_cgroup(memcg)));
Johannes Weiner6539cc02014-08-06 16:05:42 -07002325done:
Johannes Weiner05b84302014-08-06 16:05:59 -07002326 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002327}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002328
Johannes Weiner00501b52014-08-08 14:19:20 -07002329static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002330{
Johannes Weinerce00a962014-09-05 08:43:57 -04002331 if (mem_cgroup_is_root(memcg))
2332 return;
2333
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002334 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner05b84302014-08-06 16:05:59 -07002335 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002336 page_counter_uncharge(&memcg->memsw, nr_pages);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002337
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002338 css_put_many(&memcg->css, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002339}
2340
2341/*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002342 * try_get_mem_cgroup_from_page - look up page's memcg association
2343 * @page: the page
2344 *
2345 * Look up, get a css reference, and return the memcg that owns @page.
2346 *
2347 * The page must be locked to prevent racing with swap-in and page
2348 * cache charges. If coming from an unlocked page table, the caller
2349 * must ensure the page is on the LRU or this can race with charging.
2350 */
Wu Fengguange42d9d52009-12-16 12:19:59 +01002351struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002352{
Johannes Weiner29833312014-12-10 15:44:02 -08002353 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002354 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002355 swp_entry_t ent;
2356
Sasha Levin309381fea2014-01-23 15:52:54 -08002357 VM_BUG_ON_PAGE(!PageLocked(page), page);
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002358
Johannes Weiner1306a852014-12-10 15:44:52 -08002359 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002360 if (memcg) {
2361 if (!css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002362 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002363 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002364 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002365 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002366 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07002367 memcg = mem_cgroup_from_id(id);
Tejun Heoec903c02014-05-13 12:11:01 -04002368 if (memcg && !css_tryget_online(&memcg->css))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002369 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002370 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002371 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002372 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002373}
2374
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002375static void lock_page_lru(struct page *page, int *isolated)
2376{
2377 struct zone *zone = page_zone(page);
2378
2379 spin_lock_irq(&zone->lru_lock);
2380 if (PageLRU(page)) {
2381 struct lruvec *lruvec;
2382
2383 lruvec = mem_cgroup_page_lruvec(page, zone);
2384 ClearPageLRU(page);
2385 del_page_from_lru_list(page, lruvec, page_lru(page));
2386 *isolated = 1;
2387 } else
2388 *isolated = 0;
2389}
2390
2391static void unlock_page_lru(struct page *page, int isolated)
2392{
2393 struct zone *zone = page_zone(page);
2394
2395 if (isolated) {
2396 struct lruvec *lruvec;
2397
2398 lruvec = mem_cgroup_page_lruvec(page, zone);
2399 VM_BUG_ON_PAGE(PageLRU(page), page);
2400 SetPageLRU(page);
2401 add_page_to_lru_list(page, lruvec, page_lru(page));
2402 }
2403 spin_unlock_irq(&zone->lru_lock);
2404}
2405
Johannes Weiner00501b52014-08-08 14:19:20 -07002406static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002407 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002408{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002409 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002410
Johannes Weiner1306a852014-12-10 15:44:52 -08002411 VM_BUG_ON_PAGE(page->mem_cgroup, page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002412
2413 /*
2414 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2415 * may already be on some other mem_cgroup's LRU. Take care of it.
2416 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002417 if (lrucare)
2418 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002419
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002420 /*
2421 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08002422 * page->mem_cgroup at this point:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002423 *
2424 * - the page is uncharged
2425 *
2426 * - the page is off-LRU
2427 *
2428 * - an anonymous fault has exclusive page access, except for
2429 * a locked page table
2430 *
2431 * - a page cache insertion, a swapin fault, or a migration
2432 * have the page locked
2433 */
Johannes Weiner1306a852014-12-10 15:44:52 -08002434 page->mem_cgroup = memcg;
Hugh Dickins3be91272008-02-07 00:14:19 -08002435
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002436 if (lrucare)
2437 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002438}
2439
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002440#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002441int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
2442 unsigned long nr_pages)
Glauber Costa749c5412012-12-18 14:23:01 -08002443{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002444 struct page_counter *counter;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002445 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002446
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002447 ret = page_counter_try_charge(&memcg->kmem, nr_pages, &counter);
2448 if (ret < 0)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002449 return ret;
2450
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002451 ret = try_charge(memcg, gfp, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002452 if (ret == -EINTR) {
2453 /*
Johannes Weiner00501b52014-08-08 14:19:20 -07002454 * try_charge() chose to bypass to root due to OOM kill or
2455 * fatal signal. Since our only options are to either fail
2456 * the allocation or charge it to this cgroup, do it as a
2457 * temporary condition. But we can't fail. From a kmem/slab
2458 * perspective, the cache has already been selected, by
2459 * mem_cgroup_kmem_get_cache(), so it is too late to change
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002460 * our minds.
2461 *
2462 * This condition will only trigger if the task entered
Johannes Weiner00501b52014-08-08 14:19:20 -07002463 * memcg_charge_kmem in a sane state, but was OOM-killed
2464 * during try_charge() above. Tasks that were already dying
2465 * when the allocation triggers should have been already
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002466 * directed to the root cgroup in memcontrol.h
2467 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002468 page_counter_charge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002469 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002470 page_counter_charge(&memcg->memsw, nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002471 css_get_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002472 ret = 0;
2473 } else if (ret)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002474 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002475
2476 return ret;
2477}
2478
Vladimir Davydovdbf22eb2015-02-10 14:11:41 -08002479void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002480{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002481 page_counter_uncharge(&memcg->memory, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002482 if (do_swap_account)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002483 page_counter_uncharge(&memcg->memsw, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002484
Johannes Weiner64f21992014-12-10 15:42:45 -08002485 page_counter_uncharge(&memcg->kmem, nr_pages);
Glauber Costa7de37682012-12-18 14:22:07 -08002486
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002487 css_put_many(&memcg->css, nr_pages);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002488}
2489
Glauber Costa2633d7a2012-12-18 14:22:34 -08002490/*
2491 * helper for acessing a memcg's index. It will be used as an index in the
2492 * child cache array in kmem_cache, and also to derive its name. This function
2493 * will return -1 when this is not a kmem-limited memcg.
2494 */
2495int memcg_cache_id(struct mem_cgroup *memcg)
2496{
2497 return memcg ? memcg->kmemcg_id : -1;
2498}
2499
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002500static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002501{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002502 int id, size;
2503 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002504
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002505 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002506 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2507 if (id < 0)
2508 return id;
2509
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002510 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002511 return id;
2512
2513 /*
2514 * There's no space for the new id in memcg_caches arrays,
2515 * so we have to grow them.
2516 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002517 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002518
2519 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002520 if (size < MEMCG_CACHES_MIN_SIZE)
2521 size = MEMCG_CACHES_MIN_SIZE;
2522 else if (size > MEMCG_CACHES_MAX_SIZE)
2523 size = MEMCG_CACHES_MAX_SIZE;
2524
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002525 err = memcg_update_all_caches(size);
Vladimir Davydov05257a12015-02-12 14:59:01 -08002526 if (!err)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002527 err = memcg_update_all_list_lrus(size);
2528 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002529 memcg_nr_cache_ids = size;
2530
2531 up_write(&memcg_cache_ids_sem);
2532
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002533 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002534 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002535 return err;
2536 }
2537 return id;
2538}
2539
2540static void memcg_free_cache_id(int id)
2541{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002542 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002543}
2544
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002545struct memcg_kmem_cache_create_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07002546 struct mem_cgroup *memcg;
2547 struct kmem_cache *cachep;
2548 struct work_struct work;
2549};
2550
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002551static void memcg_kmem_cache_create_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08002552{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002553 struct memcg_kmem_cache_create_work *cw =
2554 container_of(w, struct memcg_kmem_cache_create_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07002555 struct mem_cgroup *memcg = cw->memcg;
2556 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002557
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002558 memcg_create_kmem_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002559
Vladimir Davydov5722d092014-04-07 15:39:24 -07002560 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002561 kfree(cw);
2562}
2563
2564/*
2565 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002566 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002567static void __memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2568 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002569{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002570 struct memcg_kmem_cache_create_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08002571
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002572 cw = kmalloc(sizeof(*cw), GFP_NOWAIT);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002573 if (!cw)
Glauber Costad7f25f82012-12-18 14:22:40 -08002574 return;
Vladimir Davydov8135be52014-12-12 16:56:38 -08002575
2576 css_get(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002577
2578 cw->memcg = memcg;
2579 cw->cachep = cachep;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002580 INIT_WORK(&cw->work, memcg_kmem_cache_create_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08002581
Glauber Costad7f25f82012-12-18 14:22:40 -08002582 schedule_work(&cw->work);
2583}
2584
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002585static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
2586 struct kmem_cache *cachep)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002587{
2588 /*
2589 * We need to stop accounting when we kmalloc, because if the
2590 * corresponding kmalloc cache is not yet created, the first allocation
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002591 * in __memcg_schedule_kmem_cache_create will recurse.
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002592 *
2593 * However, it is better to enclose the whole function. Depending on
2594 * the debugging options enabled, INIT_WORK(), for instance, can
2595 * trigger an allocation. This too, will make us recurse. Because at
2596 * this point we can't allow ourselves back into memcg_kmem_get_cache,
2597 * the safest choice is to do it like this, wrapping the whole function.
2598 */
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002599 current->memcg_kmem_skip_account = 1;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002600 __memcg_schedule_kmem_cache_create(memcg, cachep);
Vladimir Davydov6f185c22014-12-12 16:55:15 -08002601 current->memcg_kmem_skip_account = 0;
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002602}
Vladimir Davydovc67a8a62014-06-04 16:07:39 -07002603
Glauber Costad7f25f82012-12-18 14:22:40 -08002604/*
2605 * Return the kmem_cache we're supposed to use for a slab allocation.
2606 * We try to use the current memcg's version of the cache.
2607 *
2608 * If the cache does not exist yet, if we are the first user of it,
2609 * we either create it immediately, if possible, or create it asynchronously
2610 * in a workqueue.
2611 * In the latter case, we will let the current allocation go through with
2612 * the original cache.
2613 *
2614 * Can't be called in interrupt context or from kernel threads.
2615 * This function needs to be called with rcu_read_lock() held.
2616 */
Zhang Zhen056b7cc2014-12-12 16:55:38 -08002617struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002618{
2619 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08002620 struct kmem_cache *memcg_cachep;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002621 int kmemcg_id;
Glauber Costad7f25f82012-12-18 14:22:40 -08002622
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002623 VM_BUG_ON(!is_root_cache(cachep));
Glauber Costad7f25f82012-12-18 14:22:40 -08002624
Vladimir Davydov9d100c52014-12-12 16:54:53 -08002625 if (current->memcg_kmem_skip_account)
Glauber Costa0e9d92f2012-12-18 14:22:42 -08002626 return cachep;
2627
Vladimir Davydov8135be52014-12-12 16:56:38 -08002628 memcg = get_mem_cgroup_from_mm(current->mm);
Jason Low4db0c3c2015-04-15 16:14:08 -07002629 kmemcg_id = READ_ONCE(memcg->kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002630 if (kmemcg_id < 0)
Li Zefanca0dde92013-04-29 15:08:57 -07002631 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08002632
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002633 memcg_cachep = cache_from_memcg_idx(cachep, kmemcg_id);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002634 if (likely(memcg_cachep))
2635 return memcg_cachep;
Li Zefanca0dde92013-04-29 15:08:57 -07002636
2637 /*
2638 * If we are in a safe context (can wait, and not in interrupt
2639 * context), we could be be predictable and return right away.
2640 * This would guarantee that the allocation being performed
2641 * already belongs in the new cache.
2642 *
2643 * However, there are some clashes that can arrive from locking.
2644 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002645 * memcg_create_kmem_cache, this means no further allocation
2646 * could happen with the slab_mutex held. So it's better to
2647 * defer everything.
Li Zefanca0dde92013-04-29 15:08:57 -07002648 */
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002649 memcg_schedule_kmem_cache_create(memcg, cachep);
Li Zefanca0dde92013-04-29 15:08:57 -07002650out:
Vladimir Davydov8135be52014-12-12 16:56:38 -08002651 css_put(&memcg->css);
Li Zefanca0dde92013-04-29 15:08:57 -07002652 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002653}
Glauber Costad7f25f82012-12-18 14:22:40 -08002654
Vladimir Davydov8135be52014-12-12 16:56:38 -08002655void __memcg_kmem_put_cache(struct kmem_cache *cachep)
2656{
2657 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002658 css_put(&cachep->memcg_params.memcg->css);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002659}
2660
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002661/*
2662 * We need to verify if the allocation against current->mm->owner's memcg is
2663 * possible for the given order. But the page is not allocated yet, so we'll
2664 * need a further commit step to do the final arrangements.
2665 *
2666 * It is possible for the task to switch cgroups in this mean time, so at
2667 * commit time, we can't rely on task conversion any longer. We'll then use
2668 * the handle argument to return to the caller which cgroup we should commit
2669 * against. We could also return the memcg directly and avoid the pointer
2670 * passing, but a boolean return value gives better semantics considering
2671 * the compiled-out case as well.
2672 *
2673 * Returning true means the allocation is possible.
2674 */
2675bool
2676__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2677{
2678 struct mem_cgroup *memcg;
2679 int ret;
2680
2681 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07002682
Johannes Weinerdf381972014-04-07 15:37:43 -07002683 memcg = get_mem_cgroup_from_mm(current->mm);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002684
Vladimir Davydovcf2b8fb2014-10-09 15:28:59 -07002685 if (!memcg_kmem_is_active(memcg)) {
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002686 css_put(&memcg->css);
2687 return true;
2688 }
2689
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002690 ret = memcg_charge_kmem(memcg, gfp, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002691 if (!ret)
2692 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002693
2694 css_put(&memcg->css);
2695 return (ret == 0);
2696}
2697
2698void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2699 int order)
2700{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002701 VM_BUG_ON(mem_cgroup_is_root(memcg));
2702
2703 /* The page allocation failed. Revert */
2704 if (!page) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002705 memcg_uncharge_kmem(memcg, 1 << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002706 return;
2707 }
Johannes Weiner1306a852014-12-10 15:44:52 -08002708 page->mem_cgroup = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002709}
2710
2711void __memcg_kmem_uncharge_pages(struct page *page, int order)
2712{
Johannes Weiner1306a852014-12-10 15:44:52 -08002713 struct mem_cgroup *memcg = page->mem_cgroup;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002714
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002715 if (!memcg)
2716 return;
2717
Sasha Levin309381fea2014-01-23 15:52:54 -08002718 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Johannes Weiner29833312014-12-10 15:44:02 -08002719
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002720 memcg_uncharge_kmem(memcg, 1 << order);
Johannes Weiner1306a852014-12-10 15:44:52 -08002721 page->mem_cgroup = NULL;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002722}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002723
2724struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr)
2725{
2726 struct mem_cgroup *memcg = NULL;
2727 struct kmem_cache *cachep;
2728 struct page *page;
2729
2730 page = virt_to_head_page(ptr);
2731 if (PageSlab(page)) {
2732 cachep = page->slab_cache;
2733 if (!is_root_cache(cachep))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002734 memcg = cachep->memcg_params.memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002735 } else
2736 /* page allocated by alloc_kmem_pages */
2737 memcg = page->mem_cgroup;
2738
2739 return memcg;
2740}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002741#endif /* CONFIG_MEMCG_KMEM */
2742
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002743#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2744
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002745/*
2746 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002747 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2748 * charge/uncharge will be never happen and move_account() is done under
2749 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002750 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002751void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002752{
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002753 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002754
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002755 if (mem_cgroup_disabled())
2756 return;
David Rientjesb070e652013-05-07 16:18:09 -07002757
Johannes Weiner29833312014-12-10 15:44:02 -08002758 for (i = 1; i < HPAGE_PMD_NR; i++)
Johannes Weiner1306a852014-12-10 15:44:52 -08002759 head[i].mem_cgroup = head->mem_cgroup;
Michal Hockob9982f82014-12-10 15:43:51 -08002760
Johannes Weiner1306a852014-12-10 15:44:52 -08002761 __this_cpu_sub(head->mem_cgroup->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
David Rientjesb070e652013-05-07 16:18:09 -07002762 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002763}
Hugh Dickins12d27102012-01-12 17:19:52 -08002764#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002765
Andrew Mortonc255a452012-07-31 16:43:02 -07002766#ifdef CONFIG_MEMCG_SWAP
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002767static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
2768 bool charge)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002769{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002770 int val = (charge) ? 1 : -1;
2771 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002772}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002773
2774/**
2775 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2776 * @entry: swap entry to be moved
2777 * @from: mem_cgroup which the entry is moved from
2778 * @to: mem_cgroup which the entry is moved to
2779 *
2780 * It succeeds only when the swap_cgroup's record for this entry is the same
2781 * as the mem_cgroup's id of @from.
2782 *
2783 * Returns 0 on success, -EINVAL on failure.
2784 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002785 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08002786 * both res and memsw, and called css_get().
2787 */
2788static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002789 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002790{
2791 unsigned short old_id, new_id;
2792
Li Zefan34c00c32013-09-23 16:56:01 +08002793 old_id = mem_cgroup_id(from);
2794 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002795
2796 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002797 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002798 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002799 return 0;
2800 }
2801 return -EINVAL;
2802}
2803#else
2804static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002805 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002806{
2807 return -EINVAL;
2808}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002809#endif
2810
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002811static DEFINE_MUTEX(memcg_limit_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07002812
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002813static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002814 unsigned long limit)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002815{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002816 unsigned long curusage;
2817 unsigned long oldusage;
2818 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002819 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002820 int ret;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002821
2822 /*
2823 * For keeping hierarchical_reclaim simple, how long we should retry
2824 * is depends on callers. We set our retry-count to be function
2825 * of # of children which we should visit in this loop.
2826 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002827 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2828 mem_cgroup_count_children(memcg);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002829
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002830 oldusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002831
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002832 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002833 if (signal_pending(current)) {
2834 ret = -EINTR;
2835 break;
2836 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002837
2838 mutex_lock(&memcg_limit_mutex);
2839 if (limit > memcg->memsw.limit) {
2840 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002841 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002842 break;
2843 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002844 if (limit > memcg->memory.limit)
2845 enlarge = true;
2846 ret = page_counter_limit(&memcg->memory, limit);
2847 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002848
2849 if (!ret)
2850 break;
2851
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002852 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, true);
2853
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002854 curusage = page_counter_read(&memcg->memory);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002855 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002856 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002857 retry_count--;
2858 else
2859 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002860 } while (retry_count);
2861
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002862 if (!ret && enlarge)
2863 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002864
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002865 return ret;
2866}
2867
Li Zefan338c8432009-06-17 16:27:15 -07002868static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002869 unsigned long limit)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002870{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002871 unsigned long curusage;
2872 unsigned long oldusage;
2873 bool enlarge = false;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002874 int retry_count;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002875 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002876
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002877 /* see mem_cgroup_resize_res_limit */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002878 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
2879 mem_cgroup_count_children(memcg);
2880
2881 oldusage = page_counter_read(&memcg->memsw);
2882
2883 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002884 if (signal_pending(current)) {
2885 ret = -EINTR;
2886 break;
2887 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002888
2889 mutex_lock(&memcg_limit_mutex);
2890 if (limit < memcg->memory.limit) {
2891 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002892 ret = -EINVAL;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002893 break;
2894 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002895 if (limit > memcg->memsw.limit)
2896 enlarge = true;
2897 ret = page_counter_limit(&memcg->memsw, limit);
2898 mutex_unlock(&memcg_limit_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002899
2900 if (!ret)
2901 break;
2902
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002903 try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, false);
2904
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002905 curusage = page_counter_read(&memcg->memsw);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002906 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002907 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002908 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002909 else
2910 oldusage = curusage;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002911 } while (retry_count);
2912
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002913 if (!ret && enlarge)
2914 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002915
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002916 return ret;
2917}
2918
Andrew Morton0608f432013-09-24 15:27:41 -07002919unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2920 gfp_t gfp_mask,
2921 unsigned long *total_scanned)
2922{
2923 unsigned long nr_reclaimed = 0;
2924 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2925 unsigned long reclaimed;
2926 int loop = 0;
2927 struct mem_cgroup_tree_per_zone *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002928 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07002929 unsigned long nr_scanned;
2930
2931 if (order > 0)
2932 return 0;
2933
2934 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
2935 /*
2936 * This loop can run a while, specially if mem_cgroup's continuously
2937 * keep exceeding their soft limit and putting the system under
2938 * pressure
2939 */
2940 do {
2941 if (next_mz)
2942 mz = next_mz;
2943 else
2944 mz = mem_cgroup_largest_soft_limit_node(mctz);
2945 if (!mz)
2946 break;
2947
2948 nr_scanned = 0;
2949 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
2950 gfp_mask, &nr_scanned);
2951 nr_reclaimed += reclaimed;
2952 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002953 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08002954 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07002955
2956 /*
2957 * If we failed to reclaim anything from this memory cgroup
2958 * it is time to move on to the next cgroup
2959 */
2960 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08002961 if (!reclaimed)
2962 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
2963
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002964 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07002965 /*
2966 * One school of thought says that we should not add
2967 * back the node to the tree if reclaim returns 0.
2968 * But our reclaim could return 0, simply because due
2969 * to priority we are exposing a smaller subset of
2970 * memory to reclaim from. Consider this as a longer
2971 * term TODO.
2972 */
2973 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07002974 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002975 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07002976 css_put(&mz->memcg->css);
2977 loop++;
2978 /*
2979 * Could not reclaim anything and there are no more
2980 * mem cgroups to try or we seem to be looping without
2981 * reclaiming anything.
2982 */
2983 if (!nr_reclaimed &&
2984 (next_mz == NULL ||
2985 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2986 break;
2987 } while (!nr_reclaimed);
2988 if (next_mz)
2989 css_put(&next_mz->memcg->css);
2990 return nr_reclaimed;
2991}
2992
Tejun Heoea280e72014-05-16 13:22:48 -04002993/*
2994 * Test whether @memcg has children, dead or alive. Note that this
2995 * function doesn't care whether @memcg has use_hierarchy enabled and
2996 * returns %true if there are child csses according to the cgroup
2997 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
2998 */
Glauber Costab5f99b52013-02-22 16:34:53 -08002999static inline bool memcg_has_children(struct mem_cgroup *memcg)
3000{
Tejun Heoea280e72014-05-16 13:22:48 -04003001 bool ret;
3002
Johannes Weiner696ac172013-10-31 16:34:15 -07003003 /*
Tejun Heoea280e72014-05-16 13:22:48 -04003004 * The lock does not prevent addition or deletion of children, but
3005 * it prevents a new child from being initialized based on this
3006 * parent in css_online(), so it's enough to decide whether
3007 * hierarchically inherited attributes can still be changed or not.
Johannes Weiner696ac172013-10-31 16:34:15 -07003008 */
Tejun Heoea280e72014-05-16 13:22:48 -04003009 lockdep_assert_held(&memcg_create_mutex);
3010
3011 rcu_read_lock();
3012 ret = css_next_child(NULL, &memcg->css);
3013 rcu_read_unlock();
3014 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003015}
3016
3017/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003018 * Reclaims as many pages from the given memcg as possible and moves
3019 * the rest to the parent.
3020 *
3021 * Caller is responsible for holding css reference for memcg.
3022 */
3023static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3024{
3025 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003026
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003027 /* we call try-to-free pages for make this cgroup empty */
3028 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003029 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003030 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003031 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003032
Michal Hockoc26251f2012-10-26 13:37:28 +02003033 if (signal_pending(current))
3034 return -EINTR;
3035
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003036 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3037 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003038 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003039 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003040 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003041 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003042 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003043
3044 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003045
3046 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003047}
3048
Tejun Heo6770c642014-05-13 12:16:21 -04003049static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3050 char *buf, size_t nbytes,
3051 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003052{
Tejun Heo6770c642014-05-13 12:16:21 -04003053 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003054
Michal Hockod8423012012-10-26 13:37:29 +02003055 if (mem_cgroup_is_root(memcg))
3056 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003057 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003058}
3059
Tejun Heo182446d2013-08-08 20:11:24 -04003060static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3061 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003062{
Tejun Heo182446d2013-08-08 20:11:24 -04003063 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003064}
3065
Tejun Heo182446d2013-08-08 20:11:24 -04003066static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3067 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003068{
3069 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003070 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003071 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003072
Glauber Costa09998212013-02-22 16:34:55 -08003073 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07003074
3075 if (memcg->use_hierarchy == val)
3076 goto out;
3077
Balbir Singh18f59ea2009-01-07 18:08:07 -08003078 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003079 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003080 * in the child subtrees. If it is unset, then the change can
3081 * occur, provided the current cgroup has no children.
3082 *
3083 * For the root cgroup, parent_mem is NULL, we allow value to be
3084 * set if there are no children.
3085 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003086 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003087 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003088 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003089 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003090 else
3091 retval = -EBUSY;
3092 } else
3093 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003094
3095out:
Glauber Costa09998212013-02-22 16:34:55 -08003096 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003097
3098 return retval;
3099}
3100
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003101static unsigned long tree_stat(struct mem_cgroup *memcg,
3102 enum mem_cgroup_stat_index idx)
Johannes Weinerce00a962014-09-05 08:43:57 -04003103{
3104 struct mem_cgroup *iter;
3105 long val = 0;
3106
3107 /* Per-cpu values can be negative, use a signed accumulator */
3108 for_each_mem_cgroup_tree(iter, memcg)
3109 val += mem_cgroup_read_stat(iter, idx);
3110
3111 if (val < 0) /* race ? */
3112 val = 0;
3113 return val;
3114}
3115
3116static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3117{
3118 u64 val;
3119
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003120 if (mem_cgroup_is_root(memcg)) {
3121 val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE);
3122 val += tree_stat(memcg, MEM_CGROUP_STAT_RSS);
3123 if (swap)
3124 val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP);
3125 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003126 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003127 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003128 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003129 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003130 }
Johannes Weinerce00a962014-09-05 08:43:57 -04003131 return val << PAGE_SHIFT;
3132}
3133
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003134enum {
3135 RES_USAGE,
3136 RES_LIMIT,
3137 RES_MAX_USAGE,
3138 RES_FAILCNT,
3139 RES_SOFT_LIMIT,
3140};
Johannes Weinerce00a962014-09-05 08:43:57 -04003141
Tejun Heo791badb2013-12-05 12:28:02 -05003142static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003143 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003144{
Tejun Heo182446d2013-08-08 20:11:24 -04003145 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003146 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003147
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003148 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003149 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003150 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003151 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003152 case _MEMSWAP:
3153 counter = &memcg->memsw;
3154 break;
3155 case _KMEM:
3156 counter = &memcg->kmem;
3157 break;
3158 default:
3159 BUG();
3160 }
3161
3162 switch (MEMFILE_ATTR(cft->private)) {
3163 case RES_USAGE:
3164 if (counter == &memcg->memory)
3165 return mem_cgroup_usage(memcg, false);
3166 if (counter == &memcg->memsw)
3167 return mem_cgroup_usage(memcg, true);
3168 return (u64)page_counter_read(counter) * PAGE_SIZE;
3169 case RES_LIMIT:
3170 return (u64)counter->limit * PAGE_SIZE;
3171 case RES_MAX_USAGE:
3172 return (u64)counter->watermark * PAGE_SIZE;
3173 case RES_FAILCNT:
3174 return counter->failcnt;
3175 case RES_SOFT_LIMIT:
3176 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003177 default:
3178 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003179 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003180}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003181
Glauber Costa510fc4e2012-12-18 14:21:47 -08003182#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003183static int memcg_activate_kmem(struct mem_cgroup *memcg,
3184 unsigned long nr_pages)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003185{
3186 int err = 0;
3187 int memcg_id;
3188
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003189 BUG_ON(memcg->kmemcg_id >= 0);
Vladimir Davydov2788cf0c2015-02-12 14:59:38 -08003190 BUG_ON(memcg->kmem_acct_activated);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003191 BUG_ON(memcg->kmem_acct_active);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003192
3193 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -08003194 * For simplicity, we won't allow this to be disabled. It also can't
3195 * be changed if the cgroup has children already, or if tasks had
3196 * already joined.
3197 *
3198 * If tasks join before we set the limit, a person looking at
3199 * kmem.usage_in_bytes will have no way to determine when it took
3200 * place, which makes the value quite meaningless.
3201 *
3202 * After it first became limited, changes in the value of the limit are
3203 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08003204 */
Glauber Costa09998212013-02-22 16:34:55 -08003205 mutex_lock(&memcg_create_mutex);
Tejun Heoea280e72014-05-16 13:22:48 -04003206 if (cgroup_has_tasks(memcg->css.cgroup) ||
3207 (memcg->use_hierarchy && memcg_has_children(memcg)))
Vladimir Davydovd6441632014-01-23 15:53:09 -08003208 err = -EBUSY;
Glauber Costa09998212013-02-22 16:34:55 -08003209 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003210 if (err)
3211 goto out;
3212
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003213 memcg_id = memcg_alloc_cache_id();
Vladimir Davydovd6441632014-01-23 15:53:09 -08003214 if (memcg_id < 0) {
3215 err = memcg_id;
3216 goto out;
3217 }
3218
Vladimir Davydovd6441632014-01-23 15:53:09 -08003219 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003220 * We couldn't have accounted to this cgroup, because it hasn't got
3221 * activated yet, so this should succeed.
Vladimir Davydovd6441632014-01-23 15:53:09 -08003222 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003223 err = page_counter_limit(&memcg->kmem, nr_pages);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003224 VM_BUG_ON(err);
3225
3226 static_key_slow_inc(&memcg_kmem_enabled_key);
3227 /*
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003228 * A memory cgroup is considered kmem-active as soon as it gets
3229 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003230 * guarantee no one starts accounting before all call sites are
3231 * patched.
3232 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003233 memcg->kmemcg_id = memcg_id;
Vladimir Davydov2788cf0c2015-02-12 14:59:38 -08003234 memcg->kmem_acct_activated = true;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003235 memcg->kmem_acct_active = true;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003236out:
Vladimir Davydovd6441632014-01-23 15:53:09 -08003237 return err;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003238}
3239
Vladimir Davydovd6441632014-01-23 15:53:09 -08003240static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003241 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003242{
3243 int ret;
3244
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003245 mutex_lock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003246 if (!memcg_kmem_is_active(memcg))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003247 ret = memcg_activate_kmem(memcg, limit);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003248 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003249 ret = page_counter_limit(&memcg->kmem, limit);
3250 mutex_unlock(&memcg_limit_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003251 return ret;
3252}
3253
Glauber Costa55007d82012-12-18 14:22:38 -08003254static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08003255{
Glauber Costa55007d82012-12-18 14:22:38 -08003256 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003257 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003258
Glauber Costa510fc4e2012-12-18 14:21:47 -08003259 if (!parent)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003260 return 0;
Glauber Costa55007d82012-12-18 14:22:38 -08003261
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003262 mutex_lock(&memcg_limit_mutex);
Glauber Costaa8964b92012-12-18 14:22:09 -08003263 /*
Vladimir Davydovd6441632014-01-23 15:53:09 -08003264 * If the parent cgroup is not kmem-active now, it cannot be activated
3265 * after this point, because it has at least one child already.
Glauber Costaa8964b92012-12-18 14:22:09 -08003266 */
Vladimir Davydovd6441632014-01-23 15:53:09 -08003267 if (memcg_kmem_is_active(parent))
Vladimir Davydov8c0145b2014-12-10 15:43:48 -08003268 ret = memcg_activate_kmem(memcg, PAGE_COUNTER_MAX);
3269 mutex_unlock(&memcg_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08003270 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003271}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003272#else
3273static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003274 unsigned long limit)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003275{
3276 return -EINVAL;
3277}
Hugh Dickins6d0439902013-02-22 16:35:50 -08003278#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08003279
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003280/*
3281 * The user of this function is...
3282 * RES_LIMIT.
3283 */
Tejun Heo451af502014-05-13 12:16:21 -04003284static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3285 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003286{
Tejun Heo451af502014-05-13 12:16:21 -04003287 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003288 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003289 int ret;
3290
Tejun Heo451af502014-05-13 12:16:21 -04003291 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003292 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003293 if (ret)
3294 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003295
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003296 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003297 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003298 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3299 ret = -EINVAL;
3300 break;
3301 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003302 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3303 case _MEM:
3304 ret = mem_cgroup_resize_limit(memcg, nr_pages);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003305 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003306 case _MEMSWAP:
3307 ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages);
3308 break;
3309 case _KMEM:
3310 ret = memcg_update_kmem_limit(memcg, nr_pages);
3311 break;
3312 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003313 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003314 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003315 memcg->soft_limit = nr_pages;
3316 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003317 break;
3318 }
Tejun Heo451af502014-05-13 12:16:21 -04003319 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003320}
3321
Tejun Heo6770c642014-05-13 12:16:21 -04003322static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3323 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003324{
Tejun Heo6770c642014-05-13 12:16:21 -04003325 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003326 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003327
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003328 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3329 case _MEM:
3330 counter = &memcg->memory;
3331 break;
3332 case _MEMSWAP:
3333 counter = &memcg->memsw;
3334 break;
3335 case _KMEM:
3336 counter = &memcg->kmem;
3337 break;
3338 default:
3339 BUG();
3340 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003341
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003342 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003343 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003344 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003345 break;
3346 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003347 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003348 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003349 default:
3350 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003351 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003352
Tejun Heo6770c642014-05-13 12:16:21 -04003353 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003354}
3355
Tejun Heo182446d2013-08-08 20:11:24 -04003356static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003357 struct cftype *cft)
3358{
Tejun Heo182446d2013-08-08 20:11:24 -04003359 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003360}
3361
Daisuke Nishimura02491442010-03-10 15:22:17 -08003362#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003363static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003364 struct cftype *cft, u64 val)
3365{
Tejun Heo182446d2013-08-08 20:11:24 -04003366 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003367
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003368 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003369 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003370
Glauber Costaee5e8472013-02-22 16:34:50 -08003371 /*
3372 * No kind of locking is needed in here, because ->can_attach() will
3373 * check this value once in the beginning of the process, and then carry
3374 * on with stale data. This means that changes to this value will only
3375 * affect task migrations starting after the change.
3376 */
3377 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003378 return 0;
3379}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003380#else
Tejun Heo182446d2013-08-08 20:11:24 -04003381static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003382 struct cftype *cft, u64 val)
3383{
3384 return -ENOSYS;
3385}
3386#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003387
Ying Han406eb0c2011-05-26 16:25:37 -07003388#ifdef CONFIG_NUMA
Tejun Heo2da8ca82013-12-05 12:28:04 -05003389static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003390{
Greg Thelen25485de2013-11-12 15:07:40 -08003391 struct numa_stat {
3392 const char *name;
3393 unsigned int lru_mask;
3394 };
3395
3396 static const struct numa_stat stats[] = {
3397 { "total", LRU_ALL },
3398 { "file", LRU_ALL_FILE },
3399 { "anon", LRU_ALL_ANON },
3400 { "unevictable", BIT(LRU_UNEVICTABLE) },
3401 };
3402 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003403 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08003404 unsigned long nr;
Tejun Heo2da8ca82013-12-05 12:28:04 -05003405 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Ying Han406eb0c2011-05-26 16:25:37 -07003406
Greg Thelen25485de2013-11-12 15:07:40 -08003407 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3408 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
3409 seq_printf(m, "%s=%lu", stat->name, nr);
3410 for_each_node_state(nid, N_MEMORY) {
3411 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
3412 stat->lru_mask);
3413 seq_printf(m, " N%d=%lu", nid, nr);
3414 }
3415 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003416 }
Ying Han406eb0c2011-05-26 16:25:37 -07003417
Ying Han071aee12013-11-12 15:07:41 -08003418 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3419 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07003420
Ying Han071aee12013-11-12 15:07:41 -08003421 nr = 0;
3422 for_each_mem_cgroup_tree(iter, memcg)
3423 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
3424 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
3425 for_each_node_state(nid, N_MEMORY) {
3426 nr = 0;
3427 for_each_mem_cgroup_tree(iter, memcg)
3428 nr += mem_cgroup_node_nr_lru_pages(
3429 iter, nid, stat->lru_mask);
3430 seq_printf(m, " N%d=%lu", nid, nr);
3431 }
3432 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003433 }
Ying Han406eb0c2011-05-26 16:25:37 -07003434
Ying Han406eb0c2011-05-26 16:25:37 -07003435 return 0;
3436}
3437#endif /* CONFIG_NUMA */
3438
Tejun Heo2da8ca82013-12-05 12:28:04 -05003439static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003440{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003441 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003442 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003443 struct mem_cgroup *mi;
3444 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003445
Greg Thelen0ca44b12015-02-11 15:25:58 -08003446 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_stat_names) !=
3447 MEM_CGROUP_STAT_NSTATS);
3448 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_events_names) !=
3449 MEM_CGROUP_EVENTS_NSTATS);
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003450 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
3451
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003452 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003453 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003454 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003455 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
3456 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003457 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003458
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003459 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
3460 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
3461 mem_cgroup_read_events(memcg, i));
3462
3463 for (i = 0; i < NR_LRU_LISTS; i++)
3464 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
3465 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
3466
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003467 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003468 memory = memsw = PAGE_COUNTER_MAX;
3469 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
3470 memory = min(memory, mi->memory.limit);
3471 memsw = min(memsw, mi->memsw.limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003472 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003473 seq_printf(m, "hierarchical_memory_limit %llu\n",
3474 (u64)memory * PAGE_SIZE);
3475 if (do_swap_account)
3476 seq_printf(m, "hierarchical_memsw_limit %llu\n",
3477 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003478
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003479 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
3480 long long val = 0;
3481
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003482 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003483 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003484 for_each_mem_cgroup_tree(mi, memcg)
3485 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
3486 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
3487 }
3488
3489 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
3490 unsigned long long val = 0;
3491
3492 for_each_mem_cgroup_tree(mi, memcg)
3493 val += mem_cgroup_read_events(mi, i);
3494 seq_printf(m, "total_%s %llu\n",
3495 mem_cgroup_events_names[i], val);
3496 }
3497
3498 for (i = 0; i < NR_LRU_LISTS; i++) {
3499 unsigned long long val = 0;
3500
3501 for_each_mem_cgroup_tree(mi, memcg)
3502 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
3503 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003504 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003505
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003506#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003507 {
3508 int nid, zid;
3509 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07003510 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003511 unsigned long recent_rotated[2] = {0, 0};
3512 unsigned long recent_scanned[2] = {0, 0};
3513
3514 for_each_online_node(nid)
3515 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Jianyu Zhane2318752014-06-06 14:38:20 -07003516 mz = &memcg->nodeinfo[nid]->zoneinfo[zid];
Hugh Dickins89abfab2012-05-29 15:06:53 -07003517 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003518
Hugh Dickins89abfab2012-05-29 15:06:53 -07003519 recent_rotated[0] += rstat->recent_rotated[0];
3520 recent_rotated[1] += rstat->recent_rotated[1];
3521 recent_scanned[0] += rstat->recent_scanned[0];
3522 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003523 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07003524 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
3525 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
3526 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
3527 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003528 }
3529#endif
3530
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003531 return 0;
3532}
3533
Tejun Heo182446d2013-08-08 20:11:24 -04003534static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
3535 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003536{
Tejun Heo182446d2013-08-08 20:11:24 -04003537 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003538
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07003539 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003540}
3541
Tejun Heo182446d2013-08-08 20:11:24 -04003542static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
3543 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003544{
Tejun Heo182446d2013-08-08 20:11:24 -04003545 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08003546
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003547 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003548 return -EINVAL;
3549
Linus Torvalds14208b02014-06-09 15:03:33 -07003550 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003551 memcg->swappiness = val;
3552 else
3553 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08003554
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003555 return 0;
3556}
3557
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003558static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3559{
3560 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003561 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003562 int i;
3563
3564 rcu_read_lock();
3565 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003566 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003567 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003568 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003569
3570 if (!t)
3571 goto unlock;
3572
Johannes Weinerce00a962014-09-05 08:43:57 -04003573 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003574
3575 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07003576 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003577 * If it's not true, a threshold was crossed after last
3578 * call of __mem_cgroup_threshold().
3579 */
Phil Carmody5407a562010-05-26 14:42:42 -07003580 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003581
3582 /*
3583 * Iterate backward over array of thresholds starting from
3584 * current_threshold and check if a threshold is crossed.
3585 * If none of thresholds below usage is crossed, we read
3586 * only one element of the array here.
3587 */
3588 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3589 eventfd_signal(t->entries[i].eventfd, 1);
3590
3591 /* i = current_threshold + 1 */
3592 i++;
3593
3594 /*
3595 * Iterate forward over array of thresholds starting from
3596 * current_threshold+1 and check if a threshold is crossed.
3597 * If none of thresholds above usage is crossed, we read
3598 * only one element of the array here.
3599 */
3600 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3601 eventfd_signal(t->entries[i].eventfd, 1);
3602
3603 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003604 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003605unlock:
3606 rcu_read_unlock();
3607}
3608
3609static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3610{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003611 while (memcg) {
3612 __mem_cgroup_threshold(memcg, false);
3613 if (do_swap_account)
3614 __mem_cgroup_threshold(memcg, true);
3615
3616 memcg = parent_mem_cgroup(memcg);
3617 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003618}
3619
3620static int compare_thresholds(const void *a, const void *b)
3621{
3622 const struct mem_cgroup_threshold *_a = a;
3623 const struct mem_cgroup_threshold *_b = b;
3624
Greg Thelen2bff24a2013-09-11 14:23:08 -07003625 if (_a->threshold > _b->threshold)
3626 return 1;
3627
3628 if (_a->threshold < _b->threshold)
3629 return -1;
3630
3631 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003632}
3633
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003634static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003635{
3636 struct mem_cgroup_eventfd_list *ev;
3637
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003638 spin_lock(&memcg_oom_lock);
3639
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003640 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003641 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003642
3643 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003644 return 0;
3645}
3646
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003647static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003648{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003649 struct mem_cgroup *iter;
3650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003651 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003652 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003653}
3654
Tejun Heo59b6f872013-11-22 18:20:43 -05003655static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003656 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003657{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003658 struct mem_cgroup_thresholds *thresholds;
3659 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003660 unsigned long threshold;
3661 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003662 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003663
Johannes Weiner650c5e52015-02-11 15:26:03 -08003664 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003665 if (ret)
3666 return ret;
Shaohua Li2105f9a2015-10-15 15:28:29 -07003667 threshold <<= PAGE_SHIFT;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003668
3669 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003670
Johannes Weiner05b84302014-08-06 16:05:59 -07003671 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003672 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003673 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003674 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003675 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003676 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003677 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003678 BUG();
3679
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003680 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003681 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003682 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3683
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003684 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003685
3686 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003687 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003688 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003689 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003690 ret = -ENOMEM;
3691 goto unlock;
3692 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003693 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003694
3695 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003696 if (thresholds->primary) {
3697 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003698 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003699 }
3700
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003701 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003702 new->entries[size - 1].eventfd = eventfd;
3703 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003704
3705 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003706 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003707 compare_thresholds, NULL);
3708
3709 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003710 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003711 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07003712 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003713 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003714 * new->current_threshold will not be used until
3715 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003716 * it here.
3717 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003718 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07003719 } else
3720 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003721 }
3722
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003723 /* Free old spare buffer and save old primary buffer as spare */
3724 kfree(thresholds->spare);
3725 thresholds->spare = thresholds->primary;
3726
3727 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003728
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003729 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003730 synchronize_rcu();
3731
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003732unlock:
3733 mutex_unlock(&memcg->thresholds_lock);
3734
3735 return ret;
3736}
3737
Tejun Heo59b6f872013-11-22 18:20:43 -05003738static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003739 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003740{
Tejun Heo59b6f872013-11-22 18:20:43 -05003741 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003742}
3743
Tejun Heo59b6f872013-11-22 18:20:43 -05003744static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003745 struct eventfd_ctx *eventfd, const char *args)
3746{
Tejun Heo59b6f872013-11-22 18:20:43 -05003747 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003748}
3749
Tejun Heo59b6f872013-11-22 18:20:43 -05003750static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003751 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003752{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003753 struct mem_cgroup_thresholds *thresholds;
3754 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003755 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003756 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003757
3758 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07003759
3760 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003761 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003762 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003763 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003764 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003765 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003766 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003767 BUG();
3768
Anton Vorontsov371528c2012-02-24 05:14:46 +04003769 if (!thresholds->primary)
3770 goto unlock;
3771
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003772 /* Check if a threshold crossed before removing */
3773 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3774
3775 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003776 size = 0;
3777 for (i = 0; i < thresholds->primary->size; i++) {
3778 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003779 size++;
3780 }
3781
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003782 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003783
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003784 /* Set thresholds array to NULL if we don't have thresholds */
3785 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003786 kfree(new);
3787 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003788 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003789 }
3790
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003791 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003792
3793 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003794 new->current_threshold = -1;
3795 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3796 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003797 continue;
3798
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003799 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07003800 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003801 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003802 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003803 * until rcu_assign_pointer(), so it's safe to increment
3804 * it here.
3805 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003806 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003807 }
3808 j++;
3809 }
3810
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003811swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003812 /* Swap primary and spare array */
3813 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07003814
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003815 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003816
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003817 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003818 synchronize_rcu();
Martijn Coenen2c641f52016-01-15 16:57:49 -08003819
3820 /* If all events are unregistered, free the spare array */
3821 if (!new) {
3822 kfree(thresholds->spare);
3823 thresholds->spare = NULL;
3824 }
Anton Vorontsov371528c2012-02-24 05:14:46 +04003825unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003826 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003827}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003828
Tejun Heo59b6f872013-11-22 18:20:43 -05003829static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003830 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003831{
Tejun Heo59b6f872013-11-22 18:20:43 -05003832 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003833}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003834
Tejun Heo59b6f872013-11-22 18:20:43 -05003835static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003836 struct eventfd_ctx *eventfd)
3837{
Tejun Heo59b6f872013-11-22 18:20:43 -05003838 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003839}
3840
Tejun Heo59b6f872013-11-22 18:20:43 -05003841static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003842 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003843{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003844 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003845
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003846 event = kmalloc(sizeof(*event), GFP_KERNEL);
3847 if (!event)
3848 return -ENOMEM;
3849
Michal Hocko1af8efe2011-07-26 16:08:24 -07003850 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003851
3852 event->eventfd = eventfd;
3853 list_add(&event->list, &memcg->oom_notify);
3854
3855 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07003856 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003857 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07003858 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003859
3860 return 0;
3861}
3862
Tejun Heo59b6f872013-11-22 18:20:43 -05003863static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003864 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003865{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003866 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003867
Michal Hocko1af8efe2011-07-26 16:08:24 -07003868 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003869
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003870 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003871 if (ev->eventfd == eventfd) {
3872 list_del(&ev->list);
3873 kfree(ev);
3874 }
3875 }
3876
Michal Hocko1af8efe2011-07-26 16:08:24 -07003877 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003878}
3879
Tejun Heo2da8ca82013-12-05 12:28:04 -05003880static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003881{
Tejun Heo2da8ca82013-12-05 12:28:04 -05003882 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003883
Tejun Heo791badb2013-12-05 12:28:02 -05003884 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
3885 seq_printf(sf, "under_oom %d\n", (bool)atomic_read(&memcg->under_oom));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003886 return 0;
3887}
3888
Tejun Heo182446d2013-08-08 20:11:24 -04003889static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003890 struct cftype *cft, u64 val)
3891{
Tejun Heo182446d2013-08-08 20:11:24 -04003892 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003893
3894 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07003895 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003896 return -EINVAL;
3897
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003898 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003899 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003900 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003901
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003902 return 0;
3903}
3904
Andrew Mortonc255a452012-07-31 16:43:02 -07003905#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03003906static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00003907{
Glauber Costa55007d82012-12-18 14:22:38 -08003908 int ret;
3909
Glauber Costa55007d82012-12-18 14:22:38 -08003910 ret = memcg_propagate_kmem(memcg);
3911 if (ret)
3912 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003913
Glauber Costa1d62e432012-04-09 19:36:33 -03003914 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07003915}
Glauber Costae5671df2011-12-11 21:47:01 +00003916
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003917static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
3918{
Vladimir Davydov2788cf0c2015-02-12 14:59:38 -08003919 struct cgroup_subsys_state *css;
3920 struct mem_cgroup *parent, *child;
3921 int kmemcg_id;
3922
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003923 if (!memcg->kmem_acct_active)
3924 return;
3925
3926 /*
3927 * Clear the 'active' flag before clearing memcg_caches arrays entries.
3928 * Since we take the slab_mutex in memcg_deactivate_kmem_caches(), it
3929 * guarantees no cache will be created for this cgroup after we are
3930 * done (see memcg_create_kmem_cache()).
3931 */
3932 memcg->kmem_acct_active = false;
3933
3934 memcg_deactivate_kmem_caches(memcg);
Vladimir Davydov2788cf0c2015-02-12 14:59:38 -08003935
3936 kmemcg_id = memcg->kmemcg_id;
3937 BUG_ON(kmemcg_id < 0);
3938
3939 parent = parent_mem_cgroup(memcg);
3940 if (!parent)
3941 parent = root_mem_cgroup;
3942
3943 /*
3944 * Change kmemcg_id of this cgroup and all its descendants to the
3945 * parent's id, and then move all entries from this cgroup's list_lrus
3946 * to ones of the parent. After we have finished, all list_lrus
3947 * corresponding to this cgroup are guaranteed to remain empty. The
3948 * ordering is imposed by list_lru_node->lock taken by
3949 * memcg_drain_all_list_lrus().
3950 */
3951 css_for_each_descendant_pre(css, &memcg->css) {
3952 child = mem_cgroup_from_css(css);
3953 BUG_ON(child->kmemcg_id != kmemcg_id);
3954 child->kmemcg_id = parent->kmemcg_id;
3955 if (!memcg->use_hierarchy)
3956 break;
3957 }
3958 memcg_drain_all_list_lrus(kmemcg_id, parent->kmemcg_id);
3959
3960 memcg_free_cache_id(kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003961}
3962
Li Zefan10d5ebf2013-07-08 16:00:33 -07003963static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00003964{
Vladimir Davydovf48b80a2015-02-12 14:59:56 -08003965 if (memcg->kmem_acct_activated) {
3966 memcg_destroy_kmem_caches(memcg);
3967 static_key_slow_dec(&memcg_kmem_enabled_key);
3968 WARN_ON(page_counter_read(&memcg->kmem));
3969 }
Glauber Costa1d62e432012-04-09 19:36:33 -03003970 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07003971}
Glauber Costae5671df2011-12-11 21:47:01 +00003972#else
Glauber Costacbe128e32012-04-09 19:36:34 -03003973static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00003974{
3975 return 0;
3976}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00003977
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003978static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
3979{
3980}
3981
Li Zefan10d5ebf2013-07-08 16:00:33 -07003982static void memcg_destroy_kmem(struct mem_cgroup *memcg)
3983{
3984}
Glauber Costae5671df2011-12-11 21:47:01 +00003985#endif
3986
Tejun Heo6b385782015-05-22 17:13:37 -04003987#ifdef CONFIG_CGROUP_WRITEBACK
3988
3989struct list_head *mem_cgroup_cgwb_list(struct mem_cgroup *memcg)
3990{
3991 return &memcg->cgwb_list;
3992}
3993
Tejun Heo8c93c2f2015-05-22 18:23:33 -04003994static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
3995{
3996 return wb_domain_init(&memcg->cgwb_domain, gfp);
3997}
3998
3999static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4000{
4001 wb_domain_exit(&memcg->cgwb_domain);
4002}
4003
Tejun Heoe65e8b62015-05-22 18:23:34 -04004004static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4005{
4006 wb_domain_size_changed(&memcg->cgwb_domain);
4007}
4008
Tejun Heo8c93c2f2015-05-22 18:23:33 -04004009struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4010{
4011 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4012
4013 if (!memcg->css.parent)
4014 return NULL;
4015
4016 return &memcg->cgwb_domain;
4017}
4018
Tejun Heo6a139c82015-05-22 18:23:35 -04004019/**
4020 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4021 * @wb: bdi_writeback in question
Tejun Heo8debf2d2015-09-29 13:04:26 -04004022 * @pfilepages: out parameter for number of file pages
4023 * @pheadroom: out parameter for number of allocatable pages according to memcg
Tejun Heo6a139c82015-05-22 18:23:35 -04004024 * @pdirty: out parameter for number of dirty pages
4025 * @pwriteback: out parameter for number of pages under writeback
4026 *
Tejun Heo8debf2d2015-09-29 13:04:26 -04004027 * Determine the numbers of file, headroom, dirty, and writeback pages in
4028 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4029 * is a bit more involved.
Tejun Heo6a139c82015-05-22 18:23:35 -04004030 *
Tejun Heo8debf2d2015-09-29 13:04:26 -04004031 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4032 * headroom is calculated as the lowest headroom of itself and the
4033 * ancestors. Note that this doesn't consider the actual amount of
4034 * available memory in the system. The caller should further cap
4035 * *@pheadroom accordingly.
Tejun Heo6a139c82015-05-22 18:23:35 -04004036 */
Tejun Heo8debf2d2015-09-29 13:04:26 -04004037void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4038 unsigned long *pheadroom, unsigned long *pdirty,
4039 unsigned long *pwriteback)
Tejun Heo6a139c82015-05-22 18:23:35 -04004040{
4041 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4042 struct mem_cgroup *parent;
Tejun Heo6a139c82015-05-22 18:23:35 -04004043
4044 *pdirty = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_DIRTY);
4045
4046 /* this should eventually include NR_UNSTABLE_NFS */
4047 *pwriteback = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_WRITEBACK);
Tejun Heo8debf2d2015-09-29 13:04:26 -04004048 *pfilepages = mem_cgroup_nr_lru_pages(memcg, (1 << LRU_INACTIVE_FILE) |
4049 (1 << LRU_ACTIVE_FILE));
4050 *pheadroom = PAGE_COUNTER_MAX;
Tejun Heo6a139c82015-05-22 18:23:35 -04004051
Tejun Heo6a139c82015-05-22 18:23:35 -04004052 while ((parent = parent_mem_cgroup(memcg))) {
4053 unsigned long ceiling = min(memcg->memory.limit, memcg->high);
4054 unsigned long used = page_counter_read(&memcg->memory);
4055
Tejun Heo8debf2d2015-09-29 13:04:26 -04004056 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
Tejun Heo6a139c82015-05-22 18:23:35 -04004057 memcg = parent;
4058 }
Tejun Heo6a139c82015-05-22 18:23:35 -04004059}
4060
Tejun Heo8c93c2f2015-05-22 18:23:33 -04004061#else /* CONFIG_CGROUP_WRITEBACK */
4062
4063static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4064{
4065 return 0;
4066}
4067
4068static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4069{
4070}
4071
Tejun Heoe65e8b62015-05-22 18:23:34 -04004072static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4073{
4074}
4075
Tejun Heo6b385782015-05-22 17:13:37 -04004076#endif /* CONFIG_CGROUP_WRITEBACK */
4077
Tejun Heo79bd9812013-11-22 18:20:42 -05004078/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004079 * DO NOT USE IN NEW FILES.
4080 *
4081 * "cgroup.event_control" implementation.
4082 *
4083 * This is way over-engineered. It tries to support fully configurable
4084 * events for each user. Such level of flexibility is completely
4085 * unnecessary especially in the light of the planned unified hierarchy.
4086 *
4087 * Please deprecate this and replace with something simpler if at all
4088 * possible.
4089 */
4090
4091/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004092 * Unregister event and free resources.
4093 *
4094 * Gets called from workqueue.
4095 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004096static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004097{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004098 struct mem_cgroup_event *event =
4099 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004100 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004101
4102 remove_wait_queue(event->wqh, &event->wait);
4103
Tejun Heo59b6f872013-11-22 18:20:43 -05004104 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004105
4106 /* Notify userspace the event is going away. */
4107 eventfd_signal(event->eventfd, 1);
4108
4109 eventfd_ctx_put(event->eventfd);
4110 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004111 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004112}
4113
4114/*
4115 * Gets called on POLLHUP on eventfd when user closes it.
4116 *
4117 * Called with wqh->lock held and interrupts disabled.
4118 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004119static int memcg_event_wake(wait_queue_t *wait, unsigned mode,
4120 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004121{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004122 struct mem_cgroup_event *event =
4123 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004124 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004125 unsigned long flags = (unsigned long)key;
4126
4127 if (flags & POLLHUP) {
4128 /*
4129 * If the event has been detached at cgroup removal, we
4130 * can simply return knowing the other side will cleanup
4131 * for us.
4132 *
4133 * We can't race against event freeing since the other
4134 * side will require wqh->lock via remove_wait_queue(),
4135 * which we hold.
4136 */
Tejun Heofba94802013-11-22 18:20:43 -05004137 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004138 if (!list_empty(&event->list)) {
4139 list_del_init(&event->list);
4140 /*
4141 * We are in atomic context, but cgroup_event_remove()
4142 * may sleep, so we have to call it in workqueue.
4143 */
4144 schedule_work(&event->remove);
4145 }
Tejun Heofba94802013-11-22 18:20:43 -05004146 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004147 }
4148
4149 return 0;
4150}
4151
Tejun Heo3bc942f2013-11-22 18:20:44 -05004152static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004153 wait_queue_head_t *wqh, poll_table *pt)
4154{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004155 struct mem_cgroup_event *event =
4156 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004157
4158 event->wqh = wqh;
4159 add_wait_queue(wqh, &event->wait);
4160}
4161
4162/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004163 * DO NOT USE IN NEW FILES.
4164 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004165 * Parse input and register new cgroup event handler.
4166 *
4167 * Input must be in format '<event_fd> <control_fd> <args>'.
4168 * Interpretation of args is defined by control file implementation.
4169 */
Tejun Heo451af502014-05-13 12:16:21 -04004170static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4171 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004172{
Tejun Heo451af502014-05-13 12:16:21 -04004173 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004174 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004175 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004176 struct cgroup_subsys_state *cfile_css;
4177 unsigned int efd, cfd;
4178 struct fd efile;
4179 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004180 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004181 char *endp;
4182 int ret;
4183
Tejun Heo451af502014-05-13 12:16:21 -04004184 buf = strstrip(buf);
4185
4186 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004187 if (*endp != ' ')
4188 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004189 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004190
Tejun Heo451af502014-05-13 12:16:21 -04004191 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004192 if ((*endp != ' ') && (*endp != '\0'))
4193 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004194 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004195
4196 event = kzalloc(sizeof(*event), GFP_KERNEL);
4197 if (!event)
4198 return -ENOMEM;
4199
Tejun Heo59b6f872013-11-22 18:20:43 -05004200 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004201 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004202 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4203 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4204 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004205
4206 efile = fdget(efd);
4207 if (!efile.file) {
4208 ret = -EBADF;
4209 goto out_kfree;
4210 }
4211
4212 event->eventfd = eventfd_ctx_fileget(efile.file);
4213 if (IS_ERR(event->eventfd)) {
4214 ret = PTR_ERR(event->eventfd);
4215 goto out_put_efile;
4216 }
4217
4218 cfile = fdget(cfd);
4219 if (!cfile.file) {
4220 ret = -EBADF;
4221 goto out_put_eventfd;
4222 }
4223
4224 /* the process need read permission on control file */
4225 /* AV: shouldn't we check that it's been opened for read instead? */
4226 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4227 if (ret < 0)
4228 goto out_put_cfile;
4229
Tejun Heo79bd9812013-11-22 18:20:42 -05004230 /*
Tejun Heofba94802013-11-22 18:20:43 -05004231 * Determine the event callbacks and set them in @event. This used
4232 * to be done via struct cftype but cgroup core no longer knows
4233 * about these events. The following is crude but the whole thing
4234 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004235 *
4236 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004237 */
Al Virob5830432014-10-31 01:22:04 -04004238 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004239
4240 if (!strcmp(name, "memory.usage_in_bytes")) {
4241 event->register_event = mem_cgroup_usage_register_event;
4242 event->unregister_event = mem_cgroup_usage_unregister_event;
4243 } else if (!strcmp(name, "memory.oom_control")) {
4244 event->register_event = mem_cgroup_oom_register_event;
4245 event->unregister_event = mem_cgroup_oom_unregister_event;
4246 } else if (!strcmp(name, "memory.pressure_level")) {
4247 event->register_event = vmpressure_register_event;
4248 event->unregister_event = vmpressure_unregister_event;
4249 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004250 event->register_event = memsw_cgroup_usage_register_event;
4251 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004252 } else {
4253 ret = -EINVAL;
4254 goto out_put_cfile;
4255 }
4256
4257 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004258 * Verify @cfile should belong to @css. Also, remaining events are
4259 * automatically removed on cgroup destruction but the removal is
4260 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004261 */
Al Virob5830432014-10-31 01:22:04 -04004262 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004263 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004264 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004265 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004266 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004267 if (cfile_css != css) {
4268 css_put(cfile_css);
4269 goto out_put_cfile;
4270 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004271
Tejun Heo451af502014-05-13 12:16:21 -04004272 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004273 if (ret)
4274 goto out_put_css;
4275
4276 efile.file->f_op->poll(efile.file, &event->pt);
4277
Tejun Heofba94802013-11-22 18:20:43 -05004278 spin_lock(&memcg->event_list_lock);
4279 list_add(&event->list, &memcg->event_list);
4280 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004281
4282 fdput(cfile);
4283 fdput(efile);
4284
Tejun Heo451af502014-05-13 12:16:21 -04004285 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004286
4287out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004288 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004289out_put_cfile:
4290 fdput(cfile);
4291out_put_eventfd:
4292 eventfd_ctx_put(event->eventfd);
4293out_put_efile:
4294 fdput(efile);
4295out_kfree:
4296 kfree(event);
4297
4298 return ret;
4299}
4300
Johannes Weiner241994ed2015-02-11 15:26:06 -08004301static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004302 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004303 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004304 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004305 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004306 },
4307 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004308 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004309 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004310 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004311 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004312 },
4313 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004314 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004315 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004316 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004317 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004318 },
4319 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004320 .name = "soft_limit_in_bytes",
4321 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004322 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004323 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004324 },
4325 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004326 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004327 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004328 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004329 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004330 },
Balbir Singh8697d332008-02-07 00:13:59 -08004331 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004332 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004333 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004334 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004335 {
4336 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004337 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004338 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004339 {
4340 .name = "use_hierarchy",
4341 .write_u64 = mem_cgroup_hierarchy_write,
4342 .read_u64 = mem_cgroup_hierarchy_read,
4343 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004344 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004345 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004346 .write = memcg_write_event_control,
Tejun Heo79bd9812013-11-22 18:20:42 -05004347 .flags = CFTYPE_NO_PREFIX,
4348 .mode = S_IWUGO,
4349 },
4350 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004351 .name = "swappiness",
4352 .read_u64 = mem_cgroup_swappiness_read,
4353 .write_u64 = mem_cgroup_swappiness_write,
4354 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004355 {
4356 .name = "move_charge_at_immigrate",
4357 .read_u64 = mem_cgroup_move_charge_read,
4358 .write_u64 = mem_cgroup_move_charge_write,
4359 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004360 {
4361 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004362 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004363 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004364 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4365 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004366 {
4367 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004368 },
Ying Han406eb0c2011-05-26 16:25:37 -07004369#ifdef CONFIG_NUMA
4370 {
4371 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004372 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004373 },
4374#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004375#ifdef CONFIG_MEMCG_KMEM
4376 {
4377 .name = "kmem.limit_in_bytes",
4378 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004379 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004380 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004381 },
4382 {
4383 .name = "kmem.usage_in_bytes",
4384 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004385 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004386 },
4387 {
4388 .name = "kmem.failcnt",
4389 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004390 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004391 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004392 },
4393 {
4394 .name = "kmem.max_usage_in_bytes",
4395 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004396 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004397 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004398 },
Glauber Costa749c5412012-12-18 14:23:01 -08004399#ifdef CONFIG_SLABINFO
4400 {
4401 .name = "kmem.slabinfo",
Vladimir Davydovb0475012014-12-10 15:44:19 -08004402 .seq_start = slab_start,
4403 .seq_next = slab_next,
4404 .seq_stop = slab_stop,
4405 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004406 },
4407#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004408#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004409 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004410};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004411
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004412static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004413{
4414 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004415 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004416 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004417 /*
4418 * This routine is called against possible nodes.
4419 * But it's BUG to call kmalloc() against offline node.
4420 *
4421 * TODO: this routine can waste much memory for nodes which will
4422 * never be onlined. It's better to use memory hotplug callback
4423 * function.
4424 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004425 if (!node_state(node, N_NORMAL_MEMORY))
4426 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004427 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004428 if (!pn)
4429 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004430
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004431 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4432 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004433 lruvec_init(&mz->lruvec);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004434 mz->usage_in_excess = 0;
4435 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004436 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004437 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004438 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004439 return 0;
4440}
4441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004442static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004443{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004444 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004445}
4446
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004447static struct mem_cgroup *mem_cgroup_alloc(void)
4448{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004449 struct mem_cgroup *memcg;
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004450 size_t size;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004451
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004452 size = sizeof(struct mem_cgroup);
4453 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004454
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004455 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004456 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004457 return NULL;
4458
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004459 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4460 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004461 goto out_free;
Tejun Heo8c93c2f2015-05-22 18:23:33 -04004462
4463 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
4464 goto out_free_stat;
4465
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004466 spin_lock_init(&memcg->pcp_counter_lock);
4467 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004468
Tejun Heo8c93c2f2015-05-22 18:23:33 -04004469out_free_stat:
4470 free_percpu(memcg->stat);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004471out_free:
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004472 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004473 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004474}
4475
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004476/*
Glauber Costac8b2a362012-12-18 14:22:13 -08004477 * At destroying mem_cgroup, references from swap_cgroup can remain.
4478 * (scanning all at force_empty is too costly...)
4479 *
4480 * Instead of clearing all references at force_empty, we remember
4481 * the number of reference from swap_cgroup and free mem_cgroup when
4482 * it goes down to 0.
4483 *
4484 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07004485 */
Glauber Costac8b2a362012-12-18 14:22:13 -08004486
4487static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004488{
Glauber Costac8b2a362012-12-18 14:22:13 -08004489 int node;
Hugh Dickins59927fb2012-03-15 15:17:07 -07004490
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -07004491 mem_cgroup_remove_from_trees(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08004492
4493 for_each_node(node)
4494 free_mem_cgroup_per_zone_info(memcg, node);
4495
4496 free_percpu(memcg->stat);
Tejun Heo8c93c2f2015-05-22 18:23:33 -04004497 memcg_wb_domain_exit(memcg);
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004498 kfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004499}
Glauber Costa3afe36b2012-05-29 15:07:10 -07004500
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004501/*
4502 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4503 */
Glauber Costae1aab162011-12-11 21:47:03 +00004504struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004505{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004506 if (!memcg->memory.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004507 return NULL;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004508 return mem_cgroup_from_counter(memcg->memory.parent, memory);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004509}
Glauber Costae1aab162011-12-11 21:47:03 +00004510EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004511
Li Zefan0eb253e2009-01-15 13:51:25 -08004512static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04004513mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004514{
Glauber Costad142e3e2013-02-22 16:34:52 -08004515 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004516 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004517 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004518
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004519 memcg = mem_cgroup_alloc();
4520 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004521 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004522
Bob Liu3ed28fa2012-01-12 17:19:04 -08004523 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004524 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004525 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004526
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004527 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04004528 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08004529 root_mem_cgroup = memcg;
Tejun Heodbee2272015-05-22 17:13:20 -04004530 mem_cgroup_root_css = &memcg->css;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004531 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994ed2015-02-11 15:26:06 -08004532 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004533 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004534 page_counter_init(&memcg->memsw, NULL);
4535 page_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004536 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004537
Glauber Costad142e3e2013-02-22 16:34:52 -08004538 memcg->last_scanned_node = MAX_NUMNODES;
4539 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08004540 memcg->move_charge_at_immigrate = 0;
4541 mutex_init(&memcg->thresholds_lock);
4542 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004543 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05004544 INIT_LIST_HEAD(&memcg->event_list);
4545 spin_lock_init(&memcg->event_list_lock);
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004546#ifdef CONFIG_MEMCG_KMEM
4547 memcg->kmemcg_id = -1;
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004548#endif
Tejun Heo6b385782015-05-22 17:13:37 -04004549#ifdef CONFIG_CGROUP_WRITEBACK
4550 INIT_LIST_HEAD(&memcg->cgwb_list);
4551#endif
Glauber Costad142e3e2013-02-22 16:34:52 -08004552 return &memcg->css;
4553
4554free_out:
4555 __mem_cgroup_free(memcg);
4556 return ERR_PTR(error);
4557}
4558
4559static int
Tejun Heoeb954192013-08-08 20:11:23 -04004560mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08004561{
Tejun Heoeb954192013-08-08 20:11:23 -04004562 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04004563 struct mem_cgroup *parent = mem_cgroup_from_css(css->parent);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004564 int ret;
Glauber Costad142e3e2013-02-22 16:34:52 -08004565
Tejun Heo15a4c832014-05-04 15:09:14 -04004566 if (css->id > MEM_CGROUP_ID_MAX)
Li Zefan4219b2d2013-09-23 16:56:29 +08004567 return -ENOSPC;
4568
Tejun Heo63876982013-08-08 20:11:23 -04004569 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08004570 return 0;
4571
Glauber Costa09998212013-02-22 16:34:55 -08004572 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08004573
4574 memcg->use_hierarchy = parent->use_hierarchy;
4575 memcg->oom_kill_disable = parent->oom_kill_disable;
4576 memcg->swappiness = mem_cgroup_swappiness(parent);
4577
4578 if (parent->use_hierarchy) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004579 page_counter_init(&memcg->memory, &parent->memory);
Johannes Weiner241994ed2015-02-11 15:26:06 -08004580 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004581 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004582 page_counter_init(&memcg->memsw, &parent->memsw);
4583 page_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08004584
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004585 /*
Li Zefan8d76a972013-07-08 16:00:36 -07004586 * No need to take a reference to the parent because cgroup
4587 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004588 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08004589 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004590 page_counter_init(&memcg->memory, NULL);
Johannes Weiner241994ed2015-02-11 15:26:06 -08004591 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004592 memcg->soft_limit = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004593 page_counter_init(&memcg->memsw, NULL);
4594 page_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004595 /*
4596 * Deeper hierachy with use_hierarchy == false doesn't make
4597 * much sense so let cgroup subsystem know about this
4598 * unfortunate state in our controller.
4599 */
Glauber Costad142e3e2013-02-22 16:34:52 -08004600 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05004601 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004602 }
Glauber Costa09998212013-02-22 16:34:55 -08004603 mutex_unlock(&memcg_create_mutex);
Vladimir Davydovd6441632014-01-23 15:53:09 -08004604
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004605 ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
4606 if (ret)
4607 return ret;
4608
4609 /*
4610 * Make sure the memcg is initialized: mem_cgroup_iter()
4611 * orders reading memcg->initialized against its callers
4612 * reading the memcg members.
4613 */
4614 smp_store_release(&memcg->initialized, 1);
4615
4616 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004617}
4618
Tejun Heoeb954192013-08-08 20:11:23 -04004619static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004620{
Tejun Heoeb954192013-08-08 20:11:23 -04004621 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004622 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05004623
4624 /*
4625 * Unregister events and notify userspace.
4626 * Notify userspace about cgroup removing only after rmdir of cgroup
4627 * directory to avoid race between userspace and kernelspace.
4628 */
Tejun Heofba94802013-11-22 18:20:43 -05004629 spin_lock(&memcg->event_list_lock);
4630 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004631 list_del_init(&event->list);
4632 schedule_work(&event->remove);
4633 }
Tejun Heofba94802013-11-22 18:20:43 -05004634 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004635
Michal Hocko33cb8762013-07-31 13:53:51 -07004636 vmpressure_cleanup(&memcg->vmpressure);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08004637
4638 memcg_deactivate_kmem(memcg);
Tejun Heo6b385782015-05-22 17:13:37 -04004639
4640 wb_memcg_offline(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004641}
4642
Tejun Heoeb954192013-08-08 20:11:23 -04004643static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004644{
Tejun Heoeb954192013-08-08 20:11:23 -04004645 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004646
Li Zefan10d5ebf2013-07-08 16:00:33 -07004647 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07004648 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004649}
4650
Tejun Heo1ced9532014-07-08 18:02:57 -04004651/**
4652 * mem_cgroup_css_reset - reset the states of a mem_cgroup
4653 * @css: the target css
4654 *
4655 * Reset the states of the mem_cgroup associated with @css. This is
4656 * invoked when the userland requests disabling on the default hierarchy
4657 * but the memcg is pinned through dependency. The memcg should stop
4658 * applying policies and should revert to the vanilla state as it may be
4659 * made visible again.
4660 *
4661 * The current implementation only resets the essential configurations.
4662 * This needs to be expanded to cover all the visible parts.
4663 */
4664static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
4665{
4666 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4667
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004668 mem_cgroup_resize_limit(memcg, PAGE_COUNTER_MAX);
4669 mem_cgroup_resize_memsw_limit(memcg, PAGE_COUNTER_MAX);
4670 memcg_update_kmem_limit(memcg, PAGE_COUNTER_MAX);
Johannes Weiner241994ed2015-02-11 15:26:06 -08004671 memcg->low = 0;
4672 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004673 memcg->soft_limit = PAGE_COUNTER_MAX;
Tejun Heoe65e8b62015-05-22 18:23:34 -04004674 memcg_wb_domain_size_changed(memcg);
Tejun Heo1ced9532014-07-08 18:02:57 -04004675}
4676
Daisuke Nishimura02491442010-03-10 15:22:17 -08004677#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004678/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004679static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004680{
Johannes Weiner05b84302014-08-06 16:05:59 -07004681 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07004682
4683 /* Try a single bulk charge without reclaim first */
Johannes Weiner00501b52014-08-08 14:19:20 -07004684 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_WAIT, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07004685 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004686 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004687 return ret;
4688 }
Johannes Weiner692e7c42014-08-06 16:05:57 -07004689 if (ret == -EINTR) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004690 cancel_charge(root_mem_cgroup, count);
Johannes Weiner692e7c42014-08-06 16:05:57 -07004691 return ret;
4692 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004693
4694 /* Try charges one by one with reclaim */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004695 while (count--) {
Johannes Weiner00501b52014-08-08 14:19:20 -07004696 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_NORETRY, 1);
Johannes Weiner9476db92014-08-06 16:05:55 -07004697 /*
4698 * In case of failure, any residual charges against
4699 * mc.to will be dropped by mem_cgroup_clear_mc()
Johannes Weiner692e7c42014-08-06 16:05:57 -07004700 * later on. However, cancel any charges that are
4701 * bypassed to root right away or they'll be lost.
Johannes Weiner9476db92014-08-06 16:05:55 -07004702 */
Johannes Weiner692e7c42014-08-06 16:05:57 -07004703 if (ret == -EINTR)
Johannes Weiner00501b52014-08-08 14:19:20 -07004704 cancel_charge(root_mem_cgroup, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004705 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004706 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004707 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07004708 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004709 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004710 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004711}
4712
4713/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004714 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004715 * @vma: the vma the pte to be checked belongs
4716 * @addr: the address corresponding to the pte to be checked
4717 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004718 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004719 *
4720 * Returns
4721 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4722 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4723 * move charge. if @target is not NULL, the page is stored in target->page
4724 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004725 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4726 * target for charge migration. if @target is not NULL, the entry is stored
4727 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004728 *
4729 * Called with pte lock held.
4730 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004731union mc_target {
4732 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004733 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004734};
4735
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004736enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004737 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004738 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004739 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004740};
4741
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004742static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4743 unsigned long addr, pte_t ptent)
4744{
4745 struct page *page = vm_normal_page(vma, addr, ptent);
4746
4747 if (!page || !page_mapped(page))
4748 return NULL;
4749 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004750 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004751 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004752 } else {
4753 if (!(mc.flags & MOVE_FILE))
4754 return NULL;
4755 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004756 if (!get_page_unless_zero(page))
4757 return NULL;
4758
4759 return page;
4760}
4761
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004762#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004763static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4764 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4765{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004766 struct page *page = NULL;
4767 swp_entry_t ent = pte_to_swp_entry(ptent);
4768
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004769 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004770 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004771 /*
4772 * Because lookup_swap_cache() updates some statistics counter,
4773 * we call find_get_page() with swapper_space directly.
4774 */
Shaohua Li33806f02013-02-22 16:34:37 -08004775 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004776 if (do_swap_account)
4777 entry->val = ent.val;
4778
4779 return page;
4780}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004781#else
4782static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4783 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4784{
4785 return NULL;
4786}
4787#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004788
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004789static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4790 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4791{
4792 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004793 struct address_space *mapping;
4794 pgoff_t pgoff;
4795
4796 if (!vma->vm_file) /* anonymous vma */
4797 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004798 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004799 return NULL;
4800
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004801 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004802 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004803
4804 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004805#ifdef CONFIG_SWAP
4806 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07004807 if (shmem_mapping(mapping)) {
4808 page = find_get_entry(mapping, pgoff);
4809 if (radix_tree_exceptional_entry(page)) {
4810 swp_entry_t swp = radix_to_swp_entry(page);
4811 if (do_swap_account)
4812 *entry = swp;
4813 page = find_get_page(swap_address_space(swp), swp.val);
4814 }
4815 } else
4816 page = find_get_page(mapping, pgoff);
4817#else
4818 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07004819#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004820 return page;
4821}
4822
Chen Gangb1b0dea2015-04-14 15:47:35 -07004823/**
4824 * mem_cgroup_move_account - move account of the page
4825 * @page: the page
4826 * @nr_pages: number of regular pages (>1 for huge pages)
4827 * @from: mem_cgroup which the page is moved from.
4828 * @to: mem_cgroup which the page is moved to. @from != @to.
4829 *
4830 * The caller must confirm following.
4831 * - page is not on LRU (isolate_page() is useful.)
4832 * - compound_lock is held when nr_pages > 1
4833 *
4834 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
4835 * from old cgroup.
4836 */
4837static int mem_cgroup_move_account(struct page *page,
4838 unsigned int nr_pages,
4839 struct mem_cgroup *from,
4840 struct mem_cgroup *to)
4841{
4842 unsigned long flags;
4843 int ret;
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04004844 bool anon;
Chen Gangb1b0dea2015-04-14 15:47:35 -07004845
4846 VM_BUG_ON(from == to);
4847 VM_BUG_ON_PAGE(PageLRU(page), page);
4848 /*
4849 * The page is isolated from LRU. So, collapse function
4850 * will not handle this page. But page splitting can happen.
4851 * Do this check under compound_page_lock(). The caller should
4852 * hold it.
4853 */
4854 ret = -EBUSY;
4855 if (nr_pages > 1 && !PageTransHuge(page))
4856 goto out;
4857
4858 /*
4859 * Prevent mem_cgroup_migrate() from looking at page->mem_cgroup
4860 * of its source page while we change it: page migration takes
4861 * both pages off the LRU, but page cache replacement doesn't.
4862 */
4863 if (!trylock_page(page))
4864 goto out;
4865
4866 ret = -EINVAL;
4867 if (page->mem_cgroup != from)
4868 goto out_unlock;
4869
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04004870 anon = PageAnon(page);
4871
Chen Gangb1b0dea2015-04-14 15:47:35 -07004872 spin_lock_irqsave(&from->move_lock, flags);
4873
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04004874 if (!anon && page_mapped(page)) {
Chen Gangb1b0dea2015-04-14 15:47:35 -07004875 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
4876 nr_pages);
4877 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED],
4878 nr_pages);
4879 }
4880
Greg Thelen7c9d3ff2015-05-22 17:13:16 -04004881 /*
4882 * move_lock grabbed above and caller set from->moving_account, so
4883 * mem_cgroup_update_page_stat() will serialize updates to PageDirty.
4884 * So mapping should be stable for dirty pages.
4885 */
4886 if (!anon && PageDirty(page)) {
4887 struct address_space *mapping = page_mapping(page);
4888
4889 if (mapping_cap_account_dirty(mapping)) {
4890 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_DIRTY],
4891 nr_pages);
4892 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_DIRTY],
4893 nr_pages);
4894 }
4895 }
4896
Chen Gangb1b0dea2015-04-14 15:47:35 -07004897 if (PageWriteback(page)) {
4898 __this_cpu_sub(from->stat->count[MEM_CGROUP_STAT_WRITEBACK],
4899 nr_pages);
4900 __this_cpu_add(to->stat->count[MEM_CGROUP_STAT_WRITEBACK],
4901 nr_pages);
4902 }
4903
4904 /*
4905 * It is safe to change page->mem_cgroup here because the page
4906 * is referenced, charged, and isolated - we can't race with
4907 * uncharging, charging, migration, or LRU putback.
4908 */
4909
4910 /* caller should have done css_get */
4911 page->mem_cgroup = to;
4912 spin_unlock_irqrestore(&from->move_lock, flags);
4913
4914 ret = 0;
4915
4916 local_irq_disable();
4917 mem_cgroup_charge_statistics(to, page, nr_pages);
4918 memcg_check_events(to, page);
4919 mem_cgroup_charge_statistics(from, page, -nr_pages);
4920 memcg_check_events(from, page);
4921 local_irq_enable();
4922out_unlock:
4923 unlock_page(page);
4924out:
4925 return ret;
4926}
4927
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004928static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004929 unsigned long addr, pte_t ptent, union mc_target *target)
4930{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004931 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004932 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004933 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004934
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004935 if (pte_present(ptent))
4936 page = mc_handle_present_pte(vma, addr, ptent);
4937 else if (is_swap_pte(ptent))
4938 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08004939 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004940 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004941
4942 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004943 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004944 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004945 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004946 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08004947 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07004948 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08004949 */
Johannes Weiner1306a852014-12-10 15:44:52 -08004950 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004951 ret = MC_TARGET_PAGE;
4952 if (target)
4953 target->page = page;
4954 }
4955 if (!ret || !target)
4956 put_page(page);
4957 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004958 /* There is a swap entry and a page doesn't exist or isn't charged */
4959 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08004960 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004961 ret = MC_TARGET_SWAP;
4962 if (target)
4963 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004964 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004965 return ret;
4966}
4967
Naoya Horiguchi12724852012-03-21 16:34:28 -07004968#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4969/*
4970 * We don't consider swapping or file mapped pages because THP does not
4971 * support them for now.
4972 * Caller should make sure that pmd_trans_huge(pmd) is true.
4973 */
4974static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4975 unsigned long addr, pmd_t pmd, union mc_target *target)
4976{
4977 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07004978 enum mc_target_type ret = MC_TARGET_NONE;
4979
4980 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08004981 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004982 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07004983 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08004984 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07004985 ret = MC_TARGET_PAGE;
4986 if (target) {
4987 get_page(page);
4988 target->page = page;
4989 }
4990 }
4991 return ret;
4992}
4993#else
4994static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
4995 unsigned long addr, pmd_t pmd, union mc_target *target)
4996{
4997 return MC_TARGET_NONE;
4998}
4999#endif
5000
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005001static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5002 unsigned long addr, unsigned long end,
5003 struct mm_walk *walk)
5004{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005005 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005006 pte_t *pte;
5007 spinlock_t *ptl;
5008
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005009 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005010 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5011 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005012 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005013 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005014 }
Dave Hansen03319322011-03-22 16:32:56 -07005015
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005016 if (pmd_trans_unstable(pmd))
5017 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005018 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5019 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005020 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005021 mc.precharge++; /* increment precharge temporarily */
5022 pte_unmap_unlock(pte - 1, ptl);
5023 cond_resched();
5024
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005025 return 0;
5026}
5027
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005028static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5029{
5030 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005031
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005032 struct mm_walk mem_cgroup_count_precharge_walk = {
5033 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5034 .mm = mm,
5035 };
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005036 down_read(&mm->mmap_sem);
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005037 walk_page_range(0, ~0UL, &mem_cgroup_count_precharge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005038 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005039
5040 precharge = mc.precharge;
5041 mc.precharge = 0;
5042
5043 return precharge;
5044}
5045
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005046static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5047{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005048 unsigned long precharge = mem_cgroup_count_precharge(mm);
5049
5050 VM_BUG_ON(mc.moving_task);
5051 mc.moving_task = current;
5052 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005053}
5054
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005055/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5056static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005057{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005058 struct mem_cgroup *from = mc.from;
5059 struct mem_cgroup *to = mc.to;
5060
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005061 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005062 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005063 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005064 mc.precharge = 0;
5065 }
5066 /*
5067 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5068 * we must uncharge here.
5069 */
5070 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005071 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005072 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005073 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005074 /* we must fixup refcnts and charges */
5075 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005076 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005077 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005078 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005079
Johannes Weiner05b84302014-08-06 16:05:59 -07005080 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005081 * we charged both to->memory and to->memsw, so we
5082 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07005083 */
Johannes Weinerce00a962014-09-05 08:43:57 -04005084 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005085 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005086
Johannes Weinere8ea14c2014-12-10 15:42:42 -08005087 css_put_many(&mc.from->css, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005088
Li Zefan40503772013-07-08 16:00:34 -07005089 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005090 mc.moved_swap = 0;
5091 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005092 memcg_oom_recover(from);
5093 memcg_oom_recover(to);
5094 wake_up_all(&mc.waitq);
5095}
5096
5097static void mem_cgroup_clear_mc(void)
5098{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005099 /*
5100 * we must clear moving_task before waking up waiters at the end of
5101 * task migration.
5102 */
5103 mc.moving_task = NULL;
5104 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005105 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005106 mc.from = NULL;
5107 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005108 spin_unlock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005109}
5110
Tejun Heoeb954192013-08-08 20:11:23 -04005111static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005112 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005113{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005114 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005115 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04005116 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005117 unsigned long move_flags;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005118
Glauber Costaee5e8472013-02-22 16:34:50 -08005119 /*
5120 * We are now commited to this value whatever it is. Changes in this
5121 * tunable will only affect upcoming migrations, not the current one.
5122 * So we need to save it, and keep it going.
5123 */
Jason Low4db0c3c2015-04-15 16:14:08 -07005124 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005125 if (move_flags) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005126 struct mm_struct *mm;
5127 struct mem_cgroup *from = mem_cgroup_from_task(p);
5128
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005129 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005130
5131 mm = get_task_mm(p);
5132 if (!mm)
5133 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005134 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005135 if (mm->owner == p) {
5136 VM_BUG_ON(mc.from);
5137 VM_BUG_ON(mc.to);
5138 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005139 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005140 VM_BUG_ON(mc.moved_swap);
Johannes Weiner247b1442014-12-10 15:44:11 -08005141
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005142 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005143 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005144 mc.to = memcg;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005145 mc.flags = move_flags;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005146 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005147 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005148
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005149 ret = mem_cgroup_precharge_mc(mm);
5150 if (ret)
5151 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005152 }
5153 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005154 }
5155 return ret;
5156}
5157
Tejun Heoeb954192013-08-08 20:11:23 -04005158static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005159 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005160{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005161 if (mc.to)
5162 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005163}
5164
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005165static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5166 unsigned long addr, unsigned long end,
5167 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005168{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005169 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005170 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005171 pte_t *pte;
5172 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005173 enum mc_target_type target_type;
5174 union mc_target target;
5175 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005176
Naoya Horiguchi12724852012-03-21 16:34:28 -07005177 /*
5178 * We don't take compound_lock() here but no race with splitting thp
5179 * happens because:
5180 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5181 * under splitting, which means there's no concurrent thp split,
5182 * - if another thread runs into split_huge_page() just after we
5183 * entered this if-block, the thread must wait for page table lock
5184 * to be unlocked in __split_huge_page_splitting(), where the main
5185 * part of thp split is not executed yet.
5186 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005187 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005188 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005189 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005190 return 0;
5191 }
5192 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5193 if (target_type == MC_TARGET_PAGE) {
5194 page = target.page;
5195 if (!isolate_lru_page(page)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005196 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
Johannes Weiner1306a852014-12-10 15:44:52 -08005197 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005198 mc.precharge -= HPAGE_PMD_NR;
5199 mc.moved_charge += HPAGE_PMD_NR;
5200 }
5201 putback_lru_page(page);
5202 }
5203 put_page(page);
5204 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005205 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005206 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005207 }
5208
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005209 if (pmd_trans_unstable(pmd))
5210 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005211retry:
5212 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5213 for (; addr != end; addr += PAGE_SIZE) {
5214 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005215 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005216
5217 if (!mc.precharge)
5218 break;
5219
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005220 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005221 case MC_TARGET_PAGE:
5222 page = target.page;
5223 if (isolate_lru_page(page))
5224 goto put;
Johannes Weiner1306a852014-12-10 15:44:52 -08005225 if (!mem_cgroup_move_account(page, 1, mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005226 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005227 /* we uncharge from mc.from later. */
5228 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005229 }
5230 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005231put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005232 put_page(page);
5233 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005234 case MC_TARGET_SWAP:
5235 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005236 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005237 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005238 /* we fixup refcnts and charges later. */
5239 mc.moved_swap++;
5240 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005241 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005242 default:
5243 break;
5244 }
5245 }
5246 pte_unmap_unlock(pte - 1, ptl);
5247 cond_resched();
5248
5249 if (addr != end) {
5250 /*
5251 * We have consumed all precharges we got in can_attach().
5252 * We try charge one by one, but don't do any additional
5253 * charges to mc.to if we have failed in charge once in attach()
5254 * phase.
5255 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005256 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005257 if (!ret)
5258 goto retry;
5259 }
5260
5261 return ret;
5262}
5263
5264static void mem_cgroup_move_charge(struct mm_struct *mm)
5265{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005266 struct mm_walk mem_cgroup_move_charge_walk = {
5267 .pmd_entry = mem_cgroup_move_charge_pte_range,
5268 .mm = mm,
5269 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005270
5271 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08005272 /*
5273 * Signal mem_cgroup_begin_page_stat() to take the memcg's
5274 * move_lock while we're moving its pages to another memcg.
5275 * Then wait for already started RCU-only updates to finish.
5276 */
5277 atomic_inc(&mc.from->moving_account);
5278 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005279retry:
5280 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5281 /*
5282 * Someone who are holding the mmap_sem might be waiting in
5283 * waitq. So we cancel all extra charges, wake up all waiters,
5284 * and retry. Because we cancel precharges, we might not be able
5285 * to move enough charges, but moving charge is a best-effort
5286 * feature anyway, so it wouldn't be a big problem.
5287 */
5288 __mem_cgroup_clear_mc();
5289 cond_resched();
5290 goto retry;
5291 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005292 /*
5293 * When we have consumed all precharges and failed in doing
5294 * additional charge, the page walk just aborts.
5295 */
5296 walk_page_range(0, ~0UL, &mem_cgroup_move_charge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005297 up_read(&mm->mmap_sem);
Johannes Weiner312722c2014-12-10 15:44:25 -08005298 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005299}
5300
Tejun Heoeb954192013-08-08 20:11:23 -04005301static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005302 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005303{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005304 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005305 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005306
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005307 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005308 if (mc.to)
5309 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005310 mmput(mm);
5311 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005312 if (mc.to)
5313 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005314}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005315#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04005316static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005317 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005318{
5319 return 0;
5320}
Tejun Heoeb954192013-08-08 20:11:23 -04005321static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005322 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005323{
5324}
Tejun Heoeb954192013-08-08 20:11:23 -04005325static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08005326 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005327{
5328}
5329#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005330
Tejun Heof00baae2013-04-15 13:41:15 -07005331/*
5332 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04005333 * to verify whether we're attached to the default hierarchy on each mount
5334 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07005335 */
Tejun Heoeb954192013-08-08 20:11:23 -04005336static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07005337{
5338 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04005339 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07005340 * guarantees that @root doesn't have any children, so turning it
5341 * on for the root memcg is enough.
5342 */
Tejun Heoaa6ec292014-07-09 10:08:08 -04005343 if (cgroup_on_dfl(root_css->cgroup))
Vladimir Davydov7feee592015-03-12 16:26:19 -07005344 root_mem_cgroup->use_hierarchy = true;
5345 else
5346 root_mem_cgroup->use_hierarchy = false;
Tejun Heof00baae2013-04-15 13:41:15 -07005347}
5348
Johannes Weiner241994ed2015-02-11 15:26:06 -08005349static u64 memory_current_read(struct cgroup_subsys_state *css,
5350 struct cftype *cft)
5351{
5352 return mem_cgroup_usage(mem_cgroup_from_css(css), false);
5353}
5354
5355static int memory_low_show(struct seq_file *m, void *v)
5356{
5357 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005358 unsigned long low = READ_ONCE(memcg->low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005359
5360 if (low == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005361 seq_puts(m, "max\n");
Johannes Weiner241994ed2015-02-11 15:26:06 -08005362 else
5363 seq_printf(m, "%llu\n", (u64)low * PAGE_SIZE);
5364
5365 return 0;
5366}
5367
5368static ssize_t memory_low_write(struct kernfs_open_file *of,
5369 char *buf, size_t nbytes, loff_t off)
5370{
5371 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5372 unsigned long low;
5373 int err;
5374
5375 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005376 err = page_counter_memparse(buf, "max", &low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005377 if (err)
5378 return err;
5379
5380 memcg->low = low;
5381
5382 return nbytes;
5383}
5384
5385static int memory_high_show(struct seq_file *m, void *v)
5386{
5387 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005388 unsigned long high = READ_ONCE(memcg->high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005389
5390 if (high == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005391 seq_puts(m, "max\n");
Johannes Weiner241994ed2015-02-11 15:26:06 -08005392 else
5393 seq_printf(m, "%llu\n", (u64)high * PAGE_SIZE);
5394
5395 return 0;
5396}
5397
5398static ssize_t memory_high_write(struct kernfs_open_file *of,
5399 char *buf, size_t nbytes, loff_t off)
5400{
5401 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5402 unsigned long high;
5403 int err;
5404
5405 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005406 err = page_counter_memparse(buf, "max", &high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005407 if (err)
5408 return err;
5409
5410 memcg->high = high;
5411
Tejun Heoe65e8b62015-05-22 18:23:34 -04005412 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005413 return nbytes;
5414}
5415
5416static int memory_max_show(struct seq_file *m, void *v)
5417{
5418 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
Jason Low4db0c3c2015-04-15 16:14:08 -07005419 unsigned long max = READ_ONCE(memcg->memory.limit);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005420
5421 if (max == PAGE_COUNTER_MAX)
Johannes Weinerd2973692015-02-27 15:52:04 -08005422 seq_puts(m, "max\n");
Johannes Weiner241994ed2015-02-11 15:26:06 -08005423 else
5424 seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE);
5425
5426 return 0;
5427}
5428
5429static ssize_t memory_max_write(struct kernfs_open_file *of,
5430 char *buf, size_t nbytes, loff_t off)
5431{
5432 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5433 unsigned long max;
5434 int err;
5435
5436 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005437 err = page_counter_memparse(buf, "max", &max);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005438 if (err)
5439 return err;
5440
5441 err = mem_cgroup_resize_limit(memcg, max);
5442 if (err)
5443 return err;
5444
Tejun Heoe65e8b62015-05-22 18:23:34 -04005445 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005446 return nbytes;
5447}
5448
5449static int memory_events_show(struct seq_file *m, void *v)
5450{
5451 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
5452
5453 seq_printf(m, "low %lu\n", mem_cgroup_read_events(memcg, MEMCG_LOW));
5454 seq_printf(m, "high %lu\n", mem_cgroup_read_events(memcg, MEMCG_HIGH));
5455 seq_printf(m, "max %lu\n", mem_cgroup_read_events(memcg, MEMCG_MAX));
5456 seq_printf(m, "oom %lu\n", mem_cgroup_read_events(memcg, MEMCG_OOM));
5457
5458 return 0;
5459}
5460
5461static struct cftype memory_files[] = {
5462 {
5463 .name = "current",
5464 .read_u64 = memory_current_read,
5465 },
5466 {
5467 .name = "low",
5468 .flags = CFTYPE_NOT_ON_ROOT,
5469 .seq_show = memory_low_show,
5470 .write = memory_low_write,
5471 },
5472 {
5473 .name = "high",
5474 .flags = CFTYPE_NOT_ON_ROOT,
5475 .seq_show = memory_high_show,
5476 .write = memory_high_write,
5477 },
5478 {
5479 .name = "max",
5480 .flags = CFTYPE_NOT_ON_ROOT,
5481 .seq_show = memory_max_show,
5482 .write = memory_max_write,
5483 },
5484 {
5485 .name = "events",
5486 .flags = CFTYPE_NOT_ON_ROOT,
5487 .seq_show = memory_events_show,
5488 },
5489 { } /* terminate */
5490};
5491
Tejun Heo073219e2014-02-08 10:36:58 -05005492struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005493 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08005494 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08005495 .css_offline = mem_cgroup_css_offline,
5496 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04005497 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005498 .can_attach = mem_cgroup_can_attach,
5499 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005500 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07005501 .bind = mem_cgroup_bind,
Johannes Weiner241994ed2015-02-11 15:26:06 -08005502 .dfl_cftypes = memory_files,
5503 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005504 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005505};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005506
Johannes Weiner241994ed2015-02-11 15:26:06 -08005507/**
5508 * mem_cgroup_events - count memory events against a cgroup
5509 * @memcg: the memory cgroup
5510 * @idx: the event index
5511 * @nr: the number of events to account for
5512 */
5513void mem_cgroup_events(struct mem_cgroup *memcg,
5514 enum mem_cgroup_events_index idx,
5515 unsigned int nr)
5516{
5517 this_cpu_add(memcg->stat->events[idx], nr);
5518}
5519
5520/**
5521 * mem_cgroup_low - check if memory consumption is below the normal range
5522 * @root: the highest ancestor to consider
5523 * @memcg: the memory cgroup to check
5524 *
5525 * Returns %true if memory consumption of @memcg, and that of all
5526 * configurable ancestors up to @root, is below the normal range.
5527 */
5528bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg)
5529{
5530 if (mem_cgroup_disabled())
5531 return false;
5532
5533 /*
5534 * The toplevel group doesn't have a configurable range, so
5535 * it's never low when looked at directly, and it is not
5536 * considered an ancestor when assessing the hierarchy.
5537 */
5538
5539 if (memcg == root_mem_cgroup)
5540 return false;
5541
Michal Hocko4e54ded2015-02-27 15:51:46 -08005542 if (page_counter_read(&memcg->memory) >= memcg->low)
Johannes Weiner241994ed2015-02-11 15:26:06 -08005543 return false;
5544
5545 while (memcg != root) {
5546 memcg = parent_mem_cgroup(memcg);
5547
5548 if (memcg == root_mem_cgroup)
5549 break;
5550
Michal Hocko4e54ded2015-02-27 15:51:46 -08005551 if (page_counter_read(&memcg->memory) >= memcg->low)
Johannes Weiner241994ed2015-02-11 15:26:06 -08005552 return false;
5553 }
5554 return true;
5555}
5556
Johannes Weiner00501b52014-08-08 14:19:20 -07005557/**
5558 * mem_cgroup_try_charge - try charging a page
5559 * @page: page to charge
5560 * @mm: mm context of the victim
5561 * @gfp_mask: reclaim mode
5562 * @memcgp: charged memcg return
5563 *
5564 * Try to charge @page to the memcg that @mm belongs to, reclaiming
5565 * pages according to @gfp_mask if necessary.
5566 *
5567 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
5568 * Otherwise, an error code is returned.
5569 *
5570 * After page->mapping has been set up, the caller must finalize the
5571 * charge with mem_cgroup_commit_charge(). Or abort the transaction
5572 * with mem_cgroup_cancel_charge() in case page instantiation fails.
5573 */
5574int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
5575 gfp_t gfp_mask, struct mem_cgroup **memcgp)
5576{
5577 struct mem_cgroup *memcg = NULL;
5578 unsigned int nr_pages = 1;
5579 int ret = 0;
5580
5581 if (mem_cgroup_disabled())
5582 goto out;
5583
5584 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005585 /*
5586 * Every swap fault against a single page tries to charge the
5587 * page, bail as early as possible. shmem_unuse() encounters
5588 * already charged pages, too. The USED bit is protected by
5589 * the page lock, which serializes swap cache removal, which
5590 * in turn serializes uncharging.
5591 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005592 if (page->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07005593 goto out;
5594 }
5595
5596 if (PageTransHuge(page)) {
5597 nr_pages <<= compound_order(page);
5598 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5599 }
5600
5601 if (do_swap_account && PageSwapCache(page))
5602 memcg = try_get_mem_cgroup_from_page(page);
5603 if (!memcg)
5604 memcg = get_mem_cgroup_from_mm(mm);
5605
5606 ret = try_charge(memcg, gfp_mask, nr_pages);
5607
5608 css_put(&memcg->css);
5609
5610 if (ret == -EINTR) {
5611 memcg = root_mem_cgroup;
5612 ret = 0;
5613 }
5614out:
5615 *memcgp = memcg;
5616 return ret;
5617}
5618
5619/**
5620 * mem_cgroup_commit_charge - commit a page charge
5621 * @page: page to charge
5622 * @memcg: memcg to charge the page to
5623 * @lrucare: page might be on LRU already
5624 *
5625 * Finalize a charge transaction started by mem_cgroup_try_charge(),
5626 * after page->mapping has been set up. This must happen atomically
5627 * as part of the page instantiation, i.e. under the page table lock
5628 * for anonymous pages, under the page lock for page and swap cache.
5629 *
5630 * In addition, the page must not be on the LRU during the commit, to
5631 * prevent racing with task migration. If it might be, use @lrucare.
5632 *
5633 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
5634 */
5635void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
5636 bool lrucare)
5637{
5638 unsigned int nr_pages = 1;
5639
5640 VM_BUG_ON_PAGE(!page->mapping, page);
5641 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
5642
5643 if (mem_cgroup_disabled())
5644 return;
5645 /*
5646 * Swap faults will attempt to charge the same page multiple
5647 * times. But reuse_swap_page() might have removed the page
5648 * from swapcache already, so we can't check PageSwapCache().
5649 */
5650 if (!memcg)
5651 return;
5652
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005653 commit_charge(page, memcg, lrucare);
5654
Johannes Weiner00501b52014-08-08 14:19:20 -07005655 if (PageTransHuge(page)) {
5656 nr_pages <<= compound_order(page);
5657 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5658 }
5659
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005660 local_irq_disable();
5661 mem_cgroup_charge_statistics(memcg, page, nr_pages);
5662 memcg_check_events(memcg, page);
5663 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07005664
5665 if (do_swap_account && PageSwapCache(page)) {
5666 swp_entry_t entry = { .val = page_private(page) };
5667 /*
5668 * The swap entry might not get freed for a long time,
5669 * let's not wait for it. The page already received a
5670 * memory+swap charge, drop the swap entry duplicate.
5671 */
5672 mem_cgroup_uncharge_swap(entry);
5673 }
5674}
5675
5676/**
5677 * mem_cgroup_cancel_charge - cancel a page charge
5678 * @page: page to charge
5679 * @memcg: memcg to charge the page to
5680 *
5681 * Cancel a charge transaction started by mem_cgroup_try_charge().
5682 */
5683void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg)
5684{
5685 unsigned int nr_pages = 1;
5686
5687 if (mem_cgroup_disabled())
5688 return;
5689 /*
5690 * Swap faults will attempt to charge the same page multiple
5691 * times. But reuse_swap_page() might have removed the page
5692 * from swapcache already, so we can't check PageSwapCache().
5693 */
5694 if (!memcg)
5695 return;
5696
5697 if (PageTransHuge(page)) {
5698 nr_pages <<= compound_order(page);
5699 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5700 }
5701
5702 cancel_charge(memcg, nr_pages);
5703}
5704
Johannes Weiner747db952014-08-08 14:19:24 -07005705static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
Johannes Weiner747db952014-08-08 14:19:24 -07005706 unsigned long nr_anon, unsigned long nr_file,
5707 unsigned long nr_huge, struct page *dummy_page)
5708{
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005709 unsigned long nr_pages = nr_anon + nr_file;
Johannes Weiner747db952014-08-08 14:19:24 -07005710 unsigned long flags;
5711
Johannes Weinerce00a962014-09-05 08:43:57 -04005712 if (!mem_cgroup_is_root(memcg)) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005713 page_counter_uncharge(&memcg->memory, nr_pages);
5714 if (do_swap_account)
5715 page_counter_uncharge(&memcg->memsw, nr_pages);
Johannes Weinerce00a962014-09-05 08:43:57 -04005716 memcg_oom_recover(memcg);
5717 }
Johannes Weiner747db952014-08-08 14:19:24 -07005718
5719 local_irq_save(flags);
5720 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
5721 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
5722 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
5723 __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005724 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005725 memcg_check_events(memcg, dummy_page);
5726 local_irq_restore(flags);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08005727
5728 if (!mem_cgroup_is_root(memcg))
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005729 css_put_many(&memcg->css, nr_pages);
Johannes Weiner747db952014-08-08 14:19:24 -07005730}
5731
5732static void uncharge_list(struct list_head *page_list)
5733{
5734 struct mem_cgroup *memcg = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005735 unsigned long nr_anon = 0;
5736 unsigned long nr_file = 0;
5737 unsigned long nr_huge = 0;
5738 unsigned long pgpgout = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005739 struct list_head *next;
5740 struct page *page;
5741
5742 next = page_list->next;
5743 do {
5744 unsigned int nr_pages = 1;
Johannes Weiner747db952014-08-08 14:19:24 -07005745
5746 page = list_entry(next, struct page, lru);
5747 next = page->lru.next;
5748
5749 VM_BUG_ON_PAGE(PageLRU(page), page);
5750 VM_BUG_ON_PAGE(page_count(page), page);
5751
Johannes Weiner1306a852014-12-10 15:44:52 -08005752 if (!page->mem_cgroup)
Johannes Weiner747db952014-08-08 14:19:24 -07005753 continue;
5754
5755 /*
5756 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08005757 * page->mem_cgroup at this point, we have fully
Johannes Weiner29833312014-12-10 15:44:02 -08005758 * exclusive access to the page.
Johannes Weiner747db952014-08-08 14:19:24 -07005759 */
5760
Johannes Weiner1306a852014-12-10 15:44:52 -08005761 if (memcg != page->mem_cgroup) {
Johannes Weiner747db952014-08-08 14:19:24 -07005762 if (memcg) {
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005763 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5764 nr_huge, page);
5765 pgpgout = nr_anon = nr_file = nr_huge = 0;
Johannes Weiner747db952014-08-08 14:19:24 -07005766 }
Johannes Weiner1306a852014-12-10 15:44:52 -08005767 memcg = page->mem_cgroup;
Johannes Weiner747db952014-08-08 14:19:24 -07005768 }
5769
5770 if (PageTransHuge(page)) {
5771 nr_pages <<= compound_order(page);
5772 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
5773 nr_huge += nr_pages;
5774 }
5775
5776 if (PageAnon(page))
5777 nr_anon += nr_pages;
5778 else
5779 nr_file += nr_pages;
5780
Johannes Weiner1306a852014-12-10 15:44:52 -08005781 page->mem_cgroup = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07005782
5783 pgpgout++;
5784 } while (next != page_list);
5785
5786 if (memcg)
Johannes Weiner18eca2e2014-12-10 15:43:57 -08005787 uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
5788 nr_huge, page);
Johannes Weiner747db952014-08-08 14:19:24 -07005789}
5790
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005791/**
5792 * mem_cgroup_uncharge - uncharge a page
5793 * @page: page to uncharge
5794 *
5795 * Uncharge a page previously charged with mem_cgroup_try_charge() and
5796 * mem_cgroup_commit_charge().
5797 */
5798void mem_cgroup_uncharge(struct page *page)
5799{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005800 if (mem_cgroup_disabled())
5801 return;
5802
Johannes Weiner747db952014-08-08 14:19:24 -07005803 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08005804 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005805 return;
5806
Johannes Weiner747db952014-08-08 14:19:24 -07005807 INIT_LIST_HEAD(&page->lru);
5808 uncharge_list(&page->lru);
5809}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005810
Johannes Weiner747db952014-08-08 14:19:24 -07005811/**
5812 * mem_cgroup_uncharge_list - uncharge a list of page
5813 * @page_list: list of pages to uncharge
5814 *
5815 * Uncharge a list of pages previously charged with
5816 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
5817 */
5818void mem_cgroup_uncharge_list(struct list_head *page_list)
5819{
5820 if (mem_cgroup_disabled())
5821 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005822
Johannes Weiner747db952014-08-08 14:19:24 -07005823 if (!list_empty(page_list))
5824 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005825}
5826
5827/**
5828 * mem_cgroup_migrate - migrate a charge to another page
5829 * @oldpage: currently charged page
5830 * @newpage: page to transfer the charge to
Michal Hockof5e03a42015-02-05 12:25:14 -08005831 * @lrucare: either or both pages might be on the LRU already
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005832 *
5833 * Migrate the charge from @oldpage to @newpage.
5834 *
5835 * Both pages must be locked, @newpage->mapping must be set up.
5836 */
5837void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
5838 bool lrucare)
5839{
Johannes Weiner29833312014-12-10 15:44:02 -08005840 struct mem_cgroup *memcg;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005841 int isolated;
5842
5843 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
5844 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
5845 VM_BUG_ON_PAGE(!lrucare && PageLRU(oldpage), oldpage);
5846 VM_BUG_ON_PAGE(!lrucare && PageLRU(newpage), newpage);
5847 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07005848 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
5849 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005850
5851 if (mem_cgroup_disabled())
5852 return;
5853
5854 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08005855 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005856 return;
5857
Johannes Weiner7d5e3242014-12-10 15:43:46 -08005858 /*
5859 * Swapcache readahead pages can get migrated before being
5860 * charged, and migration from compaction can happen to an
5861 * uncharged page when the PFN walker finds a page that
5862 * reclaim just put back on the LRU but has not released yet.
5863 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005864 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08005865 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005866 return;
5867
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005868 if (lrucare)
5869 lock_page_lru(oldpage, &isolated);
5870
Johannes Weiner1306a852014-12-10 15:44:52 -08005871 oldpage->mem_cgroup = NULL;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005872
5873 if (lrucare)
5874 unlock_page_lru(oldpage, isolated);
5875
Johannes Weiner29833312014-12-10 15:44:02 -08005876 commit_charge(newpage, memcg, lrucare);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005877}
5878
Michal Hocko2d110852013-02-22 16:34:43 -08005879/*
Michal Hocko10813122013-02-22 16:35:41 -08005880 * subsys_initcall() for memory controller.
5881 *
5882 * Some parts like hotcpu_notifier() have to be initialized from this context
5883 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
5884 * everything that doesn't depend on a specific mem_cgroup structure should
5885 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08005886 */
5887static int __init mem_cgroup_init(void)
5888{
Johannes Weiner95a045f2015-02-11 15:26:33 -08005889 int cpu, node;
5890
Michal Hocko2d110852013-02-22 16:34:43 -08005891 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Johannes Weiner95a045f2015-02-11 15:26:33 -08005892
5893 for_each_possible_cpu(cpu)
5894 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
5895 drain_local_stock);
5896
5897 for_each_node(node) {
5898 struct mem_cgroup_tree_per_node *rtpn;
5899 int zone;
5900
5901 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
5902 node_online(node) ? node : NUMA_NO_NODE);
5903
5904 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5905 struct mem_cgroup_tree_per_zone *rtpz;
5906
5907 rtpz = &rtpn->rb_tree_per_zone[zone];
5908 rtpz->rb_root = RB_ROOT;
5909 spin_lock_init(&rtpz->lock);
5910 }
5911 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5912 }
5913
Michal Hocko2d110852013-02-22 16:34:43 -08005914 return 0;
5915}
5916subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08005917
5918#ifdef CONFIG_MEMCG_SWAP
5919/**
5920 * mem_cgroup_swapout - transfer a memsw charge to swap
5921 * @page: page whose memsw charge to transfer
5922 * @entry: swap entry to move the charge to
5923 *
5924 * Transfer the memsw charge of @page to @entry.
5925 */
5926void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
5927{
5928 struct mem_cgroup *memcg;
5929 unsigned short oldid;
5930
5931 VM_BUG_ON_PAGE(PageLRU(page), page);
5932 VM_BUG_ON_PAGE(page_count(page), page);
5933
5934 if (!do_swap_account)
5935 return;
5936
5937 memcg = page->mem_cgroup;
5938
5939 /* Readahead page, never charged */
5940 if (!memcg)
5941 return;
5942
5943 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg));
5944 VM_BUG_ON_PAGE(oldid, page);
5945 mem_cgroup_swap_statistics(memcg, true);
5946
5947 page->mem_cgroup = NULL;
5948
5949 if (!mem_cgroup_is_root(memcg))
5950 page_counter_uncharge(&memcg->memory, 1);
5951
Johannes Weinerf3717632015-06-10 11:14:54 -07005952 /* Caller disabled preemption with mapping->tree_lock */
Johannes Weiner21afa382015-02-11 15:26:36 -08005953 mem_cgroup_charge_statistics(memcg, page, -1);
5954 memcg_check_events(memcg, page);
5955}
5956
5957/**
5958 * mem_cgroup_uncharge_swap - uncharge a swap entry
5959 * @entry: swap entry to uncharge
5960 *
5961 * Drop the memsw charge associated with @entry.
5962 */
5963void mem_cgroup_uncharge_swap(swp_entry_t entry)
5964{
5965 struct mem_cgroup *memcg;
5966 unsigned short id;
5967
5968 if (!do_swap_account)
5969 return;
5970
5971 id = swap_cgroup_record(entry, 0);
5972 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07005973 memcg = mem_cgroup_from_id(id);
Johannes Weiner21afa382015-02-11 15:26:36 -08005974 if (memcg) {
5975 if (!mem_cgroup_is_root(memcg))
5976 page_counter_uncharge(&memcg->memsw, 1);
5977 mem_cgroup_swap_statistics(memcg, false);
5978 css_put(&memcg->css);
5979 }
5980 rcu_read_unlock();
5981}
5982
5983/* for remember boot option*/
5984#ifdef CONFIG_MEMCG_SWAP_ENABLED
5985static int really_do_swap_account __initdata = 1;
5986#else
5987static int really_do_swap_account __initdata;
5988#endif
5989
5990static int __init enable_swap_account(char *s)
5991{
5992 if (!strcmp(s, "1"))
5993 really_do_swap_account = 1;
5994 else if (!strcmp(s, "0"))
5995 really_do_swap_account = 0;
5996 return 1;
5997}
5998__setup("swapaccount=", enable_swap_account);
5999
6000static struct cftype memsw_cgroup_files[] = {
6001 {
6002 .name = "memsw.usage_in_bytes",
6003 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6004 .read_u64 = mem_cgroup_read_u64,
6005 },
6006 {
6007 .name = "memsw.max_usage_in_bytes",
6008 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6009 .write = mem_cgroup_reset,
6010 .read_u64 = mem_cgroup_read_u64,
6011 },
6012 {
6013 .name = "memsw.limit_in_bytes",
6014 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6015 .write = mem_cgroup_write,
6016 .read_u64 = mem_cgroup_read_u64,
6017 },
6018 {
6019 .name = "memsw.failcnt",
6020 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6021 .write = mem_cgroup_reset,
6022 .read_u64 = mem_cgroup_read_u64,
6023 },
6024 { }, /* terminate */
6025};
6026
6027static int __init mem_cgroup_swap_init(void)
6028{
6029 if (!mem_cgroup_disabled() && really_do_swap_account) {
6030 do_swap_account = 1;
6031 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
6032 memsw_cgroup_files));
6033 }
6034 return 0;
6035}
6036subsys_initcall(mem_cgroup_swap_init);
6037
6038#endif /* CONFIG_MEMCG_SWAP */