blob: 65a46eff5c3dd3932deb8c70595d64b1ea388089 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08009 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080013 * Kernel Memory Controller
14 * Copyright (C) 2012 Parallels Inc. and Google Inc.
15 * Authors: Glauber Costa and Suleiman Souhlal
16 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080017 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 */
27
28#include <linux/res_counter.h>
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080031#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080032#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080033#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080034#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080036#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070039#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040040#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080041#include <linux/mutex.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070042#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080043#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080044#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080046#include <linux/eventfd.h>
47#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080048#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080049#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070050#include <linux/vmalloc.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070051#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070052#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070053#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080054#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070055#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080056#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000057#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070058#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000059#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060
Balbir Singh8697d332008-02-07 00:13:59 -080061#include <asm/uaccess.h>
62
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070063#include <trace/events/vmscan.h>
64
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070065struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080066EXPORT_SYMBOL(mem_cgroup_subsys);
67
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070068#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070069static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080070
Andrew Mortonc255a452012-07-31 16:43:02 -070071#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070072/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080073int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080074
75/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070076#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080077static int really_do_swap_account __initdata = 1;
78#else
79static int really_do_swap_account __initdata = 0;
80#endif
81
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080082#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070083#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080084#endif
85
86
Johannes Weineraf7c4b02012-05-29 15:07:08 -070087static const char * const mem_cgroup_stat_names[] = {
88 "cache",
89 "rss",
David Rientjesb070e652013-05-07 16:18:09 -070090 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070091 "mapped_file",
Sha Zhengju3ea67d02013-09-12 15:13:53 -070092 "writeback",
Johannes Weineraf7c4b02012-05-29 15:07:08 -070093 "swap",
94};
95
Johannes Weinere9f89742011-03-23 16:42:37 -070096enum mem_cgroup_events_index {
97 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
98 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -070099 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
100 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700101 MEM_CGROUP_EVENTS_NSTATS,
102};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700103
104static const char * const mem_cgroup_events_names[] = {
105 "pgpgin",
106 "pgpgout",
107 "pgfault",
108 "pgmajfault",
109};
110
Sha Zhengju58cf1882013-02-22 16:32:05 -0800111static const char * const mem_cgroup_lru_names[] = {
112 "inactive_anon",
113 "active_anon",
114 "inactive_file",
115 "active_file",
116 "unevictable",
117};
118
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700119/*
120 * Per memcg event counter is incremented at every pagein/pageout. With THP,
121 * it will be incremated by the number of pages. This counter is used for
122 * for trigger some periodic events. This is straightforward and better
123 * than using jiffies etc. to handle periodic memcg event.
124 */
125enum mem_cgroup_events_target {
126 MEM_CGROUP_TARGET_THRESH,
Michal Hocko7d910c02013-09-12 15:13:28 -0700127 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700128 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700129 MEM_CGROUP_NTARGETS,
130};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700131#define THRESHOLDS_EVENTS_TARGET 128
132#define SOFTLIMIT_EVENTS_TARGET 1024
133#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700134
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800135struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700136 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700137 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700138 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700139 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800140};
141
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800142struct mem_cgroup_reclaim_iter {
Michal Hocko5f578162013-04-29 15:07:17 -0700143 /*
144 * last scanned hierarchy member. Valid only if last_dead_count
145 * matches memcg->dead_count of the hierarchy root group.
146 */
Michal Hocko542f85f2013-04-29 15:07:15 -0700147 struct mem_cgroup *last_visited;
Michal Hocko5f578162013-04-29 15:07:17 -0700148 unsigned long last_dead_count;
149
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800150 /* scan generation, increased every round-trip */
151 unsigned int generation;
152};
153
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800154/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800155 * per-zone information in memory controller.
156 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800157struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800158 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700159 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800160
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800161 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
162
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700163 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700164 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800165};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800166
167struct mem_cgroup_per_node {
168 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
169};
170
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800171struct mem_cgroup_threshold {
172 struct eventfd_ctx *eventfd;
173 u64 threshold;
174};
175
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700176/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800177struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700178 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700179 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800180 /* Size of entries[] */
181 unsigned int size;
182 /* Array of thresholds */
183 struct mem_cgroup_threshold entries[0];
184};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700185
186struct mem_cgroup_thresholds {
187 /* Primary thresholds array */
188 struct mem_cgroup_threshold_ary *primary;
189 /*
190 * Spare threshold array.
191 * This is needed to make mem_cgroup_unregister_event() "never fail".
192 * It must be able to store at least primary->size - 1 entries.
193 */
194 struct mem_cgroup_threshold_ary *spare;
195};
196
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700197/* for OOM */
198struct mem_cgroup_eventfd_list {
199 struct list_head list;
200 struct eventfd_ctx *eventfd;
201};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800202
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700203static void mem_cgroup_threshold(struct mem_cgroup *memcg);
204static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800205
Balbir Singhf64c3f52009-09-23 15:56:37 -0700206/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800207 * The memory controller data structure. The memory controller controls both
208 * page cache and RSS per cgroup. We would eventually like to provide
209 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
210 * to help the administrator determine what knobs to tune.
211 *
212 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800213 * we hit the water mark. May be even add a low water mark, such that
214 * no reclaim occurs from a cgroup at it's low water mark, this is
215 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800216 */
217struct mem_cgroup {
218 struct cgroup_subsys_state css;
219 /*
220 * the counter to account for memory usage
221 */
222 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700223
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700224 /* vmpressure notifications */
225 struct vmpressure vmpressure;
226
Li Zefan465939a2013-07-08 16:00:38 -0700227 /*
228 * the counter to account for mem+swap usage.
229 */
230 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700231
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800232 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800233 * the counter to account for kernel memory usage.
234 */
235 struct res_counter kmem;
236 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800237 * Should the accounting and control be hierarchical, per subtree?
238 */
239 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800240 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700241
242 bool oom_lock;
243 atomic_t under_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700244 atomic_t oom_wakeups;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700245
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700246 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700247 /* OOM-Killer disable */
248 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800249
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700250 /* set when res.limit == memsw.limit */
251 bool memsw_is_minimum;
252
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800253 /* protect arrays of thresholds */
254 struct mutex thresholds_lock;
255
256 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700257 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700258
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800259 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700260 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700261
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700262 /* For oom notifier event fd */
263 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700264
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800265 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800266 * Should we move charges of a task when a task is moved into this
267 * mem_cgroup ? And what type of charges should we move ?
268 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700269 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800270 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700271 * set > 0 if pages under this cgroup are moving to other cgroup.
272 */
273 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700274 /* taken only while moving_account > 0 */
275 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700276 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800277 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800278 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700279 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700280 /*
281 * used when a cpu is offlined or other synchronizations
282 * See mem_cgroup_read_stat().
283 */
284 struct mem_cgroup_stat_cpu nocpu_base;
285 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000286
Michal Hocko5f578162013-04-29 15:07:17 -0700287 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700288#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000289 struct tcp_memcontrol tcp_mem;
290#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800291#if defined(CONFIG_MEMCG_KMEM)
292 /* analogous to slab_common's slab_caches list. per-memcg */
293 struct list_head memcg_slab_caches;
294 /* Not a spinlock, we can take a lot of time walking the list */
295 struct mutex slab_caches_mutex;
296 /* Index in the kmem_cache->memcg_params->memcg_caches array */
297 int kmemcg_id;
298#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800299
300 int last_scanned_node;
301#if MAX_NUMNODES > 1
302 nodemask_t scan_nodes;
303 atomic_t numainfo_events;
304 atomic_t numainfo_updating;
305#endif
Michal Hocko7d910c02013-09-12 15:13:28 -0700306 /*
307 * Protects soft_contributed transitions.
308 * See mem_cgroup_update_soft_limit
309 */
310 spinlock_t soft_lock;
311
312 /*
313 * If true then this group has increased parents' children_in_excess
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700314 * when it got over the soft limit.
Michal Hocko7d910c02013-09-12 15:13:28 -0700315 * When a group falls bellow the soft limit, parents' children_in_excess
316 * is decreased and soft_contributed changed to false.
317 */
318 bool soft_contributed;
319
320 /* Number of children that are in soft limit excess */
321 atomic_t children_in_excess;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700322
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700323 struct mem_cgroup_per_node *nodeinfo[0];
324 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800325};
326
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800327static size_t memcg_size(void)
328{
329 return sizeof(struct mem_cgroup) +
330 nr_node_ids * sizeof(struct mem_cgroup_per_node);
331}
332
Glauber Costa510fc4e2012-12-18 14:21:47 -0800333/* internal only representation about the status of kmem accounting. */
334enum {
335 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800336 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800337 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800338};
339
Glauber Costaa8964b92012-12-18 14:22:09 -0800340/* We account when limit is on, but only after call sites are patched */
341#define KMEM_ACCOUNTED_MASK \
342 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800343
344#ifdef CONFIG_MEMCG_KMEM
345static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
346{
347 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
348}
Glauber Costa7de37682012-12-18 14:22:07 -0800349
350static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
351{
352 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
353}
354
Glauber Costaa8964b92012-12-18 14:22:09 -0800355static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
356{
357 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
358}
359
Glauber Costa55007d82012-12-18 14:22:38 -0800360static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
361{
362 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
363}
364
Glauber Costa7de37682012-12-18 14:22:07 -0800365static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
366{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700367 /*
368 * Our caller must use css_get() first, because memcg_uncharge_kmem()
369 * will call css_put() if it sees the memcg is dead.
370 */
371 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800372 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
373 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
374}
375
376static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
377{
378 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
379 &memcg->kmem_account_flags);
380}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800381#endif
382
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800383/* Stuffs for move charges at task migration. */
384/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800385 * Types of charges to be moved. "move_charge_at_immitgrate" and
386 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800387 */
388enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800389 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700390 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800391 NR_MOVE_TYPE,
392};
393
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800394/* "mc" and its members are protected by cgroup_mutex */
395static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800396 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800397 struct mem_cgroup *from;
398 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800399 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800400 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800401 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800402 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800403 struct task_struct *moving_task; /* a task moving charges */
404 wait_queue_head_t waitq; /* a waitq for other context */
405} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700406 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800407 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
408};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800409
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700410static bool move_anon(void)
411{
Glauber Costaee5e8472013-02-22 16:34:50 -0800412 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700413}
414
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700415static bool move_file(void)
416{
Glauber Costaee5e8472013-02-22 16:34:50 -0800417 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700418}
419
Balbir Singh4e416952009-09-23 15:56:39 -0700420/*
421 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
422 * limit reclaim to prevent infinite loops, if they ever occur.
423 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700424#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Balbir Singh4e416952009-09-23 15:56:39 -0700425
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800426enum charge_type {
427 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700428 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800429 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700430 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700431 NR_CHARGE_TYPE,
432};
433
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800434/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800435enum res_type {
436 _MEM,
437 _MEMSWAP,
438 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800439 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800440};
441
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700442#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
443#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800444#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700445/* Used for OOM nofiier */
446#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800447
Balbir Singh75822b42009-09-23 15:56:38 -0700448/*
449 * Reclaim flags for mem_cgroup_hierarchical_reclaim
450 */
451#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
452#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
453#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
454#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
455
Glauber Costa09998212013-02-22 16:34:55 -0800456/*
457 * The memcg_create_mutex will be held whenever a new cgroup is created.
458 * As a consequence, any change that needs to protect against new child cgroups
459 * appearing has to hold it as well.
460 */
461static DEFINE_MUTEX(memcg_create_mutex);
462
Wanpeng Lib2145142012-07-31 16:46:01 -0700463struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
464{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400465 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700466}
467
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700468/* Some nice accessors for the vmpressure. */
469struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
470{
471 if (!memcg)
472 memcg = root_mem_cgroup;
473 return &memcg->vmpressure;
474}
475
476struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
477{
478 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
479}
480
481struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
482{
483 return &mem_cgroup_from_css(css)->vmpressure;
484}
485
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700486static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
487{
488 return (memcg == root_mem_cgroup);
489}
490
Li Zefan4219b2d2013-09-23 16:56:29 +0800491/*
492 * We restrict the id in the range of [1, 65535], so it can fit into
493 * an unsigned short.
494 */
495#define MEM_CGROUP_ID_MAX USHRT_MAX
496
Li Zefan34c00c32013-09-23 16:56:01 +0800497static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
498{
499 /*
500 * The ID of the root cgroup is 0, but memcg treat 0 as an
501 * invalid ID, so we return (cgroup_id + 1).
502 */
503 return memcg->css.cgroup->id + 1;
504}
505
506static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
507{
508 struct cgroup_subsys_state *css;
509
510 css = css_from_id(id - 1, &mem_cgroup_subsys);
511 return mem_cgroup_from_css(css);
512}
513
Glauber Costae1aab162011-12-11 21:47:03 +0000514/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700515#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000516
Glauber Costae1aab162011-12-11 21:47:03 +0000517void sock_update_memcg(struct sock *sk)
518{
Glauber Costa376be5f2012-01-20 04:57:14 +0000519 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000520 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700521 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000522
523 BUG_ON(!sk->sk_prot->proto_cgroup);
524
Glauber Costaf3f511e2012-01-05 20:16:39 +0000525 /* Socket cloning can throw us here with sk_cgrp already
526 * filled. It won't however, necessarily happen from
527 * process context. So the test for root memcg given
528 * the current task's memcg won't help us in this case.
529 *
530 * Respecting the original socket's memcg is a better
531 * decision in this case.
532 */
533 if (sk->sk_cgrp) {
534 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700535 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000536 return;
537 }
538
Glauber Costae1aab162011-12-11 21:47:03 +0000539 rcu_read_lock();
540 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700541 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700542 if (!mem_cgroup_is_root(memcg) &&
543 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700544 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000545 }
546 rcu_read_unlock();
547 }
548}
549EXPORT_SYMBOL(sock_update_memcg);
550
551void sock_release_memcg(struct sock *sk)
552{
Glauber Costa376be5f2012-01-20 04:57:14 +0000553 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000554 struct mem_cgroup *memcg;
555 WARN_ON(!sk->sk_cgrp->memcg);
556 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700557 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000558 }
559}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000560
561struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
562{
563 if (!memcg || mem_cgroup_is_root(memcg))
564 return NULL;
565
566 return &memcg->tcp_mem.cg_proto;
567}
568EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000569
Glauber Costa3f134612012-05-29 15:07:11 -0700570static void disarm_sock_keys(struct mem_cgroup *memcg)
571{
572 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
573 return;
574 static_key_slow_dec(&memcg_socket_limit_enabled);
575}
576#else
577static void disarm_sock_keys(struct mem_cgroup *memcg)
578{
579}
580#endif
581
Glauber Costaa8964b92012-12-18 14:22:09 -0800582#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800583/*
584 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800585 * The main reason for not using cgroup id for this:
586 * this works better in sparse environments, where we have a lot of memcgs,
587 * but only a few kmem-limited. Or also, if we have, for instance, 200
588 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
589 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800590 *
591 * The current size of the caches array is stored in
592 * memcg_limited_groups_array_size. It will double each time we have to
593 * increase it.
594 */
595static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800596int memcg_limited_groups_array_size;
597
Glauber Costa55007d82012-12-18 14:22:38 -0800598/*
599 * MIN_SIZE is different than 1, because we would like to avoid going through
600 * the alloc/free process all the time. In a small machine, 4 kmem-limited
601 * cgroups is a reasonable guess. In the future, it could be a parameter or
602 * tunable, but that is strictly not necessary.
603 *
Li Zefanb8627832013-09-23 16:56:47 +0800604 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800605 * this constant directly from cgroup, but it is understandable that this is
606 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800607 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800608 * increase ours as well if it increases.
609 */
610#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800611#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800612
Glauber Costad7f25f82012-12-18 14:22:40 -0800613/*
614 * A lot of the calls to the cache allocation functions are expected to be
615 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
616 * conditional to this static branch, we'll have to allow modules that does
617 * kmem_cache_alloc and the such to see this symbol as well
618 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800619struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800620EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800621
622static void disarm_kmem_keys(struct mem_cgroup *memcg)
623{
Glauber Costa55007d82012-12-18 14:22:38 -0800624 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800625 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800626 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
627 }
Glauber Costabea207c2012-12-18 14:22:11 -0800628 /*
629 * This check can't live in kmem destruction function,
630 * since the charges will outlive the cgroup
631 */
632 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800633}
634#else
635static void disarm_kmem_keys(struct mem_cgroup *memcg)
636{
637}
638#endif /* CONFIG_MEMCG_KMEM */
639
640static void disarm_static_keys(struct mem_cgroup *memcg)
641{
642 disarm_sock_keys(memcg);
643 disarm_kmem_keys(memcg);
644}
645
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700646static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800647
Balbir Singhf64c3f52009-09-23 15:56:37 -0700648static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700649mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700650{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800651 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700652 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653}
654
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700655struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100656{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700657 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100658}
659
Balbir Singhf64c3f52009-09-23 15:56:37 -0700660static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700662{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700663 int nid = page_to_nid(page);
664 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700665
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700666 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700667}
668
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700669/*
670 * Implementation Note: reading percpu statistics for memcg.
671 *
672 * Both of vmstat[] and percpu_counter has threshold and do periodic
673 * synchronization to implement "quick" read. There are trade-off between
674 * reading cost and precision of value. Then, we may have a chance to implement
675 * a periodic synchronizion of counter in memcg's counter.
676 *
677 * But this _read() function is used for user interface now. The user accounts
678 * memory usage by memory cgroup and he _always_ requires exact value because
679 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
680 * have to visit all online cpus and make sum. So, for now, unnecessary
681 * synchronization is not implemented. (just implemented for cpu hotplug)
682 *
683 * If there are kernel internal actions which can make use of some not-exact
684 * value, and reading all cpu value can be performance bottleneck in some
685 * common workload, threashold and synchonization as vmstat[] should be
686 * implemented.
687 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700688static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700689 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800690{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700691 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800692 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800693
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700694 get_online_cpus();
695 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700696 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700697#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700698 spin_lock(&memcg->pcp_counter_lock);
699 val += memcg->nocpu_base.count[idx];
700 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700701#endif
702 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800703 return val;
704}
705
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700706static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700707 bool charge)
708{
709 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700710 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700711}
712
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700713static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700714 enum mem_cgroup_events_index idx)
715{
716 unsigned long val = 0;
717 int cpu;
718
719 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700720 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700721#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722 spin_lock(&memcg->pcp_counter_lock);
723 val += memcg->nocpu_base.events[idx];
724 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700725#endif
726 return val;
727}
728
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700729static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700730 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700731 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800732{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800733 preempt_disable();
734
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700735 /*
736 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
737 * counted as CACHE even if it's on ANON LRU.
738 */
739 if (anon)
740 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700741 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800742 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700743 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700744 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700745
David Rientjesb070e652013-05-07 16:18:09 -0700746 if (PageTransHuge(page))
747 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
748 nr_pages);
749
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800750 /* pagein of a big page is an event. So, ignore page size */
751 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700752 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800753 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700754 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800755 nr_pages = -nr_pages; /* for event */
756 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800757
Johannes Weiner13114712012-05-29 15:07:07 -0700758 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800759
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800760 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800761}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800762
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700763unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700764mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700765{
766 struct mem_cgroup_per_zone *mz;
767
768 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
769 return mz->lru_size[lru];
770}
771
772static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700773mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700774 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700775{
776 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700777 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700778 unsigned long ret = 0;
779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700780 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700781
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700782 for_each_lru(lru) {
783 if (BIT(lru) & lru_mask)
784 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700785 }
786 return ret;
787}
788
789static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700790mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700791 int nid, unsigned int lru_mask)
792{
Ying Han889976d2011-05-26 16:25:33 -0700793 u64 total = 0;
794 int zid;
795
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700796 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700797 total += mem_cgroup_zone_nr_lru_pages(memcg,
798 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700799
Ying Han889976d2011-05-26 16:25:33 -0700800 return total;
801}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700802
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700803static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700804 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800805{
Ying Han889976d2011-05-26 16:25:33 -0700806 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800807 u64 total = 0;
808
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800809 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700810 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800811 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800812}
813
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800814static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
815 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800816{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700817 unsigned long val, next;
818
Johannes Weiner13114712012-05-29 15:07:07 -0700819 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700820 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700821 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800822 if ((long)next - (long)val < 0) {
823 switch (target) {
824 case MEM_CGROUP_TARGET_THRESH:
825 next = val + THRESHOLDS_EVENTS_TARGET;
826 break;
Michal Hocko7d910c02013-09-12 15:13:28 -0700827 case MEM_CGROUP_TARGET_SOFTLIMIT:
828 next = val + SOFTLIMIT_EVENTS_TARGET;
829 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800830 case MEM_CGROUP_TARGET_NUMAINFO:
831 next = val + NUMAINFO_EVENTS_TARGET;
832 break;
833 default:
834 break;
835 }
836 __this_cpu_write(memcg->stat->targets[target], next);
837 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700838 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800839 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800840}
841
842/*
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700843 * Called from rate-limited memcg_check_events when enough
Michal Hocko7d910c02013-09-12 15:13:28 -0700844 * MEM_CGROUP_TARGET_SOFTLIMIT events are accumulated and it makes sure
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700845 * that all the parents up the hierarchy will be notified that this group
Michal Hocko7d910c02013-09-12 15:13:28 -0700846 * is in excess or that it is not in excess anymore. mmecg->soft_contributed
847 * makes the transition a single action whenever the state flips from one to
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700848 * the other.
Michal Hocko7d910c02013-09-12 15:13:28 -0700849 */
850static void mem_cgroup_update_soft_limit(struct mem_cgroup *memcg)
851{
852 unsigned long long excess = res_counter_soft_limit_excess(&memcg->res);
853 struct mem_cgroup *parent = memcg;
854 int delta = 0;
855
856 spin_lock(&memcg->soft_lock);
857 if (excess) {
858 if (!memcg->soft_contributed) {
859 delta = 1;
860 memcg->soft_contributed = true;
861 }
862 } else {
863 if (memcg->soft_contributed) {
864 delta = -1;
865 memcg->soft_contributed = false;
866 }
867 }
868
869 /*
870 * Necessary to update all ancestors when hierarchy is used
871 * because their event counter is not touched.
Michal Hocko1be171d2013-09-12 15:13:32 -0700872 * We track children even outside the hierarchy for the root
873 * cgroup because tree walk starting at root should visit
874 * all cgroups and we want to prevent from pointless tree
875 * walk if no children is below the limit.
Michal Hocko7d910c02013-09-12 15:13:28 -0700876 */
877 while (delta && (parent = parent_mem_cgroup(parent)))
878 atomic_add(delta, &parent->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -0700879 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
880 atomic_add(delta, &root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -0700881 spin_unlock(&memcg->soft_lock);
882}
883
884/*
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800885 * Check events in order.
886 *
887 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700888static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800889{
Steven Rostedt47994012011-11-02 13:38:33 -0700890 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800891 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800892 if (unlikely(mem_cgroup_event_ratelimit(memcg,
893 MEM_CGROUP_TARGET_THRESH))) {
Michal Hocko7d910c02013-09-12 15:13:28 -0700894 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800895 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800896
Michal Hocko7d910c02013-09-12 15:13:28 -0700897 do_softlimit = mem_cgroup_event_ratelimit(memcg,
898 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700899#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800900 do_numainfo = mem_cgroup_event_ratelimit(memcg,
901 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700902#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800903 preempt_enable();
904
905 mem_cgroup_threshold(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -0700906 if (unlikely(do_softlimit))
907 mem_cgroup_update_soft_limit(memcg);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800908#if MAX_NUMNODES > 1
909 if (unlikely(do_numainfo))
910 atomic_inc(&memcg->numainfo_events);
911#endif
912 } else
913 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800914}
915
Balbir Singhcf475ad2008-04-29 01:00:16 -0700916struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800917{
Balbir Singh31a78f22008-09-28 23:09:31 +0100918 /*
919 * mm_update_next_owner() may clear mm->owner to NULL
920 * if it races with swapoff, page migration, etc.
921 * So this can be called with p == NULL.
922 */
923 if (unlikely(!p))
924 return NULL;
925
Tejun Heo8af01f52013-08-08 20:11:22 -0400926 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800927}
928
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700929struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800930{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700931 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700932
933 if (!mm)
934 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800935 /*
936 * Because we have no locks, mm->owner's may be being moved to other
937 * cgroup. We use css_tryget() here even if this looks
938 * pessimistic (rather than adding locks here).
939 */
940 rcu_read_lock();
941 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700942 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
943 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800944 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700945 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800946 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700947 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800948}
949
Michal Hockode577802013-09-12 15:13:26 -0700950static enum mem_cgroup_filter_t
951mem_cgroup_filter(struct mem_cgroup *memcg, struct mem_cgroup *root,
952 mem_cgroup_iter_filter cond)
953{
954 if (!cond)
955 return VISIT;
956 return cond(memcg, root);
957}
958
Michal Hocko16248d82013-04-29 15:07:19 -0700959/*
960 * Returns a next (in a pre-order walk) alive memcg (with elevated css
961 * ref. count) or NULL if the whole root's subtree has been visited.
962 *
963 * helper function to be used by mem_cgroup_iter
964 */
965static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Michal Hockode577802013-09-12 15:13:26 -0700966 struct mem_cgroup *last_visited, mem_cgroup_iter_filter cond)
Michal Hocko16248d82013-04-29 15:07:19 -0700967{
Tejun Heo492eb212013-08-08 20:11:25 -0400968 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700969
Tejun Heobd8815a2013-08-08 20:11:27 -0400970 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -0700971skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -0400972 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -0700973
974 /*
975 * Even if we found a group we have to make sure it is
976 * alive. css && !memcg means that the groups should be
977 * skipped and we should continue the tree walk.
978 * last_visited css is safe to use because it is
979 * protected by css_get and the tree walk is rcu safe.
980 */
Tejun Heo492eb212013-08-08 20:11:25 -0400981 if (next_css) {
982 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
983
Michal Hockode577802013-09-12 15:13:26 -0700984 switch (mem_cgroup_filter(mem, root, cond)) {
985 case SKIP:
Tejun Heo492eb212013-08-08 20:11:25 -0400986 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700987 goto skip_node;
Michal Hockode577802013-09-12 15:13:26 -0700988 case SKIP_TREE:
989 if (mem == root)
990 return NULL;
991 /*
992 * css_rightmost_descendant is not an optimal way to
993 * skip through a subtree (especially for imbalanced
994 * trees leaning to right) but that's what we have right
995 * now. More effective solution would be traversing
996 * right-up for first non-NULL without calling
997 * css_next_descendant_pre afterwards.
998 */
999 prev_css = css_rightmost_descendant(next_css);
1000 goto skip_node;
1001 case VISIT:
1002 if (css_tryget(&mem->css))
1003 return mem;
1004 else {
1005 prev_css = next_css;
1006 goto skip_node;
1007 }
1008 break;
Michal Hocko16248d82013-04-29 15:07:19 -07001009 }
1010 }
1011
1012 return NULL;
1013}
1014
Johannes Weiner519ebea2013-07-03 15:04:51 -07001015static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1016{
1017 /*
1018 * When a group in the hierarchy below root is destroyed, the
1019 * hierarchy iterator can no longer be trusted since it might
1020 * have pointed to the destroyed group. Invalidate it.
1021 */
1022 atomic_inc(&root->dead_count);
1023}
1024
1025static struct mem_cgroup *
1026mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1027 struct mem_cgroup *root,
1028 int *sequence)
1029{
1030 struct mem_cgroup *position = NULL;
1031 /*
1032 * A cgroup destruction happens in two stages: offlining and
1033 * release. They are separated by a RCU grace period.
1034 *
1035 * If the iterator is valid, we may still race with an
1036 * offlining. The RCU lock ensures the object won't be
1037 * released, tryget will fail if we lost the race.
1038 */
1039 *sequence = atomic_read(&root->dead_count);
1040 if (iter->last_dead_count == *sequence) {
1041 smp_rmb();
1042 position = iter->last_visited;
1043 if (position && !css_tryget(&position->css))
1044 position = NULL;
1045 }
1046 return position;
1047}
1048
1049static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1050 struct mem_cgroup *last_visited,
1051 struct mem_cgroup *new_position,
1052 int sequence)
1053{
1054 if (last_visited)
1055 css_put(&last_visited->css);
1056 /*
1057 * We store the sequence count from the time @last_visited was
1058 * loaded successfully instead of rereading it here so that we
1059 * don't lose destruction events in between. We could have
1060 * raced with the destruction of @new_position after all.
1061 */
1062 iter->last_visited = new_position;
1063 smp_wmb();
1064 iter->last_dead_count = sequence;
1065}
1066
Johannes Weiner56600482012-01-12 17:17:59 -08001067/**
1068 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1069 * @root: hierarchy root
1070 * @prev: previously returned memcg, NULL on first invocation
1071 * @reclaim: cookie for shared reclaim walks, NULL for full walks
Michal Hockode577802013-09-12 15:13:26 -07001072 * @cond: filter for visited nodes, NULL for no filter
Johannes Weiner56600482012-01-12 17:17:59 -08001073 *
1074 * Returns references to children of the hierarchy below @root, or
1075 * @root itself, or %NULL after a full round-trip.
1076 *
1077 * Caller must pass the return value in @prev on subsequent
1078 * invocations for reference counting, or use mem_cgroup_iter_break()
1079 * to cancel a hierarchy walk before the round-trip is complete.
1080 *
1081 * Reclaimers can specify a zone and a priority level in @reclaim to
1082 * divide up the memcgs in the hierarchy among all concurrent
1083 * reclaimers operating on the same zone and priority.
1084 */
Michal Hockode577802013-09-12 15:13:26 -07001085struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001086 struct mem_cgroup *prev,
Michal Hockode577802013-09-12 15:13:26 -07001087 struct mem_cgroup_reclaim_cookie *reclaim,
1088 mem_cgroup_iter_filter cond)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001089{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001090 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001091 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001092
Michal Hockode577802013-09-12 15:13:26 -07001093 if (mem_cgroup_disabled()) {
1094 /* first call must return non-NULL, second return NULL */
1095 return (struct mem_cgroup *)(unsigned long)!prev;
1096 }
Johannes Weiner56600482012-01-12 17:17:59 -08001097
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001098 if (!root)
1099 root = root_mem_cgroup;
1100
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001101 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001102 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001103
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001104 if (!root->use_hierarchy && root != root_mem_cgroup) {
1105 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001106 goto out_css_put;
Michal Hockode577802013-09-12 15:13:26 -07001107 if (mem_cgroup_filter(root, root, cond) == VISIT)
1108 return root;
1109 return NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001110 }
1111
Michal Hocko542f85f2013-04-29 15:07:15 -07001112 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001113 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001114 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001115 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001116
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001117 if (reclaim) {
1118 int nid = zone_to_nid(reclaim->zone);
1119 int zid = zone_idx(reclaim->zone);
1120 struct mem_cgroup_per_zone *mz;
1121
1122 mz = mem_cgroup_zoneinfo(root, nid, zid);
1123 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001124 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001125 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001126 goto out_unlock;
1127 }
Michal Hocko5f578162013-04-29 15:07:17 -07001128
Johannes Weiner519ebea2013-07-03 15:04:51 -07001129 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001130 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001131
Michal Hockode577802013-09-12 15:13:26 -07001132 memcg = __mem_cgroup_iter_next(root, last_visited, cond);
Michal Hocko542f85f2013-04-29 15:07:15 -07001133
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001134 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001135 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001136
Michal Hocko19f39402013-04-29 15:07:18 -07001137 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001138 iter->generation++;
1139 else if (!prev && memcg)
1140 reclaim->generation = iter->generation;
1141 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001142
Michal Hockode577802013-09-12 15:13:26 -07001143 /*
1144 * We have finished the whole tree walk or no group has been
1145 * visited because filter told us to skip the root node.
1146 */
1147 if (!memcg && (prev || (cond && !last_visited)))
Michal Hocko542f85f2013-04-29 15:07:15 -07001148 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001149 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001150out_unlock:
1151 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001152out_css_put:
1153 if (prev && prev != root)
1154 css_put(&prev->css);
1155
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001156 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001157}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001158
Johannes Weiner56600482012-01-12 17:17:59 -08001159/**
1160 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1161 * @root: hierarchy root
1162 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1163 */
1164void mem_cgroup_iter_break(struct mem_cgroup *root,
1165 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001166{
1167 if (!root)
1168 root = root_mem_cgroup;
1169 if (prev && prev != root)
1170 css_put(&prev->css);
1171}
1172
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001173/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001174 * Iteration constructs for visiting all cgroups (under a tree). If
1175 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1176 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001177 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001178#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001179 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001180 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001181 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001182
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001183#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001184 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001185 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001186 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001187
David Rientjes68ae5642012-12-12 13:51:57 -08001188void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001189{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001190 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001191
Ying Han456f9982011-05-26 16:25:38 -07001192 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001193 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1194 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001195 goto out;
1196
1197 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001198 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001199 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1200 break;
1201 case PGMAJFAULT:
1202 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001203 break;
1204 default:
1205 BUG();
1206 }
1207out:
1208 rcu_read_unlock();
1209}
David Rientjes68ae5642012-12-12 13:51:57 -08001210EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001211
Johannes Weiner925b7672012-01-12 17:18:15 -08001212/**
1213 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1214 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001215 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001216 *
1217 * Returns the lru list vector holding pages for the given @zone and
1218 * @mem. This can be the global zone lruvec, if the memory controller
1219 * is disabled.
1220 */
1221struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1222 struct mem_cgroup *memcg)
1223{
1224 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001225 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001226
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001227 if (mem_cgroup_disabled()) {
1228 lruvec = &zone->lruvec;
1229 goto out;
1230 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001231
1232 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001233 lruvec = &mz->lruvec;
1234out:
1235 /*
1236 * Since a node can be onlined after the mem_cgroup was created,
1237 * we have to be prepared to initialize lruvec->zone here;
1238 * and if offlined then reonlined, we need to reinitialize it.
1239 */
1240 if (unlikely(lruvec->zone != zone))
1241 lruvec->zone = zone;
1242 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001243}
1244
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001245/*
1246 * Following LRU functions are allowed to be used without PCG_LOCK.
1247 * Operations are called by routine of global LRU independently from memcg.
1248 * What we have to take care of here is validness of pc->mem_cgroup.
1249 *
1250 * Changes to pc->mem_cgroup happens when
1251 * 1. charge
1252 * 2. moving account
1253 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1254 * It is added to LRU before charge.
1255 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1256 * When moving account, the page is not on LRU. It's isolated.
1257 */
1258
Johannes Weiner925b7672012-01-12 17:18:15 -08001259/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001260 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001261 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001262 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001263 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001264struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001265{
1266 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001267 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001268 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001269 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001270
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001271 if (mem_cgroup_disabled()) {
1272 lruvec = &zone->lruvec;
1273 goto out;
1274 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001275
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001276 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001277 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001278
1279 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001280 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001281 * an uncharged page off lru does nothing to secure
1282 * its former mem_cgroup from sudden removal.
1283 *
1284 * Our caller holds lru_lock, and PageCgroupUsed is updated
1285 * under page_cgroup lock: between them, they make all uses
1286 * of pc->mem_cgroup safe.
1287 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001288 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001289 pc->mem_cgroup = memcg = root_mem_cgroup;
1290
Johannes Weiner925b7672012-01-12 17:18:15 -08001291 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001292 lruvec = &mz->lruvec;
1293out:
1294 /*
1295 * Since a node can be onlined after the mem_cgroup was created,
1296 * we have to be prepared to initialize lruvec->zone here;
1297 * and if offlined then reonlined, we need to reinitialize it.
1298 */
1299 if (unlikely(lruvec->zone != zone))
1300 lruvec->zone = zone;
1301 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001302}
1303
1304/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001305 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1306 * @lruvec: mem_cgroup per zone lru vector
1307 * @lru: index of lru list the page is sitting on
1308 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001309 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001310 * This function must be called when a page is added to or removed from an
1311 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001312 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001313void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1314 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001315{
1316 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001317 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001318
1319 if (mem_cgroup_disabled())
1320 return;
1321
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001322 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1323 lru_size = mz->lru_size + lru;
1324 *lru_size += nr_pages;
1325 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001326}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001327
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001328/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001329 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001330 * hierarchy subtree
1331 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001332bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1333 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001334{
Johannes Weiner91c637342012-05-29 15:06:24 -07001335 if (root_memcg == memcg)
1336 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001337 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001338 return false;
Li Zefanb47f77b2013-09-23 16:55:43 +08001339 return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001340}
1341
1342static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1343 struct mem_cgroup *memcg)
1344{
1345 bool ret;
1346
Johannes Weiner91c637342012-05-29 15:06:24 -07001347 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001348 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001349 rcu_read_unlock();
1350 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001351}
1352
David Rientjesffbdccf2013-07-03 15:01:23 -07001353bool task_in_mem_cgroup(struct task_struct *task,
1354 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001355{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001356 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001357 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001358 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001359
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001360 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001361 if (p) {
1362 curr = try_get_mem_cgroup_from_mm(p->mm);
1363 task_unlock(p);
1364 } else {
1365 /*
1366 * All threads may have already detached their mm's, but the oom
1367 * killer still needs to detect if they have already been oom
1368 * killed to prevent needlessly killing additional tasks.
1369 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001370 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001371 curr = mem_cgroup_from_task(task);
1372 if (curr)
1373 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001374 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001375 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001376 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001377 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001378 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001379 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001380 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001381 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1382 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001383 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001384 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001385 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001386 return ret;
1387}
1388
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001389int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001390{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001391 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001392 unsigned long inactive;
1393 unsigned long active;
1394 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001395
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001396 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1397 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001398
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001399 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1400 if (gb)
1401 inactive_ratio = int_sqrt(10 * gb);
1402 else
1403 inactive_ratio = 1;
1404
Johannes Weiner9b272972011-11-02 13:38:23 -07001405 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001406}
1407
Balbir Singh6d61ef42009-01-07 18:08:06 -08001408#define mem_cgroup_from_res_counter(counter, member) \
1409 container_of(counter, struct mem_cgroup, member)
1410
Johannes Weiner19942822011-02-01 15:52:43 -08001411/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001412 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001413 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001414 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001415 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001416 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001417 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001418static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001419{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001420 unsigned long long margin;
1421
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001422 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001423 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001424 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001425 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001426}
1427
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001428int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001429{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001430 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001431 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001432 return vm_swappiness;
1433
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001434 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001435}
1436
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001437/*
1438 * memcg->moving_account is used for checking possibility that some thread is
1439 * calling move_account(). When a thread on CPU-A starts moving pages under
1440 * a memcg, other threads should check memcg->moving_account under
1441 * rcu_read_lock(), like this:
1442 *
1443 * CPU-A CPU-B
1444 * rcu_read_lock()
1445 * memcg->moving_account+1 if (memcg->mocing_account)
1446 * take heavy locks.
1447 * synchronize_rcu() update something.
1448 * rcu_read_unlock()
1449 * start move here.
1450 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001451
1452/* for quick checking without looking up memcg */
1453atomic_t memcg_moving __read_mostly;
1454
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001455static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001456{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001457 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001458 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001459 synchronize_rcu();
1460}
1461
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001462static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001463{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001464 /*
1465 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1466 * We check NULL in callee rather than caller.
1467 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001468 if (memcg) {
1469 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001470 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001471 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001472}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001473
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001474/*
1475 * 2 routines for checking "mem" is under move_account() or not.
1476 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001477 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1478 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001479 * pc->mem_cgroup may be overwritten.
1480 *
1481 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1482 * under hierarchy of moving cgroups. This is for
1483 * waiting at hith-memory prressure caused by "move".
1484 */
1485
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001486static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001487{
1488 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001489 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001490}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001491
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001492static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001493{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001494 struct mem_cgroup *from;
1495 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001496 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001497 /*
1498 * Unlike task_move routines, we access mc.to, mc.from not under
1499 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1500 */
1501 spin_lock(&mc.lock);
1502 from = mc.from;
1503 to = mc.to;
1504 if (!from)
1505 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001506
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001507 ret = mem_cgroup_same_or_subtree(memcg, from)
1508 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001509unlock:
1510 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001511 return ret;
1512}
1513
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001514static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001515{
1516 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001517 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001518 DEFINE_WAIT(wait);
1519 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1520 /* moving charge context might have finished. */
1521 if (mc.moving_task)
1522 schedule();
1523 finish_wait(&mc.waitq, &wait);
1524 return true;
1525 }
1526 }
1527 return false;
1528}
1529
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001530/*
1531 * Take this lock when
1532 * - a code tries to modify page's memcg while it's USED.
1533 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001534 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001535 */
1536static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1537 unsigned long *flags)
1538{
1539 spin_lock_irqsave(&memcg->move_lock, *flags);
1540}
1541
1542static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1543 unsigned long *flags)
1544{
1545 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1546}
1547
Sha Zhengju58cf1882013-02-22 16:32:05 -08001548#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001549/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001550 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001551 * @memcg: The memory cgroup that went over limit
1552 * @p: Task that is going to be killed
1553 *
1554 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1555 * enabled
1556 */
1557void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1558{
1559 struct cgroup *task_cgrp;
1560 struct cgroup *mem_cgrp;
1561 /*
1562 * Need a buffer in BSS, can't rely on allocations. The code relies
1563 * on the assumption that OOM is serialized for memory controller.
1564 * If this assumption is broken, revisit this code.
1565 */
1566 static char memcg_name[PATH_MAX];
1567 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001568 struct mem_cgroup *iter;
1569 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001570
Sha Zhengju58cf1882013-02-22 16:32:05 -08001571 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001572 return;
1573
Balbir Singhe2224322009-04-02 16:57:39 -07001574 rcu_read_lock();
1575
1576 mem_cgrp = memcg->css.cgroup;
1577 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1578
1579 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1580 if (ret < 0) {
1581 /*
1582 * Unfortunately, we are unable to convert to a useful name
1583 * But we'll still print out the usage information
1584 */
1585 rcu_read_unlock();
1586 goto done;
1587 }
1588 rcu_read_unlock();
1589
Andrew Mortond0451972013-02-22 16:32:06 -08001590 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001591
1592 rcu_read_lock();
1593 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1594 if (ret < 0) {
1595 rcu_read_unlock();
1596 goto done;
1597 }
1598 rcu_read_unlock();
1599
1600 /*
1601 * Continues from above, so we don't need an KERN_ level
1602 */
Andrew Mortond0451972013-02-22 16:32:06 -08001603 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001604done:
1605
Andrew Mortond0451972013-02-22 16:32:06 -08001606 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001607 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1608 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1609 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001610 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001611 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1612 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1613 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001614 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001615 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1616 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1617 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001618
1619 for_each_mem_cgroup_tree(iter, memcg) {
1620 pr_info("Memory cgroup stats");
1621
1622 rcu_read_lock();
1623 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1624 if (!ret)
1625 pr_cont(" for %s", memcg_name);
1626 rcu_read_unlock();
1627 pr_cont(":");
1628
1629 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1630 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1631 continue;
1632 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1633 K(mem_cgroup_read_stat(iter, i)));
1634 }
1635
1636 for (i = 0; i < NR_LRU_LISTS; i++)
1637 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1638 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1639
1640 pr_cont("\n");
1641 }
Balbir Singhe2224322009-04-02 16:57:39 -07001642}
1643
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001644/*
1645 * This function returns the number of memcg under hierarchy tree. Returns
1646 * 1(self count) if no children.
1647 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001648static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001649{
1650 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001651 struct mem_cgroup *iter;
1652
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001653 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001654 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001655 return num;
1656}
1657
Balbir Singh6d61ef42009-01-07 18:08:06 -08001658/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001659 * Return the memory (and swap, if configured) limit for a memcg.
1660 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001661static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001662{
1663 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001664
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001665 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001666
David Rientjesa63d83f2010-08-09 17:19:46 -07001667 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001668 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001669 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001670 if (mem_cgroup_swappiness(memcg)) {
1671 u64 memsw;
1672
1673 limit += total_swap_pages << PAGE_SHIFT;
1674 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1675
1676 /*
1677 * If memsw is finite and limits the amount of swap space
1678 * available to this memcg, return that limit.
1679 */
1680 limit = min(limit, memsw);
1681 }
1682
1683 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001684}
1685
David Rientjes19965462012-12-11 16:00:26 -08001686static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1687 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001688{
1689 struct mem_cgroup *iter;
1690 unsigned long chosen_points = 0;
1691 unsigned long totalpages;
1692 unsigned int points = 0;
1693 struct task_struct *chosen = NULL;
1694
David Rientjes876aafb2012-07-31 16:43:48 -07001695 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001696 * If current has a pending SIGKILL or is exiting, then automatically
1697 * select it. The goal is to allow it to allocate so that it may
1698 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001699 */
David Rientjes465adcf2013-04-29 15:08:45 -07001700 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001701 set_thread_flag(TIF_MEMDIE);
1702 return;
1703 }
1704
1705 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001706 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1707 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001708 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001709 struct task_struct *task;
1710
Tejun Heo72ec7022013-08-08 20:11:26 -04001711 css_task_iter_start(&iter->css, &it);
1712 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001713 switch (oom_scan_process_thread(task, totalpages, NULL,
1714 false)) {
1715 case OOM_SCAN_SELECT:
1716 if (chosen)
1717 put_task_struct(chosen);
1718 chosen = task;
1719 chosen_points = ULONG_MAX;
1720 get_task_struct(chosen);
1721 /* fall through */
1722 case OOM_SCAN_CONTINUE:
1723 continue;
1724 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001725 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001726 mem_cgroup_iter_break(memcg, iter);
1727 if (chosen)
1728 put_task_struct(chosen);
1729 return;
1730 case OOM_SCAN_OK:
1731 break;
1732 };
1733 points = oom_badness(task, memcg, NULL, totalpages);
1734 if (points > chosen_points) {
1735 if (chosen)
1736 put_task_struct(chosen);
1737 chosen = task;
1738 chosen_points = points;
1739 get_task_struct(chosen);
1740 }
1741 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001742 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001743 }
1744
1745 if (!chosen)
1746 return;
1747 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001748 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1749 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001750}
1751
Johannes Weiner56600482012-01-12 17:17:59 -08001752static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1753 gfp_t gfp_mask,
1754 unsigned long flags)
1755{
1756 unsigned long total = 0;
1757 bool noswap = false;
1758 int loop;
1759
1760 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1761 noswap = true;
1762 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1763 noswap = true;
1764
1765 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1766 if (loop)
1767 drain_all_stock_async(memcg);
1768 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1769 /*
1770 * Allow limit shrinkers, which are triggered directly
1771 * by userspace, to catch signals and stop reclaim
1772 * after minimal progress, regardless of the margin.
1773 */
1774 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1775 break;
1776 if (mem_cgroup_margin(memcg))
1777 break;
1778 /*
1779 * If nothing was reclaimed after two attempts, there
1780 * may be no reclaimable pages in this hierarchy.
1781 */
1782 if (loop && !total)
1783 break;
1784 }
1785 return total;
1786}
1787
Michal Hockoe8831102013-09-12 15:13:23 -07001788#if MAX_NUMNODES > 1
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001789/**
1790 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001791 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001792 * @nid: the node ID to be checked.
1793 * @noswap : specify true here if the user wants flle only information.
1794 *
1795 * This function returns whether the specified memcg contains any
1796 * reclaimable pages on a node. Returns true if there are any reclaimable
1797 * pages in the node.
1798 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001800 int nid, bool noswap)
1801{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001802 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001803 return true;
1804 if (noswap || !total_swap_pages)
1805 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001806 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001807 return true;
1808 return false;
1809
1810}
Ying Han889976d2011-05-26 16:25:33 -07001811
1812/*
1813 * Always updating the nodemask is not very good - even if we have an empty
1814 * list or the wrong list here, we can start from some node and traverse all
1815 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1816 *
1817 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001818static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001819{
1820 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001821 /*
1822 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1823 * pagein/pageout changes since the last update.
1824 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001825 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001826 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001827 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001828 return;
1829
Ying Han889976d2011-05-26 16:25:33 -07001830 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001831 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001832
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001833 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001834
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001835 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1836 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001837 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001838
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001839 atomic_set(&memcg->numainfo_events, 0);
1840 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001841}
1842
1843/*
1844 * Selecting a node where we start reclaim from. Because what we need is just
1845 * reducing usage counter, start from anywhere is O,K. Considering
1846 * memory reclaim from current node, there are pros. and cons.
1847 *
1848 * Freeing memory from current node means freeing memory from a node which
1849 * we'll use or we've used. So, it may make LRU bad. And if several threads
1850 * hit limits, it will see a contention on a node. But freeing from remote
1851 * node means more costs for memory reclaim because of memory latency.
1852 *
1853 * Now, we use round-robin. Better algorithm is welcomed.
1854 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001856{
1857 int node;
1858
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859 mem_cgroup_may_update_nodemask(memcg);
1860 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001861
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001862 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001863 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001864 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001865 /*
1866 * We call this when we hit limit, not when pages are added to LRU.
1867 * No LRU may hold pages because all pages are UNEVICTABLE or
1868 * memcg is too small and all pages are not on LRU. In that case,
1869 * we use curret node.
1870 */
1871 if (unlikely(node == MAX_NUMNODES))
1872 node = numa_node_id();
1873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001874 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001875 return node;
1876}
1877
1878#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001879int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001880{
1881 return 0;
1882}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001883
Ying Han889976d2011-05-26 16:25:33 -07001884#endif
1885
Michal Hocko3b387222013-09-12 15:13:21 -07001886/*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001887 * A group is eligible for the soft limit reclaim under the given root
1888 * hierarchy if
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001889 * a) it is over its soft limit
1890 * b) any parent up the hierarchy is over its soft limit
Michal Hocko7d910c02013-09-12 15:13:28 -07001891 *
1892 * If the given group doesn't have any children over the limit then it
1893 * doesn't make any sense to iterate its subtree.
Michal Hocko3b387222013-09-12 15:13:21 -07001894 */
Michal Hockode577802013-09-12 15:13:26 -07001895enum mem_cgroup_filter_t
1896mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -07001897 struct mem_cgroup *root)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001898{
Michal Hockoe839b6a2013-09-12 15:13:30 -07001899 struct mem_cgroup *parent;
1900
1901 if (!memcg)
1902 memcg = root_mem_cgroup;
1903 parent = memcg;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001904
Michal Hocko3b387222013-09-12 15:13:21 -07001905 if (res_counter_soft_limit_excess(&memcg->res))
Michal Hockode577802013-09-12 15:13:26 -07001906 return VISIT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001907
Michal Hocko3b387222013-09-12 15:13:21 -07001908 /*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001909 * If any parent up to the root in the hierarchy is over its soft limit
1910 * then we have to obey and reclaim from this group as well.
Michal Hocko3b387222013-09-12 15:13:21 -07001911 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001912 while ((parent = parent_mem_cgroup(parent))) {
Michal Hocko3b387222013-09-12 15:13:21 -07001913 if (res_counter_soft_limit_excess(&parent->res))
Michal Hockode577802013-09-12 15:13:26 -07001914 return VISIT;
Michal Hockoa5b7c872013-09-12 15:13:25 -07001915 if (parent == root)
1916 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001917 }
Michal Hocko3b387222013-09-12 15:13:21 -07001918
Michal Hocko7d910c02013-09-12 15:13:28 -07001919 if (!atomic_read(&memcg->children_in_excess))
1920 return SKIP_TREE;
Michal Hockode577802013-09-12 15:13:26 -07001921 return SKIP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001922}
1923
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001924static DEFINE_SPINLOCK(memcg_oom_lock);
1925
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001926/*
1927 * Check OOM-Killer is already running under our hierarchy.
1928 * If someone is running, return false.
1929 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001930static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001931{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001932 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001933
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001934 spin_lock(&memcg_oom_lock);
1935
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001936 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001937 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001938 /*
1939 * this subtree of our hierarchy is already locked
1940 * so we cannot give a lock.
1941 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001942 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001943 mem_cgroup_iter_break(memcg, iter);
1944 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001945 } else
1946 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001947 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001948
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001949 if (failed) {
1950 /*
1951 * OK, we failed to lock the whole subtree so we have
1952 * to clean up what we set up to the failing subtree
1953 */
1954 for_each_mem_cgroup_tree(iter, memcg) {
1955 if (iter == failed) {
1956 mem_cgroup_iter_break(memcg, iter);
1957 break;
1958 }
1959 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001960 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07001961 }
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001962
1963 spin_unlock(&memcg_oom_lock);
1964
1965 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001966}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001967
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001968static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001969{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001970 struct mem_cgroup *iter;
1971
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001972 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001973 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001974 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001975 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001976}
1977
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001978static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001979{
1980 struct mem_cgroup *iter;
1981
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001982 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001983 atomic_inc(&iter->under_oom);
1984}
1985
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001986static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001987{
1988 struct mem_cgroup *iter;
1989
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001990 /*
1991 * When a new child is created while the hierarchy is under oom,
1992 * mem_cgroup_oom_lock() may not be called. We have to use
1993 * atomic_add_unless() here.
1994 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001995 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001996 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001997}
1998
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001999static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2000
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002001struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002002 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002003 wait_queue_t wait;
2004};
2005
2006static int memcg_oom_wake_function(wait_queue_t *wait,
2007 unsigned mode, int sync, void *arg)
2008{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002009 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2010 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002011 struct oom_wait_info *oom_wait_info;
2012
2013 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002014 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002015
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002016 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002017 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002018 * Then we can use css_is_ancestor without taking care of RCU.
2019 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002020 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2021 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002022 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002023 return autoremove_wake_function(wait, mode, sync, arg);
2024}
2025
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002026static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002027{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002028 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002029 /* for filtering, pass "memcg" as argument. */
2030 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002031}
2032
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002033static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002034{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002035 if (memcg && atomic_read(&memcg->under_oom))
2036 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002037}
2038
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002039/*
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002040 * try to call OOM killer
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002041 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002042static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002043{
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002044 bool locked;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002045 int wakeups;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002046
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002047 if (!current->memcg_oom.may_oom)
2048 return;
2049
2050 current->memcg_oom.in_memcg_oom = 1;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002051
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002052 /*
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002053 * As with any blocking lock, a contender needs to start
2054 * listening for wakeups before attempting the trylock,
2055 * otherwise it can miss the wakeup from the unlock and sleep
2056 * indefinitely. This is just open-coded because our locking
2057 * is so particular to memcg hierarchies.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002058 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002059 wakeups = atomic_read(&memcg->oom_wakeups);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002060 mem_cgroup_mark_under_oom(memcg);
2061
2062 locked = mem_cgroup_oom_trylock(memcg);
2063
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002064 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002065 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002066
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002067 if (locked && !memcg->oom_kill_disable) {
2068 mem_cgroup_unmark_under_oom(memcg);
David Rientjese845e192012-03-21 16:34:10 -07002069 mem_cgroup_out_of_memory(memcg, mask, order);
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002070 mem_cgroup_oom_unlock(memcg);
2071 /*
2072 * There is no guarantee that an OOM-lock contender
2073 * sees the wakeups triggered by the OOM kill
2074 * uncharges. Wake any sleepers explicitely.
2075 */
2076 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002077 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002078 /*
2079 * A system call can just return -ENOMEM, but if this
2080 * is a page fault and somebody else is handling the
2081 * OOM already, we need to sleep on the OOM waitqueue
2082 * for this memcg until the situation is resolved.
2083 * Which can take some time because it might be
2084 * handled by a userspace task.
2085 *
2086 * However, this is the charge context, which means
2087 * that we may sit on a large call stack and hold
2088 * various filesystem locks, the mmap_sem etc. and we
2089 * don't want the OOM handler to deadlock on them
2090 * while we sit here and wait. Store the current OOM
2091 * context in the task_struct, then return -ENOMEM.
2092 * At the end of the page fault handler, with the
2093 * stack unwound, pagefault_out_of_memory() will check
2094 * back with us by calling
2095 * mem_cgroup_oom_synchronize(), possibly putting the
2096 * task to sleep.
2097 */
2098 current->memcg_oom.oom_locked = locked;
2099 current->memcg_oom.wakeups = wakeups;
2100 css_get(&memcg->css);
2101 current->memcg_oom.wait_on_memcg = memcg;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002102 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002103}
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002104
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002105/**
2106 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2107 *
2108 * This has to be called at the end of a page fault if the the memcg
2109 * OOM handler was enabled and the fault is returning %VM_FAULT_OOM.
2110 *
2111 * Memcg supports userspace OOM handling, so failed allocations must
2112 * sleep on a waitqueue until the userspace task resolves the
2113 * situation. Sleeping directly in the charge context with all kinds
2114 * of locks held is not a good idea, instead we remember an OOM state
2115 * in the task and mem_cgroup_oom_synchronize() has to be called at
2116 * the end of the page fault to put the task to sleep and clean up the
2117 * OOM state.
2118 *
2119 * Returns %true if an ongoing memcg OOM situation was detected and
2120 * finalized, %false otherwise.
2121 */
2122bool mem_cgroup_oom_synchronize(void)
2123{
2124 struct oom_wait_info owait;
2125 struct mem_cgroup *memcg;
2126
2127 /* OOM is global, do not handle */
2128 if (!current->memcg_oom.in_memcg_oom)
2129 return false;
2130
2131 /*
2132 * We invoked the OOM killer but there is a chance that a kill
2133 * did not free up any charges. Everybody else might already
2134 * be sleeping, so restart the fault and keep the rampage
2135 * going until some charges are released.
2136 */
2137 memcg = current->memcg_oom.wait_on_memcg;
2138 if (!memcg)
2139 goto out;
2140
2141 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2142 goto out_memcg;
2143
2144 owait.memcg = memcg;
2145 owait.wait.flags = 0;
2146 owait.wait.func = memcg_oom_wake_function;
2147 owait.wait.private = current;
2148 INIT_LIST_HEAD(&owait.wait.task_list);
2149
2150 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
2151 /* Only sleep if we didn't miss any wakeups since OOM */
2152 if (atomic_read(&memcg->oom_wakeups) == current->memcg_oom.wakeups)
2153 schedule();
2154 finish_wait(&memcg_oom_waitq, &owait.wait);
2155out_memcg:
2156 mem_cgroup_unmark_under_oom(memcg);
2157 if (current->memcg_oom.oom_locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002158 mem_cgroup_oom_unlock(memcg);
2159 /*
2160 * There is no guarantee that an OOM-lock contender
2161 * sees the wakeups triggered by the OOM kill
2162 * uncharges. Wake any sleepers explicitely.
2163 */
2164 memcg_oom_recover(memcg);
2165 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002166 css_put(&memcg->css);
2167 current->memcg_oom.wait_on_memcg = NULL;
2168out:
2169 current->memcg_oom.in_memcg_oom = 0;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002170 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002171}
2172
Balbir Singhd69b0422009-06-17 16:26:34 -07002173/*
2174 * Currently used to update mapped file statistics, but the routine can be
2175 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002176 *
2177 * Notes: Race condition
2178 *
2179 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2180 * it tends to be costly. But considering some conditions, we doesn't need
2181 * to do so _always_.
2182 *
2183 * Considering "charge", lock_page_cgroup() is not required because all
2184 * file-stat operations happen after a page is attached to radix-tree. There
2185 * are no race with "charge".
2186 *
2187 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2188 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2189 * if there are race with "uncharge". Statistics itself is properly handled
2190 * by flags.
2191 *
2192 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002193 * small, we check mm->moving_account and detect there are possibility of race
2194 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002195 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002196
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002197void __mem_cgroup_begin_update_page_stat(struct page *page,
2198 bool *locked, unsigned long *flags)
2199{
2200 struct mem_cgroup *memcg;
2201 struct page_cgroup *pc;
2202
2203 pc = lookup_page_cgroup(page);
2204again:
2205 memcg = pc->mem_cgroup;
2206 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2207 return;
2208 /*
2209 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002210 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002211 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002212 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002213 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002214 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002215 return;
2216
2217 move_lock_mem_cgroup(memcg, flags);
2218 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2219 move_unlock_mem_cgroup(memcg, flags);
2220 goto again;
2221 }
2222 *locked = true;
2223}
2224
2225void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2226{
2227 struct page_cgroup *pc = lookup_page_cgroup(page);
2228
2229 /*
2230 * It's guaranteed that pc->mem_cgroup never changes while
2231 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002232 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002233 */
2234 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2235}
2236
Greg Thelen2a7106f2011-01-13 15:47:37 -08002237void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002238 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002239{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002240 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002241 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002242 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002243
Johannes Weinercfa44942012-01-12 17:18:38 -08002244 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002245 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002246
Sha Zhengju658b72c2013-09-12 15:13:52 -07002247 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002248 memcg = pc->mem_cgroup;
2249 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002250 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002251
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002252 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002253}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002254
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002255/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002256 * size of first charge trial. "32" comes from vmscan.c's magic value.
2257 * TODO: maybe necessary to use big numbers in big irons.
2258 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002259#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002260struct memcg_stock_pcp {
2261 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002262 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002263 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002264 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002265#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002266};
2267static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002268static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002270/**
2271 * consume_stock: Try to consume stocked charge on this cpu.
2272 * @memcg: memcg to consume from.
2273 * @nr_pages: how many pages to charge.
2274 *
2275 * The charges will only happen if @memcg matches the current cpu's memcg
2276 * stock, and at least @nr_pages are available in that stock. Failure to
2277 * service an allocation will refill the stock.
2278 *
2279 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002280 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002281static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002282{
2283 struct memcg_stock_pcp *stock;
2284 bool ret = true;
2285
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002286 if (nr_pages > CHARGE_BATCH)
2287 return false;
2288
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002289 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002290 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2291 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002292 else /* need to call res_counter_charge */
2293 ret = false;
2294 put_cpu_var(memcg_stock);
2295 return ret;
2296}
2297
2298/*
2299 * Returns stocks cached in percpu to res_counter and reset cached information.
2300 */
2301static void drain_stock(struct memcg_stock_pcp *stock)
2302{
2303 struct mem_cgroup *old = stock->cached;
2304
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002305 if (stock->nr_pages) {
2306 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2307
2308 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002309 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002310 res_counter_uncharge(&old->memsw, bytes);
2311 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002312 }
2313 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002314}
2315
2316/*
2317 * This must be called under preempt disabled or must be called by
2318 * a thread which is pinned to local cpu.
2319 */
2320static void drain_local_stock(struct work_struct *dummy)
2321{
2322 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2323 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002324 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002325}
2326
Michal Hockoe4777492013-02-22 16:35:40 -08002327static void __init memcg_stock_init(void)
2328{
2329 int cpu;
2330
2331 for_each_possible_cpu(cpu) {
2332 struct memcg_stock_pcp *stock =
2333 &per_cpu(memcg_stock, cpu);
2334 INIT_WORK(&stock->work, drain_local_stock);
2335 }
2336}
2337
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002338/*
2339 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002340 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002341 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002342static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002343{
2344 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2345
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002346 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002347 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002348 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002349 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002350 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002351 put_cpu_var(memcg_stock);
2352}
2353
2354/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002355 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002356 * of the hierarchy under it. sync flag says whether we should block
2357 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002358 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002359static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002360{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002361 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002362
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002363 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002364 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002365 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002366 for_each_online_cpu(cpu) {
2367 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002368 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002369
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002370 memcg = stock->cached;
2371 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002372 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002374 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002375 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2376 if (cpu == curcpu)
2377 drain_local_stock(&stock->work);
2378 else
2379 schedule_work_on(cpu, &stock->work);
2380 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002381 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002382 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002383
2384 if (!sync)
2385 goto out;
2386
2387 for_each_online_cpu(cpu) {
2388 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002389 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002390 flush_work(&stock->work);
2391 }
2392out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002393 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002394}
2395
2396/*
2397 * Tries to drain stocked charges in other cpus. This function is asynchronous
2398 * and just put a work per cpu for draining localy on each cpu. Caller can
2399 * expects some charges will be back to res_counter later but cannot wait for
2400 * it.
2401 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002403{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002404 /*
2405 * If someone calls draining, avoid adding more kworker runs.
2406 */
2407 if (!mutex_trylock(&percpu_charge_mutex))
2408 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002409 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002410 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002411}
2412
2413/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002414static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002415{
2416 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002417 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002418 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002419 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002420}
2421
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002422/*
2423 * This function drains percpu counter value from DEAD cpu and
2424 * move it to local cpu. Note that this function can be preempted.
2425 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002426static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002427{
2428 int i;
2429
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002430 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002431 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002432 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002433
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002434 per_cpu(memcg->stat->count[i], cpu) = 0;
2435 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002436 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002437 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002438 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002439
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002440 per_cpu(memcg->stat->events[i], cpu) = 0;
2441 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002442 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002444}
2445
Paul Gortmaker0db06282013-06-19 14:53:51 -04002446static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002447 unsigned long action,
2448 void *hcpu)
2449{
2450 int cpu = (unsigned long)hcpu;
2451 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002452 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002453
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002454 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002455 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002456
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002457 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002458 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002459
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002460 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002461 mem_cgroup_drain_pcp_counter(iter, cpu);
2462
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002463 stock = &per_cpu(memcg_stock, cpu);
2464 drain_stock(stock);
2465 return NOTIFY_OK;
2466}
2467
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002468
2469/* See __mem_cgroup_try_charge() for details */
2470enum {
2471 CHARGE_OK, /* success */
2472 CHARGE_RETRY, /* need to retry but retry is not bad */
2473 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2474 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002475};
2476
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002478 unsigned int nr_pages, unsigned int min_pages,
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002479 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002480{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002481 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002482 struct mem_cgroup *mem_over_limit;
2483 struct res_counter *fail_res;
2484 unsigned long flags = 0;
2485 int ret;
2486
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002487 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002488
2489 if (likely(!ret)) {
2490 if (!do_swap_account)
2491 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002492 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002493 if (likely(!ret))
2494 return CHARGE_OK;
2495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002496 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002497 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2498 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2499 } else
2500 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002501 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002502 * Never reclaim on behalf of optional batching, retry with a
2503 * single page instead.
2504 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002505 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002506 return CHARGE_RETRY;
2507
2508 if (!(gfp_mask & __GFP_WAIT))
2509 return CHARGE_WOULDBLOCK;
2510
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002511 if (gfp_mask & __GFP_NORETRY)
2512 return CHARGE_NOMEM;
2513
Johannes Weiner56600482012-01-12 17:17:59 -08002514 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002515 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002516 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002517 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002518 * Even though the limit is exceeded at this point, reclaim
2519 * may have been able to free some pages. Retry the charge
2520 * before killing the task.
2521 *
2522 * Only for regular pages, though: huge pages are rather
2523 * unlikely to succeed so close to the limit, and we fall back
2524 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002525 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002526 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002527 return CHARGE_RETRY;
2528
2529 /*
2530 * At task move, charge accounts can be doubly counted. So, it's
2531 * better to wait until the end of task_move if something is going on.
2532 */
2533 if (mem_cgroup_wait_acct_move(mem_over_limit))
2534 return CHARGE_RETRY;
2535
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002536 if (invoke_oom)
2537 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002538
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002539 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002540}
2541
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002542/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002543 * __mem_cgroup_try_charge() does
2544 * 1. detect memcg to be charged against from passed *mm and *ptr,
2545 * 2. update res_counter
2546 * 3. call memory reclaim if necessary.
2547 *
2548 * In some special case, if the task is fatal, fatal_signal_pending() or
2549 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2550 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2551 * as possible without any hazards. 2: all pages should have a valid
2552 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2553 * pointer, that is treated as a charge to root_mem_cgroup.
2554 *
2555 * So __mem_cgroup_try_charge() will return
2556 * 0 ... on success, filling *ptr with a valid memcg pointer.
2557 * -ENOMEM ... charge failure because of resource limits.
2558 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2559 *
2560 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2561 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002562 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002563static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002564 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002565 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002567 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002568{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002569 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002570 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002571 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002572 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002573
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002574 /*
2575 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2576 * in system level. So, allow to go ahead dying process in addition to
2577 * MEMDIE process.
2578 */
2579 if (unlikely(test_thread_flag(TIF_MEMDIE)
2580 || fatal_signal_pending(current)))
2581 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002582
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002583 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002584 * We always charge the cgroup the mm_struct belongs to.
2585 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002586 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002587 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002588 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002589 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002590 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002591again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002592 if (*ptr) { /* css should be a valid one */
2593 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002594 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002595 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002596 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002597 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002598 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002599 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002600 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002601
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002602 rcu_read_lock();
2603 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002604 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002605 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002606 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002607 * race with swapoff. Then, we have small risk of mis-accouning.
2608 * But such kind of mis-account by race always happens because
2609 * we don't have cgroup_mutex(). It's overkill and we allo that
2610 * small race, here.
2611 * (*) swapoff at el will charge against mm-struct not against
2612 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002613 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002614 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002615 if (!memcg)
2616 memcg = root_mem_cgroup;
2617 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002618 rcu_read_unlock();
2619 goto done;
2620 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002621 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002622 /*
2623 * It seems dagerous to access memcg without css_get().
2624 * But considering how consume_stok works, it's not
2625 * necessary. If consume_stock success, some charges
2626 * from this memcg are cached on this cpu. So, we
2627 * don't need to call css_get()/css_tryget() before
2628 * calling consume_stock().
2629 */
2630 rcu_read_unlock();
2631 goto done;
2632 }
2633 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002634 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002635 rcu_read_unlock();
2636 goto again;
2637 }
2638 rcu_read_unlock();
2639 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002640
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002641 do {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002642 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002643
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002644 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002645 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002646 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002647 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002648 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002649
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002650 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2651 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002652 switch (ret) {
2653 case CHARGE_OK:
2654 break;
2655 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002656 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002657 css_put(&memcg->css);
2658 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002659 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002660 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002661 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002662 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002663 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002664 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002665 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002666 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002667 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002668 nr_oom_retries--;
2669 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002670 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002671 } while (ret != CHARGE_OK);
2672
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002673 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002674 refill_stock(memcg, batch - nr_pages);
2675 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002676done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002677 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002678 return 0;
2679nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002680 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002681 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002682bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002683 *ptr = root_mem_cgroup;
2684 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002685}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002686
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002687/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002688 * Somemtimes we have to undo a charge we got by try_charge().
2689 * This function is for that and do uncharge, put css's refcnt.
2690 * gotten by try_charge().
2691 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002692static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002693 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002694{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002695 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002696 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002697
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002698 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002699 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002700 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002701 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002702}
2703
2704/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002705 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2706 * This is useful when moving usage to parent cgroup.
2707 */
2708static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2709 unsigned int nr_pages)
2710{
2711 unsigned long bytes = nr_pages * PAGE_SIZE;
2712
2713 if (mem_cgroup_is_root(memcg))
2714 return;
2715
2716 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2717 if (do_swap_account)
2718 res_counter_uncharge_until(&memcg->memsw,
2719 memcg->memsw.parent, bytes);
2720}
2721
2722/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002723 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002724 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2725 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2726 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002727 */
2728static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2729{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002730 /* ID 0 is unused ID */
2731 if (!id)
2732 return NULL;
Li Zefan34c00c32013-09-23 16:56:01 +08002733 return mem_cgroup_from_id(id);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002734}
2735
Wu Fengguange42d9d52009-12-16 12:19:59 +01002736struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002737{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002738 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002739 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002740 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002741 swp_entry_t ent;
2742
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002743 VM_BUG_ON(!PageLocked(page));
2744
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002745 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002746 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002747 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002748 memcg = pc->mem_cgroup;
2749 if (memcg && !css_tryget(&memcg->css))
2750 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002751 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002752 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002753 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002754 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002755 memcg = mem_cgroup_lookup(id);
2756 if (memcg && !css_tryget(&memcg->css))
2757 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002758 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002759 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002760 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002761 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002762}
2763
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002764static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002765 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002766 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002767 enum charge_type ctype,
2768 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002769{
Johannes Weinerce587e62012-04-24 20:22:33 +02002770 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002771 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002772 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002773 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002774 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002775
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002776 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002777 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002778 /*
2779 * we don't need page_cgroup_lock about tail pages, becase they are not
2780 * accessed by any other context at this point.
2781 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002782
2783 /*
2784 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2785 * may already be on some other mem_cgroup's LRU. Take care of it.
2786 */
2787 if (lrucare) {
2788 zone = page_zone(page);
2789 spin_lock_irq(&zone->lru_lock);
2790 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002791 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002792 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002793 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002794 was_on_lru = true;
2795 }
2796 }
2797
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002798 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002799 /*
2800 * We access a page_cgroup asynchronously without lock_page_cgroup().
2801 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2802 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2803 * before USED bit, we need memory barrier here.
2804 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002805 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002806 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002807 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002808
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002809 if (lrucare) {
2810 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002811 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002812 VM_BUG_ON(PageLRU(page));
2813 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002814 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002815 }
2816 spin_unlock_irq(&zone->lru_lock);
2817 }
2818
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002819 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002820 anon = true;
2821 else
2822 anon = false;
2823
David Rientjesb070e652013-05-07 16:18:09 -07002824 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002825 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002826
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002827 /*
Michal Hockoe8831102013-09-12 15:13:23 -07002828 * "charge_statistics" updated event counter.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002829 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002830 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002831}
2832
Glauber Costa7cf27982012-12-18 14:22:55 -08002833static DEFINE_MUTEX(set_limit_mutex);
2834
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002835#ifdef CONFIG_MEMCG_KMEM
2836static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2837{
2838 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2839 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2840}
2841
Glauber Costa1f458cb2012-12-18 14:22:50 -08002842/*
2843 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2844 * in the memcg_cache_params struct.
2845 */
2846static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2847{
2848 struct kmem_cache *cachep;
2849
2850 VM_BUG_ON(p->is_root_cache);
2851 cachep = p->root_cache;
2852 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2853}
2854
Glauber Costa749c5412012-12-18 14:23:01 -08002855#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002856static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2857 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002858{
Tejun Heo182446d2013-08-08 20:11:24 -04002859 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002860 struct memcg_cache_params *params;
2861
2862 if (!memcg_can_account_kmem(memcg))
2863 return -EIO;
2864
2865 print_slabinfo_header(m);
2866
2867 mutex_lock(&memcg->slab_caches_mutex);
2868 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2869 cache_show(memcg_params_to_cache(params), m);
2870 mutex_unlock(&memcg->slab_caches_mutex);
2871
2872 return 0;
2873}
2874#endif
2875
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002876static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2877{
2878 struct res_counter *fail_res;
2879 struct mem_cgroup *_memcg;
2880 int ret = 0;
2881 bool may_oom;
2882
2883 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2884 if (ret)
2885 return ret;
2886
2887 /*
2888 * Conditions under which we can wait for the oom_killer. Those are
2889 * the same conditions tested by the core page allocator
2890 */
2891 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2892
2893 _memcg = memcg;
2894 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2895 &_memcg, may_oom);
2896
2897 if (ret == -EINTR) {
2898 /*
2899 * __mem_cgroup_try_charge() chosed to bypass to root due to
2900 * OOM kill or fatal signal. Since our only options are to
2901 * either fail the allocation or charge it to this cgroup, do
2902 * it as a temporary condition. But we can't fail. From a
2903 * kmem/slab perspective, the cache has already been selected,
2904 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2905 * our minds.
2906 *
2907 * This condition will only trigger if the task entered
2908 * memcg_charge_kmem in a sane state, but was OOM-killed during
2909 * __mem_cgroup_try_charge() above. Tasks that were already
2910 * dying when the allocation triggers should have been already
2911 * directed to the root cgroup in memcontrol.h
2912 */
2913 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2914 if (do_swap_account)
2915 res_counter_charge_nofail(&memcg->memsw, size,
2916 &fail_res);
2917 ret = 0;
2918 } else if (ret)
2919 res_counter_uncharge(&memcg->kmem, size);
2920
2921 return ret;
2922}
2923
2924static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2925{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002926 res_counter_uncharge(&memcg->res, size);
2927 if (do_swap_account)
2928 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002929
2930 /* Not down to 0 */
2931 if (res_counter_uncharge(&memcg->kmem, size))
2932 return;
2933
Li Zefan10d5ebf2013-07-08 16:00:33 -07002934 /*
2935 * Releases a reference taken in kmem_cgroup_css_offline in case
2936 * this last uncharge is racing with the offlining code or it is
2937 * outliving the memcg existence.
2938 *
2939 * The memory barrier imposed by test&clear is paired with the
2940 * explicit one in memcg_kmem_mark_dead().
2941 */
Glauber Costa7de37682012-12-18 14:22:07 -08002942 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002943 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002944}
2945
Glauber Costa2633d7a2012-12-18 14:22:34 -08002946void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2947{
2948 if (!memcg)
2949 return;
2950
2951 mutex_lock(&memcg->slab_caches_mutex);
2952 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2953 mutex_unlock(&memcg->slab_caches_mutex);
2954}
2955
2956/*
2957 * helper for acessing a memcg's index. It will be used as an index in the
2958 * child cache array in kmem_cache, and also to derive its name. This function
2959 * will return -1 when this is not a kmem-limited memcg.
2960 */
2961int memcg_cache_id(struct mem_cgroup *memcg)
2962{
2963 return memcg ? memcg->kmemcg_id : -1;
2964}
2965
Glauber Costa55007d82012-12-18 14:22:38 -08002966/*
2967 * This ends up being protected by the set_limit mutex, during normal
2968 * operation, because that is its main call site.
2969 *
2970 * But when we create a new cache, we can call this as well if its parent
2971 * is kmem-limited. That will have to hold set_limit_mutex as well.
2972 */
2973int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2974{
2975 int num, ret;
2976
2977 num = ida_simple_get(&kmem_limited_groups,
2978 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2979 if (num < 0)
2980 return num;
2981 /*
2982 * After this point, kmem_accounted (that we test atomically in
2983 * the beginning of this conditional), is no longer 0. This
2984 * guarantees only one process will set the following boolean
2985 * to true. We don't need test_and_set because we're protected
2986 * by the set_limit_mutex anyway.
2987 */
2988 memcg_kmem_set_activated(memcg);
2989
2990 ret = memcg_update_all_caches(num+1);
2991 if (ret) {
2992 ida_simple_remove(&kmem_limited_groups, num);
2993 memcg_kmem_clear_activated(memcg);
2994 return ret;
2995 }
2996
2997 memcg->kmemcg_id = num;
2998 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2999 mutex_init(&memcg->slab_caches_mutex);
3000 return 0;
3001}
3002
3003static size_t memcg_caches_array_size(int num_groups)
3004{
3005 ssize_t size;
3006 if (num_groups <= 0)
3007 return 0;
3008
3009 size = 2 * num_groups;
3010 if (size < MEMCG_CACHES_MIN_SIZE)
3011 size = MEMCG_CACHES_MIN_SIZE;
3012 else if (size > MEMCG_CACHES_MAX_SIZE)
3013 size = MEMCG_CACHES_MAX_SIZE;
3014
3015 return size;
3016}
3017
3018/*
3019 * We should update the current array size iff all caches updates succeed. This
3020 * can only be done from the slab side. The slab mutex needs to be held when
3021 * calling this.
3022 */
3023void memcg_update_array_size(int num)
3024{
3025 if (num > memcg_limited_groups_array_size)
3026 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3027}
3028
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003029static void kmem_cache_destroy_work_func(struct work_struct *w);
3030
Glauber Costa55007d82012-12-18 14:22:38 -08003031int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3032{
3033 struct memcg_cache_params *cur_params = s->memcg_params;
3034
3035 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3036
3037 if (num_groups > memcg_limited_groups_array_size) {
3038 int i;
3039 ssize_t size = memcg_caches_array_size(num_groups);
3040
3041 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003042 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003043
3044 s->memcg_params = kzalloc(size, GFP_KERNEL);
3045 if (!s->memcg_params) {
3046 s->memcg_params = cur_params;
3047 return -ENOMEM;
3048 }
3049
3050 s->memcg_params->is_root_cache = true;
3051
3052 /*
3053 * There is the chance it will be bigger than
3054 * memcg_limited_groups_array_size, if we failed an allocation
3055 * in a cache, in which case all caches updated before it, will
3056 * have a bigger array.
3057 *
3058 * But if that is the case, the data after
3059 * memcg_limited_groups_array_size is certainly unused
3060 */
3061 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3062 if (!cur_params->memcg_caches[i])
3063 continue;
3064 s->memcg_params->memcg_caches[i] =
3065 cur_params->memcg_caches[i];
3066 }
3067
3068 /*
3069 * Ideally, we would wait until all caches succeed, and only
3070 * then free the old one. But this is not worth the extra
3071 * pointer per-cache we'd have to have for this.
3072 *
3073 * It is not a big deal if some caches are left with a size
3074 * bigger than the others. And all updates will reset this
3075 * anyway.
3076 */
3077 kfree(cur_params);
3078 }
3079 return 0;
3080}
3081
Glauber Costa943a4512012-12-18 14:23:03 -08003082int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3083 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003084{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003085 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003086
3087 if (!memcg_kmem_enabled())
3088 return 0;
3089
Andrey Vagin90c7a792013-09-11 14:22:18 -07003090 if (!memcg) {
3091 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003092 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003093 } else
3094 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003095
Glauber Costa2633d7a2012-12-18 14:22:34 -08003096 s->memcg_params = kzalloc(size, GFP_KERNEL);
3097 if (!s->memcg_params)
3098 return -ENOMEM;
3099
Glauber Costa943a4512012-12-18 14:23:03 -08003100 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003101 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003102 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003103 INIT_WORK(&s->memcg_params->destroy,
3104 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003105 } else
3106 s->memcg_params->is_root_cache = true;
3107
Glauber Costa2633d7a2012-12-18 14:22:34 -08003108 return 0;
3109}
3110
3111void memcg_release_cache(struct kmem_cache *s)
3112{
Glauber Costad7f25f82012-12-18 14:22:40 -08003113 struct kmem_cache *root;
3114 struct mem_cgroup *memcg;
3115 int id;
3116
3117 /*
3118 * This happens, for instance, when a root cache goes away before we
3119 * add any memcg.
3120 */
3121 if (!s->memcg_params)
3122 return;
3123
3124 if (s->memcg_params->is_root_cache)
3125 goto out;
3126
3127 memcg = s->memcg_params->memcg;
3128 id = memcg_cache_id(memcg);
3129
3130 root = s->memcg_params->root_cache;
3131 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003132
3133 mutex_lock(&memcg->slab_caches_mutex);
3134 list_del(&s->memcg_params->list);
3135 mutex_unlock(&memcg->slab_caches_mutex);
3136
Li Zefan20f05312013-07-08 16:00:31 -07003137 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003138out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003139 kfree(s->memcg_params);
3140}
3141
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003142/*
3143 * During the creation a new cache, we need to disable our accounting mechanism
3144 * altogether. This is true even if we are not creating, but rather just
3145 * enqueing new caches to be created.
3146 *
3147 * This is because that process will trigger allocations; some visible, like
3148 * explicit kmallocs to auxiliary data structures, name strings and internal
3149 * cache structures; some well concealed, like INIT_WORK() that can allocate
3150 * objects during debug.
3151 *
3152 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3153 * to it. This may not be a bounded recursion: since the first cache creation
3154 * failed to complete (waiting on the allocation), we'll just try to create the
3155 * cache again, failing at the same point.
3156 *
3157 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3158 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3159 * inside the following two functions.
3160 */
3161static inline void memcg_stop_kmem_account(void)
3162{
3163 VM_BUG_ON(!current->mm);
3164 current->memcg_kmem_skip_account++;
3165}
3166
3167static inline void memcg_resume_kmem_account(void)
3168{
3169 VM_BUG_ON(!current->mm);
3170 current->memcg_kmem_skip_account--;
3171}
3172
Glauber Costa1f458cb2012-12-18 14:22:50 -08003173static void kmem_cache_destroy_work_func(struct work_struct *w)
3174{
3175 struct kmem_cache *cachep;
3176 struct memcg_cache_params *p;
3177
3178 p = container_of(w, struct memcg_cache_params, destroy);
3179
3180 cachep = memcg_params_to_cache(p);
3181
Glauber Costa22933152012-12-18 14:22:59 -08003182 /*
3183 * If we get down to 0 after shrink, we could delete right away.
3184 * However, memcg_release_pages() already puts us back in the workqueue
3185 * in that case. If we proceed deleting, we'll get a dangling
3186 * reference, and removing the object from the workqueue in that case
3187 * is unnecessary complication. We are not a fast path.
3188 *
3189 * Note that this case is fundamentally different from racing with
3190 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3191 * kmem_cache_shrink, not only we would be reinserting a dead cache
3192 * into the queue, but doing so from inside the worker racing to
3193 * destroy it.
3194 *
3195 * So if we aren't down to zero, we'll just schedule a worker and try
3196 * again
3197 */
3198 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3199 kmem_cache_shrink(cachep);
3200 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3201 return;
3202 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003203 kmem_cache_destroy(cachep);
3204}
3205
3206void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3207{
3208 if (!cachep->memcg_params->dead)
3209 return;
3210
3211 /*
Glauber Costa22933152012-12-18 14:22:59 -08003212 * There are many ways in which we can get here.
3213 *
3214 * We can get to a memory-pressure situation while the delayed work is
3215 * still pending to run. The vmscan shrinkers can then release all
3216 * cache memory and get us to destruction. If this is the case, we'll
3217 * be executed twice, which is a bug (the second time will execute over
3218 * bogus data). In this case, cancelling the work should be fine.
3219 *
3220 * But we can also get here from the worker itself, if
3221 * kmem_cache_shrink is enough to shake all the remaining objects and
3222 * get the page count to 0. In this case, we'll deadlock if we try to
3223 * cancel the work (the worker runs with an internal lock held, which
3224 * is the same lock we would hold for cancel_work_sync().)
3225 *
3226 * Since we can't possibly know who got us here, just refrain from
3227 * running if there is already work pending
3228 */
3229 if (work_pending(&cachep->memcg_params->destroy))
3230 return;
3231 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003232 * We have to defer the actual destroying to a workqueue, because
3233 * we might currently be in a context that cannot sleep.
3234 */
3235 schedule_work(&cachep->memcg_params->destroy);
3236}
3237
Glauber Costad7f25f82012-12-18 14:22:40 -08003238/*
3239 * This lock protects updaters, not readers. We want readers to be as fast as
3240 * they can, and they will either see NULL or a valid cache value. Our model
3241 * allow them to see NULL, in which case the root memcg will be selected.
3242 *
3243 * We need this lock because multiple allocations to the same cache from a non
3244 * will span more than one worker. Only one of them can create the cache.
3245 */
3246static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003247
3248/*
3249 * Called with memcg_cache_mutex held
3250 */
3251static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3252 struct kmem_cache *s)
3253{
3254 struct kmem_cache *new;
3255 static char *tmp_name = NULL;
3256
3257 lockdep_assert_held(&memcg_cache_mutex);
3258
3259 /*
3260 * kmem_cache_create_memcg duplicates the given name and
3261 * cgroup_name for this name requires RCU context.
3262 * This static temporary buffer is used to prevent from
3263 * pointless shortliving allocation.
3264 */
3265 if (!tmp_name) {
3266 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3267 if (!tmp_name)
3268 return NULL;
3269 }
3270
3271 rcu_read_lock();
3272 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3273 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3274 rcu_read_unlock();
3275
3276 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3277 (s->flags & ~SLAB_PANIC), s->ctor, s);
3278
3279 if (new)
3280 new->allocflags |= __GFP_KMEMCG;
3281
3282 return new;
3283}
3284
Glauber Costad7f25f82012-12-18 14:22:40 -08003285static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3286 struct kmem_cache *cachep)
3287{
3288 struct kmem_cache *new_cachep;
3289 int idx;
3290
3291 BUG_ON(!memcg_can_account_kmem(memcg));
3292
3293 idx = memcg_cache_id(memcg);
3294
3295 mutex_lock(&memcg_cache_mutex);
3296 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003297 if (new_cachep) {
3298 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003299 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003300 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003301
3302 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003303 if (new_cachep == NULL) {
3304 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003305 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003306 goto out;
3307 }
3308
Glauber Costa1f458cb2012-12-18 14:22:50 -08003309 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003310
3311 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3312 /*
3313 * the readers won't lock, make sure everybody sees the updated value,
3314 * so they won't put stuff in the queue again for no reason
3315 */
3316 wmb();
3317out:
3318 mutex_unlock(&memcg_cache_mutex);
3319 return new_cachep;
3320}
3321
Glauber Costa7cf27982012-12-18 14:22:55 -08003322void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3323{
3324 struct kmem_cache *c;
3325 int i;
3326
3327 if (!s->memcg_params)
3328 return;
3329 if (!s->memcg_params->is_root_cache)
3330 return;
3331
3332 /*
3333 * If the cache is being destroyed, we trust that there is no one else
3334 * requesting objects from it. Even if there are, the sanity checks in
3335 * kmem_cache_destroy should caught this ill-case.
3336 *
3337 * Still, we don't want anyone else freeing memcg_caches under our
3338 * noses, which can happen if a new memcg comes to life. As usual,
3339 * we'll take the set_limit_mutex to protect ourselves against this.
3340 */
3341 mutex_lock(&set_limit_mutex);
3342 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3343 c = s->memcg_params->memcg_caches[i];
3344 if (!c)
3345 continue;
3346
3347 /*
3348 * We will now manually delete the caches, so to avoid races
3349 * we need to cancel all pending destruction workers and
3350 * proceed with destruction ourselves.
3351 *
3352 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3353 * and that could spawn the workers again: it is likely that
3354 * the cache still have active pages until this very moment.
3355 * This would lead us back to mem_cgroup_destroy_cache.
3356 *
3357 * But that will not execute at all if the "dead" flag is not
3358 * set, so flip it down to guarantee we are in control.
3359 */
3360 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003361 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003362 kmem_cache_destroy(c);
3363 }
3364 mutex_unlock(&set_limit_mutex);
3365}
3366
Glauber Costad7f25f82012-12-18 14:22:40 -08003367struct create_work {
3368 struct mem_cgroup *memcg;
3369 struct kmem_cache *cachep;
3370 struct work_struct work;
3371};
3372
Glauber Costa1f458cb2012-12-18 14:22:50 -08003373static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3374{
3375 struct kmem_cache *cachep;
3376 struct memcg_cache_params *params;
3377
3378 if (!memcg_kmem_is_active(memcg))
3379 return;
3380
3381 mutex_lock(&memcg->slab_caches_mutex);
3382 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3383 cachep = memcg_params_to_cache(params);
3384 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003385 schedule_work(&cachep->memcg_params->destroy);
3386 }
3387 mutex_unlock(&memcg->slab_caches_mutex);
3388}
3389
Glauber Costad7f25f82012-12-18 14:22:40 -08003390static void memcg_create_cache_work_func(struct work_struct *w)
3391{
3392 struct create_work *cw;
3393
3394 cw = container_of(w, struct create_work, work);
3395 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003396 kfree(cw);
3397}
3398
3399/*
3400 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003401 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003402static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3403 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003404{
3405 struct create_work *cw;
3406
3407 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003408 if (cw == NULL) {
3409 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003410 return;
3411 }
3412
3413 cw->memcg = memcg;
3414 cw->cachep = cachep;
3415
3416 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3417 schedule_work(&cw->work);
3418}
3419
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003420static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3421 struct kmem_cache *cachep)
3422{
3423 /*
3424 * We need to stop accounting when we kmalloc, because if the
3425 * corresponding kmalloc cache is not yet created, the first allocation
3426 * in __memcg_create_cache_enqueue will recurse.
3427 *
3428 * However, it is better to enclose the whole function. Depending on
3429 * the debugging options enabled, INIT_WORK(), for instance, can
3430 * trigger an allocation. This too, will make us recurse. Because at
3431 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3432 * the safest choice is to do it like this, wrapping the whole function.
3433 */
3434 memcg_stop_kmem_account();
3435 __memcg_create_cache_enqueue(memcg, cachep);
3436 memcg_resume_kmem_account();
3437}
Glauber Costad7f25f82012-12-18 14:22:40 -08003438/*
3439 * Return the kmem_cache we're supposed to use for a slab allocation.
3440 * We try to use the current memcg's version of the cache.
3441 *
3442 * If the cache does not exist yet, if we are the first user of it,
3443 * we either create it immediately, if possible, or create it asynchronously
3444 * in a workqueue.
3445 * In the latter case, we will let the current allocation go through with
3446 * the original cache.
3447 *
3448 * Can't be called in interrupt context or from kernel threads.
3449 * This function needs to be called with rcu_read_lock() held.
3450 */
3451struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3452 gfp_t gfp)
3453{
3454 struct mem_cgroup *memcg;
3455 int idx;
3456
3457 VM_BUG_ON(!cachep->memcg_params);
3458 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3459
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003460 if (!current->mm || current->memcg_kmem_skip_account)
3461 return cachep;
3462
Glauber Costad7f25f82012-12-18 14:22:40 -08003463 rcu_read_lock();
3464 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003465
3466 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003467 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003468
3469 idx = memcg_cache_id(memcg);
3470
3471 /*
3472 * barrier to mare sure we're always seeing the up to date value. The
3473 * code updating memcg_caches will issue a write barrier to match this.
3474 */
3475 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003476 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3477 cachep = cachep->memcg_params->memcg_caches[idx];
3478 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003479 }
3480
Li Zefanca0dde92013-04-29 15:08:57 -07003481 /* The corresponding put will be done in the workqueue. */
3482 if (!css_tryget(&memcg->css))
3483 goto out;
3484 rcu_read_unlock();
3485
3486 /*
3487 * If we are in a safe context (can wait, and not in interrupt
3488 * context), we could be be predictable and return right away.
3489 * This would guarantee that the allocation being performed
3490 * already belongs in the new cache.
3491 *
3492 * However, there are some clashes that can arrive from locking.
3493 * For instance, because we acquire the slab_mutex while doing
3494 * kmem_cache_dup, this means no further allocation could happen
3495 * with the slab_mutex held.
3496 *
3497 * Also, because cache creation issue get_online_cpus(), this
3498 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3499 * that ends up reversed during cpu hotplug. (cpuset allocates
3500 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3501 * better to defer everything.
3502 */
3503 memcg_create_cache_enqueue(memcg, cachep);
3504 return cachep;
3505out:
3506 rcu_read_unlock();
3507 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003508}
3509EXPORT_SYMBOL(__memcg_kmem_get_cache);
3510
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003511/*
3512 * We need to verify if the allocation against current->mm->owner's memcg is
3513 * possible for the given order. But the page is not allocated yet, so we'll
3514 * need a further commit step to do the final arrangements.
3515 *
3516 * It is possible for the task to switch cgroups in this mean time, so at
3517 * commit time, we can't rely on task conversion any longer. We'll then use
3518 * the handle argument to return to the caller which cgroup we should commit
3519 * against. We could also return the memcg directly and avoid the pointer
3520 * passing, but a boolean return value gives better semantics considering
3521 * the compiled-out case as well.
3522 *
3523 * Returning true means the allocation is possible.
3524 */
3525bool
3526__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3527{
3528 struct mem_cgroup *memcg;
3529 int ret;
3530
3531 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003532
3533 /*
3534 * Disabling accounting is only relevant for some specific memcg
3535 * internal allocations. Therefore we would initially not have such
3536 * check here, since direct calls to the page allocator that are marked
3537 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3538 * concerned with cache allocations, and by having this test at
3539 * memcg_kmem_get_cache, we are already able to relay the allocation to
3540 * the root cache and bypass the memcg cache altogether.
3541 *
3542 * There is one exception, though: the SLUB allocator does not create
3543 * large order caches, but rather service large kmallocs directly from
3544 * the page allocator. Therefore, the following sequence when backed by
3545 * the SLUB allocator:
3546 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003547 * memcg_stop_kmem_account();
3548 * kmalloc(<large_number>)
3549 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003550 *
3551 * would effectively ignore the fact that we should skip accounting,
3552 * since it will drive us directly to this function without passing
3553 * through the cache selector memcg_kmem_get_cache. Such large
3554 * allocations are extremely rare but can happen, for instance, for the
3555 * cache arrays. We bring this test here.
3556 */
3557 if (!current->mm || current->memcg_kmem_skip_account)
3558 return true;
3559
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003560 memcg = try_get_mem_cgroup_from_mm(current->mm);
3561
3562 /*
3563 * very rare case described in mem_cgroup_from_task. Unfortunately there
3564 * isn't much we can do without complicating this too much, and it would
3565 * be gfp-dependent anyway. Just let it go
3566 */
3567 if (unlikely(!memcg))
3568 return true;
3569
3570 if (!memcg_can_account_kmem(memcg)) {
3571 css_put(&memcg->css);
3572 return true;
3573 }
3574
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003575 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3576 if (!ret)
3577 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003578
3579 css_put(&memcg->css);
3580 return (ret == 0);
3581}
3582
3583void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3584 int order)
3585{
3586 struct page_cgroup *pc;
3587
3588 VM_BUG_ON(mem_cgroup_is_root(memcg));
3589
3590 /* The page allocation failed. Revert */
3591 if (!page) {
3592 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003593 return;
3594 }
3595
3596 pc = lookup_page_cgroup(page);
3597 lock_page_cgroup(pc);
3598 pc->mem_cgroup = memcg;
3599 SetPageCgroupUsed(pc);
3600 unlock_page_cgroup(pc);
3601}
3602
3603void __memcg_kmem_uncharge_pages(struct page *page, int order)
3604{
3605 struct mem_cgroup *memcg = NULL;
3606 struct page_cgroup *pc;
3607
3608
3609 pc = lookup_page_cgroup(page);
3610 /*
3611 * Fast unlocked return. Theoretically might have changed, have to
3612 * check again after locking.
3613 */
3614 if (!PageCgroupUsed(pc))
3615 return;
3616
3617 lock_page_cgroup(pc);
3618 if (PageCgroupUsed(pc)) {
3619 memcg = pc->mem_cgroup;
3620 ClearPageCgroupUsed(pc);
3621 }
3622 unlock_page_cgroup(pc);
3623
3624 /*
3625 * We trust that only if there is a memcg associated with the page, it
3626 * is a valid allocation
3627 */
3628 if (!memcg)
3629 return;
3630
3631 VM_BUG_ON(mem_cgroup_is_root(memcg));
3632 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003633}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003634#else
3635static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3636{
3637}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003638#endif /* CONFIG_MEMCG_KMEM */
3639
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003640#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3641
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003642#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003643/*
3644 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003645 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3646 * charge/uncharge will be never happen and move_account() is done under
3647 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003648 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003649void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003650{
3651 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003652 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003653 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003654 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003655
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003656 if (mem_cgroup_disabled())
3657 return;
David Rientjesb070e652013-05-07 16:18:09 -07003658
3659 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003660 for (i = 1; i < HPAGE_PMD_NR; i++) {
3661 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003662 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003663 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003664 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3665 }
David Rientjesb070e652013-05-07 16:18:09 -07003666 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3667 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003668}
Hugh Dickins12d27102012-01-12 17:19:52 -08003669#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003670
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003671static inline
3672void mem_cgroup_move_account_page_stat(struct mem_cgroup *from,
3673 struct mem_cgroup *to,
3674 unsigned int nr_pages,
3675 enum mem_cgroup_stat_index idx)
3676{
3677 /* Update stat data for mem_cgroup */
3678 preempt_disable();
3679 WARN_ON_ONCE(from->stat->count[idx] < nr_pages);
3680 __this_cpu_add(from->stat->count[idx], -nr_pages);
3681 __this_cpu_add(to->stat->count[idx], nr_pages);
3682 preempt_enable();
3683}
3684
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003685/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003686 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003687 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003688 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003689 * @pc: page_cgroup of the page.
3690 * @from: mem_cgroup which the page is moved from.
3691 * @to: mem_cgroup which the page is moved to. @from != @to.
3692 *
3693 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003694 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003695 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003696 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003697 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3698 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003699 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003700static int mem_cgroup_move_account(struct page *page,
3701 unsigned int nr_pages,
3702 struct page_cgroup *pc,
3703 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003704 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003705{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003706 unsigned long flags;
3707 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003708 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003709
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003711 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003712 /*
3713 * The page is isolated from LRU. So, collapse function
3714 * will not handle this page. But page splitting can happen.
3715 * Do this check under compound_page_lock(). The caller should
3716 * hold it.
3717 */
3718 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003719 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003720 goto out;
3721
3722 lock_page_cgroup(pc);
3723
3724 ret = -EINVAL;
3725 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3726 goto unlock;
3727
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003728 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003729
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003730 if (!anon && page_mapped(page))
3731 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3732 MEM_CGROUP_STAT_FILE_MAPPED);
3733
3734 if (PageWriteback(page))
3735 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3736 MEM_CGROUP_STAT_WRITEBACK);
3737
David Rientjesb070e652013-05-07 16:18:09 -07003738 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003739
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003740 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003741 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003742 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003743 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003744 ret = 0;
3745unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003746 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003747 /*
3748 * check events
3749 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003750 memcg_check_events(to, page);
3751 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003752out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 return ret;
3754}
3755
Michal Hocko2ef37d32012-10-26 13:37:30 +02003756/**
3757 * mem_cgroup_move_parent - moves page to the parent group
3758 * @page: the page to move
3759 * @pc: page_cgroup of the page
3760 * @child: page's cgroup
3761 *
3762 * move charges to its parent or the root cgroup if the group has no
3763 * parent (aka use_hierarchy==0).
3764 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3765 * mem_cgroup_move_account fails) the failure is always temporary and
3766 * it signals a race with a page removal/uncharge or migration. In the
3767 * first case the page is on the way out and it will vanish from the LRU
3768 * on the next attempt and the call should be retried later.
3769 * Isolation from the LRU fails only if page has been isolated from
3770 * the LRU since we looked at it and that usually means either global
3771 * reclaim or migration going on. The page will either get back to the
3772 * LRU or vanish.
3773 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3774 * (!PageCgroupUsed) or moved to a different group. The page will
3775 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003776 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003777static int mem_cgroup_move_parent(struct page *page,
3778 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003779 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003780{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003781 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003782 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003783 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003784 int ret;
3785
Michal Hockod8423012012-10-26 13:37:29 +02003786 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003787
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003788 ret = -EBUSY;
3789 if (!get_page_unless_zero(page))
3790 goto out;
3791 if (isolate_lru_page(page))
3792 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003793
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003794 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003795
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003796 parent = parent_mem_cgroup(child);
3797 /*
3798 * If no parent, move charges to root cgroup.
3799 */
3800 if (!parent)
3801 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003802
Michal Hocko2ef37d32012-10-26 13:37:30 +02003803 if (nr_pages > 1) {
3804 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003805 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003806 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003807
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003808 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003809 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003810 if (!ret)
3811 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003812
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003813 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003814 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003815 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003816put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003817 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003818out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003819 return ret;
3820}
3821
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003822/*
3823 * Charge the memory controller for page usage.
3824 * Return
3825 * 0 if the charge was successful
3826 * < 0 if the cgroup is over its limit
3827 */
3828static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003829 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003830{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003831 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003832 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003833 bool oom = true;
3834 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003835
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003836 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003837 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003838 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003839 /*
3840 * Never OOM-kill a process for a huge page. The
3841 * fault handler will fall back to regular pages.
3842 */
3843 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003844 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003845
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003846 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003847 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003848 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003849 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003850 return 0;
3851}
3852
3853int mem_cgroup_newpage_charge(struct page *page,
3854 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003855{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003856 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003857 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003858 VM_BUG_ON(page_mapped(page));
3859 VM_BUG_ON(page->mapping && !PageAnon(page));
3860 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003861 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003862 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003863}
3864
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003865/*
3866 * While swap-in, try_charge -> commit or cancel, the page is locked.
3867 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003868 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003869 * "commit()" or removed by "cancel()"
3870 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003871static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3872 struct page *page,
3873 gfp_t mask,
3874 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003875{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003876 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003877 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003878 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003879
Johannes Weiner90deb782012-07-31 16:45:47 -07003880 pc = lookup_page_cgroup(page);
3881 /*
3882 * Every swap fault against a single page tries to charge the
3883 * page, bail as early as possible. shmem_unuse() encounters
3884 * already charged pages, too. The USED bit is protected by
3885 * the page lock, which serializes swap cache removal, which
3886 * in turn serializes uncharging.
3887 */
3888 if (PageCgroupUsed(pc))
3889 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003890 if (!do_swap_account)
3891 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003892 memcg = try_get_mem_cgroup_from_page(page);
3893 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003894 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003895 *memcgp = memcg;
3896 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003897 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003898 if (ret == -EINTR)
3899 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003900 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003901charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003902 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3903 if (ret == -EINTR)
3904 ret = 0;
3905 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003906}
3907
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003908int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3909 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3910{
3911 *memcgp = NULL;
3912 if (mem_cgroup_disabled())
3913 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003914 /*
3915 * A racing thread's fault, or swapoff, may have already
3916 * updated the pte, and even removed page from swap cache: in
3917 * those cases unuse_pte()'s pte_same() test will fail; but
3918 * there's also a KSM case which does need to charge the page.
3919 */
3920 if (!PageSwapCache(page)) {
3921 int ret;
3922
3923 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3924 if (ret == -EINTR)
3925 ret = 0;
3926 return ret;
3927 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003928 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3929}
3930
Johannes Weiner827a03d2012-07-31 16:45:36 -07003931void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3932{
3933 if (mem_cgroup_disabled())
3934 return;
3935 if (!memcg)
3936 return;
3937 __mem_cgroup_cancel_charge(memcg, 1);
3938}
3939
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003940static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003941__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003942 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003943{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003944 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003945 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003946 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003947 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003948
Johannes Weinerce587e62012-04-24 20:22:33 +02003949 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003950 /*
3951 * Now swap is on-memory. This means this page may be
3952 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003953 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3954 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3955 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003956 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003957 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003958 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003959 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003960 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003961}
3962
Johannes Weiner72835c82012-01-12 17:18:32 -08003963void mem_cgroup_commit_charge_swapin(struct page *page,
3964 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003965{
Johannes Weiner72835c82012-01-12 17:18:32 -08003966 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003967 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003968}
3969
Johannes Weiner827a03d2012-07-31 16:45:36 -07003970int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3971 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003972{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003973 struct mem_cgroup *memcg = NULL;
3974 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3975 int ret;
3976
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003977 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003978 return 0;
3979 if (PageCompound(page))
3980 return 0;
3981
Johannes Weiner827a03d2012-07-31 16:45:36 -07003982 if (!PageSwapCache(page))
3983 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3984 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003985 ret = __mem_cgroup_try_charge_swapin(mm, page,
3986 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003987 if (!ret)
3988 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3989 }
3990 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003991}
3992
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003993static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003994 unsigned int nr_pages,
3995 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003996{
3997 struct memcg_batch_info *batch = NULL;
3998 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003999
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004000 /* If swapout, usage of swap doesn't decrease */
4001 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4002 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004003
4004 batch = &current->memcg_batch;
4005 /*
4006 * In usual, we do css_get() when we remember memcg pointer.
4007 * But in this case, we keep res->usage until end of a series of
4008 * uncharges. Then, it's ok to ignore memcg's refcnt.
4009 */
4010 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004011 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004012 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004013 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004014 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004015 * the same cgroup and we have chance to coalesce uncharges.
4016 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4017 * because we want to do uncharge as soon as possible.
4018 */
4019
4020 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4021 goto direct_uncharge;
4022
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004023 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004024 goto direct_uncharge;
4025
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004026 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004027 * In typical case, batch->memcg == mem. This means we can
4028 * merge a series of uncharges to an uncharge of res_counter.
4029 * If not, we uncharge res_counter ony by one.
4030 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004031 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004032 goto direct_uncharge;
4033 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004034 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004035 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004036 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004037 return;
4038direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004039 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004040 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004041 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4042 if (unlikely(batch->memcg != memcg))
4043 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004044}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004045
Balbir Singh8697d332008-02-07 00:13:59 -08004046/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004047 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004048 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004049static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004050__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4051 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004052{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004053 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004054 unsigned int nr_pages = 1;
4055 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004056 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004057
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004058 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004059 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004060
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004061 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004062 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004063 VM_BUG_ON(!PageTransHuge(page));
4064 }
Balbir Singh8697d332008-02-07 00:13:59 -08004065 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004066 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004067 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004068 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004069 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004070 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004071
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004072 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004073
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004074 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004075
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004076 if (!PageCgroupUsed(pc))
4077 goto unlock_out;
4078
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004079 anon = PageAnon(page);
4080
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004081 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004082 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004083 /*
4084 * Generally PageAnon tells if it's the anon statistics to be
4085 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4086 * used before page reached the stage of being marked PageAnon.
4087 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004088 anon = true;
4089 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004090 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004091 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004092 if (page_mapped(page))
4093 goto unlock_out;
4094 /*
4095 * Pages under migration may not be uncharged. But
4096 * end_migration() /must/ be the one uncharging the
4097 * unused post-migration page and so it has to call
4098 * here with the migration bit still set. See the
4099 * res_counter handling below.
4100 */
4101 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004102 goto unlock_out;
4103 break;
4104 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4105 if (!PageAnon(page)) { /* Shared memory */
4106 if (page->mapping && !page_is_file_cache(page))
4107 goto unlock_out;
4108 } else if (page_mapped(page)) /* Anon */
4109 goto unlock_out;
4110 break;
4111 default:
4112 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004113 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004114
David Rientjesb070e652013-05-07 16:18:09 -07004115 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004116
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004117 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004118 /*
4119 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4120 * freed from LRU. This is safe because uncharged page is expected not
4121 * to be reused (freed soon). Exception is SwapCache, it's handled by
4122 * special functions.
4123 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004124
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004125 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004126 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004127 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004128 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004129 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004130 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004131 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004132 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004133 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004134 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004135 /*
4136 * Migration does not charge the res_counter for the
4137 * replacement page, so leave it alone when phasing out the
4138 * page that is unused after the migration.
4139 */
4140 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004141 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004142
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004143 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004144
4145unlock_out:
4146 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004147 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004148}
4149
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004150void mem_cgroup_uncharge_page(struct page *page)
4151{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004152 /* early check. */
4153 if (page_mapped(page))
4154 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004155 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004156 /*
4157 * If the page is in swap cache, uncharge should be deferred
4158 * to the swap path, which also properly accounts swap usage
4159 * and handles memcg lifetime.
4160 *
4161 * Note that this check is not stable and reclaim may add the
4162 * page to swap cache at any time after this. However, if the
4163 * page is not in swap cache by the time page->mapcount hits
4164 * 0, there won't be any page table references to the swap
4165 * slot, and reclaim will free it and not actually write the
4166 * page to disk.
4167 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004168 if (PageSwapCache(page))
4169 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004170 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004171}
4172
4173void mem_cgroup_uncharge_cache_page(struct page *page)
4174{
4175 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004176 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004177 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004178}
4179
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004180/*
4181 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4182 * In that cases, pages are freed continuously and we can expect pages
4183 * are in the same memcg. All these calls itself limits the number of
4184 * pages freed at once, then uncharge_start/end() is called properly.
4185 * This may be called prural(2) times in a context,
4186 */
4187
4188void mem_cgroup_uncharge_start(void)
4189{
4190 current->memcg_batch.do_batch++;
4191 /* We can do nest. */
4192 if (current->memcg_batch.do_batch == 1) {
4193 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004194 current->memcg_batch.nr_pages = 0;
4195 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004196 }
4197}
4198
4199void mem_cgroup_uncharge_end(void)
4200{
4201 struct memcg_batch_info *batch = &current->memcg_batch;
4202
4203 if (!batch->do_batch)
4204 return;
4205
4206 batch->do_batch--;
4207 if (batch->do_batch) /* If stacked, do nothing. */
4208 return;
4209
4210 if (!batch->memcg)
4211 return;
4212 /*
4213 * This "batch->memcg" is valid without any css_get/put etc...
4214 * bacause we hide charges behind us.
4215 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004216 if (batch->nr_pages)
4217 res_counter_uncharge(&batch->memcg->res,
4218 batch->nr_pages * PAGE_SIZE);
4219 if (batch->memsw_nr_pages)
4220 res_counter_uncharge(&batch->memcg->memsw,
4221 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004222 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004223 /* forget this pointer (for sanity check) */
4224 batch->memcg = NULL;
4225}
4226
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004227#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004228/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004229 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004230 * memcg information is recorded to swap_cgroup of "ent"
4231 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004232void
4233mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004234{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004235 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004236 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004237
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004238 if (!swapout) /* this was a swap cache but the swap is unused ! */
4239 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4240
Johannes Weiner0030f532012-07-31 16:45:25 -07004241 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004242
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004243 /*
4244 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004245 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004246 */
4247 if (do_swap_account && swapout && memcg)
Li Zefan34c00c32013-09-23 16:56:01 +08004248 swap_cgroup_record(ent, mem_cgroup_id(memcg));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004249}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004250#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004251
Andrew Mortonc255a452012-07-31 16:43:02 -07004252#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004253/*
4254 * called from swap_entry_free(). remove record in swap_cgroup and
4255 * uncharge "memsw" account.
4256 */
4257void mem_cgroup_uncharge_swap(swp_entry_t ent)
4258{
4259 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004260 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004261
4262 if (!do_swap_account)
4263 return;
4264
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004265 id = swap_cgroup_record(ent, 0);
4266 rcu_read_lock();
4267 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004268 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004269 /*
4270 * We uncharge this because swap is freed.
4271 * This memcg can be obsolete one. We avoid calling css_tryget
4272 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004273 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004274 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004275 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004276 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004277 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004278 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004279}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004280
4281/**
4282 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4283 * @entry: swap entry to be moved
4284 * @from: mem_cgroup which the entry is moved from
4285 * @to: mem_cgroup which the entry is moved to
4286 *
4287 * It succeeds only when the swap_cgroup's record for this entry is the same
4288 * as the mem_cgroup's id of @from.
4289 *
4290 * Returns 0 on success, -EINVAL on failure.
4291 *
4292 * The caller must have charged to @to, IOW, called res_counter_charge() about
4293 * both res and memsw, and called css_get().
4294 */
4295static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004296 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004297{
4298 unsigned short old_id, new_id;
4299
Li Zefan34c00c32013-09-23 16:56:01 +08004300 old_id = mem_cgroup_id(from);
4301 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004302
4303 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004304 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004305 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004306 /*
4307 * This function is only called from task migration context now.
4308 * It postpones res_counter and refcount handling till the end
4309 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004310 * improvement. But we cannot postpone css_get(to) because if
4311 * the process that has been moved to @to does swap-in, the
4312 * refcount of @to might be decreased to 0.
4313 *
4314 * We are in attach() phase, so the cgroup is guaranteed to be
4315 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004316 */
Li Zefan40503772013-07-08 16:00:34 -07004317 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004318 return 0;
4319 }
4320 return -EINVAL;
4321}
4322#else
4323static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004324 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004325{
4326 return -EINVAL;
4327}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004328#endif
4329
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004330/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004331 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4332 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004333 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004334void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4335 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004336{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004337 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004338 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004339 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004340 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004341
Johannes Weiner72835c82012-01-12 17:18:32 -08004342 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004343
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004344 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004345 return;
Balbir Singh40779602008-04-04 14:29:59 -07004346
Mel Gormanb32967f2012-11-19 12:35:47 +00004347 if (PageTransHuge(page))
4348 nr_pages <<= compound_order(page);
4349
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004350 pc = lookup_page_cgroup(page);
4351 lock_page_cgroup(pc);
4352 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004353 memcg = pc->mem_cgroup;
4354 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004355 /*
4356 * At migrating an anonymous page, its mapcount goes down
4357 * to 0 and uncharge() will be called. But, even if it's fully
4358 * unmapped, migration may fail and this page has to be
4359 * charged again. We set MIGRATION flag here and delay uncharge
4360 * until end_migration() is called
4361 *
4362 * Corner Case Thinking
4363 * A)
4364 * When the old page was mapped as Anon and it's unmap-and-freed
4365 * while migration was ongoing.
4366 * If unmap finds the old page, uncharge() of it will be delayed
4367 * until end_migration(). If unmap finds a new page, it's
4368 * uncharged when it make mapcount to be 1->0. If unmap code
4369 * finds swap_migration_entry, the new page will not be mapped
4370 * and end_migration() will find it(mapcount==0).
4371 *
4372 * B)
4373 * When the old page was mapped but migraion fails, the kernel
4374 * remaps it. A charge for it is kept by MIGRATION flag even
4375 * if mapcount goes down to 0. We can do remap successfully
4376 * without charging it again.
4377 *
4378 * C)
4379 * The "old" page is under lock_page() until the end of
4380 * migration, so, the old page itself will not be swapped-out.
4381 * If the new page is swapped out before end_migraton, our
4382 * hook to usual swap-out path will catch the event.
4383 */
4384 if (PageAnon(page))
4385 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004386 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004387 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004388 /*
4389 * If the page is not charged at this point,
4390 * we return here.
4391 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004392 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004393 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004394
Johannes Weiner72835c82012-01-12 17:18:32 -08004395 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004396 /*
4397 * We charge new page before it's used/mapped. So, even if unlock_page()
4398 * is called before end_migration, we can catch all events on this new
4399 * page. In the case new page is migrated but not remapped, new page's
4400 * mapcount will be finally 0 and we call uncharge in end_migration().
4401 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004402 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004403 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004404 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004405 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004406 /*
4407 * The page is committed to the memcg, but it's not actually
4408 * charged to the res_counter since we plan on replacing the
4409 * old one and only one page is going to be left afterwards.
4410 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004411 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004412}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004413
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004414/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004415void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004416 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004417{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004418 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004419 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004420 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004421
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004422 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004423 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004424
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004425 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004426 used = oldpage;
4427 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004428 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004429 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004430 unused = oldpage;
4431 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004432 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004433 __mem_cgroup_uncharge_common(unused,
4434 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4435 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4436 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004437 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004438 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004439 * We disallowed uncharge of pages under migration because mapcount
4440 * of the page goes down to zero, temporarly.
4441 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004442 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004443 pc = lookup_page_cgroup(oldpage);
4444 lock_page_cgroup(pc);
4445 ClearPageCgroupMigration(pc);
4446 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004447
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004448 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004449 * If a page is a file cache, radix-tree replacement is very atomic
4450 * and we can skip this check. When it was an Anon page, its mapcount
4451 * goes down to 0. But because we added MIGRATION flage, it's not
4452 * uncharged yet. There are several case but page->mapcount check
4453 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4454 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004455 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004456 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004457 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004458}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004459
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004460/*
4461 * At replace page cache, newpage is not under any memcg but it's on
4462 * LRU. So, this function doesn't touch res_counter but handles LRU
4463 * in correct way. Both pages are locked so we cannot race with uncharge.
4464 */
4465void mem_cgroup_replace_page_cache(struct page *oldpage,
4466 struct page *newpage)
4467{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004468 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004469 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004470 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004471
4472 if (mem_cgroup_disabled())
4473 return;
4474
4475 pc = lookup_page_cgroup(oldpage);
4476 /* fix accounting on old pages */
4477 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004478 if (PageCgroupUsed(pc)) {
4479 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004480 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004481 ClearPageCgroupUsed(pc);
4482 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004483 unlock_page_cgroup(pc);
4484
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004485 /*
4486 * When called from shmem_replace_page(), in some cases the
4487 * oldpage has already been charged, and in some cases not.
4488 */
4489 if (!memcg)
4490 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004491 /*
4492 * Even if newpage->mapping was NULL before starting replacement,
4493 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4494 * LRU while we overwrite pc->mem_cgroup.
4495 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004496 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004497}
4498
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004499#ifdef CONFIG_DEBUG_VM
4500static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4501{
4502 struct page_cgroup *pc;
4503
4504 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004505 /*
4506 * Can be NULL while feeding pages into the page allocator for
4507 * the first time, i.e. during boot or memory hotplug;
4508 * or when mem_cgroup_disabled().
4509 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004510 if (likely(pc) && PageCgroupUsed(pc))
4511 return pc;
4512 return NULL;
4513}
4514
4515bool mem_cgroup_bad_page_check(struct page *page)
4516{
4517 if (mem_cgroup_disabled())
4518 return false;
4519
4520 return lookup_page_cgroup_used(page) != NULL;
4521}
4522
4523void mem_cgroup_print_bad_page(struct page *page)
4524{
4525 struct page_cgroup *pc;
4526
4527 pc = lookup_page_cgroup_used(page);
4528 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004529 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4530 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004531 }
4532}
4533#endif
4534
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004535static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004536 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004537{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004538 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004539 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004540 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004541 int children = mem_cgroup_count_children(memcg);
4542 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004543 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004544
4545 /*
4546 * For keeping hierarchical_reclaim simple, how long we should retry
4547 * is depends on callers. We set our retry-count to be function
4548 * of # of children which we should visit in this loop.
4549 */
4550 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4551
4552 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004553
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004554 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004555 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004556 if (signal_pending(current)) {
4557 ret = -EINTR;
4558 break;
4559 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004560 /*
4561 * Rather than hide all in some function, I do this in
4562 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004563 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004564 */
4565 mutex_lock(&set_limit_mutex);
4566 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4567 if (memswlimit < val) {
4568 ret = -EINVAL;
4569 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004570 break;
4571 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004572
4573 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4574 if (memlimit < val)
4575 enlarge = 1;
4576
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004577 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004578 if (!ret) {
4579 if (memswlimit == val)
4580 memcg->memsw_is_minimum = true;
4581 else
4582 memcg->memsw_is_minimum = false;
4583 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004584 mutex_unlock(&set_limit_mutex);
4585
4586 if (!ret)
4587 break;
4588
Johannes Weiner56600482012-01-12 17:17:59 -08004589 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4590 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004591 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4592 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004593 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004594 retry_count--;
4595 else
4596 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004597 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004598 if (!ret && enlarge)
4599 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004600
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004601 return ret;
4602}
4603
Li Zefan338c8432009-06-17 16:27:15 -07004604static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4605 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004606{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004607 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004608 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004609 int children = mem_cgroup_count_children(memcg);
4610 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004611 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004612
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004613 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004614 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004615 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004616 while (retry_count) {
4617 if (signal_pending(current)) {
4618 ret = -EINTR;
4619 break;
4620 }
4621 /*
4622 * Rather than hide all in some function, I do this in
4623 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004624 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004625 */
4626 mutex_lock(&set_limit_mutex);
4627 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4628 if (memlimit > val) {
4629 ret = -EINVAL;
4630 mutex_unlock(&set_limit_mutex);
4631 break;
4632 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004633 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4634 if (memswlimit < val)
4635 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004636 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004637 if (!ret) {
4638 if (memlimit == val)
4639 memcg->memsw_is_minimum = true;
4640 else
4641 memcg->memsw_is_minimum = false;
4642 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004643 mutex_unlock(&set_limit_mutex);
4644
4645 if (!ret)
4646 break;
4647
Johannes Weiner56600482012-01-12 17:17:59 -08004648 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4649 MEM_CGROUP_RECLAIM_NOSWAP |
4650 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004651 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004652 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004653 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004654 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004655 else
4656 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004657 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004658 if (!ret && enlarge)
4659 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004660 return ret;
4661}
4662
Michal Hocko2ef37d32012-10-26 13:37:30 +02004663/**
4664 * mem_cgroup_force_empty_list - clears LRU of a group
4665 * @memcg: group to clear
4666 * @node: NUMA node
4667 * @zid: zone id
4668 * @lru: lru to to clear
4669 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004670 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004671 * reclaim the pages page themselves - pages are moved to the parent (or root)
4672 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004673 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004674static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004675 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004676{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004677 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004678 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004679 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004680 struct page *busy;
4681 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004682
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004683 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004684 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4685 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004686
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004687 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004688 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004689 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004690 struct page *page;
4691
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004692 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004693 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004694 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004695 break;
4696 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004697 page = list_entry(list->prev, struct page, lru);
4698 if (busy == page) {
4699 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004700 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004701 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004702 continue;
4703 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004704 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004705
Johannes Weiner925b7672012-01-12 17:18:15 -08004706 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004707
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004708 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004709 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004710 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004711 cond_resched();
4712 } else
4713 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004714 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004715}
4716
4717/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004718 * make mem_cgroup's charge to be 0 if there is no task by moving
4719 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004720 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004721 *
4722 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004723 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004724static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004725{
Michal Hockoc26251f2012-10-26 13:37:28 +02004726 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004727 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004728
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004729 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004730 /* This is for making all *used* pages to be on LRU. */
4731 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004732 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004733 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004734 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004735 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004736 enum lru_list lru;
4737 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004738 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004739 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004740 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004741 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004742 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004743 mem_cgroup_end_move(memcg);
4744 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004745 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004746
Michal Hocko2ef37d32012-10-26 13:37:30 +02004747 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004748 * Kernel memory may not necessarily be trackable to a specific
4749 * process. So they are not migrated, and therefore we can't
4750 * expect their value to drop to 0 here.
4751 * Having res filled up with kmem only is enough.
4752 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004753 * This is a safety check because mem_cgroup_force_empty_list
4754 * could have raced with mem_cgroup_replace_page_cache callers
4755 * so the lru seemed empty but the page could have been added
4756 * right after the check. RES_USAGE should be safe as we always
4757 * charge before adding to the LRU.
4758 */
Glauber Costabea207c2012-12-18 14:22:11 -08004759 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4760 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4761 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004762}
4763
4764/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004765 * This mainly exists for tests during the setting of set of use_hierarchy.
4766 * Since this is the very setting we are changing, the current hierarchy value
4767 * is meaningless
4768 */
4769static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4770{
Tejun Heo492eb212013-08-08 20:11:25 -04004771 struct cgroup_subsys_state *pos;
Glauber Costab5f99b52013-02-22 16:34:53 -08004772
4773 /* bounce at first found */
Tejun Heo492eb212013-08-08 20:11:25 -04004774 css_for_each_child(pos, &memcg->css)
Glauber Costab5f99b52013-02-22 16:34:53 -08004775 return true;
4776 return false;
4777}
4778
4779/*
Glauber Costa09998212013-02-22 16:34:55 -08004780 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4781 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004782 * from mem_cgroup_count_children(), in the sense that we don't really care how
4783 * many children we have; we only need to know if we have any. It also counts
4784 * any memcg without hierarchy as infertile.
4785 */
4786static inline bool memcg_has_children(struct mem_cgroup *memcg)
4787{
4788 return memcg->use_hierarchy && __memcg_has_children(memcg);
4789}
4790
4791/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004792 * Reclaims as many pages from the given memcg as possible and moves
4793 * the rest to the parent.
4794 *
4795 * Caller is responsible for holding css reference for memcg.
4796 */
4797static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4798{
4799 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4800 struct cgroup *cgrp = memcg->css.cgroup;
4801
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004802 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004803 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4804 return -EBUSY;
4805
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004806 /* we call try-to-free pages for make this cgroup empty */
4807 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004808 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004809 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004810 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004811
Michal Hockoc26251f2012-10-26 13:37:28 +02004812 if (signal_pending(current))
4813 return -EINTR;
4814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004816 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004817 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004818 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004819 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004820 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004821 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004822
4823 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004824 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004825 mem_cgroup_reparent_charges(memcg);
4826
4827 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004828}
4829
Tejun Heo182446d2013-08-08 20:11:24 -04004830static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
4831 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004832{
Tejun Heo182446d2013-08-08 20:11:24 -04004833 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02004834
Michal Hockod8423012012-10-26 13:37:29 +02004835 if (mem_cgroup_is_root(memcg))
4836 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07004837 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004838}
4839
Tejun Heo182446d2013-08-08 20:11:24 -04004840static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
4841 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004842{
Tejun Heo182446d2013-08-08 20:11:24 -04004843 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004844}
4845
Tejun Heo182446d2013-08-08 20:11:24 -04004846static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4847 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004848{
4849 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004850 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04004851 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08004852
Glauber Costa09998212013-02-22 16:34:55 -08004853 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004854
4855 if (memcg->use_hierarchy == val)
4856 goto out;
4857
Balbir Singh18f59ea2009-01-07 18:08:07 -08004858 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004859 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004860 * in the child subtrees. If it is unset, then the change can
4861 * occur, provided the current cgroup has no children.
4862 *
4863 * For the root cgroup, parent_mem is NULL, we allow value to be
4864 * set if there are no children.
4865 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004866 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004867 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004868 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004869 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004870 else
4871 retval = -EBUSY;
4872 } else
4873 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004874
4875out:
Glauber Costa09998212013-02-22 16:34:55 -08004876 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004877
4878 return retval;
4879}
4880
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004881
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004882static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004883 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004884{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004885 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004886 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004887
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004888 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004889 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004890 val += mem_cgroup_read_stat(iter, idx);
4891
4892 if (val < 0) /* race ? */
4893 val = 0;
4894 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004895}
4896
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004897static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004898{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004899 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004900
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004901 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004902 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004903 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004904 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004905 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004906 }
4907
David Rientjesb070e652013-05-07 16:18:09 -07004908 /*
4909 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4910 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4911 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004912 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4913 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004914
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004915 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004916 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004917
4918 return val << PAGE_SHIFT;
4919}
4920
Tejun Heo182446d2013-08-08 20:11:24 -04004921static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
4922 struct cftype *cft, struct file *file,
4923 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004924{
Tejun Heo182446d2013-08-08 20:11:24 -04004925 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07004926 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004927 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004928 int name, len;
4929 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004930
4931 type = MEMFILE_TYPE(cft->private);
4932 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004933
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004934 switch (type) {
4935 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004936 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004937 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004938 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004939 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004940 break;
4941 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004942 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004943 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004944 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004945 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004946 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004947 case _KMEM:
4948 val = res_counter_read_u64(&memcg->kmem, name);
4949 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004950 default:
4951 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004952 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004953
4954 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4955 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004956}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004957
Tejun Heo182446d2013-08-08 20:11:24 -04004958static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004959{
4960 int ret = -EINVAL;
4961#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04004962 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004963 /*
4964 * For simplicity, we won't allow this to be disabled. It also can't
4965 * be changed if the cgroup has children already, or if tasks had
4966 * already joined.
4967 *
4968 * If tasks join before we set the limit, a person looking at
4969 * kmem.usage_in_bytes will have no way to determine when it took
4970 * place, which makes the value quite meaningless.
4971 *
4972 * After it first became limited, changes in the value of the limit are
4973 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004974 */
Glauber Costa09998212013-02-22 16:34:55 -08004975 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004976 mutex_lock(&set_limit_mutex);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004977 if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04004978 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004979 ret = -EBUSY;
4980 goto out;
4981 }
4982 ret = res_counter_set_limit(&memcg->kmem, val);
4983 VM_BUG_ON(ret);
4984
Glauber Costa55007d82012-12-18 14:22:38 -08004985 ret = memcg_update_cache_sizes(memcg);
4986 if (ret) {
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004987 res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
Glauber Costa55007d82012-12-18 14:22:38 -08004988 goto out;
4989 }
Glauber Costa692e89a2013-02-22 16:34:56 -08004990 static_key_slow_inc(&memcg_kmem_enabled_key);
4991 /*
4992 * setting the active bit after the inc will guarantee no one
4993 * starts accounting before all call sites are patched
4994 */
4995 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004996 } else
4997 ret = res_counter_set_limit(&memcg->kmem, val);
4998out:
4999 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005000 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005001#endif
5002 return ret;
5003}
5004
Hugh Dickins6d0439902013-02-22 16:35:50 -08005005#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005006static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005007{
Glauber Costa55007d82012-12-18 14:22:38 -08005008 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005009 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5010 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005011 goto out;
5012
Glauber Costa510fc4e2012-12-18 14:21:47 -08005013 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005014 /*
5015 * When that happen, we need to disable the static branch only on those
5016 * memcgs that enabled it. To achieve this, we would be forced to
5017 * complicate the code by keeping track of which memcgs were the ones
5018 * that actually enabled limits, and which ones got it from its
5019 * parents.
5020 *
5021 * It is a lot simpler just to do static_key_slow_inc() on every child
5022 * that is accounted.
5023 */
Glauber Costa55007d82012-12-18 14:22:38 -08005024 if (!memcg_kmem_is_active(memcg))
5025 goto out;
5026
5027 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07005028 * __mem_cgroup_free() will issue static_key_slow_dec() because this
5029 * memcg is active already. If the later initialization fails then the
5030 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08005031 */
Glauber Costa55007d82012-12-18 14:22:38 -08005032 static_key_slow_inc(&memcg_kmem_enabled_key);
5033
5034 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07005035 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005036 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07005037 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005038 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005039out:
5040 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005041}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005042#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005043
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005044/*
5045 * The user of this function is...
5046 * RES_LIMIT.
5047 */
Tejun Heo182446d2013-08-08 20:11:24 -04005048static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07005049 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005050{
Tejun Heo182446d2013-08-08 20:11:24 -04005051 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005052 enum res_type type;
5053 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005054 unsigned long long val;
5055 int ret;
5056
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005057 type = MEMFILE_TYPE(cft->private);
5058 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005059
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005060 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005061 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005062 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5063 ret = -EINVAL;
5064 break;
5065 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005066 /* This function does all necessary parse...reuse it */
5067 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005068 if (ret)
5069 break;
5070 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005071 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005072 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005073 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005074 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04005075 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005076 else
5077 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005078 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005079 case RES_SOFT_LIMIT:
5080 ret = res_counter_memparse_write_strategy(buffer, &val);
5081 if (ret)
5082 break;
5083 /*
5084 * For memsw, soft limits are hard to implement in terms
5085 * of semantics, for now, we support soft limits for
5086 * control without swap
5087 */
5088 if (type == _MEM)
5089 ret = res_counter_set_soft_limit(&memcg->res, val);
5090 else
5091 ret = -EINVAL;
5092 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005093 default:
5094 ret = -EINVAL; /* should be BUG() ? */
5095 break;
5096 }
5097 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005098}
5099
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005100static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5101 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5102{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005103 unsigned long long min_limit, min_memsw_limit, tmp;
5104
5105 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5106 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005107 if (!memcg->use_hierarchy)
5108 goto out;
5109
Tejun Heo63876982013-08-08 20:11:23 -04005110 while (css_parent(&memcg->css)) {
5111 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005112 if (!memcg->use_hierarchy)
5113 break;
5114 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5115 min_limit = min(min_limit, tmp);
5116 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5117 min_memsw_limit = min(min_memsw_limit, tmp);
5118 }
5119out:
5120 *mem_limit = min_limit;
5121 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005122}
5123
Tejun Heo182446d2013-08-08 20:11:24 -04005124static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005125{
Tejun Heo182446d2013-08-08 20:11:24 -04005126 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005127 int name;
5128 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005129
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005130 type = MEMFILE_TYPE(event);
5131 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005132
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005133 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005134 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005135 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005136 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005137 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005138 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005139 else if (type == _KMEM)
5140 res_counter_reset_max(&memcg->kmem);
5141 else
5142 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005143 break;
5144 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005145 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005146 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005147 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005148 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005149 else if (type == _KMEM)
5150 res_counter_reset_failcnt(&memcg->kmem);
5151 else
5152 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005153 break;
5154 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005155
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005156 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005157}
5158
Tejun Heo182446d2013-08-08 20:11:24 -04005159static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005160 struct cftype *cft)
5161{
Tejun Heo182446d2013-08-08 20:11:24 -04005162 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005163}
5164
Daisuke Nishimura02491442010-03-10 15:22:17 -08005165#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005166static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005167 struct cftype *cft, u64 val)
5168{
Tejun Heo182446d2013-08-08 20:11:24 -04005169 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005170
5171 if (val >= (1 << NR_MOVE_TYPE))
5172 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005173
Glauber Costaee5e8472013-02-22 16:34:50 -08005174 /*
5175 * No kind of locking is needed in here, because ->can_attach() will
5176 * check this value once in the beginning of the process, and then carry
5177 * on with stale data. This means that changes to this value will only
5178 * affect task migrations starting after the change.
5179 */
5180 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005181 return 0;
5182}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005183#else
Tejun Heo182446d2013-08-08 20:11:24 -04005184static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005185 struct cftype *cft, u64 val)
5186{
5187 return -ENOSYS;
5188}
5189#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005190
Ying Han406eb0c2011-05-26 16:25:37 -07005191#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005192static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5193 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005194{
5195 int nid;
5196 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5197 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005198 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005199
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005200 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005201 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005202 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005203 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005204 seq_printf(m, " N%d=%lu", nid, node_nr);
5205 }
5206 seq_putc(m, '\n');
5207
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005208 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005209 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005210 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005211 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005212 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005213 seq_printf(m, " N%d=%lu", nid, node_nr);
5214 }
5215 seq_putc(m, '\n');
5216
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005217 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005218 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005219 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005220 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005221 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005222 seq_printf(m, " N%d=%lu", nid, node_nr);
5223 }
5224 seq_putc(m, '\n');
5225
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005226 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005227 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005228 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005229 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005230 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005231 seq_printf(m, " N%d=%lu", nid, node_nr);
5232 }
5233 seq_putc(m, '\n');
5234 return 0;
5235}
5236#endif /* CONFIG_NUMA */
5237
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005238static inline void mem_cgroup_lru_names_not_uptodate(void)
5239{
5240 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5241}
5242
Tejun Heo182446d2013-08-08 20:11:24 -04005243static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005244 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005245{
Tejun Heo182446d2013-08-08 20:11:24 -04005246 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005247 struct mem_cgroup *mi;
5248 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005249
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005250 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005251 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005252 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005253 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5254 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005255 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005256
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005257 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5258 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5259 mem_cgroup_read_events(memcg, i));
5260
5261 for (i = 0; i < NR_LRU_LISTS; i++)
5262 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5263 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5264
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005265 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005266 {
5267 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005268 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005269 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005270 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005271 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5272 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005273 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005274
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005275 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5276 long long val = 0;
5277
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005278 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005279 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005280 for_each_mem_cgroup_tree(mi, memcg)
5281 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5282 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5283 }
5284
5285 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5286 unsigned long long val = 0;
5287
5288 for_each_mem_cgroup_tree(mi, memcg)
5289 val += mem_cgroup_read_events(mi, i);
5290 seq_printf(m, "total_%s %llu\n",
5291 mem_cgroup_events_names[i], val);
5292 }
5293
5294 for (i = 0; i < NR_LRU_LISTS; i++) {
5295 unsigned long long val = 0;
5296
5297 for_each_mem_cgroup_tree(mi, memcg)
5298 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5299 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005300 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005301
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005302#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005303 {
5304 int nid, zid;
5305 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005306 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005307 unsigned long recent_rotated[2] = {0, 0};
5308 unsigned long recent_scanned[2] = {0, 0};
5309
5310 for_each_online_node(nid)
5311 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005312 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005313 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005314
Hugh Dickins89abfab2012-05-29 15:06:53 -07005315 recent_rotated[0] += rstat->recent_rotated[0];
5316 recent_rotated[1] += rstat->recent_rotated[1];
5317 recent_scanned[0] += rstat->recent_scanned[0];
5318 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005319 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005320 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5321 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5322 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5323 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005324 }
5325#endif
5326
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005327 return 0;
5328}
5329
Tejun Heo182446d2013-08-08 20:11:24 -04005330static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5331 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005332{
Tejun Heo182446d2013-08-08 20:11:24 -04005333 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005334
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005335 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005336}
5337
Tejun Heo182446d2013-08-08 20:11:24 -04005338static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5339 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005340{
Tejun Heo182446d2013-08-08 20:11:24 -04005341 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005342 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005343
Tejun Heo63876982013-08-08 20:11:23 -04005344 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005345 return -EINVAL;
5346
Glauber Costa09998212013-02-22 16:34:55 -08005347 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005348
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005349 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005350 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005351 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005352 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005353 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005354
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005355 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005356
Glauber Costa09998212013-02-22 16:34:55 -08005357 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005358
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005359 return 0;
5360}
5361
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005362static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5363{
5364 struct mem_cgroup_threshold_ary *t;
5365 u64 usage;
5366 int i;
5367
5368 rcu_read_lock();
5369 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005370 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005371 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005372 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005373
5374 if (!t)
5375 goto unlock;
5376
5377 usage = mem_cgroup_usage(memcg, swap);
5378
5379 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005380 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005381 * If it's not true, a threshold was crossed after last
5382 * call of __mem_cgroup_threshold().
5383 */
Phil Carmody5407a562010-05-26 14:42:42 -07005384 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005385
5386 /*
5387 * Iterate backward over array of thresholds starting from
5388 * current_threshold and check if a threshold is crossed.
5389 * If none of thresholds below usage is crossed, we read
5390 * only one element of the array here.
5391 */
5392 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5393 eventfd_signal(t->entries[i].eventfd, 1);
5394
5395 /* i = current_threshold + 1 */
5396 i++;
5397
5398 /*
5399 * Iterate forward over array of thresholds starting from
5400 * current_threshold+1 and check if a threshold is crossed.
5401 * If none of thresholds above usage is crossed, we read
5402 * only one element of the array here.
5403 */
5404 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5405 eventfd_signal(t->entries[i].eventfd, 1);
5406
5407 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005408 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005409unlock:
5410 rcu_read_unlock();
5411}
5412
5413static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5414{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005415 while (memcg) {
5416 __mem_cgroup_threshold(memcg, false);
5417 if (do_swap_account)
5418 __mem_cgroup_threshold(memcg, true);
5419
5420 memcg = parent_mem_cgroup(memcg);
5421 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005422}
5423
5424static int compare_thresholds(const void *a, const void *b)
5425{
5426 const struct mem_cgroup_threshold *_a = a;
5427 const struct mem_cgroup_threshold *_b = b;
5428
Greg Thelen2bff24a2013-09-11 14:23:08 -07005429 if (_a->threshold > _b->threshold)
5430 return 1;
5431
5432 if (_a->threshold < _b->threshold)
5433 return -1;
5434
5435 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005436}
5437
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005438static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005439{
5440 struct mem_cgroup_eventfd_list *ev;
5441
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005442 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005443 eventfd_signal(ev->eventfd, 1);
5444 return 0;
5445}
5446
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005447static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005448{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005449 struct mem_cgroup *iter;
5450
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005451 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005452 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005453}
5454
Tejun Heo81eeaf02013-08-08 20:11:26 -04005455static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005456 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005457{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005458 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005459 struct mem_cgroup_thresholds *thresholds;
5460 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005461 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005462 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005463 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005464
5465 ret = res_counter_memparse_write_strategy(args, &threshold);
5466 if (ret)
5467 return ret;
5468
5469 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005470
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005471 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005472 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005473 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005474 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005475 else
5476 BUG();
5477
5478 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5479
5480 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005481 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005482 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5483
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005484 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005485
5486 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005487 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005488 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005489 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005490 ret = -ENOMEM;
5491 goto unlock;
5492 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005493 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005494
5495 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005496 if (thresholds->primary) {
5497 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005498 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005499 }
5500
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005501 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005502 new->entries[size - 1].eventfd = eventfd;
5503 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005504
5505 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005506 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005507 compare_thresholds, NULL);
5508
5509 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005510 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005511 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005512 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005513 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005514 * new->current_threshold will not be used until
5515 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005516 * it here.
5517 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005518 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005519 } else
5520 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005521 }
5522
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005523 /* Free old spare buffer and save old primary buffer as spare */
5524 kfree(thresholds->spare);
5525 thresholds->spare = thresholds->primary;
5526
5527 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005528
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005529 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005530 synchronize_rcu();
5531
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005532unlock:
5533 mutex_unlock(&memcg->thresholds_lock);
5534
5535 return ret;
5536}
5537
Tejun Heo81eeaf02013-08-08 20:11:26 -04005538static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005539 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005540{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005541 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005542 struct mem_cgroup_thresholds *thresholds;
5543 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005544 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005545 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005546 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005547
5548 mutex_lock(&memcg->thresholds_lock);
5549 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005550 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005551 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005552 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005553 else
5554 BUG();
5555
Anton Vorontsov371528c2012-02-24 05:14:46 +04005556 if (!thresholds->primary)
5557 goto unlock;
5558
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005559 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5560
5561 /* Check if a threshold crossed before removing */
5562 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5563
5564 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005565 size = 0;
5566 for (i = 0; i < thresholds->primary->size; i++) {
5567 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005568 size++;
5569 }
5570
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005571 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005572
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005573 /* Set thresholds array to NULL if we don't have thresholds */
5574 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005575 kfree(new);
5576 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005577 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005578 }
5579
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005580 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005581
5582 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005583 new->current_threshold = -1;
5584 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5585 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005586 continue;
5587
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005588 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005589 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005590 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005591 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005592 * until rcu_assign_pointer(), so it's safe to increment
5593 * it here.
5594 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005595 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005596 }
5597 j++;
5598 }
5599
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005600swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005601 /* Swap primary and spare array */
5602 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005603 /* If all events are unregistered, free the spare array */
5604 if (!new) {
5605 kfree(thresholds->spare);
5606 thresholds->spare = NULL;
5607 }
5608
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005609 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005610
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005611 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005612 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005613unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005614 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005615}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005616
Tejun Heo81eeaf02013-08-08 20:11:26 -04005617static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005618 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5619{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005620 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005621 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005622 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005623
5624 BUG_ON(type != _OOM_TYPE);
5625 event = kmalloc(sizeof(*event), GFP_KERNEL);
5626 if (!event)
5627 return -ENOMEM;
5628
Michal Hocko1af8efe2011-07-26 16:08:24 -07005629 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005630
5631 event->eventfd = eventfd;
5632 list_add(&event->list, &memcg->oom_notify);
5633
5634 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005635 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005636 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005637 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005638
5639 return 0;
5640}
5641
Tejun Heo81eeaf02013-08-08 20:11:26 -04005642static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005643 struct cftype *cft, struct eventfd_ctx *eventfd)
5644{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005645 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005646 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005647 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005648
5649 BUG_ON(type != _OOM_TYPE);
5650
Michal Hocko1af8efe2011-07-26 16:08:24 -07005651 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005652
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005653 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005654 if (ev->eventfd == eventfd) {
5655 list_del(&ev->list);
5656 kfree(ev);
5657 }
5658 }
5659
Michal Hocko1af8efe2011-07-26 16:08:24 -07005660 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005661}
5662
Tejun Heo182446d2013-08-08 20:11:24 -04005663static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005664 struct cftype *cft, struct cgroup_map_cb *cb)
5665{
Tejun Heo182446d2013-08-08 20:11:24 -04005666 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005667
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005668 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005669
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005670 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005671 cb->fill(cb, "under_oom", 1);
5672 else
5673 cb->fill(cb, "under_oom", 0);
5674 return 0;
5675}
5676
Tejun Heo182446d2013-08-08 20:11:24 -04005677static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005678 struct cftype *cft, u64 val)
5679{
Tejun Heo182446d2013-08-08 20:11:24 -04005680 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005681 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005682
5683 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005684 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005685 return -EINVAL;
5686
Glauber Costa09998212013-02-22 16:34:55 -08005687 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005688 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005689 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005690 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005691 return -EINVAL;
5692 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005693 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005694 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005695 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005696 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005697 return 0;
5698}
5699
Andrew Mortonc255a452012-07-31 16:43:02 -07005700#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005701static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005702{
Glauber Costa55007d82012-12-18 14:22:38 -08005703 int ret;
5704
Glauber Costa2633d7a2012-12-18 14:22:34 -08005705 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005706 ret = memcg_propagate_kmem(memcg);
5707 if (ret)
5708 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005709
Glauber Costa1d62e432012-04-09 19:36:33 -03005710 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005711}
Glauber Costae5671df2011-12-11 21:47:01 +00005712
Li Zefan10d5ebf2013-07-08 16:00:33 -07005713static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005714{
Glauber Costa1d62e432012-04-09 19:36:33 -03005715 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005716}
5717
5718static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5719{
5720 if (!memcg_kmem_is_active(memcg))
5721 return;
5722
5723 /*
5724 * kmem charges can outlive the cgroup. In the case of slab
5725 * pages, for instance, a page contain objects from various
5726 * processes. As we prevent from taking a reference for every
5727 * such allocation we have to be careful when doing uncharge
5728 * (see memcg_uncharge_kmem) and here during offlining.
5729 *
5730 * The idea is that that only the _last_ uncharge which sees
5731 * the dead memcg will drop the last reference. An additional
5732 * reference is taken here before the group is marked dead
5733 * which is then paired with css_put during uncharge resp. here.
5734 *
5735 * Although this might sound strange as this path is called from
5736 * css_offline() when the referencemight have dropped down to 0
5737 * and shouldn't be incremented anymore (css_tryget would fail)
5738 * we do not have other options because of the kmem allocations
5739 * lifetime.
5740 */
5741 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005742
5743 memcg_kmem_mark_dead(memcg);
5744
5745 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5746 return;
5747
Glauber Costa7de37682012-12-18 14:22:07 -08005748 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005749 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005750}
Glauber Costae5671df2011-12-11 21:47:01 +00005751#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005752static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005753{
5754 return 0;
5755}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005756
Li Zefan10d5ebf2013-07-08 16:00:33 -07005757static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5758{
5759}
5760
5761static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005762{
5763}
Glauber Costae5671df2011-12-11 21:47:01 +00005764#endif
5765
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005766static struct cftype mem_cgroup_files[] = {
5767 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005768 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005769 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005770 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005771 .register_event = mem_cgroup_usage_register_event,
5772 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005773 },
5774 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005775 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005776 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005777 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005778 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005779 },
5780 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005781 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005782 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005783 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005784 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005785 },
5786 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005787 .name = "soft_limit_in_bytes",
5788 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5789 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005790 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005791 },
5792 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005793 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005794 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005795 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005796 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005797 },
Balbir Singh8697d332008-02-07 00:13:59 -08005798 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005799 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005800 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005801 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005802 {
5803 .name = "force_empty",
5804 .trigger = mem_cgroup_force_empty_write,
5805 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005806 {
5807 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005808 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005809 .write_u64 = mem_cgroup_hierarchy_write,
5810 .read_u64 = mem_cgroup_hierarchy_read,
5811 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005812 {
5813 .name = "swappiness",
5814 .read_u64 = mem_cgroup_swappiness_read,
5815 .write_u64 = mem_cgroup_swappiness_write,
5816 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005817 {
5818 .name = "move_charge_at_immigrate",
5819 .read_u64 = mem_cgroup_move_charge_read,
5820 .write_u64 = mem_cgroup_move_charge_write,
5821 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005822 {
5823 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005824 .read_map = mem_cgroup_oom_control_read,
5825 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005826 .register_event = mem_cgroup_oom_register_event,
5827 .unregister_event = mem_cgroup_oom_unregister_event,
5828 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5829 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005830 {
5831 .name = "pressure_level",
5832 .register_event = vmpressure_register_event,
5833 .unregister_event = vmpressure_unregister_event,
5834 },
Ying Han406eb0c2011-05-26 16:25:37 -07005835#ifdef CONFIG_NUMA
5836 {
5837 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005838 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005839 },
5840#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005841#ifdef CONFIG_MEMCG_KMEM
5842 {
5843 .name = "kmem.limit_in_bytes",
5844 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5845 .write_string = mem_cgroup_write,
5846 .read = mem_cgroup_read,
5847 },
5848 {
5849 .name = "kmem.usage_in_bytes",
5850 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5851 .read = mem_cgroup_read,
5852 },
5853 {
5854 .name = "kmem.failcnt",
5855 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5856 .trigger = mem_cgroup_reset,
5857 .read = mem_cgroup_read,
5858 },
5859 {
5860 .name = "kmem.max_usage_in_bytes",
5861 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5862 .trigger = mem_cgroup_reset,
5863 .read = mem_cgroup_read,
5864 },
Glauber Costa749c5412012-12-18 14:23:01 -08005865#ifdef CONFIG_SLABINFO
5866 {
5867 .name = "kmem.slabinfo",
5868 .read_seq_string = mem_cgroup_slabinfo_read,
5869 },
5870#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005871#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005872 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005873};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005874
Michal Hocko2d110852013-02-22 16:34:43 -08005875#ifdef CONFIG_MEMCG_SWAP
5876static struct cftype memsw_cgroup_files[] = {
5877 {
5878 .name = "memsw.usage_in_bytes",
5879 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5880 .read = mem_cgroup_read,
5881 .register_event = mem_cgroup_usage_register_event,
5882 .unregister_event = mem_cgroup_usage_unregister_event,
5883 },
5884 {
5885 .name = "memsw.max_usage_in_bytes",
5886 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5887 .trigger = mem_cgroup_reset,
5888 .read = mem_cgroup_read,
5889 },
5890 {
5891 .name = "memsw.limit_in_bytes",
5892 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5893 .write_string = mem_cgroup_write,
5894 .read = mem_cgroup_read,
5895 },
5896 {
5897 .name = "memsw.failcnt",
5898 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5899 .trigger = mem_cgroup_reset,
5900 .read = mem_cgroup_read,
5901 },
5902 { }, /* terminate */
5903};
5904#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005905static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005906{
5907 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005908 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005909 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005910 /*
5911 * This routine is called against possible nodes.
5912 * But it's BUG to call kmalloc() against offline node.
5913 *
5914 * TODO: this routine can waste much memory for nodes which will
5915 * never be onlined. It's better to use memory hotplug callback
5916 * function.
5917 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005918 if (!node_state(node, N_NORMAL_MEMORY))
5919 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005920 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005921 if (!pn)
5922 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005923
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005924 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5925 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005926 lruvec_init(&mz->lruvec);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005927 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005928 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005929 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005930 return 0;
5931}
5932
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005933static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005934{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005935 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005936}
5937
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005938static struct mem_cgroup *mem_cgroup_alloc(void)
5939{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005940 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005941 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005942
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005943 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005944 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005945 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005946 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005947 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005948
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005949 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005950 return NULL;
5951
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005952 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5953 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005954 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005955 spin_lock_init(&memcg->pcp_counter_lock);
5956 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005957
5958out_free:
5959 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005960 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005961 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005962 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005963 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005964}
5965
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005966/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005967 * At destroying mem_cgroup, references from swap_cgroup can remain.
5968 * (scanning all at force_empty is too costly...)
5969 *
5970 * Instead of clearing all references at force_empty, we remember
5971 * the number of reference from swap_cgroup and free mem_cgroup when
5972 * it goes down to 0.
5973 *
5974 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005975 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005976
5977static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005978{
Glauber Costac8b2a362012-12-18 14:22:13 -08005979 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005980 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005981
Glauber Costac8b2a362012-12-18 14:22:13 -08005982 for_each_node(node)
5983 free_mem_cgroup_per_zone_info(memcg, node);
5984
5985 free_percpu(memcg->stat);
5986
Glauber Costa3f134612012-05-29 15:07:11 -07005987 /*
5988 * We need to make sure that (at least for now), the jump label
5989 * destruction code runs outside of the cgroup lock. This is because
5990 * get_online_cpus(), which is called from the static_branch update,
5991 * can't be called inside the cgroup_lock. cpusets are the ones
5992 * enforcing this dependency, so if they ever change, we might as well.
5993 *
5994 * schedule_work() will guarantee this happens. Be careful if you need
5995 * to move this code around, and make sure it is outside
5996 * the cgroup_lock.
5997 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005998 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005999 if (size < PAGE_SIZE)
6000 kfree(memcg);
6001 else
6002 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006003}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006004
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006005/*
6006 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6007 */
Glauber Costae1aab162011-12-11 21:47:03 +00006008struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006009{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006010 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006011 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006012 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006013}
Glauber Costae1aab162011-12-11 21:47:03 +00006014EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006015
Li Zefan0eb253e2009-01-15 13:51:25 -08006016static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04006017mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006018{
Glauber Costad142e3e2013-02-22 16:34:52 -08006019 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006020 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006021 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006022
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006023 memcg = mem_cgroup_alloc();
6024 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006025 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006026
Bob Liu3ed28fa2012-01-12 17:19:04 -08006027 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006028 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006029 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006030
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006031 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04006032 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006033 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006034 res_counter_init(&memcg->res, NULL);
6035 res_counter_init(&memcg->memsw, NULL);
6036 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006037 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006038
Glauber Costad142e3e2013-02-22 16:34:52 -08006039 memcg->last_scanned_node = MAX_NUMNODES;
6040 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08006041 memcg->move_charge_at_immigrate = 0;
6042 mutex_init(&memcg->thresholds_lock);
6043 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006044 vmpressure_init(&memcg->vmpressure);
Michal Hocko7d910c02013-09-12 15:13:28 -07006045 spin_lock_init(&memcg->soft_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08006046
6047 return &memcg->css;
6048
6049free_out:
6050 __mem_cgroup_free(memcg);
6051 return ERR_PTR(error);
6052}
6053
6054static int
Tejun Heoeb954192013-08-08 20:11:23 -04006055mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08006056{
Tejun Heoeb954192013-08-08 20:11:23 -04006057 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6058 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08006059 int error = 0;
6060
Li Zefan4219b2d2013-09-23 16:56:29 +08006061 if (css->cgroup->id > MEM_CGROUP_ID_MAX)
6062 return -ENOSPC;
6063
Tejun Heo63876982013-08-08 20:11:23 -04006064 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08006065 return 0;
6066
Glauber Costa09998212013-02-22 16:34:55 -08006067 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006068
6069 memcg->use_hierarchy = parent->use_hierarchy;
6070 memcg->oom_kill_disable = parent->oom_kill_disable;
6071 memcg->swappiness = mem_cgroup_swappiness(parent);
6072
6073 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006074 res_counter_init(&memcg->res, &parent->res);
6075 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006076 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006077
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006078 /*
Li Zefan8d76a972013-07-08 16:00:36 -07006079 * No need to take a reference to the parent because cgroup
6080 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006081 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08006082 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006083 res_counter_init(&memcg->res, NULL);
6084 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006085 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006086 /*
6087 * Deeper hierachy with use_hierarchy == false doesn't make
6088 * much sense so let cgroup subsystem know about this
6089 * unfortunate state in our controller.
6090 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006091 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006092 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006093 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006094
6095 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006096 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006097 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006098}
6099
Michal Hocko5f578162013-04-29 15:07:17 -07006100/*
6101 * Announce all parents that a group from their hierarchy is gone.
6102 */
6103static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6104{
6105 struct mem_cgroup *parent = memcg;
6106
6107 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006108 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006109
6110 /*
6111 * if the root memcg is not hierarchical we have to check it
6112 * explicitely.
6113 */
6114 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006115 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006116}
6117
Tejun Heoeb954192013-08-08 20:11:23 -04006118static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006119{
Tejun Heoeb954192013-08-08 20:11:23 -04006120 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006121
Li Zefan10d5ebf2013-07-08 16:00:33 -07006122 kmem_cgroup_css_offline(memcg);
6123
Michal Hocko5f578162013-04-29 15:07:17 -07006124 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006125 mem_cgroup_reparent_charges(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -07006126 if (memcg->soft_contributed) {
6127 while ((memcg = parent_mem_cgroup(memcg)))
6128 atomic_dec(&memcg->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -07006129
6130 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
6131 atomic_dec(&root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -07006132 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08006133 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006134 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006135}
6136
Tejun Heoeb954192013-08-08 20:11:23 -04006137static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006138{
Tejun Heoeb954192013-08-08 20:11:23 -04006139 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006140
Li Zefan10d5ebf2013-07-08 16:00:33 -07006141 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006142 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006143}
6144
Daisuke Nishimura02491442010-03-10 15:22:17 -08006145#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006146/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006147#define PRECHARGE_COUNT_AT_ONCE 256
6148static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006149{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006150 int ret = 0;
6151 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006152 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006153
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006154 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006155 mc.precharge += count;
6156 /* we don't need css_get for root */
6157 return ret;
6158 }
6159 /* try to charge at once */
6160 if (count > 1) {
6161 struct res_counter *dummy;
6162 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006163 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006164 * by cgroup_lock_live_cgroup() that it is not removed and we
6165 * are still under the same cgroup_mutex. So we can postpone
6166 * css_get().
6167 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006168 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006169 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006170 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006171 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006172 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006173 goto one_by_one;
6174 }
6175 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006176 return ret;
6177 }
6178one_by_one:
6179 /* fall back to one by one charge */
6180 while (count--) {
6181 if (signal_pending(current)) {
6182 ret = -EINTR;
6183 break;
6184 }
6185 if (!batch_count--) {
6186 batch_count = PRECHARGE_COUNT_AT_ONCE;
6187 cond_resched();
6188 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006189 ret = __mem_cgroup_try_charge(NULL,
6190 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006191 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006192 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006193 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006194 mc.precharge++;
6195 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006196 return ret;
6197}
6198
6199/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006200 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006201 * @vma: the vma the pte to be checked belongs
6202 * @addr: the address corresponding to the pte to be checked
6203 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006204 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006205 *
6206 * Returns
6207 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6208 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6209 * move charge. if @target is not NULL, the page is stored in target->page
6210 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006211 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6212 * target for charge migration. if @target is not NULL, the entry is stored
6213 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006214 *
6215 * Called with pte lock held.
6216 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006217union mc_target {
6218 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006219 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006220};
6221
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006222enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006223 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006224 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006225 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006226};
6227
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006228static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6229 unsigned long addr, pte_t ptent)
6230{
6231 struct page *page = vm_normal_page(vma, addr, ptent);
6232
6233 if (!page || !page_mapped(page))
6234 return NULL;
6235 if (PageAnon(page)) {
6236 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006237 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006238 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006239 } else if (!move_file())
6240 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006241 return NULL;
6242 if (!get_page_unless_zero(page))
6243 return NULL;
6244
6245 return page;
6246}
6247
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006248#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006249static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6250 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6251{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006252 struct page *page = NULL;
6253 swp_entry_t ent = pte_to_swp_entry(ptent);
6254
6255 if (!move_anon() || non_swap_entry(ent))
6256 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006257 /*
6258 * Because lookup_swap_cache() updates some statistics counter,
6259 * we call find_get_page() with swapper_space directly.
6260 */
Shaohua Li33806f02013-02-22 16:34:37 -08006261 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006262 if (do_swap_account)
6263 entry->val = ent.val;
6264
6265 return page;
6266}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006267#else
6268static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6269 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6270{
6271 return NULL;
6272}
6273#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006274
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006275static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6276 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6277{
6278 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006279 struct address_space *mapping;
6280 pgoff_t pgoff;
6281
6282 if (!vma->vm_file) /* anonymous vma */
6283 return NULL;
6284 if (!move_file())
6285 return NULL;
6286
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006287 mapping = vma->vm_file->f_mapping;
6288 if (pte_none(ptent))
6289 pgoff = linear_page_index(vma, addr);
6290 else /* pte_file(ptent) is true */
6291 pgoff = pte_to_pgoff(ptent);
6292
6293 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006294 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006295
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006296#ifdef CONFIG_SWAP
6297 /* shmem/tmpfs may report page out on swap: account for that too. */
6298 if (radix_tree_exceptional_entry(page)) {
6299 swp_entry_t swap = radix_to_swp_entry(page);
6300 if (do_swap_account)
6301 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006302 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006303 }
6304#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006305 return page;
6306}
6307
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006308static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006309 unsigned long addr, pte_t ptent, union mc_target *target)
6310{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006311 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006312 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006313 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006314 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006315
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006316 if (pte_present(ptent))
6317 page = mc_handle_present_pte(vma, addr, ptent);
6318 else if (is_swap_pte(ptent))
6319 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006320 else if (pte_none(ptent) || pte_file(ptent))
6321 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006322
6323 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006324 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006325 if (page) {
6326 pc = lookup_page_cgroup(page);
6327 /*
6328 * Do only loose check w/o page_cgroup lock.
6329 * mem_cgroup_move_account() checks the pc is valid or not under
6330 * the lock.
6331 */
6332 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6333 ret = MC_TARGET_PAGE;
6334 if (target)
6335 target->page = page;
6336 }
6337 if (!ret || !target)
6338 put_page(page);
6339 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006340 /* There is a swap entry and a page doesn't exist or isn't charged */
6341 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08006342 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006343 ret = MC_TARGET_SWAP;
6344 if (target)
6345 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006346 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006347 return ret;
6348}
6349
Naoya Horiguchi12724852012-03-21 16:34:28 -07006350#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6351/*
6352 * We don't consider swapping or file mapped pages because THP does not
6353 * support them for now.
6354 * Caller should make sure that pmd_trans_huge(pmd) is true.
6355 */
6356static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6357 unsigned long addr, pmd_t pmd, union mc_target *target)
6358{
6359 struct page *page = NULL;
6360 struct page_cgroup *pc;
6361 enum mc_target_type ret = MC_TARGET_NONE;
6362
6363 page = pmd_page(pmd);
6364 VM_BUG_ON(!page || !PageHead(page));
6365 if (!move_anon())
6366 return ret;
6367 pc = lookup_page_cgroup(page);
6368 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6369 ret = MC_TARGET_PAGE;
6370 if (target) {
6371 get_page(page);
6372 target->page = page;
6373 }
6374 }
6375 return ret;
6376}
6377#else
6378static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6379 unsigned long addr, pmd_t pmd, union mc_target *target)
6380{
6381 return MC_TARGET_NONE;
6382}
6383#endif
6384
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006385static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6386 unsigned long addr, unsigned long end,
6387 struct mm_walk *walk)
6388{
6389 struct vm_area_struct *vma = walk->private;
6390 pte_t *pte;
6391 spinlock_t *ptl;
6392
Naoya Horiguchi12724852012-03-21 16:34:28 -07006393 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6394 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6395 mc.precharge += HPAGE_PMD_NR;
6396 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006397 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006398 }
Dave Hansen03319322011-03-22 16:32:56 -07006399
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006400 if (pmd_trans_unstable(pmd))
6401 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006402 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6403 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006404 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006405 mc.precharge++; /* increment precharge temporarily */
6406 pte_unmap_unlock(pte - 1, ptl);
6407 cond_resched();
6408
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006409 return 0;
6410}
6411
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006412static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6413{
6414 unsigned long precharge;
6415 struct vm_area_struct *vma;
6416
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006417 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006418 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6419 struct mm_walk mem_cgroup_count_precharge_walk = {
6420 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6421 .mm = mm,
6422 .private = vma,
6423 };
6424 if (is_vm_hugetlb_page(vma))
6425 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006426 walk_page_range(vma->vm_start, vma->vm_end,
6427 &mem_cgroup_count_precharge_walk);
6428 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006429 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006430
6431 precharge = mc.precharge;
6432 mc.precharge = 0;
6433
6434 return precharge;
6435}
6436
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006437static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6438{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006439 unsigned long precharge = mem_cgroup_count_precharge(mm);
6440
6441 VM_BUG_ON(mc.moving_task);
6442 mc.moving_task = current;
6443 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006444}
6445
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006446/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6447static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006448{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006449 struct mem_cgroup *from = mc.from;
6450 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006451 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006452
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006453 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006454 if (mc.precharge) {
6455 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6456 mc.precharge = 0;
6457 }
6458 /*
6459 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6460 * we must uncharge here.
6461 */
6462 if (mc.moved_charge) {
6463 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6464 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006465 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006466 /* we must fixup refcnts and charges */
6467 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006468 /* uncharge swap account from the old cgroup */
6469 if (!mem_cgroup_is_root(mc.from))
6470 res_counter_uncharge(&mc.from->memsw,
6471 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006472
6473 for (i = 0; i < mc.moved_swap; i++)
6474 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006475
6476 if (!mem_cgroup_is_root(mc.to)) {
6477 /*
6478 * we charged both to->res and to->memsw, so we should
6479 * uncharge to->res.
6480 */
6481 res_counter_uncharge(&mc.to->res,
6482 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006483 }
Li Zefan40503772013-07-08 16:00:34 -07006484 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006485 mc.moved_swap = 0;
6486 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006487 memcg_oom_recover(from);
6488 memcg_oom_recover(to);
6489 wake_up_all(&mc.waitq);
6490}
6491
6492static void mem_cgroup_clear_mc(void)
6493{
6494 struct mem_cgroup *from = mc.from;
6495
6496 /*
6497 * we must clear moving_task before waking up waiters at the end of
6498 * task migration.
6499 */
6500 mc.moving_task = NULL;
6501 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006502 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006503 mc.from = NULL;
6504 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006505 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006506 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006507}
6508
Tejun Heoeb954192013-08-08 20:11:23 -04006509static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006510 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006511{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006512 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006513 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006514 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006515 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006516
Glauber Costaee5e8472013-02-22 16:34:50 -08006517 /*
6518 * We are now commited to this value whatever it is. Changes in this
6519 * tunable will only affect upcoming migrations, not the current one.
6520 * So we need to save it, and keep it going.
6521 */
6522 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6523 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006524 struct mm_struct *mm;
6525 struct mem_cgroup *from = mem_cgroup_from_task(p);
6526
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006527 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006528
6529 mm = get_task_mm(p);
6530 if (!mm)
6531 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006532 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006533 if (mm->owner == p) {
6534 VM_BUG_ON(mc.from);
6535 VM_BUG_ON(mc.to);
6536 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006537 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006538 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006539 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006540 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006541 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006542 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006543 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006544 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006545 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006546
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006547 ret = mem_cgroup_precharge_mc(mm);
6548 if (ret)
6549 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006550 }
6551 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006552 }
6553 return ret;
6554}
6555
Tejun Heoeb954192013-08-08 20:11:23 -04006556static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006557 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006558{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006559 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006560}
6561
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006562static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6563 unsigned long addr, unsigned long end,
6564 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006565{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006566 int ret = 0;
6567 struct vm_area_struct *vma = walk->private;
6568 pte_t *pte;
6569 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006570 enum mc_target_type target_type;
6571 union mc_target target;
6572 struct page *page;
6573 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006574
Naoya Horiguchi12724852012-03-21 16:34:28 -07006575 /*
6576 * We don't take compound_lock() here but no race with splitting thp
6577 * happens because:
6578 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6579 * under splitting, which means there's no concurrent thp split,
6580 * - if another thread runs into split_huge_page() just after we
6581 * entered this if-block, the thread must wait for page table lock
6582 * to be unlocked in __split_huge_page_splitting(), where the main
6583 * part of thp split is not executed yet.
6584 */
6585 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006586 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006587 spin_unlock(&vma->vm_mm->page_table_lock);
6588 return 0;
6589 }
6590 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6591 if (target_type == MC_TARGET_PAGE) {
6592 page = target.page;
6593 if (!isolate_lru_page(page)) {
6594 pc = lookup_page_cgroup(page);
6595 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006596 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006597 mc.precharge -= HPAGE_PMD_NR;
6598 mc.moved_charge += HPAGE_PMD_NR;
6599 }
6600 putback_lru_page(page);
6601 }
6602 put_page(page);
6603 }
6604 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006605 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006606 }
6607
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006608 if (pmd_trans_unstable(pmd))
6609 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006610retry:
6611 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6612 for (; addr != end; addr += PAGE_SIZE) {
6613 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006614 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006615
6616 if (!mc.precharge)
6617 break;
6618
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006619 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006620 case MC_TARGET_PAGE:
6621 page = target.page;
6622 if (isolate_lru_page(page))
6623 goto put;
6624 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006625 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006626 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006627 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006628 /* we uncharge from mc.from later. */
6629 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006630 }
6631 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006632put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006633 put_page(page);
6634 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006635 case MC_TARGET_SWAP:
6636 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006637 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006638 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006639 /* we fixup refcnts and charges later. */
6640 mc.moved_swap++;
6641 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006642 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006643 default:
6644 break;
6645 }
6646 }
6647 pte_unmap_unlock(pte - 1, ptl);
6648 cond_resched();
6649
6650 if (addr != end) {
6651 /*
6652 * We have consumed all precharges we got in can_attach().
6653 * We try charge one by one, but don't do any additional
6654 * charges to mc.to if we have failed in charge once in attach()
6655 * phase.
6656 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006657 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006658 if (!ret)
6659 goto retry;
6660 }
6661
6662 return ret;
6663}
6664
6665static void mem_cgroup_move_charge(struct mm_struct *mm)
6666{
6667 struct vm_area_struct *vma;
6668
6669 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006670retry:
6671 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6672 /*
6673 * Someone who are holding the mmap_sem might be waiting in
6674 * waitq. So we cancel all extra charges, wake up all waiters,
6675 * and retry. Because we cancel precharges, we might not be able
6676 * to move enough charges, but moving charge is a best-effort
6677 * feature anyway, so it wouldn't be a big problem.
6678 */
6679 __mem_cgroup_clear_mc();
6680 cond_resched();
6681 goto retry;
6682 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006683 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6684 int ret;
6685 struct mm_walk mem_cgroup_move_charge_walk = {
6686 .pmd_entry = mem_cgroup_move_charge_pte_range,
6687 .mm = mm,
6688 .private = vma,
6689 };
6690 if (is_vm_hugetlb_page(vma))
6691 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006692 ret = walk_page_range(vma->vm_start, vma->vm_end,
6693 &mem_cgroup_move_charge_walk);
6694 if (ret)
6695 /*
6696 * means we have consumed all precharges and failed in
6697 * doing additional charge. Just abandon here.
6698 */
6699 break;
6700 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006701 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006702}
6703
Tejun Heoeb954192013-08-08 20:11:23 -04006704static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006705 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006706{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006707 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006708 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006709
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006710 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006711 if (mc.to)
6712 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006713 mmput(mm);
6714 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006715 if (mc.to)
6716 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006717}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006718#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006719static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006720 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006721{
6722 return 0;
6723}
Tejun Heoeb954192013-08-08 20:11:23 -04006724static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006725 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006726{
6727}
Tejun Heoeb954192013-08-08 20:11:23 -04006728static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006729 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006730{
6731}
6732#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006733
Tejun Heof00baae2013-04-15 13:41:15 -07006734/*
6735 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6736 * to verify sane_behavior flag on each mount attempt.
6737 */
Tejun Heoeb954192013-08-08 20:11:23 -04006738static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006739{
6740 /*
6741 * use_hierarchy is forced with sane_behavior. cgroup core
6742 * guarantees that @root doesn't have any children, so turning it
6743 * on for the root memcg is enough.
6744 */
Tejun Heoeb954192013-08-08 20:11:23 -04006745 if (cgroup_sane_behavior(root_css->cgroup))
6746 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006747}
6748
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006749struct cgroup_subsys mem_cgroup_subsys = {
6750 .name = "memory",
6751 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006752 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006753 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006754 .css_offline = mem_cgroup_css_offline,
6755 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006756 .can_attach = mem_cgroup_can_attach,
6757 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006758 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006759 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006760 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006761 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006762};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006763
Andrew Mortonc255a452012-07-31 16:43:02 -07006764#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006765static int __init enable_swap_account(char *s)
6766{
Michal Hockoa2c89902011-05-24 17:12:50 -07006767 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006768 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006769 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006770 really_do_swap_account = 0;
6771 return 1;
6772}
Michal Hockoa2c89902011-05-24 17:12:50 -07006773__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006774
Michal Hocko2d110852013-02-22 16:34:43 -08006775static void __init memsw_file_init(void)
6776{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006777 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006778}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006779
6780static void __init enable_swap_cgroup(void)
6781{
6782 if (!mem_cgroup_disabled() && really_do_swap_account) {
6783 do_swap_account = 1;
6784 memsw_file_init();
6785 }
6786}
6787
Michal Hocko2d110852013-02-22 16:34:43 -08006788#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006789static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006790{
6791}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006792#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006793
6794/*
Michal Hocko10813122013-02-22 16:35:41 -08006795 * subsys_initcall() for memory controller.
6796 *
6797 * Some parts like hotcpu_notifier() have to be initialized from this context
6798 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6799 * everything that doesn't depend on a specific mem_cgroup structure should
6800 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006801 */
6802static int __init mem_cgroup_init(void)
6803{
6804 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006805 enable_swap_cgroup();
Michal Hockoe4777492013-02-22 16:35:40 -08006806 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006807 return 0;
6808}
6809subsys_initcall(mem_cgroup_init);