blob: 404a13a09efa0ee17f268aec1075e80b6eb1f488 [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 Zefan34c00c32013-09-23 16:56:01 +0800491static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
492{
493 /*
494 * The ID of the root cgroup is 0, but memcg treat 0 as an
495 * invalid ID, so we return (cgroup_id + 1).
496 */
497 return memcg->css.cgroup->id + 1;
498}
499
500static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
501{
502 struct cgroup_subsys_state *css;
503
504 css = css_from_id(id - 1, &mem_cgroup_subsys);
505 return mem_cgroup_from_css(css);
506}
507
Glauber Costae1aab162011-12-11 21:47:03 +0000508/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700509#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000510
Glauber Costae1aab162011-12-11 21:47:03 +0000511void sock_update_memcg(struct sock *sk)
512{
Glauber Costa376be5f2012-01-20 04:57:14 +0000513 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000514 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700515 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000516
517 BUG_ON(!sk->sk_prot->proto_cgroup);
518
Glauber Costaf3f511e2012-01-05 20:16:39 +0000519 /* Socket cloning can throw us here with sk_cgrp already
520 * filled. It won't however, necessarily happen from
521 * process context. So the test for root memcg given
522 * the current task's memcg won't help us in this case.
523 *
524 * Respecting the original socket's memcg is a better
525 * decision in this case.
526 */
527 if (sk->sk_cgrp) {
528 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700529 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000530 return;
531 }
532
Glauber Costae1aab162011-12-11 21:47:03 +0000533 rcu_read_lock();
534 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700535 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700536 if (!mem_cgroup_is_root(memcg) &&
537 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700538 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000539 }
540 rcu_read_unlock();
541 }
542}
543EXPORT_SYMBOL(sock_update_memcg);
544
545void sock_release_memcg(struct sock *sk)
546{
Glauber Costa376be5f2012-01-20 04:57:14 +0000547 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000548 struct mem_cgroup *memcg;
549 WARN_ON(!sk->sk_cgrp->memcg);
550 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700551 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000552 }
553}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000554
555struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
556{
557 if (!memcg || mem_cgroup_is_root(memcg))
558 return NULL;
559
560 return &memcg->tcp_mem.cg_proto;
561}
562EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000563
Glauber Costa3f134612012-05-29 15:07:11 -0700564static void disarm_sock_keys(struct mem_cgroup *memcg)
565{
566 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
567 return;
568 static_key_slow_dec(&memcg_socket_limit_enabled);
569}
570#else
571static void disarm_sock_keys(struct mem_cgroup *memcg)
572{
573}
574#endif
575
Glauber Costaa8964b92012-12-18 14:22:09 -0800576#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800577/*
578 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
579 * There are two main reasons for not using the css_id for this:
580 * 1) this works better in sparse environments, where we have a lot of memcgs,
581 * but only a few kmem-limited. Or also, if we have, for instance, 200
582 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
583 * 200 entry array for that.
584 *
585 * 2) In order not to violate the cgroup API, we would like to do all memory
586 * allocation in ->create(). At that point, we haven't yet allocated the
587 * css_id. Having a separate index prevents us from messing with the cgroup
588 * core for this
589 *
590 * The current size of the caches array is stored in
591 * memcg_limited_groups_array_size. It will double each time we have to
592 * increase it.
593 */
594static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800595int memcg_limited_groups_array_size;
596
Glauber Costa55007d82012-12-18 14:22:38 -0800597/*
598 * MIN_SIZE is different than 1, because we would like to avoid going through
599 * the alloc/free process all the time. In a small machine, 4 kmem-limited
600 * cgroups is a reasonable guess. In the future, it could be a parameter or
601 * tunable, but that is strictly not necessary.
602 *
603 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
604 * this constant directly from cgroup, but it is understandable that this is
605 * better kept as an internal representation in cgroup.c. In any case, the
606 * css_id space is not getting any smaller, and we don't have to necessarily
607 * increase ours as well if it increases.
608 */
609#define MEMCG_CACHES_MIN_SIZE 4
610#define MEMCG_CACHES_MAX_SIZE 65535
611
Glauber Costad7f25f82012-12-18 14:22:40 -0800612/*
613 * A lot of the calls to the cache allocation functions are expected to be
614 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
615 * conditional to this static branch, we'll have to allow modules that does
616 * kmem_cache_alloc and the such to see this symbol as well
617 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800618struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800619EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800620
621static void disarm_kmem_keys(struct mem_cgroup *memcg)
622{
Glauber Costa55007d82012-12-18 14:22:38 -0800623 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800624 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800625 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
626 }
Glauber Costabea207c2012-12-18 14:22:11 -0800627 /*
628 * This check can't live in kmem destruction function,
629 * since the charges will outlive the cgroup
630 */
631 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800632}
633#else
634static void disarm_kmem_keys(struct mem_cgroup *memcg)
635{
636}
637#endif /* CONFIG_MEMCG_KMEM */
638
639static void disarm_static_keys(struct mem_cgroup *memcg)
640{
641 disarm_sock_keys(memcg);
642 disarm_kmem_keys(memcg);
643}
644
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700645static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800646
Balbir Singhf64c3f52009-09-23 15:56:37 -0700647static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700648mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700649{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800650 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700651 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700652}
653
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700654struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100655{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700656 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100657}
658
Balbir Singhf64c3f52009-09-23 15:56:37 -0700659static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700660page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700661{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700662 int nid = page_to_nid(page);
663 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700664
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700665 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700666}
667
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700668/*
669 * Implementation Note: reading percpu statistics for memcg.
670 *
671 * Both of vmstat[] and percpu_counter has threshold and do periodic
672 * synchronization to implement "quick" read. There are trade-off between
673 * reading cost and precision of value. Then, we may have a chance to implement
674 * a periodic synchronizion of counter in memcg's counter.
675 *
676 * But this _read() function is used for user interface now. The user accounts
677 * memory usage by memory cgroup and he _always_ requires exact value because
678 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
679 * have to visit all online cpus and make sum. So, for now, unnecessary
680 * synchronization is not implemented. (just implemented for cpu hotplug)
681 *
682 * If there are kernel internal actions which can make use of some not-exact
683 * value, and reading all cpu value can be performance bottleneck in some
684 * common workload, threashold and synchonization as vmstat[] should be
685 * implemented.
686 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700687static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700688 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800689{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700690 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800691 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800692
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700693 get_online_cpus();
694 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700695 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700696#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700697 spin_lock(&memcg->pcp_counter_lock);
698 val += memcg->nocpu_base.count[idx];
699 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700700#endif
701 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800702 return val;
703}
704
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700705static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700706 bool charge)
707{
708 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700709 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700710}
711
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700712static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700713 enum mem_cgroup_events_index idx)
714{
715 unsigned long val = 0;
716 int cpu;
717
718 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700719 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700720#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700721 spin_lock(&memcg->pcp_counter_lock);
722 val += memcg->nocpu_base.events[idx];
723 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700724#endif
725 return val;
726}
727
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700728static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700729 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700730 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800731{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800732 preempt_disable();
733
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700734 /*
735 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
736 * counted as CACHE even if it's on ANON LRU.
737 */
738 if (anon)
739 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700740 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800741 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700742 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700743 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700744
David Rientjesb070e652013-05-07 16:18:09 -0700745 if (PageTransHuge(page))
746 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
747 nr_pages);
748
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800749 /* pagein of a big page is an event. So, ignore page size */
750 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700751 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800752 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700753 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800754 nr_pages = -nr_pages; /* for event */
755 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800756
Johannes Weiner13114712012-05-29 15:07:07 -0700757 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800758
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800759 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800760}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800761
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700762unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700763mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700764{
765 struct mem_cgroup_per_zone *mz;
766
767 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
768 return mz->lru_size[lru];
769}
770
771static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700772mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700773 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700774{
775 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700776 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700777 unsigned long ret = 0;
778
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700779 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700780
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700781 for_each_lru(lru) {
782 if (BIT(lru) & lru_mask)
783 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700784 }
785 return ret;
786}
787
788static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700789mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700790 int nid, unsigned int lru_mask)
791{
Ying Han889976d2011-05-26 16:25:33 -0700792 u64 total = 0;
793 int zid;
794
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700795 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700796 total += mem_cgroup_zone_nr_lru_pages(memcg,
797 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700798
Ying Han889976d2011-05-26 16:25:33 -0700799 return total;
800}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700801
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700802static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700803 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800804{
Ying Han889976d2011-05-26 16:25:33 -0700805 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800806 u64 total = 0;
807
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800808 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700809 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800810 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800811}
812
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800813static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
814 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800815{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700816 unsigned long val, next;
817
Johannes Weiner13114712012-05-29 15:07:07 -0700818 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700819 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700820 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800821 if ((long)next - (long)val < 0) {
822 switch (target) {
823 case MEM_CGROUP_TARGET_THRESH:
824 next = val + THRESHOLDS_EVENTS_TARGET;
825 break;
Michal Hocko7d910c02013-09-12 15:13:28 -0700826 case MEM_CGROUP_TARGET_SOFTLIMIT:
827 next = val + SOFTLIMIT_EVENTS_TARGET;
828 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800829 case MEM_CGROUP_TARGET_NUMAINFO:
830 next = val + NUMAINFO_EVENTS_TARGET;
831 break;
832 default:
833 break;
834 }
835 __this_cpu_write(memcg->stat->targets[target], next);
836 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700837 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800838 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800839}
840
841/*
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700842 * Called from rate-limited memcg_check_events when enough
Michal Hocko7d910c02013-09-12 15:13:28 -0700843 * MEM_CGROUP_TARGET_SOFTLIMIT events are accumulated and it makes sure
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700844 * that all the parents up the hierarchy will be notified that this group
Michal Hocko7d910c02013-09-12 15:13:28 -0700845 * is in excess or that it is not in excess anymore. mmecg->soft_contributed
846 * makes the transition a single action whenever the state flips from one to
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700847 * the other.
Michal Hocko7d910c02013-09-12 15:13:28 -0700848 */
849static void mem_cgroup_update_soft_limit(struct mem_cgroup *memcg)
850{
851 unsigned long long excess = res_counter_soft_limit_excess(&memcg->res);
852 struct mem_cgroup *parent = memcg;
853 int delta = 0;
854
855 spin_lock(&memcg->soft_lock);
856 if (excess) {
857 if (!memcg->soft_contributed) {
858 delta = 1;
859 memcg->soft_contributed = true;
860 }
861 } else {
862 if (memcg->soft_contributed) {
863 delta = -1;
864 memcg->soft_contributed = false;
865 }
866 }
867
868 /*
869 * Necessary to update all ancestors when hierarchy is used
870 * because their event counter is not touched.
Michal Hocko1be171d2013-09-12 15:13:32 -0700871 * We track children even outside the hierarchy for the root
872 * cgroup because tree walk starting at root should visit
873 * all cgroups and we want to prevent from pointless tree
874 * walk if no children is below the limit.
Michal Hocko7d910c02013-09-12 15:13:28 -0700875 */
876 while (delta && (parent = parent_mem_cgroup(parent)))
877 atomic_add(delta, &parent->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -0700878 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
879 atomic_add(delta, &root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -0700880 spin_unlock(&memcg->soft_lock);
881}
882
883/*
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800884 * Check events in order.
885 *
886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700887static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800888{
Steven Rostedt47994012011-11-02 13:38:33 -0700889 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800890 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800891 if (unlikely(mem_cgroup_event_ratelimit(memcg,
892 MEM_CGROUP_TARGET_THRESH))) {
Michal Hocko7d910c02013-09-12 15:13:28 -0700893 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800894 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800895
Michal Hocko7d910c02013-09-12 15:13:28 -0700896 do_softlimit = mem_cgroup_event_ratelimit(memcg,
897 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700898#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800899 do_numainfo = mem_cgroup_event_ratelimit(memcg,
900 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700901#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800902 preempt_enable();
903
904 mem_cgroup_threshold(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -0700905 if (unlikely(do_softlimit))
906 mem_cgroup_update_soft_limit(memcg);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800907#if MAX_NUMNODES > 1
908 if (unlikely(do_numainfo))
909 atomic_inc(&memcg->numainfo_events);
910#endif
911 } else
912 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800913}
914
Balbir Singhcf475ad2008-04-29 01:00:16 -0700915struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800916{
Balbir Singh31a78f22008-09-28 23:09:31 +0100917 /*
918 * mm_update_next_owner() may clear mm->owner to NULL
919 * if it races with swapoff, page migration, etc.
920 * So this can be called with p == NULL.
921 */
922 if (unlikely(!p))
923 return NULL;
924
Tejun Heo8af01f52013-08-08 20:11:22 -0400925 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800926}
927
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700928struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800929{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700930 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700931
932 if (!mm)
933 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800934 /*
935 * Because we have no locks, mm->owner's may be being moved to other
936 * cgroup. We use css_tryget() here even if this looks
937 * pessimistic (rather than adding locks here).
938 */
939 rcu_read_lock();
940 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700941 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
942 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800943 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700944 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800945 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700946 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800947}
948
Michal Hockode577802013-09-12 15:13:26 -0700949static enum mem_cgroup_filter_t
950mem_cgroup_filter(struct mem_cgroup *memcg, struct mem_cgroup *root,
951 mem_cgroup_iter_filter cond)
952{
953 if (!cond)
954 return VISIT;
955 return cond(memcg, root);
956}
957
Michal Hocko16248d82013-04-29 15:07:19 -0700958/*
959 * Returns a next (in a pre-order walk) alive memcg (with elevated css
960 * ref. count) or NULL if the whole root's subtree has been visited.
961 *
962 * helper function to be used by mem_cgroup_iter
963 */
964static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Michal Hockode577802013-09-12 15:13:26 -0700965 struct mem_cgroup *last_visited, mem_cgroup_iter_filter cond)
Michal Hocko16248d82013-04-29 15:07:19 -0700966{
Tejun Heo492eb212013-08-08 20:11:25 -0400967 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700968
Tejun Heobd8815a2013-08-08 20:11:27 -0400969 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -0700970skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -0400971 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -0700972
973 /*
974 * Even if we found a group we have to make sure it is
975 * alive. css && !memcg means that the groups should be
976 * skipped and we should continue the tree walk.
977 * last_visited css is safe to use because it is
978 * protected by css_get and the tree walk is rcu safe.
979 */
Tejun Heo492eb212013-08-08 20:11:25 -0400980 if (next_css) {
981 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
982
Michal Hockode577802013-09-12 15:13:26 -0700983 switch (mem_cgroup_filter(mem, root, cond)) {
984 case SKIP:
Tejun Heo492eb212013-08-08 20:11:25 -0400985 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700986 goto skip_node;
Michal Hockode577802013-09-12 15:13:26 -0700987 case SKIP_TREE:
988 if (mem == root)
989 return NULL;
990 /*
991 * css_rightmost_descendant is not an optimal way to
992 * skip through a subtree (especially for imbalanced
993 * trees leaning to right) but that's what we have right
994 * now. More effective solution would be traversing
995 * right-up for first non-NULL without calling
996 * css_next_descendant_pre afterwards.
997 */
998 prev_css = css_rightmost_descendant(next_css);
999 goto skip_node;
1000 case VISIT:
1001 if (css_tryget(&mem->css))
1002 return mem;
1003 else {
1004 prev_css = next_css;
1005 goto skip_node;
1006 }
1007 break;
Michal Hocko16248d82013-04-29 15:07:19 -07001008 }
1009 }
1010
1011 return NULL;
1012}
1013
Johannes Weiner519ebea2013-07-03 15:04:51 -07001014static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1015{
1016 /*
1017 * When a group in the hierarchy below root is destroyed, the
1018 * hierarchy iterator can no longer be trusted since it might
1019 * have pointed to the destroyed group. Invalidate it.
1020 */
1021 atomic_inc(&root->dead_count);
1022}
1023
1024static struct mem_cgroup *
1025mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1026 struct mem_cgroup *root,
1027 int *sequence)
1028{
1029 struct mem_cgroup *position = NULL;
1030 /*
1031 * A cgroup destruction happens in two stages: offlining and
1032 * release. They are separated by a RCU grace period.
1033 *
1034 * If the iterator is valid, we may still race with an
1035 * offlining. The RCU lock ensures the object won't be
1036 * released, tryget will fail if we lost the race.
1037 */
1038 *sequence = atomic_read(&root->dead_count);
1039 if (iter->last_dead_count == *sequence) {
1040 smp_rmb();
1041 position = iter->last_visited;
1042 if (position && !css_tryget(&position->css))
1043 position = NULL;
1044 }
1045 return position;
1046}
1047
1048static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1049 struct mem_cgroup *last_visited,
1050 struct mem_cgroup *new_position,
1051 int sequence)
1052{
1053 if (last_visited)
1054 css_put(&last_visited->css);
1055 /*
1056 * We store the sequence count from the time @last_visited was
1057 * loaded successfully instead of rereading it here so that we
1058 * don't lose destruction events in between. We could have
1059 * raced with the destruction of @new_position after all.
1060 */
1061 iter->last_visited = new_position;
1062 smp_wmb();
1063 iter->last_dead_count = sequence;
1064}
1065
Johannes Weiner56600482012-01-12 17:17:59 -08001066/**
1067 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1068 * @root: hierarchy root
1069 * @prev: previously returned memcg, NULL on first invocation
1070 * @reclaim: cookie for shared reclaim walks, NULL for full walks
Michal Hockode577802013-09-12 15:13:26 -07001071 * @cond: filter for visited nodes, NULL for no filter
Johannes Weiner56600482012-01-12 17:17:59 -08001072 *
1073 * Returns references to children of the hierarchy below @root, or
1074 * @root itself, or %NULL after a full round-trip.
1075 *
1076 * Caller must pass the return value in @prev on subsequent
1077 * invocations for reference counting, or use mem_cgroup_iter_break()
1078 * to cancel a hierarchy walk before the round-trip is complete.
1079 *
1080 * Reclaimers can specify a zone and a priority level in @reclaim to
1081 * divide up the memcgs in the hierarchy among all concurrent
1082 * reclaimers operating on the same zone and priority.
1083 */
Michal Hockode577802013-09-12 15:13:26 -07001084struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001085 struct mem_cgroup *prev,
Michal Hockode577802013-09-12 15:13:26 -07001086 struct mem_cgroup_reclaim_cookie *reclaim,
1087 mem_cgroup_iter_filter cond)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001088{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001089 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001090 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001091
Michal Hockode577802013-09-12 15:13:26 -07001092 if (mem_cgroup_disabled()) {
1093 /* first call must return non-NULL, second return NULL */
1094 return (struct mem_cgroup *)(unsigned long)!prev;
1095 }
Johannes Weiner56600482012-01-12 17:17:59 -08001096
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001097 if (!root)
1098 root = root_mem_cgroup;
1099
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001100 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001101 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001102
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001103 if (!root->use_hierarchy && root != root_mem_cgroup) {
1104 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001105 goto out_css_put;
Michal Hockode577802013-09-12 15:13:26 -07001106 if (mem_cgroup_filter(root, root, cond) == VISIT)
1107 return root;
1108 return NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001109 }
1110
Michal Hocko542f85f2013-04-29 15:07:15 -07001111 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001112 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001113 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001114 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001115
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001116 if (reclaim) {
1117 int nid = zone_to_nid(reclaim->zone);
1118 int zid = zone_idx(reclaim->zone);
1119 struct mem_cgroup_per_zone *mz;
1120
1121 mz = mem_cgroup_zoneinfo(root, nid, zid);
1122 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001123 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001124 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001125 goto out_unlock;
1126 }
Michal Hocko5f578162013-04-29 15:07:17 -07001127
Johannes Weiner519ebea2013-07-03 15:04:51 -07001128 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001129 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001130
Michal Hockode577802013-09-12 15:13:26 -07001131 memcg = __mem_cgroup_iter_next(root, last_visited, cond);
Michal Hocko542f85f2013-04-29 15:07:15 -07001132
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001133 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001134 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001135
Michal Hocko19f39402013-04-29 15:07:18 -07001136 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001137 iter->generation++;
1138 else if (!prev && memcg)
1139 reclaim->generation = iter->generation;
1140 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001141
Michal Hockode577802013-09-12 15:13:26 -07001142 /*
1143 * We have finished the whole tree walk or no group has been
1144 * visited because filter told us to skip the root node.
1145 */
1146 if (!memcg && (prev || (cond && !last_visited)))
Michal Hocko542f85f2013-04-29 15:07:15 -07001147 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001148 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001149out_unlock:
1150 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001151out_css_put:
1152 if (prev && prev != root)
1153 css_put(&prev->css);
1154
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001155 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001156}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001157
Johannes Weiner56600482012-01-12 17:17:59 -08001158/**
1159 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1160 * @root: hierarchy root
1161 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1162 */
1163void mem_cgroup_iter_break(struct mem_cgroup *root,
1164 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001165{
1166 if (!root)
1167 root = root_mem_cgroup;
1168 if (prev && prev != root)
1169 css_put(&prev->css);
1170}
1171
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001172/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001173 * Iteration constructs for visiting all cgroups (under a tree). If
1174 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1175 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001176 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001177#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001178 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001179 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001180 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001181
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001182#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001183 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001184 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001185 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001186
David Rientjes68ae5642012-12-12 13:51:57 -08001187void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001188{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001189 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001190
Ying Han456f9982011-05-26 16:25:38 -07001191 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001192 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1193 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001194 goto out;
1195
1196 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001197 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001198 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1199 break;
1200 case PGMAJFAULT:
1201 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001202 break;
1203 default:
1204 BUG();
1205 }
1206out:
1207 rcu_read_unlock();
1208}
David Rientjes68ae5642012-12-12 13:51:57 -08001209EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001210
Johannes Weiner925b7672012-01-12 17:18:15 -08001211/**
1212 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1213 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001214 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001215 *
1216 * Returns the lru list vector holding pages for the given @zone and
1217 * @mem. This can be the global zone lruvec, if the memory controller
1218 * is disabled.
1219 */
1220struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1221 struct mem_cgroup *memcg)
1222{
1223 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001224 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001225
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001226 if (mem_cgroup_disabled()) {
1227 lruvec = &zone->lruvec;
1228 goto out;
1229 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001230
1231 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001232 lruvec = &mz->lruvec;
1233out:
1234 /*
1235 * Since a node can be onlined after the mem_cgroup was created,
1236 * we have to be prepared to initialize lruvec->zone here;
1237 * and if offlined then reonlined, we need to reinitialize it.
1238 */
1239 if (unlikely(lruvec->zone != zone))
1240 lruvec->zone = zone;
1241 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001242}
1243
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001244/*
1245 * Following LRU functions are allowed to be used without PCG_LOCK.
1246 * Operations are called by routine of global LRU independently from memcg.
1247 * What we have to take care of here is validness of pc->mem_cgroup.
1248 *
1249 * Changes to pc->mem_cgroup happens when
1250 * 1. charge
1251 * 2. moving account
1252 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1253 * It is added to LRU before charge.
1254 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1255 * When moving account, the page is not on LRU. It's isolated.
1256 */
1257
Johannes Weiner925b7672012-01-12 17:18:15 -08001258/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001259 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001260 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001261 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001262 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001263struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001264{
1265 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001266 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001267 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001268 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001269
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001270 if (mem_cgroup_disabled()) {
1271 lruvec = &zone->lruvec;
1272 goto out;
1273 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001274
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001275 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001276 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001277
1278 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001279 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001280 * an uncharged page off lru does nothing to secure
1281 * its former mem_cgroup from sudden removal.
1282 *
1283 * Our caller holds lru_lock, and PageCgroupUsed is updated
1284 * under page_cgroup lock: between them, they make all uses
1285 * of pc->mem_cgroup safe.
1286 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001287 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001288 pc->mem_cgroup = memcg = root_mem_cgroup;
1289
Johannes Weiner925b7672012-01-12 17:18:15 -08001290 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001291 lruvec = &mz->lruvec;
1292out:
1293 /*
1294 * Since a node can be onlined after the mem_cgroup was created,
1295 * we have to be prepared to initialize lruvec->zone here;
1296 * and if offlined then reonlined, we need to reinitialize it.
1297 */
1298 if (unlikely(lruvec->zone != zone))
1299 lruvec->zone = zone;
1300 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001301}
1302
1303/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001304 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1305 * @lruvec: mem_cgroup per zone lru vector
1306 * @lru: index of lru list the page is sitting on
1307 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001308 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001309 * This function must be called when a page is added to or removed from an
1310 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001311 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001312void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1313 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001314{
1315 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001316 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001317
1318 if (mem_cgroup_disabled())
1319 return;
1320
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001321 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1322 lru_size = mz->lru_size + lru;
1323 *lru_size += nr_pages;
1324 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001325}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001326
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001327/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001328 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001329 * hierarchy subtree
1330 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001331bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1332 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001333{
Johannes Weiner91c637342012-05-29 15:06:24 -07001334 if (root_memcg == memcg)
1335 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001336 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001337 return false;
Li Zefanb47f77b2013-09-23 16:55:43 +08001338 return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup);
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001339}
1340
1341static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1342 struct mem_cgroup *memcg)
1343{
1344 bool ret;
1345
Johannes Weiner91c637342012-05-29 15:06:24 -07001346 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001347 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001348 rcu_read_unlock();
1349 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001350}
1351
David Rientjesffbdccf2013-07-03 15:01:23 -07001352bool task_in_mem_cgroup(struct task_struct *task,
1353 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001354{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001355 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001356 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001357 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001358
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001359 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001360 if (p) {
1361 curr = try_get_mem_cgroup_from_mm(p->mm);
1362 task_unlock(p);
1363 } else {
1364 /*
1365 * All threads may have already detached their mm's, but the oom
1366 * killer still needs to detect if they have already been oom
1367 * killed to prevent needlessly killing additional tasks.
1368 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001369 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001370 curr = mem_cgroup_from_task(task);
1371 if (curr)
1372 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001373 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001374 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001375 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001376 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001377 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001378 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001379 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001380 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1381 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001382 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001383 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001384 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001385 return ret;
1386}
1387
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001388int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001389{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001390 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001391 unsigned long inactive;
1392 unsigned long active;
1393 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001394
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001395 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1396 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001397
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001398 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1399 if (gb)
1400 inactive_ratio = int_sqrt(10 * gb);
1401 else
1402 inactive_ratio = 1;
1403
Johannes Weiner9b272972011-11-02 13:38:23 -07001404 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001405}
1406
Balbir Singh6d61ef42009-01-07 18:08:06 -08001407#define mem_cgroup_from_res_counter(counter, member) \
1408 container_of(counter, struct mem_cgroup, member)
1409
Johannes Weiner19942822011-02-01 15:52:43 -08001410/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001411 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001412 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001413 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001414 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001415 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001416 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001418{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001419 unsigned long long margin;
1420
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001421 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001422 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001423 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001424 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001425}
1426
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001427int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001428{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001429 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001430 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001431 return vm_swappiness;
1432
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001433 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001434}
1435
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001436/*
1437 * memcg->moving_account is used for checking possibility that some thread is
1438 * calling move_account(). When a thread on CPU-A starts moving pages under
1439 * a memcg, other threads should check memcg->moving_account under
1440 * rcu_read_lock(), like this:
1441 *
1442 * CPU-A CPU-B
1443 * rcu_read_lock()
1444 * memcg->moving_account+1 if (memcg->mocing_account)
1445 * take heavy locks.
1446 * synchronize_rcu() update something.
1447 * rcu_read_unlock()
1448 * start move here.
1449 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001450
1451/* for quick checking without looking up memcg */
1452atomic_t memcg_moving __read_mostly;
1453
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001454static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001455{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001456 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001457 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001458 synchronize_rcu();
1459}
1460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001461static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001462{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001463 /*
1464 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1465 * We check NULL in callee rather than caller.
1466 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001467 if (memcg) {
1468 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001469 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001470 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001471}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001472
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001473/*
1474 * 2 routines for checking "mem" is under move_account() or not.
1475 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001476 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1477 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001478 * pc->mem_cgroup may be overwritten.
1479 *
1480 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1481 * under hierarchy of moving cgroups. This is for
1482 * waiting at hith-memory prressure caused by "move".
1483 */
1484
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001485static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001486{
1487 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001488 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001489}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001490
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001491static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001492{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001493 struct mem_cgroup *from;
1494 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001495 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001496 /*
1497 * Unlike task_move routines, we access mc.to, mc.from not under
1498 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1499 */
1500 spin_lock(&mc.lock);
1501 from = mc.from;
1502 to = mc.to;
1503 if (!from)
1504 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001505
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001506 ret = mem_cgroup_same_or_subtree(memcg, from)
1507 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001508unlock:
1509 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001510 return ret;
1511}
1512
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001513static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001514{
1515 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001517 DEFINE_WAIT(wait);
1518 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1519 /* moving charge context might have finished. */
1520 if (mc.moving_task)
1521 schedule();
1522 finish_wait(&mc.waitq, &wait);
1523 return true;
1524 }
1525 }
1526 return false;
1527}
1528
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001529/*
1530 * Take this lock when
1531 * - a code tries to modify page's memcg while it's USED.
1532 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001533 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001534 */
1535static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1536 unsigned long *flags)
1537{
1538 spin_lock_irqsave(&memcg->move_lock, *flags);
1539}
1540
1541static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1542 unsigned long *flags)
1543{
1544 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1545}
1546
Sha Zhengju58cf1882013-02-22 16:32:05 -08001547#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001548/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001549 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001550 * @memcg: The memory cgroup that went over limit
1551 * @p: Task that is going to be killed
1552 *
1553 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1554 * enabled
1555 */
1556void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1557{
1558 struct cgroup *task_cgrp;
1559 struct cgroup *mem_cgrp;
1560 /*
1561 * Need a buffer in BSS, can't rely on allocations. The code relies
1562 * on the assumption that OOM is serialized for memory controller.
1563 * If this assumption is broken, revisit this code.
1564 */
1565 static char memcg_name[PATH_MAX];
1566 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001567 struct mem_cgroup *iter;
1568 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001569
Sha Zhengju58cf1882013-02-22 16:32:05 -08001570 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001571 return;
1572
Balbir Singhe2224322009-04-02 16:57:39 -07001573 rcu_read_lock();
1574
1575 mem_cgrp = memcg->css.cgroup;
1576 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1577
1578 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1579 if (ret < 0) {
1580 /*
1581 * Unfortunately, we are unable to convert to a useful name
1582 * But we'll still print out the usage information
1583 */
1584 rcu_read_unlock();
1585 goto done;
1586 }
1587 rcu_read_unlock();
1588
Andrew Mortond0451972013-02-22 16:32:06 -08001589 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001590
1591 rcu_read_lock();
1592 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1593 if (ret < 0) {
1594 rcu_read_unlock();
1595 goto done;
1596 }
1597 rcu_read_unlock();
1598
1599 /*
1600 * Continues from above, so we don't need an KERN_ level
1601 */
Andrew Mortond0451972013-02-22 16:32:06 -08001602 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001603done:
1604
Andrew Mortond0451972013-02-22 16:32:06 -08001605 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001606 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1607 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1608 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001609 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001610 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1611 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1612 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001613 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001614 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1615 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1616 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001617
1618 for_each_mem_cgroup_tree(iter, memcg) {
1619 pr_info("Memory cgroup stats");
1620
1621 rcu_read_lock();
1622 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1623 if (!ret)
1624 pr_cont(" for %s", memcg_name);
1625 rcu_read_unlock();
1626 pr_cont(":");
1627
1628 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1629 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1630 continue;
1631 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1632 K(mem_cgroup_read_stat(iter, i)));
1633 }
1634
1635 for (i = 0; i < NR_LRU_LISTS; i++)
1636 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1637 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1638
1639 pr_cont("\n");
1640 }
Balbir Singhe2224322009-04-02 16:57:39 -07001641}
1642
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001643/*
1644 * This function returns the number of memcg under hierarchy tree. Returns
1645 * 1(self count) if no children.
1646 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001647static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001648{
1649 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001650 struct mem_cgroup *iter;
1651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001652 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001653 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001654 return num;
1655}
1656
Balbir Singh6d61ef42009-01-07 18:08:06 -08001657/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001658 * Return the memory (and swap, if configured) limit for a memcg.
1659 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001660static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001661{
1662 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001663
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001664 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001665
David Rientjesa63d83f2010-08-09 17:19:46 -07001666 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001667 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001668 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001669 if (mem_cgroup_swappiness(memcg)) {
1670 u64 memsw;
1671
1672 limit += total_swap_pages << PAGE_SHIFT;
1673 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1674
1675 /*
1676 * If memsw is finite and limits the amount of swap space
1677 * available to this memcg, return that limit.
1678 */
1679 limit = min(limit, memsw);
1680 }
1681
1682 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001683}
1684
David Rientjes19965462012-12-11 16:00:26 -08001685static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1686 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001687{
1688 struct mem_cgroup *iter;
1689 unsigned long chosen_points = 0;
1690 unsigned long totalpages;
1691 unsigned int points = 0;
1692 struct task_struct *chosen = NULL;
1693
David Rientjes876aafb2012-07-31 16:43:48 -07001694 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001695 * If current has a pending SIGKILL or is exiting, then automatically
1696 * select it. The goal is to allow it to allocate so that it may
1697 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001698 */
David Rientjes465adcf2013-04-29 15:08:45 -07001699 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001700 set_thread_flag(TIF_MEMDIE);
1701 return;
1702 }
1703
1704 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001705 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1706 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001707 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001708 struct task_struct *task;
1709
Tejun Heo72ec7022013-08-08 20:11:26 -04001710 css_task_iter_start(&iter->css, &it);
1711 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001712 switch (oom_scan_process_thread(task, totalpages, NULL,
1713 false)) {
1714 case OOM_SCAN_SELECT:
1715 if (chosen)
1716 put_task_struct(chosen);
1717 chosen = task;
1718 chosen_points = ULONG_MAX;
1719 get_task_struct(chosen);
1720 /* fall through */
1721 case OOM_SCAN_CONTINUE:
1722 continue;
1723 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001724 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001725 mem_cgroup_iter_break(memcg, iter);
1726 if (chosen)
1727 put_task_struct(chosen);
1728 return;
1729 case OOM_SCAN_OK:
1730 break;
1731 };
1732 points = oom_badness(task, memcg, NULL, totalpages);
1733 if (points > chosen_points) {
1734 if (chosen)
1735 put_task_struct(chosen);
1736 chosen = task;
1737 chosen_points = points;
1738 get_task_struct(chosen);
1739 }
1740 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001741 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001742 }
1743
1744 if (!chosen)
1745 return;
1746 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001747 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1748 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001749}
1750
Johannes Weiner56600482012-01-12 17:17:59 -08001751static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1752 gfp_t gfp_mask,
1753 unsigned long flags)
1754{
1755 unsigned long total = 0;
1756 bool noswap = false;
1757 int loop;
1758
1759 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1760 noswap = true;
1761 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1762 noswap = true;
1763
1764 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1765 if (loop)
1766 drain_all_stock_async(memcg);
1767 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1768 /*
1769 * Allow limit shrinkers, which are triggered directly
1770 * by userspace, to catch signals and stop reclaim
1771 * after minimal progress, regardless of the margin.
1772 */
1773 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1774 break;
1775 if (mem_cgroup_margin(memcg))
1776 break;
1777 /*
1778 * If nothing was reclaimed after two attempts, there
1779 * may be no reclaimable pages in this hierarchy.
1780 */
1781 if (loop && !total)
1782 break;
1783 }
1784 return total;
1785}
1786
Michal Hockoe8831102013-09-12 15:13:23 -07001787#if MAX_NUMNODES > 1
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001788/**
1789 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001790 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001791 * @nid: the node ID to be checked.
1792 * @noswap : specify true here if the user wants flle only information.
1793 *
1794 * This function returns whether the specified memcg contains any
1795 * reclaimable pages on a node. Returns true if there are any reclaimable
1796 * pages in the node.
1797 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001798static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001799 int nid, bool noswap)
1800{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001801 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001802 return true;
1803 if (noswap || !total_swap_pages)
1804 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001805 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001806 return true;
1807 return false;
1808
1809}
Ying Han889976d2011-05-26 16:25:33 -07001810
1811/*
1812 * Always updating the nodemask is not very good - even if we have an empty
1813 * list or the wrong list here, we can start from some node and traverse all
1814 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1815 *
1816 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001817static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001818{
1819 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001820 /*
1821 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1822 * pagein/pageout changes since the last update.
1823 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001824 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001825 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001826 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001827 return;
1828
Ying Han889976d2011-05-26 16:25:33 -07001829 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001830 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001831
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001832 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001833
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1835 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001836 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001837
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001838 atomic_set(&memcg->numainfo_events, 0);
1839 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001840}
1841
1842/*
1843 * Selecting a node where we start reclaim from. Because what we need is just
1844 * reducing usage counter, start from anywhere is O,K. Considering
1845 * memory reclaim from current node, there are pros. and cons.
1846 *
1847 * Freeing memory from current node means freeing memory from a node which
1848 * we'll use or we've used. So, it may make LRU bad. And if several threads
1849 * hit limits, it will see a contention on a node. But freeing from remote
1850 * node means more costs for memory reclaim because of memory latency.
1851 *
1852 * Now, we use round-robin. Better algorithm is welcomed.
1853 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001855{
1856 int node;
1857
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001858 mem_cgroup_may_update_nodemask(memcg);
1859 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001860
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001861 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001862 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001863 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001864 /*
1865 * We call this when we hit limit, not when pages are added to LRU.
1866 * No LRU may hold pages because all pages are UNEVICTABLE or
1867 * memcg is too small and all pages are not on LRU. In that case,
1868 * we use curret node.
1869 */
1870 if (unlikely(node == MAX_NUMNODES))
1871 node = numa_node_id();
1872
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001873 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001874 return node;
1875}
1876
1877#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001878int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001879{
1880 return 0;
1881}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001882
Ying Han889976d2011-05-26 16:25:33 -07001883#endif
1884
Michal Hocko3b387222013-09-12 15:13:21 -07001885/*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001886 * A group is eligible for the soft limit reclaim under the given root
1887 * hierarchy if
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001888 * a) it is over its soft limit
1889 * b) any parent up the hierarchy is over its soft limit
Michal Hocko7d910c02013-09-12 15:13:28 -07001890 *
1891 * If the given group doesn't have any children over the limit then it
1892 * doesn't make any sense to iterate its subtree.
Michal Hocko3b387222013-09-12 15:13:21 -07001893 */
Michal Hockode577802013-09-12 15:13:26 -07001894enum mem_cgroup_filter_t
1895mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -07001896 struct mem_cgroup *root)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001897{
Michal Hockoe839b6a2013-09-12 15:13:30 -07001898 struct mem_cgroup *parent;
1899
1900 if (!memcg)
1901 memcg = root_mem_cgroup;
1902 parent = memcg;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001903
Michal Hocko3b387222013-09-12 15:13:21 -07001904 if (res_counter_soft_limit_excess(&memcg->res))
Michal Hockode577802013-09-12 15:13:26 -07001905 return VISIT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001906
Michal Hocko3b387222013-09-12 15:13:21 -07001907 /*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001908 * If any parent up to the root in the hierarchy is over its soft limit
1909 * then we have to obey and reclaim from this group as well.
Michal Hocko3b387222013-09-12 15:13:21 -07001910 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001911 while ((parent = parent_mem_cgroup(parent))) {
Michal Hocko3b387222013-09-12 15:13:21 -07001912 if (res_counter_soft_limit_excess(&parent->res))
Michal Hockode577802013-09-12 15:13:26 -07001913 return VISIT;
Michal Hockoa5b7c872013-09-12 15:13:25 -07001914 if (parent == root)
1915 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001916 }
Michal Hocko3b387222013-09-12 15:13:21 -07001917
Michal Hocko7d910c02013-09-12 15:13:28 -07001918 if (!atomic_read(&memcg->children_in_excess))
1919 return SKIP_TREE;
Michal Hockode577802013-09-12 15:13:26 -07001920 return SKIP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001921}
1922
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001923static DEFINE_SPINLOCK(memcg_oom_lock);
1924
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001925/*
1926 * Check OOM-Killer is already running under our hierarchy.
1927 * If someone is running, return false.
1928 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001929static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001930{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001931 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001932
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001933 spin_lock(&memcg_oom_lock);
1934
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001935 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001936 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001937 /*
1938 * this subtree of our hierarchy is already locked
1939 * so we cannot give a lock.
1940 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001941 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001942 mem_cgroup_iter_break(memcg, iter);
1943 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001944 } else
1945 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001946 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001947
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001948 if (failed) {
1949 /*
1950 * OK, we failed to lock the whole subtree so we have
1951 * to clean up what we set up to the failing subtree
1952 */
1953 for_each_mem_cgroup_tree(iter, memcg) {
1954 if (iter == failed) {
1955 mem_cgroup_iter_break(memcg, iter);
1956 break;
1957 }
1958 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001959 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07001960 }
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001961
1962 spin_unlock(&memcg_oom_lock);
1963
1964 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001965}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001966
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001967static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001968{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001969 struct mem_cgroup *iter;
1970
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001971 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001972 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001973 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001974 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001975}
1976
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001978{
1979 struct mem_cgroup *iter;
1980
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001982 atomic_inc(&iter->under_oom);
1983}
1984
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001985static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001986{
1987 struct mem_cgroup *iter;
1988
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001989 /*
1990 * When a new child is created while the hierarchy is under oom,
1991 * mem_cgroup_oom_lock() may not be called. We have to use
1992 * atomic_add_unless() here.
1993 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001994 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001995 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001996}
1997
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001998static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1999
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002000struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002001 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002002 wait_queue_t wait;
2003};
2004
2005static int memcg_oom_wake_function(wait_queue_t *wait,
2006 unsigned mode, int sync, void *arg)
2007{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002008 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2009 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002010 struct oom_wait_info *oom_wait_info;
2011
2012 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002013 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002014
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002015 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002016 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002017 * Then we can use css_is_ancestor without taking care of RCU.
2018 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002019 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2020 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002021 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002022 return autoremove_wake_function(wait, mode, sync, arg);
2023}
2024
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002025static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002026{
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002027 atomic_inc(&memcg->oom_wakeups);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002028 /* for filtering, pass "memcg" as argument. */
2029 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002030}
2031
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002032static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002033{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002034 if (memcg && atomic_read(&memcg->under_oom))
2035 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002036}
2037
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002038/*
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002039 * try to call OOM killer
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002040 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002041static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002042{
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002043 bool locked;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002044 int wakeups;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002045
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002046 if (!current->memcg_oom.may_oom)
2047 return;
2048
2049 current->memcg_oom.in_memcg_oom = 1;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002050
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002051 /*
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002052 * As with any blocking lock, a contender needs to start
2053 * listening for wakeups before attempting the trylock,
2054 * otherwise it can miss the wakeup from the unlock and sleep
2055 * indefinitely. This is just open-coded because our locking
2056 * is so particular to memcg hierarchies.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002057 */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002058 wakeups = atomic_read(&memcg->oom_wakeups);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002059 mem_cgroup_mark_under_oom(memcg);
2060
2061 locked = mem_cgroup_oom_trylock(memcg);
2062
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002063 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002064 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002065
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002066 if (locked && !memcg->oom_kill_disable) {
2067 mem_cgroup_unmark_under_oom(memcg);
David Rientjese845e192012-03-21 16:34:10 -07002068 mem_cgroup_out_of_memory(memcg, mask, order);
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002069 mem_cgroup_oom_unlock(memcg);
2070 /*
2071 * There is no guarantee that an OOM-lock contender
2072 * sees the wakeups triggered by the OOM kill
2073 * uncharges. Wake any sleepers explicitely.
2074 */
2075 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002076 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002077 /*
2078 * A system call can just return -ENOMEM, but if this
2079 * is a page fault and somebody else is handling the
2080 * OOM already, we need to sleep on the OOM waitqueue
2081 * for this memcg until the situation is resolved.
2082 * Which can take some time because it might be
2083 * handled by a userspace task.
2084 *
2085 * However, this is the charge context, which means
2086 * that we may sit on a large call stack and hold
2087 * various filesystem locks, the mmap_sem etc. and we
2088 * don't want the OOM handler to deadlock on them
2089 * while we sit here and wait. Store the current OOM
2090 * context in the task_struct, then return -ENOMEM.
2091 * At the end of the page fault handler, with the
2092 * stack unwound, pagefault_out_of_memory() will check
2093 * back with us by calling
2094 * mem_cgroup_oom_synchronize(), possibly putting the
2095 * task to sleep.
2096 */
2097 current->memcg_oom.oom_locked = locked;
2098 current->memcg_oom.wakeups = wakeups;
2099 css_get(&memcg->css);
2100 current->memcg_oom.wait_on_memcg = memcg;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002101 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002102}
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002103
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002104/**
2105 * mem_cgroup_oom_synchronize - complete memcg OOM handling
2106 *
2107 * This has to be called at the end of a page fault if the the memcg
2108 * OOM handler was enabled and the fault is returning %VM_FAULT_OOM.
2109 *
2110 * Memcg supports userspace OOM handling, so failed allocations must
2111 * sleep on a waitqueue until the userspace task resolves the
2112 * situation. Sleeping directly in the charge context with all kinds
2113 * of locks held is not a good idea, instead we remember an OOM state
2114 * in the task and mem_cgroup_oom_synchronize() has to be called at
2115 * the end of the page fault to put the task to sleep and clean up the
2116 * OOM state.
2117 *
2118 * Returns %true if an ongoing memcg OOM situation was detected and
2119 * finalized, %false otherwise.
2120 */
2121bool mem_cgroup_oom_synchronize(void)
2122{
2123 struct oom_wait_info owait;
2124 struct mem_cgroup *memcg;
2125
2126 /* OOM is global, do not handle */
2127 if (!current->memcg_oom.in_memcg_oom)
2128 return false;
2129
2130 /*
2131 * We invoked the OOM killer but there is a chance that a kill
2132 * did not free up any charges. Everybody else might already
2133 * be sleeping, so restart the fault and keep the rampage
2134 * going until some charges are released.
2135 */
2136 memcg = current->memcg_oom.wait_on_memcg;
2137 if (!memcg)
2138 goto out;
2139
2140 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2141 goto out_memcg;
2142
2143 owait.memcg = memcg;
2144 owait.wait.flags = 0;
2145 owait.wait.func = memcg_oom_wake_function;
2146 owait.wait.private = current;
2147 INIT_LIST_HEAD(&owait.wait.task_list);
2148
2149 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
2150 /* Only sleep if we didn't miss any wakeups since OOM */
2151 if (atomic_read(&memcg->oom_wakeups) == current->memcg_oom.wakeups)
2152 schedule();
2153 finish_wait(&memcg_oom_waitq, &owait.wait);
2154out_memcg:
2155 mem_cgroup_unmark_under_oom(memcg);
2156 if (current->memcg_oom.oom_locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002157 mem_cgroup_oom_unlock(memcg);
2158 /*
2159 * There is no guarantee that an OOM-lock contender
2160 * sees the wakeups triggered by the OOM kill
2161 * uncharges. Wake any sleepers explicitely.
2162 */
2163 memcg_oom_recover(memcg);
2164 }
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002165 css_put(&memcg->css);
2166 current->memcg_oom.wait_on_memcg = NULL;
2167out:
2168 current->memcg_oom.in_memcg_oom = 0;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002169 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002170}
2171
Balbir Singhd69b0422009-06-17 16:26:34 -07002172/*
2173 * Currently used to update mapped file statistics, but the routine can be
2174 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002175 *
2176 * Notes: Race condition
2177 *
2178 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2179 * it tends to be costly. But considering some conditions, we doesn't need
2180 * to do so _always_.
2181 *
2182 * Considering "charge", lock_page_cgroup() is not required because all
2183 * file-stat operations happen after a page is attached to radix-tree. There
2184 * are no race with "charge".
2185 *
2186 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2187 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2188 * if there are race with "uncharge". Statistics itself is properly handled
2189 * by flags.
2190 *
2191 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002192 * small, we check mm->moving_account and detect there are possibility of race
2193 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002194 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002195
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002196void __mem_cgroup_begin_update_page_stat(struct page *page,
2197 bool *locked, unsigned long *flags)
2198{
2199 struct mem_cgroup *memcg;
2200 struct page_cgroup *pc;
2201
2202 pc = lookup_page_cgroup(page);
2203again:
2204 memcg = pc->mem_cgroup;
2205 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2206 return;
2207 /*
2208 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002209 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002210 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002211 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002212 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002213 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002214 return;
2215
2216 move_lock_mem_cgroup(memcg, flags);
2217 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2218 move_unlock_mem_cgroup(memcg, flags);
2219 goto again;
2220 }
2221 *locked = true;
2222}
2223
2224void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2225{
2226 struct page_cgroup *pc = lookup_page_cgroup(page);
2227
2228 /*
2229 * It's guaranteed that pc->mem_cgroup never changes while
2230 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002231 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002232 */
2233 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2234}
2235
Greg Thelen2a7106f2011-01-13 15:47:37 -08002236void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -07002237 enum mem_cgroup_stat_index idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002238{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002239 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002240 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002241 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002242
Johannes Weinercfa44942012-01-12 17:18:38 -08002243 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002244 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002245
Sha Zhengju658b72c2013-09-12 15:13:52 -07002246 VM_BUG_ON(!rcu_read_lock_held());
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002247 memcg = pc->mem_cgroup;
2248 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002249 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002250
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002251 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002252}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002253
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002254/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002255 * size of first charge trial. "32" comes from vmscan.c's magic value.
2256 * TODO: maybe necessary to use big numbers in big irons.
2257 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002258#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002259struct memcg_stock_pcp {
2260 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002261 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002262 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002263 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002264#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002265};
2266static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002267static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002268
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002269/**
2270 * consume_stock: Try to consume stocked charge on this cpu.
2271 * @memcg: memcg to consume from.
2272 * @nr_pages: how many pages to charge.
2273 *
2274 * The charges will only happen if @memcg matches the current cpu's memcg
2275 * stock, and at least @nr_pages are available in that stock. Failure to
2276 * service an allocation will refill the stock.
2277 *
2278 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002280static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002281{
2282 struct memcg_stock_pcp *stock;
2283 bool ret = true;
2284
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002285 if (nr_pages > CHARGE_BATCH)
2286 return false;
2287
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002288 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002289 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2290 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002291 else /* need to call res_counter_charge */
2292 ret = false;
2293 put_cpu_var(memcg_stock);
2294 return ret;
2295}
2296
2297/*
2298 * Returns stocks cached in percpu to res_counter and reset cached information.
2299 */
2300static void drain_stock(struct memcg_stock_pcp *stock)
2301{
2302 struct mem_cgroup *old = stock->cached;
2303
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002304 if (stock->nr_pages) {
2305 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2306
2307 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002308 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002309 res_counter_uncharge(&old->memsw, bytes);
2310 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002311 }
2312 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313}
2314
2315/*
2316 * This must be called under preempt disabled or must be called by
2317 * a thread which is pinned to local cpu.
2318 */
2319static void drain_local_stock(struct work_struct *dummy)
2320{
2321 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2322 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002323 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002324}
2325
Michal Hockoe4777492013-02-22 16:35:40 -08002326static void __init memcg_stock_init(void)
2327{
2328 int cpu;
2329
2330 for_each_possible_cpu(cpu) {
2331 struct memcg_stock_pcp *stock =
2332 &per_cpu(memcg_stock, cpu);
2333 INIT_WORK(&stock->work, drain_local_stock);
2334 }
2335}
2336
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002337/*
2338 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002339 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002340 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002341static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002342{
2343 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2344
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002345 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002346 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002347 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002348 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002349 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002350 put_cpu_var(memcg_stock);
2351}
2352
2353/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002354 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002355 * of the hierarchy under it. sync flag says whether we should block
2356 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002357 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002359{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002360 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002361
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002362 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002363 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002364 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002365 for_each_online_cpu(cpu) {
2366 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002367 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002368
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002369 memcg = stock->cached;
2370 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002371 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002372 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002373 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002374 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2375 if (cpu == curcpu)
2376 drain_local_stock(&stock->work);
2377 else
2378 schedule_work_on(cpu, &stock->work);
2379 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002380 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002381 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002382
2383 if (!sync)
2384 goto out;
2385
2386 for_each_online_cpu(cpu) {
2387 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002388 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002389 flush_work(&stock->work);
2390 }
2391out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002392 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002393}
2394
2395/*
2396 * Tries to drain stocked charges in other cpus. This function is asynchronous
2397 * and just put a work per cpu for draining localy on each cpu. Caller can
2398 * expects some charges will be back to res_counter later but cannot wait for
2399 * it.
2400 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002401static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002402{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002403 /*
2404 * If someone calls draining, avoid adding more kworker runs.
2405 */
2406 if (!mutex_trylock(&percpu_charge_mutex))
2407 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002408 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002409 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002410}
2411
2412/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002413static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002414{
2415 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002416 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002417 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002418 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002419}
2420
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002421/*
2422 * This function drains percpu counter value from DEAD cpu and
2423 * move it to local cpu. Note that this function can be preempted.
2424 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002425static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002426{
2427 int i;
2428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002429 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002430 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002431 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002432
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002433 per_cpu(memcg->stat->count[i], cpu) = 0;
2434 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002435 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002436 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002437 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002438
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002439 per_cpu(memcg->stat->events[i], cpu) = 0;
2440 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002441 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002442 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002443}
2444
Paul Gortmaker0db06282013-06-19 14:53:51 -04002445static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002446 unsigned long action,
2447 void *hcpu)
2448{
2449 int cpu = (unsigned long)hcpu;
2450 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002451 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002452
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002453 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002454 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002455
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002456 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002457 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002458
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002459 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002460 mem_cgroup_drain_pcp_counter(iter, cpu);
2461
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002462 stock = &per_cpu(memcg_stock, cpu);
2463 drain_stock(stock);
2464 return NOTIFY_OK;
2465}
2466
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002467
2468/* See __mem_cgroup_try_charge() for details */
2469enum {
2470 CHARGE_OK, /* success */
2471 CHARGE_RETRY, /* need to retry but retry is not bad */
2472 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2473 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002474};
2475
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002476static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002477 unsigned int nr_pages, unsigned int min_pages,
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002478 bool invoke_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002479{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002480 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002481 struct mem_cgroup *mem_over_limit;
2482 struct res_counter *fail_res;
2483 unsigned long flags = 0;
2484 int ret;
2485
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002486 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002487
2488 if (likely(!ret)) {
2489 if (!do_swap_account)
2490 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002491 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002492 if (likely(!ret))
2493 return CHARGE_OK;
2494
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002495 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002496 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2497 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2498 } else
2499 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002500 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002501 * Never reclaim on behalf of optional batching, retry with a
2502 * single page instead.
2503 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002504 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002505 return CHARGE_RETRY;
2506
2507 if (!(gfp_mask & __GFP_WAIT))
2508 return CHARGE_WOULDBLOCK;
2509
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002510 if (gfp_mask & __GFP_NORETRY)
2511 return CHARGE_NOMEM;
2512
Johannes Weiner56600482012-01-12 17:17:59 -08002513 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002514 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002515 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002516 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002517 * Even though the limit is exceeded at this point, reclaim
2518 * may have been able to free some pages. Retry the charge
2519 * before killing the task.
2520 *
2521 * Only for regular pages, though: huge pages are rather
2522 * unlikely to succeed so close to the limit, and we fall back
2523 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002524 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002525 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002526 return CHARGE_RETRY;
2527
2528 /*
2529 * At task move, charge accounts can be doubly counted. So, it's
2530 * better to wait until the end of task_move if something is going on.
2531 */
2532 if (mem_cgroup_wait_acct_move(mem_over_limit))
2533 return CHARGE_RETRY;
2534
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002535 if (invoke_oom)
2536 mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002537
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002538 return CHARGE_NOMEM;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002539}
2540
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002541/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002542 * __mem_cgroup_try_charge() does
2543 * 1. detect memcg to be charged against from passed *mm and *ptr,
2544 * 2. update res_counter
2545 * 3. call memory reclaim if necessary.
2546 *
2547 * In some special case, if the task is fatal, fatal_signal_pending() or
2548 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2549 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2550 * as possible without any hazards. 2: all pages should have a valid
2551 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2552 * pointer, that is treated as a charge to root_mem_cgroup.
2553 *
2554 * So __mem_cgroup_try_charge() will return
2555 * 0 ... on success, filling *ptr with a valid memcg pointer.
2556 * -ENOMEM ... charge failure because of resource limits.
2557 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2558 *
2559 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2560 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002561 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002562static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002563 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002564 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002565 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002566 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002567{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002568 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002569 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002570 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002571 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002572
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002573 /*
2574 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2575 * in system level. So, allow to go ahead dying process in addition to
2576 * MEMDIE process.
2577 */
2578 if (unlikely(test_thread_flag(TIF_MEMDIE)
2579 || fatal_signal_pending(current)))
2580 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002581
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002582 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002583 * We always charge the cgroup the mm_struct belongs to.
2584 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002585 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002586 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002587 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002588 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002589 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002590again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002591 if (*ptr) { /* css should be a valid one */
2592 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002593 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002594 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002595 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002596 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002597 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002598 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002599 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002600
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002601 rcu_read_lock();
2602 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002603 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002604 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002605 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002606 * race with swapoff. Then, we have small risk of mis-accouning.
2607 * But such kind of mis-account by race always happens because
2608 * we don't have cgroup_mutex(). It's overkill and we allo that
2609 * small race, here.
2610 * (*) swapoff at el will charge against mm-struct not against
2611 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002612 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002613 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002614 if (!memcg)
2615 memcg = root_mem_cgroup;
2616 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002617 rcu_read_unlock();
2618 goto done;
2619 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002620 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002621 /*
2622 * It seems dagerous to access memcg without css_get().
2623 * But considering how consume_stok works, it's not
2624 * necessary. If consume_stock success, some charges
2625 * from this memcg are cached on this cpu. So, we
2626 * don't need to call css_get()/css_tryget() before
2627 * calling consume_stock().
2628 */
2629 rcu_read_unlock();
2630 goto done;
2631 }
2632 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002633 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002634 rcu_read_unlock();
2635 goto again;
2636 }
2637 rcu_read_unlock();
2638 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002639
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002640 do {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002641 bool invoke_oom = oom && !nr_oom_retries;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002642
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002643 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002644 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002645 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002646 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002647 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002648
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002649 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2650 nr_pages, invoke_oom);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002651 switch (ret) {
2652 case CHARGE_OK:
2653 break;
2654 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002655 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002656 css_put(&memcg->css);
2657 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002658 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002659 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002660 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002661 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002662 case CHARGE_NOMEM: /* OOM routine works */
Johannes Weiner3812c8c2013-09-12 15:13:44 -07002663 if (!oom || invoke_oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002664 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002665 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002666 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002667 nr_oom_retries--;
2668 break;
Balbir Singh66e17072008-02-07 00:13:56 -08002669 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002670 } while (ret != CHARGE_OK);
2671
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002672 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002673 refill_stock(memcg, batch - nr_pages);
2674 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002675done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002676 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002677 return 0;
2678nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002679 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002680 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002681bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002682 *ptr = root_mem_cgroup;
2683 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002684}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002685
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002686/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002687 * Somemtimes we have to undo a charge we got by try_charge().
2688 * This function is for that and do uncharge, put css's refcnt.
2689 * gotten by try_charge().
2690 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002691static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002692 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002693{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002694 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002695 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002696
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002697 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002698 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002699 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002700 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002701}
2702
2703/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002704 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2705 * This is useful when moving usage to parent cgroup.
2706 */
2707static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2708 unsigned int nr_pages)
2709{
2710 unsigned long bytes = nr_pages * PAGE_SIZE;
2711
2712 if (mem_cgroup_is_root(memcg))
2713 return;
2714
2715 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2716 if (do_swap_account)
2717 res_counter_uncharge_until(&memcg->memsw,
2718 memcg->memsw.parent, bytes);
2719}
2720
2721/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002722 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002723 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2724 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2725 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002726 */
2727static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2728{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002729 /* ID 0 is unused ID */
2730 if (!id)
2731 return NULL;
Li Zefan34c00c32013-09-23 16:56:01 +08002732 return mem_cgroup_from_id(id);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002733}
2734
Wu Fengguange42d9d52009-12-16 12:19:59 +01002735struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002736{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002737 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002738 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002739 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002740 swp_entry_t ent;
2741
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002742 VM_BUG_ON(!PageLocked(page));
2743
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002744 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002745 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002746 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002747 memcg = pc->mem_cgroup;
2748 if (memcg && !css_tryget(&memcg->css))
2749 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002750 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002751 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002752 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002753 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002754 memcg = mem_cgroup_lookup(id);
2755 if (memcg && !css_tryget(&memcg->css))
2756 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002757 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002758 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002759 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002760 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002761}
2762
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002763static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002764 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002765 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002766 enum charge_type ctype,
2767 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002768{
Johannes Weinerce587e62012-04-24 20:22:33 +02002769 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002770 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002771 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002772 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002773 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002774
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002775 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002776 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002777 /*
2778 * we don't need page_cgroup_lock about tail pages, becase they are not
2779 * accessed by any other context at this point.
2780 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002781
2782 /*
2783 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2784 * may already be on some other mem_cgroup's LRU. Take care of it.
2785 */
2786 if (lrucare) {
2787 zone = page_zone(page);
2788 spin_lock_irq(&zone->lru_lock);
2789 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002790 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002791 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002792 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002793 was_on_lru = true;
2794 }
2795 }
2796
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002797 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002798 /*
2799 * We access a page_cgroup asynchronously without lock_page_cgroup().
2800 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2801 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2802 * before USED bit, we need memory barrier here.
2803 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002804 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002805 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002806 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002807
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002808 if (lrucare) {
2809 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002810 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002811 VM_BUG_ON(PageLRU(page));
2812 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002813 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002814 }
2815 spin_unlock_irq(&zone->lru_lock);
2816 }
2817
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002818 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002819 anon = true;
2820 else
2821 anon = false;
2822
David Rientjesb070e652013-05-07 16:18:09 -07002823 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002824 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002825
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002826 /*
Michal Hockoe8831102013-09-12 15:13:23 -07002827 * "charge_statistics" updated event counter.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002828 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002829 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002830}
2831
Glauber Costa7cf27982012-12-18 14:22:55 -08002832static DEFINE_MUTEX(set_limit_mutex);
2833
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002834#ifdef CONFIG_MEMCG_KMEM
2835static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2836{
2837 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2838 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2839}
2840
Glauber Costa1f458cb2012-12-18 14:22:50 -08002841/*
2842 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2843 * in the memcg_cache_params struct.
2844 */
2845static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2846{
2847 struct kmem_cache *cachep;
2848
2849 VM_BUG_ON(p->is_root_cache);
2850 cachep = p->root_cache;
2851 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2852}
2853
Glauber Costa749c5412012-12-18 14:23:01 -08002854#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002855static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2856 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002857{
Tejun Heo182446d2013-08-08 20:11:24 -04002858 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002859 struct memcg_cache_params *params;
2860
2861 if (!memcg_can_account_kmem(memcg))
2862 return -EIO;
2863
2864 print_slabinfo_header(m);
2865
2866 mutex_lock(&memcg->slab_caches_mutex);
2867 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2868 cache_show(memcg_params_to_cache(params), m);
2869 mutex_unlock(&memcg->slab_caches_mutex);
2870
2871 return 0;
2872}
2873#endif
2874
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002875static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2876{
2877 struct res_counter *fail_res;
2878 struct mem_cgroup *_memcg;
2879 int ret = 0;
2880 bool may_oom;
2881
2882 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2883 if (ret)
2884 return ret;
2885
2886 /*
2887 * Conditions under which we can wait for the oom_killer. Those are
2888 * the same conditions tested by the core page allocator
2889 */
2890 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2891
2892 _memcg = memcg;
2893 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2894 &_memcg, may_oom);
2895
2896 if (ret == -EINTR) {
2897 /*
2898 * __mem_cgroup_try_charge() chosed to bypass to root due to
2899 * OOM kill or fatal signal. Since our only options are to
2900 * either fail the allocation or charge it to this cgroup, do
2901 * it as a temporary condition. But we can't fail. From a
2902 * kmem/slab perspective, the cache has already been selected,
2903 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2904 * our minds.
2905 *
2906 * This condition will only trigger if the task entered
2907 * memcg_charge_kmem in a sane state, but was OOM-killed during
2908 * __mem_cgroup_try_charge() above. Tasks that were already
2909 * dying when the allocation triggers should have been already
2910 * directed to the root cgroup in memcontrol.h
2911 */
2912 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2913 if (do_swap_account)
2914 res_counter_charge_nofail(&memcg->memsw, size,
2915 &fail_res);
2916 ret = 0;
2917 } else if (ret)
2918 res_counter_uncharge(&memcg->kmem, size);
2919
2920 return ret;
2921}
2922
2923static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2924{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002925 res_counter_uncharge(&memcg->res, size);
2926 if (do_swap_account)
2927 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002928
2929 /* Not down to 0 */
2930 if (res_counter_uncharge(&memcg->kmem, size))
2931 return;
2932
Li Zefan10d5ebf2013-07-08 16:00:33 -07002933 /*
2934 * Releases a reference taken in kmem_cgroup_css_offline in case
2935 * this last uncharge is racing with the offlining code or it is
2936 * outliving the memcg existence.
2937 *
2938 * The memory barrier imposed by test&clear is paired with the
2939 * explicit one in memcg_kmem_mark_dead().
2940 */
Glauber Costa7de37682012-12-18 14:22:07 -08002941 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002942 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002943}
2944
Glauber Costa2633d7a2012-12-18 14:22:34 -08002945void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2946{
2947 if (!memcg)
2948 return;
2949
2950 mutex_lock(&memcg->slab_caches_mutex);
2951 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2952 mutex_unlock(&memcg->slab_caches_mutex);
2953}
2954
2955/*
2956 * helper for acessing a memcg's index. It will be used as an index in the
2957 * child cache array in kmem_cache, and also to derive its name. This function
2958 * will return -1 when this is not a kmem-limited memcg.
2959 */
2960int memcg_cache_id(struct mem_cgroup *memcg)
2961{
2962 return memcg ? memcg->kmemcg_id : -1;
2963}
2964
Glauber Costa55007d82012-12-18 14:22:38 -08002965/*
2966 * This ends up being protected by the set_limit mutex, during normal
2967 * operation, because that is its main call site.
2968 *
2969 * But when we create a new cache, we can call this as well if its parent
2970 * is kmem-limited. That will have to hold set_limit_mutex as well.
2971 */
2972int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2973{
2974 int num, ret;
2975
2976 num = ida_simple_get(&kmem_limited_groups,
2977 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2978 if (num < 0)
2979 return num;
2980 /*
2981 * After this point, kmem_accounted (that we test atomically in
2982 * the beginning of this conditional), is no longer 0. This
2983 * guarantees only one process will set the following boolean
2984 * to true. We don't need test_and_set because we're protected
2985 * by the set_limit_mutex anyway.
2986 */
2987 memcg_kmem_set_activated(memcg);
2988
2989 ret = memcg_update_all_caches(num+1);
2990 if (ret) {
2991 ida_simple_remove(&kmem_limited_groups, num);
2992 memcg_kmem_clear_activated(memcg);
2993 return ret;
2994 }
2995
2996 memcg->kmemcg_id = num;
2997 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2998 mutex_init(&memcg->slab_caches_mutex);
2999 return 0;
3000}
3001
3002static size_t memcg_caches_array_size(int num_groups)
3003{
3004 ssize_t size;
3005 if (num_groups <= 0)
3006 return 0;
3007
3008 size = 2 * num_groups;
3009 if (size < MEMCG_CACHES_MIN_SIZE)
3010 size = MEMCG_CACHES_MIN_SIZE;
3011 else if (size > MEMCG_CACHES_MAX_SIZE)
3012 size = MEMCG_CACHES_MAX_SIZE;
3013
3014 return size;
3015}
3016
3017/*
3018 * We should update the current array size iff all caches updates succeed. This
3019 * can only be done from the slab side. The slab mutex needs to be held when
3020 * calling this.
3021 */
3022void memcg_update_array_size(int num)
3023{
3024 if (num > memcg_limited_groups_array_size)
3025 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3026}
3027
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003028static void kmem_cache_destroy_work_func(struct work_struct *w);
3029
Glauber Costa55007d82012-12-18 14:22:38 -08003030int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3031{
3032 struct memcg_cache_params *cur_params = s->memcg_params;
3033
3034 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3035
3036 if (num_groups > memcg_limited_groups_array_size) {
3037 int i;
3038 ssize_t size = memcg_caches_array_size(num_groups);
3039
3040 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003041 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003042
3043 s->memcg_params = kzalloc(size, GFP_KERNEL);
3044 if (!s->memcg_params) {
3045 s->memcg_params = cur_params;
3046 return -ENOMEM;
3047 }
3048
3049 s->memcg_params->is_root_cache = true;
3050
3051 /*
3052 * There is the chance it will be bigger than
3053 * memcg_limited_groups_array_size, if we failed an allocation
3054 * in a cache, in which case all caches updated before it, will
3055 * have a bigger array.
3056 *
3057 * But if that is the case, the data after
3058 * memcg_limited_groups_array_size is certainly unused
3059 */
3060 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3061 if (!cur_params->memcg_caches[i])
3062 continue;
3063 s->memcg_params->memcg_caches[i] =
3064 cur_params->memcg_caches[i];
3065 }
3066
3067 /*
3068 * Ideally, we would wait until all caches succeed, and only
3069 * then free the old one. But this is not worth the extra
3070 * pointer per-cache we'd have to have for this.
3071 *
3072 * It is not a big deal if some caches are left with a size
3073 * bigger than the others. And all updates will reset this
3074 * anyway.
3075 */
3076 kfree(cur_params);
3077 }
3078 return 0;
3079}
3080
Glauber Costa943a4512012-12-18 14:23:03 -08003081int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3082 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003083{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003084 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003085
3086 if (!memcg_kmem_enabled())
3087 return 0;
3088
Andrey Vagin90c7a792013-09-11 14:22:18 -07003089 if (!memcg) {
3090 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003091 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003092 } else
3093 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003094
Glauber Costa2633d7a2012-12-18 14:22:34 -08003095 s->memcg_params = kzalloc(size, GFP_KERNEL);
3096 if (!s->memcg_params)
3097 return -ENOMEM;
3098
Glauber Costa943a4512012-12-18 14:23:03 -08003099 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003100 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003101 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003102 INIT_WORK(&s->memcg_params->destroy,
3103 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003104 } else
3105 s->memcg_params->is_root_cache = true;
3106
Glauber Costa2633d7a2012-12-18 14:22:34 -08003107 return 0;
3108}
3109
3110void memcg_release_cache(struct kmem_cache *s)
3111{
Glauber Costad7f25f82012-12-18 14:22:40 -08003112 struct kmem_cache *root;
3113 struct mem_cgroup *memcg;
3114 int id;
3115
3116 /*
3117 * This happens, for instance, when a root cache goes away before we
3118 * add any memcg.
3119 */
3120 if (!s->memcg_params)
3121 return;
3122
3123 if (s->memcg_params->is_root_cache)
3124 goto out;
3125
3126 memcg = s->memcg_params->memcg;
3127 id = memcg_cache_id(memcg);
3128
3129 root = s->memcg_params->root_cache;
3130 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003131
3132 mutex_lock(&memcg->slab_caches_mutex);
3133 list_del(&s->memcg_params->list);
3134 mutex_unlock(&memcg->slab_caches_mutex);
3135
Li Zefan20f05312013-07-08 16:00:31 -07003136 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003137out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003138 kfree(s->memcg_params);
3139}
3140
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003141/*
3142 * During the creation a new cache, we need to disable our accounting mechanism
3143 * altogether. This is true even if we are not creating, but rather just
3144 * enqueing new caches to be created.
3145 *
3146 * This is because that process will trigger allocations; some visible, like
3147 * explicit kmallocs to auxiliary data structures, name strings and internal
3148 * cache structures; some well concealed, like INIT_WORK() that can allocate
3149 * objects during debug.
3150 *
3151 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3152 * to it. This may not be a bounded recursion: since the first cache creation
3153 * failed to complete (waiting on the allocation), we'll just try to create the
3154 * cache again, failing at the same point.
3155 *
3156 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3157 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3158 * inside the following two functions.
3159 */
3160static inline void memcg_stop_kmem_account(void)
3161{
3162 VM_BUG_ON(!current->mm);
3163 current->memcg_kmem_skip_account++;
3164}
3165
3166static inline void memcg_resume_kmem_account(void)
3167{
3168 VM_BUG_ON(!current->mm);
3169 current->memcg_kmem_skip_account--;
3170}
3171
Glauber Costa1f458cb2012-12-18 14:22:50 -08003172static void kmem_cache_destroy_work_func(struct work_struct *w)
3173{
3174 struct kmem_cache *cachep;
3175 struct memcg_cache_params *p;
3176
3177 p = container_of(w, struct memcg_cache_params, destroy);
3178
3179 cachep = memcg_params_to_cache(p);
3180
Glauber Costa22933152012-12-18 14:22:59 -08003181 /*
3182 * If we get down to 0 after shrink, we could delete right away.
3183 * However, memcg_release_pages() already puts us back in the workqueue
3184 * in that case. If we proceed deleting, we'll get a dangling
3185 * reference, and removing the object from the workqueue in that case
3186 * is unnecessary complication. We are not a fast path.
3187 *
3188 * Note that this case is fundamentally different from racing with
3189 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3190 * kmem_cache_shrink, not only we would be reinserting a dead cache
3191 * into the queue, but doing so from inside the worker racing to
3192 * destroy it.
3193 *
3194 * So if we aren't down to zero, we'll just schedule a worker and try
3195 * again
3196 */
3197 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3198 kmem_cache_shrink(cachep);
3199 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3200 return;
3201 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003202 kmem_cache_destroy(cachep);
3203}
3204
3205void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3206{
3207 if (!cachep->memcg_params->dead)
3208 return;
3209
3210 /*
Glauber Costa22933152012-12-18 14:22:59 -08003211 * There are many ways in which we can get here.
3212 *
3213 * We can get to a memory-pressure situation while the delayed work is
3214 * still pending to run. The vmscan shrinkers can then release all
3215 * cache memory and get us to destruction. If this is the case, we'll
3216 * be executed twice, which is a bug (the second time will execute over
3217 * bogus data). In this case, cancelling the work should be fine.
3218 *
3219 * But we can also get here from the worker itself, if
3220 * kmem_cache_shrink is enough to shake all the remaining objects and
3221 * get the page count to 0. In this case, we'll deadlock if we try to
3222 * cancel the work (the worker runs with an internal lock held, which
3223 * is the same lock we would hold for cancel_work_sync().)
3224 *
3225 * Since we can't possibly know who got us here, just refrain from
3226 * running if there is already work pending
3227 */
3228 if (work_pending(&cachep->memcg_params->destroy))
3229 return;
3230 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003231 * We have to defer the actual destroying to a workqueue, because
3232 * we might currently be in a context that cannot sleep.
3233 */
3234 schedule_work(&cachep->memcg_params->destroy);
3235}
3236
Glauber Costad7f25f82012-12-18 14:22:40 -08003237/*
3238 * This lock protects updaters, not readers. We want readers to be as fast as
3239 * they can, and they will either see NULL or a valid cache value. Our model
3240 * allow them to see NULL, in which case the root memcg will be selected.
3241 *
3242 * We need this lock because multiple allocations to the same cache from a non
3243 * will span more than one worker. Only one of them can create the cache.
3244 */
3245static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003246
3247/*
3248 * Called with memcg_cache_mutex held
3249 */
3250static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3251 struct kmem_cache *s)
3252{
3253 struct kmem_cache *new;
3254 static char *tmp_name = NULL;
3255
3256 lockdep_assert_held(&memcg_cache_mutex);
3257
3258 /*
3259 * kmem_cache_create_memcg duplicates the given name and
3260 * cgroup_name for this name requires RCU context.
3261 * This static temporary buffer is used to prevent from
3262 * pointless shortliving allocation.
3263 */
3264 if (!tmp_name) {
3265 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3266 if (!tmp_name)
3267 return NULL;
3268 }
3269
3270 rcu_read_lock();
3271 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3272 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3273 rcu_read_unlock();
3274
3275 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3276 (s->flags & ~SLAB_PANIC), s->ctor, s);
3277
3278 if (new)
3279 new->allocflags |= __GFP_KMEMCG;
3280
3281 return new;
3282}
3283
Glauber Costad7f25f82012-12-18 14:22:40 -08003284static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3285 struct kmem_cache *cachep)
3286{
3287 struct kmem_cache *new_cachep;
3288 int idx;
3289
3290 BUG_ON(!memcg_can_account_kmem(memcg));
3291
3292 idx = memcg_cache_id(memcg);
3293
3294 mutex_lock(&memcg_cache_mutex);
3295 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003296 if (new_cachep) {
3297 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003298 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003299 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003300
3301 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003302 if (new_cachep == NULL) {
3303 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003304 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003305 goto out;
3306 }
3307
Glauber Costa1f458cb2012-12-18 14:22:50 -08003308 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003309
3310 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3311 /*
3312 * the readers won't lock, make sure everybody sees the updated value,
3313 * so they won't put stuff in the queue again for no reason
3314 */
3315 wmb();
3316out:
3317 mutex_unlock(&memcg_cache_mutex);
3318 return new_cachep;
3319}
3320
Glauber Costa7cf27982012-12-18 14:22:55 -08003321void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3322{
3323 struct kmem_cache *c;
3324 int i;
3325
3326 if (!s->memcg_params)
3327 return;
3328 if (!s->memcg_params->is_root_cache)
3329 return;
3330
3331 /*
3332 * If the cache is being destroyed, we trust that there is no one else
3333 * requesting objects from it. Even if there are, the sanity checks in
3334 * kmem_cache_destroy should caught this ill-case.
3335 *
3336 * Still, we don't want anyone else freeing memcg_caches under our
3337 * noses, which can happen if a new memcg comes to life. As usual,
3338 * we'll take the set_limit_mutex to protect ourselves against this.
3339 */
3340 mutex_lock(&set_limit_mutex);
3341 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3342 c = s->memcg_params->memcg_caches[i];
3343 if (!c)
3344 continue;
3345
3346 /*
3347 * We will now manually delete the caches, so to avoid races
3348 * we need to cancel all pending destruction workers and
3349 * proceed with destruction ourselves.
3350 *
3351 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3352 * and that could spawn the workers again: it is likely that
3353 * the cache still have active pages until this very moment.
3354 * This would lead us back to mem_cgroup_destroy_cache.
3355 *
3356 * But that will not execute at all if the "dead" flag is not
3357 * set, so flip it down to guarantee we are in control.
3358 */
3359 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003360 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003361 kmem_cache_destroy(c);
3362 }
3363 mutex_unlock(&set_limit_mutex);
3364}
3365
Glauber Costad7f25f82012-12-18 14:22:40 -08003366struct create_work {
3367 struct mem_cgroup *memcg;
3368 struct kmem_cache *cachep;
3369 struct work_struct work;
3370};
3371
Glauber Costa1f458cb2012-12-18 14:22:50 -08003372static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3373{
3374 struct kmem_cache *cachep;
3375 struct memcg_cache_params *params;
3376
3377 if (!memcg_kmem_is_active(memcg))
3378 return;
3379
3380 mutex_lock(&memcg->slab_caches_mutex);
3381 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3382 cachep = memcg_params_to_cache(params);
3383 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003384 schedule_work(&cachep->memcg_params->destroy);
3385 }
3386 mutex_unlock(&memcg->slab_caches_mutex);
3387}
3388
Glauber Costad7f25f82012-12-18 14:22:40 -08003389static void memcg_create_cache_work_func(struct work_struct *w)
3390{
3391 struct create_work *cw;
3392
3393 cw = container_of(w, struct create_work, work);
3394 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003395 kfree(cw);
3396}
3397
3398/*
3399 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003400 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003401static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3402 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003403{
3404 struct create_work *cw;
3405
3406 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003407 if (cw == NULL) {
3408 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003409 return;
3410 }
3411
3412 cw->memcg = memcg;
3413 cw->cachep = cachep;
3414
3415 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3416 schedule_work(&cw->work);
3417}
3418
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003419static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3420 struct kmem_cache *cachep)
3421{
3422 /*
3423 * We need to stop accounting when we kmalloc, because if the
3424 * corresponding kmalloc cache is not yet created, the first allocation
3425 * in __memcg_create_cache_enqueue will recurse.
3426 *
3427 * However, it is better to enclose the whole function. Depending on
3428 * the debugging options enabled, INIT_WORK(), for instance, can
3429 * trigger an allocation. This too, will make us recurse. Because at
3430 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3431 * the safest choice is to do it like this, wrapping the whole function.
3432 */
3433 memcg_stop_kmem_account();
3434 __memcg_create_cache_enqueue(memcg, cachep);
3435 memcg_resume_kmem_account();
3436}
Glauber Costad7f25f82012-12-18 14:22:40 -08003437/*
3438 * Return the kmem_cache we're supposed to use for a slab allocation.
3439 * We try to use the current memcg's version of the cache.
3440 *
3441 * If the cache does not exist yet, if we are the first user of it,
3442 * we either create it immediately, if possible, or create it asynchronously
3443 * in a workqueue.
3444 * In the latter case, we will let the current allocation go through with
3445 * the original cache.
3446 *
3447 * Can't be called in interrupt context or from kernel threads.
3448 * This function needs to be called with rcu_read_lock() held.
3449 */
3450struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3451 gfp_t gfp)
3452{
3453 struct mem_cgroup *memcg;
3454 int idx;
3455
3456 VM_BUG_ON(!cachep->memcg_params);
3457 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3458
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003459 if (!current->mm || current->memcg_kmem_skip_account)
3460 return cachep;
3461
Glauber Costad7f25f82012-12-18 14:22:40 -08003462 rcu_read_lock();
3463 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003464
3465 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003466 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003467
3468 idx = memcg_cache_id(memcg);
3469
3470 /*
3471 * barrier to mare sure we're always seeing the up to date value. The
3472 * code updating memcg_caches will issue a write barrier to match this.
3473 */
3474 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003475 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3476 cachep = cachep->memcg_params->memcg_caches[idx];
3477 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003478 }
3479
Li Zefanca0dde92013-04-29 15:08:57 -07003480 /* The corresponding put will be done in the workqueue. */
3481 if (!css_tryget(&memcg->css))
3482 goto out;
3483 rcu_read_unlock();
3484
3485 /*
3486 * If we are in a safe context (can wait, and not in interrupt
3487 * context), we could be be predictable and return right away.
3488 * This would guarantee that the allocation being performed
3489 * already belongs in the new cache.
3490 *
3491 * However, there are some clashes that can arrive from locking.
3492 * For instance, because we acquire the slab_mutex while doing
3493 * kmem_cache_dup, this means no further allocation could happen
3494 * with the slab_mutex held.
3495 *
3496 * Also, because cache creation issue get_online_cpus(), this
3497 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3498 * that ends up reversed during cpu hotplug. (cpuset allocates
3499 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3500 * better to defer everything.
3501 */
3502 memcg_create_cache_enqueue(memcg, cachep);
3503 return cachep;
3504out:
3505 rcu_read_unlock();
3506 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003507}
3508EXPORT_SYMBOL(__memcg_kmem_get_cache);
3509
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003510/*
3511 * We need to verify if the allocation against current->mm->owner's memcg is
3512 * possible for the given order. But the page is not allocated yet, so we'll
3513 * need a further commit step to do the final arrangements.
3514 *
3515 * It is possible for the task to switch cgroups in this mean time, so at
3516 * commit time, we can't rely on task conversion any longer. We'll then use
3517 * the handle argument to return to the caller which cgroup we should commit
3518 * against. We could also return the memcg directly and avoid the pointer
3519 * passing, but a boolean return value gives better semantics considering
3520 * the compiled-out case as well.
3521 *
3522 * Returning true means the allocation is possible.
3523 */
3524bool
3525__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3526{
3527 struct mem_cgroup *memcg;
3528 int ret;
3529
3530 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003531
3532 /*
3533 * Disabling accounting is only relevant for some specific memcg
3534 * internal allocations. Therefore we would initially not have such
3535 * check here, since direct calls to the page allocator that are marked
3536 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3537 * concerned with cache allocations, and by having this test at
3538 * memcg_kmem_get_cache, we are already able to relay the allocation to
3539 * the root cache and bypass the memcg cache altogether.
3540 *
3541 * There is one exception, though: the SLUB allocator does not create
3542 * large order caches, but rather service large kmallocs directly from
3543 * the page allocator. Therefore, the following sequence when backed by
3544 * the SLUB allocator:
3545 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003546 * memcg_stop_kmem_account();
3547 * kmalloc(<large_number>)
3548 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003549 *
3550 * would effectively ignore the fact that we should skip accounting,
3551 * since it will drive us directly to this function without passing
3552 * through the cache selector memcg_kmem_get_cache. Such large
3553 * allocations are extremely rare but can happen, for instance, for the
3554 * cache arrays. We bring this test here.
3555 */
3556 if (!current->mm || current->memcg_kmem_skip_account)
3557 return true;
3558
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003559 memcg = try_get_mem_cgroup_from_mm(current->mm);
3560
3561 /*
3562 * very rare case described in mem_cgroup_from_task. Unfortunately there
3563 * isn't much we can do without complicating this too much, and it would
3564 * be gfp-dependent anyway. Just let it go
3565 */
3566 if (unlikely(!memcg))
3567 return true;
3568
3569 if (!memcg_can_account_kmem(memcg)) {
3570 css_put(&memcg->css);
3571 return true;
3572 }
3573
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003574 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3575 if (!ret)
3576 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003577
3578 css_put(&memcg->css);
3579 return (ret == 0);
3580}
3581
3582void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3583 int order)
3584{
3585 struct page_cgroup *pc;
3586
3587 VM_BUG_ON(mem_cgroup_is_root(memcg));
3588
3589 /* The page allocation failed. Revert */
3590 if (!page) {
3591 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003592 return;
3593 }
3594
3595 pc = lookup_page_cgroup(page);
3596 lock_page_cgroup(pc);
3597 pc->mem_cgroup = memcg;
3598 SetPageCgroupUsed(pc);
3599 unlock_page_cgroup(pc);
3600}
3601
3602void __memcg_kmem_uncharge_pages(struct page *page, int order)
3603{
3604 struct mem_cgroup *memcg = NULL;
3605 struct page_cgroup *pc;
3606
3607
3608 pc = lookup_page_cgroup(page);
3609 /*
3610 * Fast unlocked return. Theoretically might have changed, have to
3611 * check again after locking.
3612 */
3613 if (!PageCgroupUsed(pc))
3614 return;
3615
3616 lock_page_cgroup(pc);
3617 if (PageCgroupUsed(pc)) {
3618 memcg = pc->mem_cgroup;
3619 ClearPageCgroupUsed(pc);
3620 }
3621 unlock_page_cgroup(pc);
3622
3623 /*
3624 * We trust that only if there is a memcg associated with the page, it
3625 * is a valid allocation
3626 */
3627 if (!memcg)
3628 return;
3629
3630 VM_BUG_ON(mem_cgroup_is_root(memcg));
3631 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003632}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003633#else
3634static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3635{
3636}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003637#endif /* CONFIG_MEMCG_KMEM */
3638
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003639#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3640
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003641#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003642/*
3643 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003644 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3645 * charge/uncharge will be never happen and move_account() is done under
3646 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003647 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003648void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003649{
3650 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003651 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003652 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003653 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003654
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003655 if (mem_cgroup_disabled())
3656 return;
David Rientjesb070e652013-05-07 16:18:09 -07003657
3658 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003659 for (i = 1; i < HPAGE_PMD_NR; i++) {
3660 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003661 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003662 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003663 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3664 }
David Rientjesb070e652013-05-07 16:18:09 -07003665 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3666 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003667}
Hugh Dickins12d27102012-01-12 17:19:52 -08003668#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003669
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003670static inline
3671void mem_cgroup_move_account_page_stat(struct mem_cgroup *from,
3672 struct mem_cgroup *to,
3673 unsigned int nr_pages,
3674 enum mem_cgroup_stat_index idx)
3675{
3676 /* Update stat data for mem_cgroup */
3677 preempt_disable();
3678 WARN_ON_ONCE(from->stat->count[idx] < nr_pages);
3679 __this_cpu_add(from->stat->count[idx], -nr_pages);
3680 __this_cpu_add(to->stat->count[idx], nr_pages);
3681 preempt_enable();
3682}
3683
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003684/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003685 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003686 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003687 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003688 * @pc: page_cgroup of the page.
3689 * @from: mem_cgroup which the page is moved from.
3690 * @to: mem_cgroup which the page is moved to. @from != @to.
3691 *
3692 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003693 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003694 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003695 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003696 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3697 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003698 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003699static int mem_cgroup_move_account(struct page *page,
3700 unsigned int nr_pages,
3701 struct page_cgroup *pc,
3702 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003703 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003704{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003705 unsigned long flags;
3706 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003707 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003708
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003710 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003711 /*
3712 * The page is isolated from LRU. So, collapse function
3713 * will not handle this page. But page splitting can happen.
3714 * Do this check under compound_page_lock(). The caller should
3715 * hold it.
3716 */
3717 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003718 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003719 goto out;
3720
3721 lock_page_cgroup(pc);
3722
3723 ret = -EINVAL;
3724 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3725 goto unlock;
3726
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003727 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003728
Sha Zhengju3ea67d02013-09-12 15:13:53 -07003729 if (!anon && page_mapped(page))
3730 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3731 MEM_CGROUP_STAT_FILE_MAPPED);
3732
3733 if (PageWriteback(page))
3734 mem_cgroup_move_account_page_stat(from, to, nr_pages,
3735 MEM_CGROUP_STAT_WRITEBACK);
3736
David Rientjesb070e652013-05-07 16:18:09 -07003737 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003738
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003739 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003740 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003741 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003742 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003743 ret = 0;
3744unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003745 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003746 /*
3747 * check events
3748 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003749 memcg_check_events(to, page);
3750 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003751out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003752 return ret;
3753}
3754
Michal Hocko2ef37d32012-10-26 13:37:30 +02003755/**
3756 * mem_cgroup_move_parent - moves page to the parent group
3757 * @page: the page to move
3758 * @pc: page_cgroup of the page
3759 * @child: page's cgroup
3760 *
3761 * move charges to its parent or the root cgroup if the group has no
3762 * parent (aka use_hierarchy==0).
3763 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3764 * mem_cgroup_move_account fails) the failure is always temporary and
3765 * it signals a race with a page removal/uncharge or migration. In the
3766 * first case the page is on the way out and it will vanish from the LRU
3767 * on the next attempt and the call should be retried later.
3768 * Isolation from the LRU fails only if page has been isolated from
3769 * the LRU since we looked at it and that usually means either global
3770 * reclaim or migration going on. The page will either get back to the
3771 * LRU or vanish.
3772 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3773 * (!PageCgroupUsed) or moved to a different group. The page will
3774 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003775 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003776static int mem_cgroup_move_parent(struct page *page,
3777 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003778 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003779{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003780 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003781 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003782 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003783 int ret;
3784
Michal Hockod8423012012-10-26 13:37:29 +02003785 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003786
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003787 ret = -EBUSY;
3788 if (!get_page_unless_zero(page))
3789 goto out;
3790 if (isolate_lru_page(page))
3791 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003792
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003793 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003794
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003795 parent = parent_mem_cgroup(child);
3796 /*
3797 * If no parent, move charges to root cgroup.
3798 */
3799 if (!parent)
3800 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003801
Michal Hocko2ef37d32012-10-26 13:37:30 +02003802 if (nr_pages > 1) {
3803 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003804 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003805 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003806
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003807 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003808 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003809 if (!ret)
3810 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003811
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003812 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003813 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003814 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003815put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003816 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003817out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003818 return ret;
3819}
3820
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003821/*
3822 * Charge the memory controller for page usage.
3823 * Return
3824 * 0 if the charge was successful
3825 * < 0 if the cgroup is over its limit
3826 */
3827static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003828 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003829{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003830 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003831 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003832 bool oom = true;
3833 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003834
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003835 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003836 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003837 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003838 /*
3839 * Never OOM-kill a process for a huge page. The
3840 * fault handler will fall back to regular pages.
3841 */
3842 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003843 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003844
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003845 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003846 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003847 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003848 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003849 return 0;
3850}
3851
3852int mem_cgroup_newpage_charge(struct page *page,
3853 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003854{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003855 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003856 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003857 VM_BUG_ON(page_mapped(page));
3858 VM_BUG_ON(page->mapping && !PageAnon(page));
3859 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003860 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003861 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003862}
3863
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003864/*
3865 * While swap-in, try_charge -> commit or cancel, the page is locked.
3866 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003867 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003868 * "commit()" or removed by "cancel()"
3869 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003870static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3871 struct page *page,
3872 gfp_t mask,
3873 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003874{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003875 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003876 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003877 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003878
Johannes Weiner90deb782012-07-31 16:45:47 -07003879 pc = lookup_page_cgroup(page);
3880 /*
3881 * Every swap fault against a single page tries to charge the
3882 * page, bail as early as possible. shmem_unuse() encounters
3883 * already charged pages, too. The USED bit is protected by
3884 * the page lock, which serializes swap cache removal, which
3885 * in turn serializes uncharging.
3886 */
3887 if (PageCgroupUsed(pc))
3888 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003889 if (!do_swap_account)
3890 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003891 memcg = try_get_mem_cgroup_from_page(page);
3892 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003893 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003894 *memcgp = memcg;
3895 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003896 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003897 if (ret == -EINTR)
3898 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003899 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003900charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003901 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3902 if (ret == -EINTR)
3903 ret = 0;
3904 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003905}
3906
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003907int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3908 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3909{
3910 *memcgp = NULL;
3911 if (mem_cgroup_disabled())
3912 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003913 /*
3914 * A racing thread's fault, or swapoff, may have already
3915 * updated the pte, and even removed page from swap cache: in
3916 * those cases unuse_pte()'s pte_same() test will fail; but
3917 * there's also a KSM case which does need to charge the page.
3918 */
3919 if (!PageSwapCache(page)) {
3920 int ret;
3921
3922 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3923 if (ret == -EINTR)
3924 ret = 0;
3925 return ret;
3926 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003927 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3928}
3929
Johannes Weiner827a03d2012-07-31 16:45:36 -07003930void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3931{
3932 if (mem_cgroup_disabled())
3933 return;
3934 if (!memcg)
3935 return;
3936 __mem_cgroup_cancel_charge(memcg, 1);
3937}
3938
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003939static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003940__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003941 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003942{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003943 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003944 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003945 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003946 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003947
Johannes Weinerce587e62012-04-24 20:22:33 +02003948 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003949 /*
3950 * Now swap is on-memory. This means this page may be
3951 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003952 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3953 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3954 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003955 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003956 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003957 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003958 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003959 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003960}
3961
Johannes Weiner72835c82012-01-12 17:18:32 -08003962void mem_cgroup_commit_charge_swapin(struct page *page,
3963 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003964{
Johannes Weiner72835c82012-01-12 17:18:32 -08003965 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003966 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003967}
3968
Johannes Weiner827a03d2012-07-31 16:45:36 -07003969int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3970 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003971{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003972 struct mem_cgroup *memcg = NULL;
3973 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3974 int ret;
3975
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003976 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003977 return 0;
3978 if (PageCompound(page))
3979 return 0;
3980
Johannes Weiner827a03d2012-07-31 16:45:36 -07003981 if (!PageSwapCache(page))
3982 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3983 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003984 ret = __mem_cgroup_try_charge_swapin(mm, page,
3985 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003986 if (!ret)
3987 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3988 }
3989 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003990}
3991
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003992static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003993 unsigned int nr_pages,
3994 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003995{
3996 struct memcg_batch_info *batch = NULL;
3997 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003998
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003999 /* If swapout, usage of swap doesn't decrease */
4000 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4001 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004002
4003 batch = &current->memcg_batch;
4004 /*
4005 * In usual, we do css_get() when we remember memcg pointer.
4006 * But in this case, we keep res->usage until end of a series of
4007 * uncharges. Then, it's ok to ignore memcg's refcnt.
4008 */
4009 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004010 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004011 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004012 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004013 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004014 * the same cgroup and we have chance to coalesce uncharges.
4015 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
4016 * because we want to do uncharge as soon as possible.
4017 */
4018
4019 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4020 goto direct_uncharge;
4021
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004022 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08004023 goto direct_uncharge;
4024
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004025 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004026 * In typical case, batch->memcg == mem. This means we can
4027 * merge a series of uncharges to an uncharge of res_counter.
4028 * If not, we uncharge res_counter ony by one.
4029 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004030 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004031 goto direct_uncharge;
4032 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004033 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004034 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004035 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004036 return;
4037direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004038 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004039 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004040 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
4041 if (unlikely(batch->memcg != memcg))
4042 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004043}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08004044
Balbir Singh8697d332008-02-07 00:13:59 -08004045/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004046 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004047 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004048static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07004049__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4050 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004051{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004052 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004053 unsigned int nr_pages = 1;
4054 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004055 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004056
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004057 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004058 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07004059
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004060 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004061 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08004062 VM_BUG_ON(!PageTransHuge(page));
4063 }
Balbir Singh8697d332008-02-07 00:13:59 -08004064 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004065 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004066 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004067 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004068 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004069 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004070
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004071 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004072
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004074
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004075 if (!PageCgroupUsed(pc))
4076 goto unlock_out;
4077
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004078 anon = PageAnon(page);
4079
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004080 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004081 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004082 /*
4083 * Generally PageAnon tells if it's the anon statistics to be
4084 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4085 * used before page reached the stage of being marked PageAnon.
4086 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004087 anon = true;
4088 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004089 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004090 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004091 if (page_mapped(page))
4092 goto unlock_out;
4093 /*
4094 * Pages under migration may not be uncharged. But
4095 * end_migration() /must/ be the one uncharging the
4096 * unused post-migration page and so it has to call
4097 * here with the migration bit still set. See the
4098 * res_counter handling below.
4099 */
4100 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004101 goto unlock_out;
4102 break;
4103 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4104 if (!PageAnon(page)) { /* Shared memory */
4105 if (page->mapping && !page_is_file_cache(page))
4106 goto unlock_out;
4107 } else if (page_mapped(page)) /* Anon */
4108 goto unlock_out;
4109 break;
4110 default:
4111 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004112 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004113
David Rientjesb070e652013-05-07 16:18:09 -07004114 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004115
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004116 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004117 /*
4118 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4119 * freed from LRU. This is safe because uncharged page is expected not
4120 * to be reused (freed soon). Exception is SwapCache, it's handled by
4121 * special functions.
4122 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004123
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004124 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004125 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004126 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004127 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004128 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004129 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004130 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004131 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004132 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004133 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004134 /*
4135 * Migration does not charge the res_counter for the
4136 * replacement page, so leave it alone when phasing out the
4137 * page that is unused after the migration.
4138 */
4139 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004140 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004141
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004142 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004143
4144unlock_out:
4145 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004146 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004147}
4148
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004149void mem_cgroup_uncharge_page(struct page *page)
4150{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004151 /* early check. */
4152 if (page_mapped(page))
4153 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004154 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004155 /*
4156 * If the page is in swap cache, uncharge should be deferred
4157 * to the swap path, which also properly accounts swap usage
4158 * and handles memcg lifetime.
4159 *
4160 * Note that this check is not stable and reclaim may add the
4161 * page to swap cache at any time after this. However, if the
4162 * page is not in swap cache by the time page->mapcount hits
4163 * 0, there won't be any page table references to the swap
4164 * slot, and reclaim will free it and not actually write the
4165 * page to disk.
4166 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004167 if (PageSwapCache(page))
4168 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004169 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004170}
4171
4172void mem_cgroup_uncharge_cache_page(struct page *page)
4173{
4174 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004175 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004176 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004177}
4178
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004179/*
4180 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4181 * In that cases, pages are freed continuously and we can expect pages
4182 * are in the same memcg. All these calls itself limits the number of
4183 * pages freed at once, then uncharge_start/end() is called properly.
4184 * This may be called prural(2) times in a context,
4185 */
4186
4187void mem_cgroup_uncharge_start(void)
4188{
4189 current->memcg_batch.do_batch++;
4190 /* We can do nest. */
4191 if (current->memcg_batch.do_batch == 1) {
4192 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004193 current->memcg_batch.nr_pages = 0;
4194 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004195 }
4196}
4197
4198void mem_cgroup_uncharge_end(void)
4199{
4200 struct memcg_batch_info *batch = &current->memcg_batch;
4201
4202 if (!batch->do_batch)
4203 return;
4204
4205 batch->do_batch--;
4206 if (batch->do_batch) /* If stacked, do nothing. */
4207 return;
4208
4209 if (!batch->memcg)
4210 return;
4211 /*
4212 * This "batch->memcg" is valid without any css_get/put etc...
4213 * bacause we hide charges behind us.
4214 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004215 if (batch->nr_pages)
4216 res_counter_uncharge(&batch->memcg->res,
4217 batch->nr_pages * PAGE_SIZE);
4218 if (batch->memsw_nr_pages)
4219 res_counter_uncharge(&batch->memcg->memsw,
4220 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004221 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004222 /* forget this pointer (for sanity check) */
4223 batch->memcg = NULL;
4224}
4225
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004226#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004227/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004228 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004229 * memcg information is recorded to swap_cgroup of "ent"
4230 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004231void
4232mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004233{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004234 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004235 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004236
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004237 if (!swapout) /* this was a swap cache but the swap is unused ! */
4238 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4239
Johannes Weiner0030f532012-07-31 16:45:25 -07004240 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004241
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004242 /*
4243 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004244 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004245 */
4246 if (do_swap_account && swapout && memcg)
Li Zefan34c00c32013-09-23 16:56:01 +08004247 swap_cgroup_record(ent, mem_cgroup_id(memcg));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004248}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004249#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004250
Andrew Mortonc255a452012-07-31 16:43:02 -07004251#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004252/*
4253 * called from swap_entry_free(). remove record in swap_cgroup and
4254 * uncharge "memsw" account.
4255 */
4256void mem_cgroup_uncharge_swap(swp_entry_t ent)
4257{
4258 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004259 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004260
4261 if (!do_swap_account)
4262 return;
4263
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004264 id = swap_cgroup_record(ent, 0);
4265 rcu_read_lock();
4266 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004267 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004268 /*
4269 * We uncharge this because swap is freed.
4270 * This memcg can be obsolete one. We avoid calling css_tryget
4271 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004272 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004273 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004274 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004275 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004276 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004277 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004278}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004279
4280/**
4281 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4282 * @entry: swap entry to be moved
4283 * @from: mem_cgroup which the entry is moved from
4284 * @to: mem_cgroup which the entry is moved to
4285 *
4286 * It succeeds only when the swap_cgroup's record for this entry is the same
4287 * as the mem_cgroup's id of @from.
4288 *
4289 * Returns 0 on success, -EINVAL on failure.
4290 *
4291 * The caller must have charged to @to, IOW, called res_counter_charge() about
4292 * both res and memsw, and called css_get().
4293 */
4294static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004295 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004296{
4297 unsigned short old_id, new_id;
4298
Li Zefan34c00c32013-09-23 16:56:01 +08004299 old_id = mem_cgroup_id(from);
4300 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004301
4302 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004303 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004304 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004305 /*
4306 * This function is only called from task migration context now.
4307 * It postpones res_counter and refcount handling till the end
4308 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004309 * improvement. But we cannot postpone css_get(to) because if
4310 * the process that has been moved to @to does swap-in, the
4311 * refcount of @to might be decreased to 0.
4312 *
4313 * We are in attach() phase, so the cgroup is guaranteed to be
4314 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004315 */
Li Zefan40503772013-07-08 16:00:34 -07004316 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004317 return 0;
4318 }
4319 return -EINVAL;
4320}
4321#else
4322static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004323 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004324{
4325 return -EINVAL;
4326}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004327#endif
4328
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004329/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004330 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4331 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004332 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004333void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4334 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004335{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004336 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004337 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004338 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004339 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004340
Johannes Weiner72835c82012-01-12 17:18:32 -08004341 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004342
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004343 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004344 return;
Balbir Singh40779602008-04-04 14:29:59 -07004345
Mel Gormanb32967f2012-11-19 12:35:47 +00004346 if (PageTransHuge(page))
4347 nr_pages <<= compound_order(page);
4348
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004349 pc = lookup_page_cgroup(page);
4350 lock_page_cgroup(pc);
4351 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004352 memcg = pc->mem_cgroup;
4353 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004354 /*
4355 * At migrating an anonymous page, its mapcount goes down
4356 * to 0 and uncharge() will be called. But, even if it's fully
4357 * unmapped, migration may fail and this page has to be
4358 * charged again. We set MIGRATION flag here and delay uncharge
4359 * until end_migration() is called
4360 *
4361 * Corner Case Thinking
4362 * A)
4363 * When the old page was mapped as Anon and it's unmap-and-freed
4364 * while migration was ongoing.
4365 * If unmap finds the old page, uncharge() of it will be delayed
4366 * until end_migration(). If unmap finds a new page, it's
4367 * uncharged when it make mapcount to be 1->0. If unmap code
4368 * finds swap_migration_entry, the new page will not be mapped
4369 * and end_migration() will find it(mapcount==0).
4370 *
4371 * B)
4372 * When the old page was mapped but migraion fails, the kernel
4373 * remaps it. A charge for it is kept by MIGRATION flag even
4374 * if mapcount goes down to 0. We can do remap successfully
4375 * without charging it again.
4376 *
4377 * C)
4378 * The "old" page is under lock_page() until the end of
4379 * migration, so, the old page itself will not be swapped-out.
4380 * If the new page is swapped out before end_migraton, our
4381 * hook to usual swap-out path will catch the event.
4382 */
4383 if (PageAnon(page))
4384 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004385 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004386 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004387 /*
4388 * If the page is not charged at this point,
4389 * we return here.
4390 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004391 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004392 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004393
Johannes Weiner72835c82012-01-12 17:18:32 -08004394 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004395 /*
4396 * We charge new page before it's used/mapped. So, even if unlock_page()
4397 * is called before end_migration, we can catch all events on this new
4398 * page. In the case new page is migrated but not remapped, new page's
4399 * mapcount will be finally 0 and we call uncharge in end_migration().
4400 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004401 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004402 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004403 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004404 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004405 /*
4406 * The page is committed to the memcg, but it's not actually
4407 * charged to the res_counter since we plan on replacing the
4408 * old one and only one page is going to be left afterwards.
4409 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004410 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004411}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004412
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004413/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004414void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004415 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004416{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004417 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004418 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004419 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004420
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004421 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004422 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004423
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004424 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004425 used = oldpage;
4426 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004427 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004428 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004429 unused = oldpage;
4430 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004431 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004432 __mem_cgroup_uncharge_common(unused,
4433 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4434 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4435 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004436 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004437 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004438 * We disallowed uncharge of pages under migration because mapcount
4439 * of the page goes down to zero, temporarly.
4440 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004441 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004442 pc = lookup_page_cgroup(oldpage);
4443 lock_page_cgroup(pc);
4444 ClearPageCgroupMigration(pc);
4445 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004446
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004447 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004448 * If a page is a file cache, radix-tree replacement is very atomic
4449 * and we can skip this check. When it was an Anon page, its mapcount
4450 * goes down to 0. But because we added MIGRATION flage, it's not
4451 * uncharged yet. There are several case but page->mapcount check
4452 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4453 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004454 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004455 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004456 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004457}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004458
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004459/*
4460 * At replace page cache, newpage is not under any memcg but it's on
4461 * LRU. So, this function doesn't touch res_counter but handles LRU
4462 * in correct way. Both pages are locked so we cannot race with uncharge.
4463 */
4464void mem_cgroup_replace_page_cache(struct page *oldpage,
4465 struct page *newpage)
4466{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004467 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004468 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004469 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004470
4471 if (mem_cgroup_disabled())
4472 return;
4473
4474 pc = lookup_page_cgroup(oldpage);
4475 /* fix accounting on old pages */
4476 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004477 if (PageCgroupUsed(pc)) {
4478 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004479 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004480 ClearPageCgroupUsed(pc);
4481 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004482 unlock_page_cgroup(pc);
4483
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004484 /*
4485 * When called from shmem_replace_page(), in some cases the
4486 * oldpage has already been charged, and in some cases not.
4487 */
4488 if (!memcg)
4489 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004490 /*
4491 * Even if newpage->mapping was NULL before starting replacement,
4492 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4493 * LRU while we overwrite pc->mem_cgroup.
4494 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004495 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004496}
4497
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004498#ifdef CONFIG_DEBUG_VM
4499static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4500{
4501 struct page_cgroup *pc;
4502
4503 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004504 /*
4505 * Can be NULL while feeding pages into the page allocator for
4506 * the first time, i.e. during boot or memory hotplug;
4507 * or when mem_cgroup_disabled().
4508 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004509 if (likely(pc) && PageCgroupUsed(pc))
4510 return pc;
4511 return NULL;
4512}
4513
4514bool mem_cgroup_bad_page_check(struct page *page)
4515{
4516 if (mem_cgroup_disabled())
4517 return false;
4518
4519 return lookup_page_cgroup_used(page) != NULL;
4520}
4521
4522void mem_cgroup_print_bad_page(struct page *page)
4523{
4524 struct page_cgroup *pc;
4525
4526 pc = lookup_page_cgroup_used(page);
4527 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004528 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4529 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004530 }
4531}
4532#endif
4533
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004534static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004535 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004536{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004537 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004538 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004539 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004540 int children = mem_cgroup_count_children(memcg);
4541 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004542 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004543
4544 /*
4545 * For keeping hierarchical_reclaim simple, how long we should retry
4546 * is depends on callers. We set our retry-count to be function
4547 * of # of children which we should visit in this loop.
4548 */
4549 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4550
4551 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004552
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004553 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004554 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004555 if (signal_pending(current)) {
4556 ret = -EINTR;
4557 break;
4558 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004559 /*
4560 * Rather than hide all in some function, I do this in
4561 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004562 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004563 */
4564 mutex_lock(&set_limit_mutex);
4565 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4566 if (memswlimit < val) {
4567 ret = -EINVAL;
4568 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004569 break;
4570 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004571
4572 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4573 if (memlimit < val)
4574 enlarge = 1;
4575
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004576 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004577 if (!ret) {
4578 if (memswlimit == val)
4579 memcg->memsw_is_minimum = true;
4580 else
4581 memcg->memsw_is_minimum = false;
4582 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004583 mutex_unlock(&set_limit_mutex);
4584
4585 if (!ret)
4586 break;
4587
Johannes Weiner56600482012-01-12 17:17:59 -08004588 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4589 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004590 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4591 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004592 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004593 retry_count--;
4594 else
4595 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004596 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004597 if (!ret && enlarge)
4598 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004599
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004600 return ret;
4601}
4602
Li Zefan338c8432009-06-17 16:27:15 -07004603static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4604 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004605{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004606 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004607 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004608 int children = mem_cgroup_count_children(memcg);
4609 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004610 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004611
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004612 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004613 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004614 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004615 while (retry_count) {
4616 if (signal_pending(current)) {
4617 ret = -EINTR;
4618 break;
4619 }
4620 /*
4621 * Rather than hide all in some function, I do this in
4622 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004623 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004624 */
4625 mutex_lock(&set_limit_mutex);
4626 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4627 if (memlimit > val) {
4628 ret = -EINVAL;
4629 mutex_unlock(&set_limit_mutex);
4630 break;
4631 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004632 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4633 if (memswlimit < val)
4634 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004635 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004636 if (!ret) {
4637 if (memlimit == val)
4638 memcg->memsw_is_minimum = true;
4639 else
4640 memcg->memsw_is_minimum = false;
4641 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004642 mutex_unlock(&set_limit_mutex);
4643
4644 if (!ret)
4645 break;
4646
Johannes Weiner56600482012-01-12 17:17:59 -08004647 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4648 MEM_CGROUP_RECLAIM_NOSWAP |
4649 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004650 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004651 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004652 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004653 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004654 else
4655 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004656 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004657 if (!ret && enlarge)
4658 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004659 return ret;
4660}
4661
Michal Hocko2ef37d32012-10-26 13:37:30 +02004662/**
4663 * mem_cgroup_force_empty_list - clears LRU of a group
4664 * @memcg: group to clear
4665 * @node: NUMA node
4666 * @zid: zone id
4667 * @lru: lru to to clear
4668 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004669 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004670 * reclaim the pages page themselves - pages are moved to the parent (or root)
4671 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004672 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004673static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004674 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004675{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004676 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004677 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004678 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004679 struct page *busy;
4680 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004681
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004682 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004683 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4684 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004685
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004686 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004687 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004688 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004689 struct page *page;
4690
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004691 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004692 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004693 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004694 break;
4695 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004696 page = list_entry(list->prev, struct page, lru);
4697 if (busy == page) {
4698 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004699 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004700 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004701 continue;
4702 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004703 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004704
Johannes Weiner925b7672012-01-12 17:18:15 -08004705 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004706
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004707 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004708 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004709 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004710 cond_resched();
4711 } else
4712 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004713 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004714}
4715
4716/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004717 * make mem_cgroup's charge to be 0 if there is no task by moving
4718 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004719 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004720 *
4721 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004722 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004723static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004724{
Michal Hockoc26251f2012-10-26 13:37:28 +02004725 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004726 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004727
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004728 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004729 /* This is for making all *used* pages to be on LRU. */
4730 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004731 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004732 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004733 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004734 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004735 enum lru_list lru;
4736 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004737 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004738 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004739 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004740 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004741 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004742 mem_cgroup_end_move(memcg);
4743 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004744 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004745
Michal Hocko2ef37d32012-10-26 13:37:30 +02004746 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004747 * Kernel memory may not necessarily be trackable to a specific
4748 * process. So they are not migrated, and therefore we can't
4749 * expect their value to drop to 0 here.
4750 * Having res filled up with kmem only is enough.
4751 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004752 * This is a safety check because mem_cgroup_force_empty_list
4753 * could have raced with mem_cgroup_replace_page_cache callers
4754 * so the lru seemed empty but the page could have been added
4755 * right after the check. RES_USAGE should be safe as we always
4756 * charge before adding to the LRU.
4757 */
Glauber Costabea207c2012-12-18 14:22:11 -08004758 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4759 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4760 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004761}
4762
4763/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004764 * This mainly exists for tests during the setting of set of use_hierarchy.
4765 * Since this is the very setting we are changing, the current hierarchy value
4766 * is meaningless
4767 */
4768static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4769{
Tejun Heo492eb212013-08-08 20:11:25 -04004770 struct cgroup_subsys_state *pos;
Glauber Costab5f99b52013-02-22 16:34:53 -08004771
4772 /* bounce at first found */
Tejun Heo492eb212013-08-08 20:11:25 -04004773 css_for_each_child(pos, &memcg->css)
Glauber Costab5f99b52013-02-22 16:34:53 -08004774 return true;
4775 return false;
4776}
4777
4778/*
Glauber Costa09998212013-02-22 16:34:55 -08004779 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4780 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004781 * from mem_cgroup_count_children(), in the sense that we don't really care how
4782 * many children we have; we only need to know if we have any. It also counts
4783 * any memcg without hierarchy as infertile.
4784 */
4785static inline bool memcg_has_children(struct mem_cgroup *memcg)
4786{
4787 return memcg->use_hierarchy && __memcg_has_children(memcg);
4788}
4789
4790/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004791 * Reclaims as many pages from the given memcg as possible and moves
4792 * the rest to the parent.
4793 *
4794 * Caller is responsible for holding css reference for memcg.
4795 */
4796static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4797{
4798 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4799 struct cgroup *cgrp = memcg->css.cgroup;
4800
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004801 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004802 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4803 return -EBUSY;
4804
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004805 /* we call try-to-free pages for make this cgroup empty */
4806 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004807 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004808 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004809 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004810
Michal Hockoc26251f2012-10-26 13:37:28 +02004811 if (signal_pending(current))
4812 return -EINTR;
4813
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004814 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004815 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004816 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004817 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004818 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004819 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004820 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004821
4822 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004823 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004824 mem_cgroup_reparent_charges(memcg);
4825
4826 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004827}
4828
Tejun Heo182446d2013-08-08 20:11:24 -04004829static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
4830 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004831{
Tejun Heo182446d2013-08-08 20:11:24 -04004832 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02004833
Michal Hockod8423012012-10-26 13:37:29 +02004834 if (mem_cgroup_is_root(memcg))
4835 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07004836 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004837}
4838
Tejun Heo182446d2013-08-08 20:11:24 -04004839static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
4840 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004841{
Tejun Heo182446d2013-08-08 20:11:24 -04004842 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004843}
4844
Tejun Heo182446d2013-08-08 20:11:24 -04004845static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4846 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004847{
4848 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004849 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04004850 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08004851
Glauber Costa09998212013-02-22 16:34:55 -08004852 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004853
4854 if (memcg->use_hierarchy == val)
4855 goto out;
4856
Balbir Singh18f59ea2009-01-07 18:08:07 -08004857 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004858 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004859 * in the child subtrees. If it is unset, then the change can
4860 * occur, provided the current cgroup has no children.
4861 *
4862 * For the root cgroup, parent_mem is NULL, we allow value to be
4863 * set if there are no children.
4864 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004865 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004866 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004867 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004868 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004869 else
4870 retval = -EBUSY;
4871 } else
4872 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004873
4874out:
Glauber Costa09998212013-02-22 16:34:55 -08004875 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004876
4877 return retval;
4878}
4879
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004881static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004882 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004883{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004884 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004885 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004886
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004887 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004888 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004889 val += mem_cgroup_read_stat(iter, idx);
4890
4891 if (val < 0) /* race ? */
4892 val = 0;
4893 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004894}
4895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004896static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004897{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004898 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004899
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004900 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004901 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004902 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004903 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004904 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004905 }
4906
David Rientjesb070e652013-05-07 16:18:09 -07004907 /*
4908 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4909 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4910 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004911 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4912 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004913
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004914 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004915 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004916
4917 return val << PAGE_SHIFT;
4918}
4919
Tejun Heo182446d2013-08-08 20:11:24 -04004920static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
4921 struct cftype *cft, struct file *file,
4922 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004923{
Tejun Heo182446d2013-08-08 20:11:24 -04004924 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07004925 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004926 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004927 int name, len;
4928 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004929
4930 type = MEMFILE_TYPE(cft->private);
4931 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004932
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004933 switch (type) {
4934 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004935 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004936 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004937 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004938 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004939 break;
4940 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004941 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004942 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004943 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004944 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004945 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004946 case _KMEM:
4947 val = res_counter_read_u64(&memcg->kmem, name);
4948 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004949 default:
4950 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004951 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004952
4953 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4954 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004955}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004956
Tejun Heo182446d2013-08-08 20:11:24 -04004957static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004958{
4959 int ret = -EINVAL;
4960#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04004961 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004962 /*
4963 * For simplicity, we won't allow this to be disabled. It also can't
4964 * be changed if the cgroup has children already, or if tasks had
4965 * already joined.
4966 *
4967 * If tasks join before we set the limit, a person looking at
4968 * kmem.usage_in_bytes will have no way to determine when it took
4969 * place, which makes the value quite meaningless.
4970 *
4971 * After it first became limited, changes in the value of the limit are
4972 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004973 */
Glauber Costa09998212013-02-22 16:34:55 -08004974 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004975 mutex_lock(&set_limit_mutex);
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004976 if (!memcg->kmem_account_flags && val != RES_COUNTER_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04004977 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004978 ret = -EBUSY;
4979 goto out;
4980 }
4981 ret = res_counter_set_limit(&memcg->kmem, val);
4982 VM_BUG_ON(ret);
4983
Glauber Costa55007d82012-12-18 14:22:38 -08004984 ret = memcg_update_cache_sizes(memcg);
4985 if (ret) {
Sha Zhengju6de5a8b2013-09-12 15:13:47 -07004986 res_counter_set_limit(&memcg->kmem, RES_COUNTER_MAX);
Glauber Costa55007d82012-12-18 14:22:38 -08004987 goto out;
4988 }
Glauber Costa692e89a2013-02-22 16:34:56 -08004989 static_key_slow_inc(&memcg_kmem_enabled_key);
4990 /*
4991 * setting the active bit after the inc will guarantee no one
4992 * starts accounting before all call sites are patched
4993 */
4994 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004995 } else
4996 ret = res_counter_set_limit(&memcg->kmem, val);
4997out:
4998 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08004999 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005000#endif
5001 return ret;
5002}
5003
Hugh Dickins6d0439902013-02-22 16:35:50 -08005004#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005005static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005006{
Glauber Costa55007d82012-12-18 14:22:38 -08005007 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005008 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5009 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005010 goto out;
5011
Glauber Costa510fc4e2012-12-18 14:21:47 -08005012 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005013 /*
5014 * When that happen, we need to disable the static branch only on those
5015 * memcgs that enabled it. To achieve this, we would be forced to
5016 * complicate the code by keeping track of which memcgs were the ones
5017 * that actually enabled limits, and which ones got it from its
5018 * parents.
5019 *
5020 * It is a lot simpler just to do static_key_slow_inc() on every child
5021 * that is accounted.
5022 */
Glauber Costa55007d82012-12-18 14:22:38 -08005023 if (!memcg_kmem_is_active(memcg))
5024 goto out;
5025
5026 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07005027 * __mem_cgroup_free() will issue static_key_slow_dec() because this
5028 * memcg is active already. If the later initialization fails then the
5029 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08005030 */
Glauber Costa55007d82012-12-18 14:22:38 -08005031 static_key_slow_inc(&memcg_kmem_enabled_key);
5032
5033 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07005034 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005035 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07005036 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08005037 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005038out:
5039 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005040}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005041#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005042
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005043/*
5044 * The user of this function is...
5045 * RES_LIMIT.
5046 */
Tejun Heo182446d2013-08-08 20:11:24 -04005047static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07005048 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005049{
Tejun Heo182446d2013-08-08 20:11:24 -04005050 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005051 enum res_type type;
5052 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005053 unsigned long long val;
5054 int ret;
5055
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005056 type = MEMFILE_TYPE(cft->private);
5057 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005058
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005059 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005060 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005061 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5062 ret = -EINVAL;
5063 break;
5064 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005065 /* This function does all necessary parse...reuse it */
5066 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005067 if (ret)
5068 break;
5069 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005070 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005071 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005072 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005073 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04005074 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005075 else
5076 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005077 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005078 case RES_SOFT_LIMIT:
5079 ret = res_counter_memparse_write_strategy(buffer, &val);
5080 if (ret)
5081 break;
5082 /*
5083 * For memsw, soft limits are hard to implement in terms
5084 * of semantics, for now, we support soft limits for
5085 * control without swap
5086 */
5087 if (type == _MEM)
5088 ret = res_counter_set_soft_limit(&memcg->res, val);
5089 else
5090 ret = -EINVAL;
5091 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005092 default:
5093 ret = -EINVAL; /* should be BUG() ? */
5094 break;
5095 }
5096 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005097}
5098
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005099static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5100 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5101{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005102 unsigned long long min_limit, min_memsw_limit, tmp;
5103
5104 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5105 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005106 if (!memcg->use_hierarchy)
5107 goto out;
5108
Tejun Heo63876982013-08-08 20:11:23 -04005109 while (css_parent(&memcg->css)) {
5110 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005111 if (!memcg->use_hierarchy)
5112 break;
5113 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5114 min_limit = min(min_limit, tmp);
5115 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5116 min_memsw_limit = min(min_memsw_limit, tmp);
5117 }
5118out:
5119 *mem_limit = min_limit;
5120 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005121}
5122
Tejun Heo182446d2013-08-08 20:11:24 -04005123static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005124{
Tejun Heo182446d2013-08-08 20:11:24 -04005125 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005126 int name;
5127 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005128
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005129 type = MEMFILE_TYPE(event);
5130 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005131
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005132 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005133 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005134 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005135 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005136 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005137 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005138 else if (type == _KMEM)
5139 res_counter_reset_max(&memcg->kmem);
5140 else
5141 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005142 break;
5143 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005144 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005145 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005146 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005147 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005148 else if (type == _KMEM)
5149 res_counter_reset_failcnt(&memcg->kmem);
5150 else
5151 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005152 break;
5153 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005154
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005155 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005156}
5157
Tejun Heo182446d2013-08-08 20:11:24 -04005158static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005159 struct cftype *cft)
5160{
Tejun Heo182446d2013-08-08 20:11:24 -04005161 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005162}
5163
Daisuke Nishimura02491442010-03-10 15:22:17 -08005164#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005165static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005166 struct cftype *cft, u64 val)
5167{
Tejun Heo182446d2013-08-08 20:11:24 -04005168 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005169
5170 if (val >= (1 << NR_MOVE_TYPE))
5171 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005172
Glauber Costaee5e8472013-02-22 16:34:50 -08005173 /*
5174 * No kind of locking is needed in here, because ->can_attach() will
5175 * check this value once in the beginning of the process, and then carry
5176 * on with stale data. This means that changes to this value will only
5177 * affect task migrations starting after the change.
5178 */
5179 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005180 return 0;
5181}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005182#else
Tejun Heo182446d2013-08-08 20:11:24 -04005183static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005184 struct cftype *cft, u64 val)
5185{
5186 return -ENOSYS;
5187}
5188#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005189
Ying Han406eb0c2011-05-26 16:25:37 -07005190#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005191static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5192 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005193{
5194 int nid;
5195 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5196 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005197 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005198
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005199 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005200 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005201 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005202 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005203 seq_printf(m, " N%d=%lu", nid, node_nr);
5204 }
5205 seq_putc(m, '\n');
5206
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005207 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005208 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005209 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005210 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005211 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005212 seq_printf(m, " N%d=%lu", nid, node_nr);
5213 }
5214 seq_putc(m, '\n');
5215
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005216 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005217 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005218 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005219 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005220 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005221 seq_printf(m, " N%d=%lu", nid, node_nr);
5222 }
5223 seq_putc(m, '\n');
5224
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005225 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005226 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005227 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005228 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005229 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005230 seq_printf(m, " N%d=%lu", nid, node_nr);
5231 }
5232 seq_putc(m, '\n');
5233 return 0;
5234}
5235#endif /* CONFIG_NUMA */
5236
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005237static inline void mem_cgroup_lru_names_not_uptodate(void)
5238{
5239 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5240}
5241
Tejun Heo182446d2013-08-08 20:11:24 -04005242static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005243 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005244{
Tejun Heo182446d2013-08-08 20:11:24 -04005245 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005246 struct mem_cgroup *mi;
5247 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005248
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005249 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005250 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005251 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005252 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5253 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005254 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005255
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005256 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5257 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5258 mem_cgroup_read_events(memcg, i));
5259
5260 for (i = 0; i < NR_LRU_LISTS; i++)
5261 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5262 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5263
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005264 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005265 {
5266 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005267 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005268 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005269 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005270 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5271 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005272 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005273
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005274 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5275 long long val = 0;
5276
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005277 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005278 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005279 for_each_mem_cgroup_tree(mi, memcg)
5280 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5281 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5282 }
5283
5284 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5285 unsigned long long val = 0;
5286
5287 for_each_mem_cgroup_tree(mi, memcg)
5288 val += mem_cgroup_read_events(mi, i);
5289 seq_printf(m, "total_%s %llu\n",
5290 mem_cgroup_events_names[i], val);
5291 }
5292
5293 for (i = 0; i < NR_LRU_LISTS; i++) {
5294 unsigned long long val = 0;
5295
5296 for_each_mem_cgroup_tree(mi, memcg)
5297 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5298 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005299 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005300
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005301#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005302 {
5303 int nid, zid;
5304 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005305 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005306 unsigned long recent_rotated[2] = {0, 0};
5307 unsigned long recent_scanned[2] = {0, 0};
5308
5309 for_each_online_node(nid)
5310 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005311 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005312 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005313
Hugh Dickins89abfab2012-05-29 15:06:53 -07005314 recent_rotated[0] += rstat->recent_rotated[0];
5315 recent_rotated[1] += rstat->recent_rotated[1];
5316 recent_scanned[0] += rstat->recent_scanned[0];
5317 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005318 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005319 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5320 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5321 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5322 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005323 }
5324#endif
5325
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005326 return 0;
5327}
5328
Tejun Heo182446d2013-08-08 20:11:24 -04005329static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5330 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005331{
Tejun Heo182446d2013-08-08 20:11:24 -04005332 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005333
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005334 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005335}
5336
Tejun Heo182446d2013-08-08 20:11:24 -04005337static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5338 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005339{
Tejun Heo182446d2013-08-08 20:11:24 -04005340 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005341 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005342
Tejun Heo63876982013-08-08 20:11:23 -04005343 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005344 return -EINVAL;
5345
Glauber Costa09998212013-02-22 16:34:55 -08005346 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005347
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005348 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005349 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005350 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005351 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005352 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005353
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005354 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005355
Glauber Costa09998212013-02-22 16:34:55 -08005356 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005357
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005358 return 0;
5359}
5360
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005361static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5362{
5363 struct mem_cgroup_threshold_ary *t;
5364 u64 usage;
5365 int i;
5366
5367 rcu_read_lock();
5368 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005369 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005370 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005371 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005372
5373 if (!t)
5374 goto unlock;
5375
5376 usage = mem_cgroup_usage(memcg, swap);
5377
5378 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005379 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005380 * If it's not true, a threshold was crossed after last
5381 * call of __mem_cgroup_threshold().
5382 */
Phil Carmody5407a562010-05-26 14:42:42 -07005383 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005384
5385 /*
5386 * Iterate backward over array of thresholds starting from
5387 * current_threshold and check if a threshold is crossed.
5388 * If none of thresholds below usage is crossed, we read
5389 * only one element of the array here.
5390 */
5391 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5392 eventfd_signal(t->entries[i].eventfd, 1);
5393
5394 /* i = current_threshold + 1 */
5395 i++;
5396
5397 /*
5398 * Iterate forward over array of thresholds starting from
5399 * current_threshold+1 and check if a threshold is crossed.
5400 * If none of thresholds above usage is crossed, we read
5401 * only one element of the array here.
5402 */
5403 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5404 eventfd_signal(t->entries[i].eventfd, 1);
5405
5406 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005407 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005408unlock:
5409 rcu_read_unlock();
5410}
5411
5412static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5413{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005414 while (memcg) {
5415 __mem_cgroup_threshold(memcg, false);
5416 if (do_swap_account)
5417 __mem_cgroup_threshold(memcg, true);
5418
5419 memcg = parent_mem_cgroup(memcg);
5420 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005421}
5422
5423static int compare_thresholds(const void *a, const void *b)
5424{
5425 const struct mem_cgroup_threshold *_a = a;
5426 const struct mem_cgroup_threshold *_b = b;
5427
Greg Thelen2bff24a2013-09-11 14:23:08 -07005428 if (_a->threshold > _b->threshold)
5429 return 1;
5430
5431 if (_a->threshold < _b->threshold)
5432 return -1;
5433
5434 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005435}
5436
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005437static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005438{
5439 struct mem_cgroup_eventfd_list *ev;
5440
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005441 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005442 eventfd_signal(ev->eventfd, 1);
5443 return 0;
5444}
5445
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005446static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005447{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005448 struct mem_cgroup *iter;
5449
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005450 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005451 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005452}
5453
Tejun Heo81eeaf02013-08-08 20:11:26 -04005454static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005455 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005456{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005457 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005458 struct mem_cgroup_thresholds *thresholds;
5459 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005460 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005461 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005462 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005463
5464 ret = res_counter_memparse_write_strategy(args, &threshold);
5465 if (ret)
5466 return ret;
5467
5468 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005469
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005470 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005471 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005472 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005473 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005474 else
5475 BUG();
5476
5477 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5478
5479 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005480 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005481 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5482
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005483 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005484
5485 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005486 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005487 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005488 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005489 ret = -ENOMEM;
5490 goto unlock;
5491 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005492 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005493
5494 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005495 if (thresholds->primary) {
5496 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005497 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005498 }
5499
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005500 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005501 new->entries[size - 1].eventfd = eventfd;
5502 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005503
5504 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506 compare_thresholds, NULL);
5507
5508 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005509 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005510 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005511 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005512 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005513 * new->current_threshold will not be used until
5514 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005515 * it here.
5516 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005517 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005518 } else
5519 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005520 }
5521
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005522 /* Free old spare buffer and save old primary buffer as spare */
5523 kfree(thresholds->spare);
5524 thresholds->spare = thresholds->primary;
5525
5526 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005527
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005528 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529 synchronize_rcu();
5530
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005531unlock:
5532 mutex_unlock(&memcg->thresholds_lock);
5533
5534 return ret;
5535}
5536
Tejun Heo81eeaf02013-08-08 20:11:26 -04005537static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005538 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005539{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005540 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005541 struct mem_cgroup_thresholds *thresholds;
5542 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005543 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005544 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005545 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005546
5547 mutex_lock(&memcg->thresholds_lock);
5548 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005549 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005550 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005551 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005552 else
5553 BUG();
5554
Anton Vorontsov371528c2012-02-24 05:14:46 +04005555 if (!thresholds->primary)
5556 goto unlock;
5557
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005558 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5559
5560 /* Check if a threshold crossed before removing */
5561 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5562
5563 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005564 size = 0;
5565 for (i = 0; i < thresholds->primary->size; i++) {
5566 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005567 size++;
5568 }
5569
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005570 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005571
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005572 /* Set thresholds array to NULL if we don't have thresholds */
5573 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005574 kfree(new);
5575 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005576 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005577 }
5578
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005579 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005580
5581 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005582 new->current_threshold = -1;
5583 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5584 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005585 continue;
5586
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005587 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005588 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005589 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005590 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005591 * until rcu_assign_pointer(), so it's safe to increment
5592 * it here.
5593 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005594 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005595 }
5596 j++;
5597 }
5598
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005599swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005600 /* Swap primary and spare array */
5601 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005602 /* If all events are unregistered, free the spare array */
5603 if (!new) {
5604 kfree(thresholds->spare);
5605 thresholds->spare = NULL;
5606 }
5607
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005608 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005609
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005610 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005611 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005612unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005613 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005614}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005615
Tejun Heo81eeaf02013-08-08 20:11:26 -04005616static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005617 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5618{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005619 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005620 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005621 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005622
5623 BUG_ON(type != _OOM_TYPE);
5624 event = kmalloc(sizeof(*event), GFP_KERNEL);
5625 if (!event)
5626 return -ENOMEM;
5627
Michal Hocko1af8efe2011-07-26 16:08:24 -07005628 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005629
5630 event->eventfd = eventfd;
5631 list_add(&event->list, &memcg->oom_notify);
5632
5633 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005634 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005635 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005636 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005637
5638 return 0;
5639}
5640
Tejun Heo81eeaf02013-08-08 20:11:26 -04005641static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005642 struct cftype *cft, struct eventfd_ctx *eventfd)
5643{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005644 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005645 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005646 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005647
5648 BUG_ON(type != _OOM_TYPE);
5649
Michal Hocko1af8efe2011-07-26 16:08:24 -07005650 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005652 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005653 if (ev->eventfd == eventfd) {
5654 list_del(&ev->list);
5655 kfree(ev);
5656 }
5657 }
5658
Michal Hocko1af8efe2011-07-26 16:08:24 -07005659 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005660}
5661
Tejun Heo182446d2013-08-08 20:11:24 -04005662static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005663 struct cftype *cft, struct cgroup_map_cb *cb)
5664{
Tejun Heo182446d2013-08-08 20:11:24 -04005665 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005666
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005667 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005668
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005669 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005670 cb->fill(cb, "under_oom", 1);
5671 else
5672 cb->fill(cb, "under_oom", 0);
5673 return 0;
5674}
5675
Tejun Heo182446d2013-08-08 20:11:24 -04005676static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005677 struct cftype *cft, u64 val)
5678{
Tejun Heo182446d2013-08-08 20:11:24 -04005679 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005680 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005681
5682 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005683 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005684 return -EINVAL;
5685
Glauber Costa09998212013-02-22 16:34:55 -08005686 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005687 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005688 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005689 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005690 return -EINVAL;
5691 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005692 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005693 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005694 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005695 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005696 return 0;
5697}
5698
Andrew Mortonc255a452012-07-31 16:43:02 -07005699#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005700static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005701{
Glauber Costa55007d82012-12-18 14:22:38 -08005702 int ret;
5703
Glauber Costa2633d7a2012-12-18 14:22:34 -08005704 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005705 ret = memcg_propagate_kmem(memcg);
5706 if (ret)
5707 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005708
Glauber Costa1d62e432012-04-09 19:36:33 -03005709 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005710}
Glauber Costae5671df2011-12-11 21:47:01 +00005711
Li Zefan10d5ebf2013-07-08 16:00:33 -07005712static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005713{
Glauber Costa1d62e432012-04-09 19:36:33 -03005714 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005715}
5716
5717static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5718{
5719 if (!memcg_kmem_is_active(memcg))
5720 return;
5721
5722 /*
5723 * kmem charges can outlive the cgroup. In the case of slab
5724 * pages, for instance, a page contain objects from various
5725 * processes. As we prevent from taking a reference for every
5726 * such allocation we have to be careful when doing uncharge
5727 * (see memcg_uncharge_kmem) and here during offlining.
5728 *
5729 * The idea is that that only the _last_ uncharge which sees
5730 * the dead memcg will drop the last reference. An additional
5731 * reference is taken here before the group is marked dead
5732 * which is then paired with css_put during uncharge resp. here.
5733 *
5734 * Although this might sound strange as this path is called from
5735 * css_offline() when the referencemight have dropped down to 0
5736 * and shouldn't be incremented anymore (css_tryget would fail)
5737 * we do not have other options because of the kmem allocations
5738 * lifetime.
5739 */
5740 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005741
5742 memcg_kmem_mark_dead(memcg);
5743
5744 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5745 return;
5746
Glauber Costa7de37682012-12-18 14:22:07 -08005747 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005748 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005749}
Glauber Costae5671df2011-12-11 21:47:01 +00005750#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005751static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005752{
5753 return 0;
5754}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005755
Li Zefan10d5ebf2013-07-08 16:00:33 -07005756static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5757{
5758}
5759
5760static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005761{
5762}
Glauber Costae5671df2011-12-11 21:47:01 +00005763#endif
5764
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005765static struct cftype mem_cgroup_files[] = {
5766 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005767 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005768 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005769 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005770 .register_event = mem_cgroup_usage_register_event,
5771 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005772 },
5773 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005774 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005775 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005776 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005777 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005778 },
5779 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005780 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005781 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005782 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005783 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005784 },
5785 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005786 .name = "soft_limit_in_bytes",
5787 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5788 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005789 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005790 },
5791 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005792 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005793 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005794 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005795 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005796 },
Balbir Singh8697d332008-02-07 00:13:59 -08005797 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005798 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005799 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005800 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005801 {
5802 .name = "force_empty",
5803 .trigger = mem_cgroup_force_empty_write,
5804 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005805 {
5806 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005807 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005808 .write_u64 = mem_cgroup_hierarchy_write,
5809 .read_u64 = mem_cgroup_hierarchy_read,
5810 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005811 {
5812 .name = "swappiness",
5813 .read_u64 = mem_cgroup_swappiness_read,
5814 .write_u64 = mem_cgroup_swappiness_write,
5815 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005816 {
5817 .name = "move_charge_at_immigrate",
5818 .read_u64 = mem_cgroup_move_charge_read,
5819 .write_u64 = mem_cgroup_move_charge_write,
5820 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005821 {
5822 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005823 .read_map = mem_cgroup_oom_control_read,
5824 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005825 .register_event = mem_cgroup_oom_register_event,
5826 .unregister_event = mem_cgroup_oom_unregister_event,
5827 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5828 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005829 {
5830 .name = "pressure_level",
5831 .register_event = vmpressure_register_event,
5832 .unregister_event = vmpressure_unregister_event,
5833 },
Ying Han406eb0c2011-05-26 16:25:37 -07005834#ifdef CONFIG_NUMA
5835 {
5836 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005837 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005838 },
5839#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005840#ifdef CONFIG_MEMCG_KMEM
5841 {
5842 .name = "kmem.limit_in_bytes",
5843 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5844 .write_string = mem_cgroup_write,
5845 .read = mem_cgroup_read,
5846 },
5847 {
5848 .name = "kmem.usage_in_bytes",
5849 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5850 .read = mem_cgroup_read,
5851 },
5852 {
5853 .name = "kmem.failcnt",
5854 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5855 .trigger = mem_cgroup_reset,
5856 .read = mem_cgroup_read,
5857 },
5858 {
5859 .name = "kmem.max_usage_in_bytes",
5860 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5861 .trigger = mem_cgroup_reset,
5862 .read = mem_cgroup_read,
5863 },
Glauber Costa749c5412012-12-18 14:23:01 -08005864#ifdef CONFIG_SLABINFO
5865 {
5866 .name = "kmem.slabinfo",
5867 .read_seq_string = mem_cgroup_slabinfo_read,
5868 },
5869#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005870#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005871 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005872};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005873
Michal Hocko2d110852013-02-22 16:34:43 -08005874#ifdef CONFIG_MEMCG_SWAP
5875static struct cftype memsw_cgroup_files[] = {
5876 {
5877 .name = "memsw.usage_in_bytes",
5878 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5879 .read = mem_cgroup_read,
5880 .register_event = mem_cgroup_usage_register_event,
5881 .unregister_event = mem_cgroup_usage_unregister_event,
5882 },
5883 {
5884 .name = "memsw.max_usage_in_bytes",
5885 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5886 .trigger = mem_cgroup_reset,
5887 .read = mem_cgroup_read,
5888 },
5889 {
5890 .name = "memsw.limit_in_bytes",
5891 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5892 .write_string = mem_cgroup_write,
5893 .read = mem_cgroup_read,
5894 },
5895 {
5896 .name = "memsw.failcnt",
5897 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5898 .trigger = mem_cgroup_reset,
5899 .read = mem_cgroup_read,
5900 },
5901 { }, /* terminate */
5902};
5903#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005904static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005905{
5906 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005907 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005908 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005909 /*
5910 * This routine is called against possible nodes.
5911 * But it's BUG to call kmalloc() against offline node.
5912 *
5913 * TODO: this routine can waste much memory for nodes which will
5914 * never be onlined. It's better to use memory hotplug callback
5915 * function.
5916 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005917 if (!node_state(node, N_NORMAL_MEMORY))
5918 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005919 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005920 if (!pn)
5921 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005922
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005923 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5924 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005925 lruvec_init(&mz->lruvec);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005926 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005927 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005928 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005929 return 0;
5930}
5931
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005932static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005933{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005934 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005935}
5936
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005937static struct mem_cgroup *mem_cgroup_alloc(void)
5938{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005939 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005940 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005941
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005942 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005943 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005944 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005945 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005946 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005947
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005948 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005949 return NULL;
5950
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005951 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5952 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005953 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005954 spin_lock_init(&memcg->pcp_counter_lock);
5955 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005956
5957out_free:
5958 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005959 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005960 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005961 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005962 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005963}
5964
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005965/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005966 * At destroying mem_cgroup, references from swap_cgroup can remain.
5967 * (scanning all at force_empty is too costly...)
5968 *
5969 * Instead of clearing all references at force_empty, we remember
5970 * the number of reference from swap_cgroup and free mem_cgroup when
5971 * it goes down to 0.
5972 *
5973 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005974 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005975
5976static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005977{
Glauber Costac8b2a362012-12-18 14:22:13 -08005978 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005979 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005980
Glauber Costac8b2a362012-12-18 14:22:13 -08005981 free_css_id(&mem_cgroup_subsys, &memcg->css);
5982
5983 for_each_node(node)
5984 free_mem_cgroup_per_zone_info(memcg, node);
5985
5986 free_percpu(memcg->stat);
5987
Glauber Costa3f134612012-05-29 15:07:11 -07005988 /*
5989 * We need to make sure that (at least for now), the jump label
5990 * destruction code runs outside of the cgroup lock. This is because
5991 * get_online_cpus(), which is called from the static_branch update,
5992 * can't be called inside the cgroup_lock. cpusets are the ones
5993 * enforcing this dependency, so if they ever change, we might as well.
5994 *
5995 * schedule_work() will guarantee this happens. Be careful if you need
5996 * to move this code around, and make sure it is outside
5997 * the cgroup_lock.
5998 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005999 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006000 if (size < PAGE_SIZE)
6001 kfree(memcg);
6002 else
6003 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006004}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006005
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006006/*
6007 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6008 */
Glauber Costae1aab162011-12-11 21:47:03 +00006009struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006010{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006011 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006012 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006013 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006014}
Glauber Costae1aab162011-12-11 21:47:03 +00006015EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006016
Li Zefan0eb253e2009-01-15 13:51:25 -08006017static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04006018mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006019{
Glauber Costad142e3e2013-02-22 16:34:52 -08006020 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006021 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006022 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006023
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006024 memcg = mem_cgroup_alloc();
6025 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006026 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006027
Bob Liu3ed28fa2012-01-12 17:19:04 -08006028 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006029 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006030 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006031
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006032 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04006033 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006034 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006035 res_counter_init(&memcg->res, NULL);
6036 res_counter_init(&memcg->memsw, NULL);
6037 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006038 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006039
Glauber Costad142e3e2013-02-22 16:34:52 -08006040 memcg->last_scanned_node = MAX_NUMNODES;
6041 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08006042 memcg->move_charge_at_immigrate = 0;
6043 mutex_init(&memcg->thresholds_lock);
6044 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07006045 vmpressure_init(&memcg->vmpressure);
Michal Hocko7d910c02013-09-12 15:13:28 -07006046 spin_lock_init(&memcg->soft_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08006047
6048 return &memcg->css;
6049
6050free_out:
6051 __mem_cgroup_free(memcg);
6052 return ERR_PTR(error);
6053}
6054
6055static int
Tejun Heoeb954192013-08-08 20:11:23 -04006056mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08006057{
Tejun Heoeb954192013-08-08 20:11:23 -04006058 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6059 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08006060 int error = 0;
6061
Tejun Heo63876982013-08-08 20:11:23 -04006062 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08006063 return 0;
6064
Glauber Costa09998212013-02-22 16:34:55 -08006065 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006066
6067 memcg->use_hierarchy = parent->use_hierarchy;
6068 memcg->oom_kill_disable = parent->oom_kill_disable;
6069 memcg->swappiness = mem_cgroup_swappiness(parent);
6070
6071 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006072 res_counter_init(&memcg->res, &parent->res);
6073 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006074 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006075
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006076 /*
Li Zefan8d76a972013-07-08 16:00:36 -07006077 * No need to take a reference to the parent because cgroup
6078 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006079 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08006080 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006081 res_counter_init(&memcg->res, NULL);
6082 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006083 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006084 /*
6085 * Deeper hierachy with use_hierarchy == false doesn't make
6086 * much sense so let cgroup subsystem know about this
6087 * unfortunate state in our controller.
6088 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006089 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006090 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006091 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006092
6093 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006094 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006095 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006096}
6097
Michal Hocko5f578162013-04-29 15:07:17 -07006098/*
6099 * Announce all parents that a group from their hierarchy is gone.
6100 */
6101static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6102{
6103 struct mem_cgroup *parent = memcg;
6104
6105 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006106 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006107
6108 /*
6109 * if the root memcg is not hierarchical we have to check it
6110 * explicitely.
6111 */
6112 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006113 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006114}
6115
Tejun Heoeb954192013-08-08 20:11:23 -04006116static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006117{
Tejun Heoeb954192013-08-08 20:11:23 -04006118 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006119
Li Zefan10d5ebf2013-07-08 16:00:33 -07006120 kmem_cgroup_css_offline(memcg);
6121
Michal Hocko5f578162013-04-29 15:07:17 -07006122 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006123 mem_cgroup_reparent_charges(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -07006124 if (memcg->soft_contributed) {
6125 while ((memcg = parent_mem_cgroup(memcg)))
6126 atomic_dec(&memcg->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -07006127
6128 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
6129 atomic_dec(&root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -07006130 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08006131 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006132 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006133}
6134
Tejun Heoeb954192013-08-08 20:11:23 -04006135static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006136{
Tejun Heoeb954192013-08-08 20:11:23 -04006137 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006138
Li Zefan10d5ebf2013-07-08 16:00:33 -07006139 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006140 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006141}
6142
Daisuke Nishimura02491442010-03-10 15:22:17 -08006143#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006144/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006145#define PRECHARGE_COUNT_AT_ONCE 256
6146static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006147{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006148 int ret = 0;
6149 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006150 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006151
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006152 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006153 mc.precharge += count;
6154 /* we don't need css_get for root */
6155 return ret;
6156 }
6157 /* try to charge at once */
6158 if (count > 1) {
6159 struct res_counter *dummy;
6160 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006161 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006162 * by cgroup_lock_live_cgroup() that it is not removed and we
6163 * are still under the same cgroup_mutex. So we can postpone
6164 * css_get().
6165 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006166 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006167 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006168 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006169 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006170 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006171 goto one_by_one;
6172 }
6173 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006174 return ret;
6175 }
6176one_by_one:
6177 /* fall back to one by one charge */
6178 while (count--) {
6179 if (signal_pending(current)) {
6180 ret = -EINTR;
6181 break;
6182 }
6183 if (!batch_count--) {
6184 batch_count = PRECHARGE_COUNT_AT_ONCE;
6185 cond_resched();
6186 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006187 ret = __mem_cgroup_try_charge(NULL,
6188 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006189 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006190 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006191 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006192 mc.precharge++;
6193 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006194 return ret;
6195}
6196
6197/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006198 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006199 * @vma: the vma the pte to be checked belongs
6200 * @addr: the address corresponding to the pte to be checked
6201 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006202 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006203 *
6204 * Returns
6205 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6206 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6207 * move charge. if @target is not NULL, the page is stored in target->page
6208 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006209 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6210 * target for charge migration. if @target is not NULL, the entry is stored
6211 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006212 *
6213 * Called with pte lock held.
6214 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006215union mc_target {
6216 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006217 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006218};
6219
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006220enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006221 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006222 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006223 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006224};
6225
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006226static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6227 unsigned long addr, pte_t ptent)
6228{
6229 struct page *page = vm_normal_page(vma, addr, ptent);
6230
6231 if (!page || !page_mapped(page))
6232 return NULL;
6233 if (PageAnon(page)) {
6234 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006235 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006236 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006237 } else if (!move_file())
6238 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006239 return NULL;
6240 if (!get_page_unless_zero(page))
6241 return NULL;
6242
6243 return page;
6244}
6245
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006246#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006247static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6248 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6249{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006250 struct page *page = NULL;
6251 swp_entry_t ent = pte_to_swp_entry(ptent);
6252
6253 if (!move_anon() || non_swap_entry(ent))
6254 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006255 /*
6256 * Because lookup_swap_cache() updates some statistics counter,
6257 * we call find_get_page() with swapper_space directly.
6258 */
Shaohua Li33806f02013-02-22 16:34:37 -08006259 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006260 if (do_swap_account)
6261 entry->val = ent.val;
6262
6263 return page;
6264}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006265#else
6266static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6267 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6268{
6269 return NULL;
6270}
6271#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006272
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006273static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6274 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6275{
6276 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006277 struct address_space *mapping;
6278 pgoff_t pgoff;
6279
6280 if (!vma->vm_file) /* anonymous vma */
6281 return NULL;
6282 if (!move_file())
6283 return NULL;
6284
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006285 mapping = vma->vm_file->f_mapping;
6286 if (pte_none(ptent))
6287 pgoff = linear_page_index(vma, addr);
6288 else /* pte_file(ptent) is true */
6289 pgoff = pte_to_pgoff(ptent);
6290
6291 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006292 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006293
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006294#ifdef CONFIG_SWAP
6295 /* shmem/tmpfs may report page out on swap: account for that too. */
6296 if (radix_tree_exceptional_entry(page)) {
6297 swp_entry_t swap = radix_to_swp_entry(page);
6298 if (do_swap_account)
6299 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006300 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006301 }
6302#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006303 return page;
6304}
6305
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006306static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006307 unsigned long addr, pte_t ptent, union mc_target *target)
6308{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006309 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006310 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006311 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006312 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006313
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006314 if (pte_present(ptent))
6315 page = mc_handle_present_pte(vma, addr, ptent);
6316 else if (is_swap_pte(ptent))
6317 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006318 else if (pte_none(ptent) || pte_file(ptent))
6319 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006320
6321 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006322 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006323 if (page) {
6324 pc = lookup_page_cgroup(page);
6325 /*
6326 * Do only loose check w/o page_cgroup lock.
6327 * mem_cgroup_move_account() checks the pc is valid or not under
6328 * the lock.
6329 */
6330 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6331 ret = MC_TARGET_PAGE;
6332 if (target)
6333 target->page = page;
6334 }
6335 if (!ret || !target)
6336 put_page(page);
6337 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006338 /* There is a swap entry and a page doesn't exist or isn't charged */
6339 if (ent.val && !ret &&
Li Zefan34c00c32013-09-23 16:56:01 +08006340 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006341 ret = MC_TARGET_SWAP;
6342 if (target)
6343 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006344 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006345 return ret;
6346}
6347
Naoya Horiguchi12724852012-03-21 16:34:28 -07006348#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6349/*
6350 * We don't consider swapping or file mapped pages because THP does not
6351 * support them for now.
6352 * Caller should make sure that pmd_trans_huge(pmd) is true.
6353 */
6354static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6355 unsigned long addr, pmd_t pmd, union mc_target *target)
6356{
6357 struct page *page = NULL;
6358 struct page_cgroup *pc;
6359 enum mc_target_type ret = MC_TARGET_NONE;
6360
6361 page = pmd_page(pmd);
6362 VM_BUG_ON(!page || !PageHead(page));
6363 if (!move_anon())
6364 return ret;
6365 pc = lookup_page_cgroup(page);
6366 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6367 ret = MC_TARGET_PAGE;
6368 if (target) {
6369 get_page(page);
6370 target->page = page;
6371 }
6372 }
6373 return ret;
6374}
6375#else
6376static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6377 unsigned long addr, pmd_t pmd, union mc_target *target)
6378{
6379 return MC_TARGET_NONE;
6380}
6381#endif
6382
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006383static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6384 unsigned long addr, unsigned long end,
6385 struct mm_walk *walk)
6386{
6387 struct vm_area_struct *vma = walk->private;
6388 pte_t *pte;
6389 spinlock_t *ptl;
6390
Naoya Horiguchi12724852012-03-21 16:34:28 -07006391 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6392 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6393 mc.precharge += HPAGE_PMD_NR;
6394 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006395 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006396 }
Dave Hansen03319322011-03-22 16:32:56 -07006397
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006398 if (pmd_trans_unstable(pmd))
6399 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006400 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6401 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006402 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006403 mc.precharge++; /* increment precharge temporarily */
6404 pte_unmap_unlock(pte - 1, ptl);
6405 cond_resched();
6406
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006407 return 0;
6408}
6409
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006410static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6411{
6412 unsigned long precharge;
6413 struct vm_area_struct *vma;
6414
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006415 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006416 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6417 struct mm_walk mem_cgroup_count_precharge_walk = {
6418 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6419 .mm = mm,
6420 .private = vma,
6421 };
6422 if (is_vm_hugetlb_page(vma))
6423 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006424 walk_page_range(vma->vm_start, vma->vm_end,
6425 &mem_cgroup_count_precharge_walk);
6426 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006427 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006428
6429 precharge = mc.precharge;
6430 mc.precharge = 0;
6431
6432 return precharge;
6433}
6434
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6436{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006437 unsigned long precharge = mem_cgroup_count_precharge(mm);
6438
6439 VM_BUG_ON(mc.moving_task);
6440 mc.moving_task = current;
6441 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006442}
6443
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006444/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6445static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006446{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006447 struct mem_cgroup *from = mc.from;
6448 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006449 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006450
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006451 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006452 if (mc.precharge) {
6453 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6454 mc.precharge = 0;
6455 }
6456 /*
6457 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6458 * we must uncharge here.
6459 */
6460 if (mc.moved_charge) {
6461 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6462 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006463 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006464 /* we must fixup refcnts and charges */
6465 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006466 /* uncharge swap account from the old cgroup */
6467 if (!mem_cgroup_is_root(mc.from))
6468 res_counter_uncharge(&mc.from->memsw,
6469 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006470
6471 for (i = 0; i < mc.moved_swap; i++)
6472 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006473
6474 if (!mem_cgroup_is_root(mc.to)) {
6475 /*
6476 * we charged both to->res and to->memsw, so we should
6477 * uncharge to->res.
6478 */
6479 res_counter_uncharge(&mc.to->res,
6480 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006481 }
Li Zefan40503772013-07-08 16:00:34 -07006482 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006483 mc.moved_swap = 0;
6484 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006485 memcg_oom_recover(from);
6486 memcg_oom_recover(to);
6487 wake_up_all(&mc.waitq);
6488}
6489
6490static void mem_cgroup_clear_mc(void)
6491{
6492 struct mem_cgroup *from = mc.from;
6493
6494 /*
6495 * we must clear moving_task before waking up waiters at the end of
6496 * task migration.
6497 */
6498 mc.moving_task = NULL;
6499 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006500 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006501 mc.from = NULL;
6502 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006503 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006504 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006505}
6506
Tejun Heoeb954192013-08-08 20:11:23 -04006507static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006508 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006509{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006510 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006511 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006512 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006513 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006514
Glauber Costaee5e8472013-02-22 16:34:50 -08006515 /*
6516 * We are now commited to this value whatever it is. Changes in this
6517 * tunable will only affect upcoming migrations, not the current one.
6518 * So we need to save it, and keep it going.
6519 */
6520 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6521 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006522 struct mm_struct *mm;
6523 struct mem_cgroup *from = mem_cgroup_from_task(p);
6524
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006525 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006526
6527 mm = get_task_mm(p);
6528 if (!mm)
6529 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006530 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006531 if (mm->owner == p) {
6532 VM_BUG_ON(mc.from);
6533 VM_BUG_ON(mc.to);
6534 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006535 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006536 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006537 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006538 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006539 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006540 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006541 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006542 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006543 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006544
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006545 ret = mem_cgroup_precharge_mc(mm);
6546 if (ret)
6547 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006548 }
6549 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006550 }
6551 return ret;
6552}
6553
Tejun Heoeb954192013-08-08 20:11:23 -04006554static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006555 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006556{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006557 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006558}
6559
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006560static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6561 unsigned long addr, unsigned long end,
6562 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006563{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006564 int ret = 0;
6565 struct vm_area_struct *vma = walk->private;
6566 pte_t *pte;
6567 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006568 enum mc_target_type target_type;
6569 union mc_target target;
6570 struct page *page;
6571 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006572
Naoya Horiguchi12724852012-03-21 16:34:28 -07006573 /*
6574 * We don't take compound_lock() here but no race with splitting thp
6575 * happens because:
6576 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6577 * under splitting, which means there's no concurrent thp split,
6578 * - if another thread runs into split_huge_page() just after we
6579 * entered this if-block, the thread must wait for page table lock
6580 * to be unlocked in __split_huge_page_splitting(), where the main
6581 * part of thp split is not executed yet.
6582 */
6583 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006584 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006585 spin_unlock(&vma->vm_mm->page_table_lock);
6586 return 0;
6587 }
6588 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6589 if (target_type == MC_TARGET_PAGE) {
6590 page = target.page;
6591 if (!isolate_lru_page(page)) {
6592 pc = lookup_page_cgroup(page);
6593 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006594 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006595 mc.precharge -= HPAGE_PMD_NR;
6596 mc.moved_charge += HPAGE_PMD_NR;
6597 }
6598 putback_lru_page(page);
6599 }
6600 put_page(page);
6601 }
6602 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006603 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006604 }
6605
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006606 if (pmd_trans_unstable(pmd))
6607 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006608retry:
6609 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6610 for (; addr != end; addr += PAGE_SIZE) {
6611 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006612 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006613
6614 if (!mc.precharge)
6615 break;
6616
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006617 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006618 case MC_TARGET_PAGE:
6619 page = target.page;
6620 if (isolate_lru_page(page))
6621 goto put;
6622 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006623 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006624 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006625 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006626 /* we uncharge from mc.from later. */
6627 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006628 }
6629 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006630put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006631 put_page(page);
6632 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006633 case MC_TARGET_SWAP:
6634 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006635 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006636 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006637 /* we fixup refcnts and charges later. */
6638 mc.moved_swap++;
6639 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006640 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006641 default:
6642 break;
6643 }
6644 }
6645 pte_unmap_unlock(pte - 1, ptl);
6646 cond_resched();
6647
6648 if (addr != end) {
6649 /*
6650 * We have consumed all precharges we got in can_attach().
6651 * We try charge one by one, but don't do any additional
6652 * charges to mc.to if we have failed in charge once in attach()
6653 * phase.
6654 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006655 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006656 if (!ret)
6657 goto retry;
6658 }
6659
6660 return ret;
6661}
6662
6663static void mem_cgroup_move_charge(struct mm_struct *mm)
6664{
6665 struct vm_area_struct *vma;
6666
6667 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006668retry:
6669 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6670 /*
6671 * Someone who are holding the mmap_sem might be waiting in
6672 * waitq. So we cancel all extra charges, wake up all waiters,
6673 * and retry. Because we cancel precharges, we might not be able
6674 * to move enough charges, but moving charge is a best-effort
6675 * feature anyway, so it wouldn't be a big problem.
6676 */
6677 __mem_cgroup_clear_mc();
6678 cond_resched();
6679 goto retry;
6680 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006681 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6682 int ret;
6683 struct mm_walk mem_cgroup_move_charge_walk = {
6684 .pmd_entry = mem_cgroup_move_charge_pte_range,
6685 .mm = mm,
6686 .private = vma,
6687 };
6688 if (is_vm_hugetlb_page(vma))
6689 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006690 ret = walk_page_range(vma->vm_start, vma->vm_end,
6691 &mem_cgroup_move_charge_walk);
6692 if (ret)
6693 /*
6694 * means we have consumed all precharges and failed in
6695 * doing additional charge. Just abandon here.
6696 */
6697 break;
6698 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006699 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006700}
6701
Tejun Heoeb954192013-08-08 20:11:23 -04006702static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006703 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006704{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006705 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006706 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006707
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006708 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006709 if (mc.to)
6710 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006711 mmput(mm);
6712 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006713 if (mc.to)
6714 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006715}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006716#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006717static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006718 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006719{
6720 return 0;
6721}
Tejun Heoeb954192013-08-08 20:11:23 -04006722static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006723 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006724{
6725}
Tejun Heoeb954192013-08-08 20:11:23 -04006726static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006727 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006728{
6729}
6730#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006731
Tejun Heof00baae2013-04-15 13:41:15 -07006732/*
6733 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6734 * to verify sane_behavior flag on each mount attempt.
6735 */
Tejun Heoeb954192013-08-08 20:11:23 -04006736static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006737{
6738 /*
6739 * use_hierarchy is forced with sane_behavior. cgroup core
6740 * guarantees that @root doesn't have any children, so turning it
6741 * on for the root memcg is enough.
6742 */
Tejun Heoeb954192013-08-08 20:11:23 -04006743 if (cgroup_sane_behavior(root_css->cgroup))
6744 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006745}
6746
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006747struct cgroup_subsys mem_cgroup_subsys = {
6748 .name = "memory",
6749 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006750 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006751 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006752 .css_offline = mem_cgroup_css_offline,
6753 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006754 .can_attach = mem_cgroup_can_attach,
6755 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006756 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006757 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006758 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006759 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006760 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006761};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006762
Andrew Mortonc255a452012-07-31 16:43:02 -07006763#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006764static int __init enable_swap_account(char *s)
6765{
Michal Hockoa2c89902011-05-24 17:12:50 -07006766 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006767 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006768 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006769 really_do_swap_account = 0;
6770 return 1;
6771}
Michal Hockoa2c89902011-05-24 17:12:50 -07006772__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006773
Michal Hocko2d110852013-02-22 16:34:43 -08006774static void __init memsw_file_init(void)
6775{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006776 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006777}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006778
6779static void __init enable_swap_cgroup(void)
6780{
6781 if (!mem_cgroup_disabled() && really_do_swap_account) {
6782 do_swap_account = 1;
6783 memsw_file_init();
6784 }
6785}
6786
Michal Hocko2d110852013-02-22 16:34:43 -08006787#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006788static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006789{
6790}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006791#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006792
6793/*
Michal Hocko10813122013-02-22 16:35:41 -08006794 * subsys_initcall() for memory controller.
6795 *
6796 * Some parts like hotcpu_notifier() have to be initialized from this context
6797 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6798 * everything that doesn't depend on a specific mem_cgroup structure should
6799 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006800 */
6801static int __init mem_cgroup_init(void)
6802{
6803 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006804 enable_swap_cgroup();
Michal Hockoe4777492013-02-22 16:35:40 -08006805 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006806 return 0;
6807}
6808subsys_initcall(mem_cgroup_init);