blob: 04250cbf46c6112acbc07a7634bcb6a67b98cbbf [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
Balbir Singh8cdea7c2008-02-07 00:13:50 -080087/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080088 * Statistics for memory cgroup.
89 */
90enum mem_cgroup_stat_index {
91 /*
92 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
93 */
David Rientjesb070e652013-05-07 16:18:09 -070094 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
95 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
96 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
97 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
98 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080099 MEM_CGROUP_STAT_NSTATS,
100};
101
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700102static const char * const mem_cgroup_stat_names[] = {
103 "cache",
104 "rss",
David Rientjesb070e652013-05-07 16:18:09 -0700105 "rss_huge",
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700106 "mapped_file",
107 "swap",
108};
109
Johannes Weinere9f89742011-03-23 16:42:37 -0700110enum mem_cgroup_events_index {
111 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
112 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700113 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
114 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700115 MEM_CGROUP_EVENTS_NSTATS,
116};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700117
118static const char * const mem_cgroup_events_names[] = {
119 "pgpgin",
120 "pgpgout",
121 "pgfault",
122 "pgmajfault",
123};
124
Sha Zhengju58cf1882013-02-22 16:32:05 -0800125static const char * const mem_cgroup_lru_names[] = {
126 "inactive_anon",
127 "active_anon",
128 "inactive_file",
129 "active_file",
130 "unevictable",
131};
132
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133/*
134 * Per memcg event counter is incremented at every pagein/pageout. With THP,
135 * it will be incremated by the number of pages. This counter is used for
136 * for trigger some periodic events. This is straightforward and better
137 * than using jiffies etc. to handle periodic memcg event.
138 */
139enum mem_cgroup_events_target {
140 MEM_CGROUP_TARGET_THRESH,
Michal Hocko7d910c02013-09-12 15:13:28 -0700141 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700142 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700143 MEM_CGROUP_NTARGETS,
144};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700145#define THRESHOLDS_EVENTS_TARGET 128
146#define SOFTLIMIT_EVENTS_TARGET 1024
147#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700148
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800149struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700150 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700151 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700152 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700153 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800154};
155
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800156struct mem_cgroup_reclaim_iter {
Michal Hocko5f578162013-04-29 15:07:17 -0700157 /*
158 * last scanned hierarchy member. Valid only if last_dead_count
159 * matches memcg->dead_count of the hierarchy root group.
160 */
Michal Hocko542f85f2013-04-29 15:07:15 -0700161 struct mem_cgroup *last_visited;
Michal Hocko5f578162013-04-29 15:07:17 -0700162 unsigned long last_dead_count;
163
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800164 /* scan generation, increased every round-trip */
165 unsigned int generation;
166};
167
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800168/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800169 * per-zone information in memory controller.
170 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800171struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800172 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700173 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800174
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800175 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
176
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700177 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700178 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800179};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800180
181struct mem_cgroup_per_node {
182 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
183};
184
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800185struct mem_cgroup_threshold {
186 struct eventfd_ctx *eventfd;
187 u64 threshold;
188};
189
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700190/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800191struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700192 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700193 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800194 /* Size of entries[] */
195 unsigned int size;
196 /* Array of thresholds */
197 struct mem_cgroup_threshold entries[0];
198};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700199
200struct mem_cgroup_thresholds {
201 /* Primary thresholds array */
202 struct mem_cgroup_threshold_ary *primary;
203 /*
204 * Spare threshold array.
205 * This is needed to make mem_cgroup_unregister_event() "never fail".
206 * It must be able to store at least primary->size - 1 entries.
207 */
208 struct mem_cgroup_threshold_ary *spare;
209};
210
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700211/* for OOM */
212struct mem_cgroup_eventfd_list {
213 struct list_head list;
214 struct eventfd_ctx *eventfd;
215};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800216
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700217static void mem_cgroup_threshold(struct mem_cgroup *memcg);
218static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800219
Balbir Singhf64c3f52009-09-23 15:56:37 -0700220/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800221 * The memory controller data structure. The memory controller controls both
222 * page cache and RSS per cgroup. We would eventually like to provide
223 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
224 * to help the administrator determine what knobs to tune.
225 *
226 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800227 * we hit the water mark. May be even add a low water mark, such that
228 * no reclaim occurs from a cgroup at it's low water mark, this is
229 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800230 */
231struct mem_cgroup {
232 struct cgroup_subsys_state css;
233 /*
234 * the counter to account for memory usage
235 */
236 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700237
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700238 /* vmpressure notifications */
239 struct vmpressure vmpressure;
240
Li Zefan465939a2013-07-08 16:00:38 -0700241 /*
242 * the counter to account for mem+swap usage.
243 */
244 struct res_counter memsw;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700245
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800246 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800247 * the counter to account for kernel memory usage.
248 */
249 struct res_counter kmem;
250 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800251 * Should the accounting and control be hierarchical, per subtree?
252 */
253 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800254 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700255
256 bool oom_lock;
257 atomic_t under_oom;
258
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700259 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700260 /* OOM-Killer disable */
261 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800262
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700263 /* set when res.limit == memsw.limit */
264 bool memsw_is_minimum;
265
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800266 /* protect arrays of thresholds */
267 struct mutex thresholds_lock;
268
269 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700270 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700271
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800272 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700273 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700274
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700275 /* For oom notifier event fd */
276 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700277
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800278 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800279 * Should we move charges of a task when a task is moved into this
280 * mem_cgroup ? And what type of charges should we move ?
281 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700282 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800283 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700284 * set > 0 if pages under this cgroup are moving to other cgroup.
285 */
286 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700287 /* taken only while moving_account > 0 */
288 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700289 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800290 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800291 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700292 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700293 /*
294 * used when a cpu is offlined or other synchronizations
295 * See mem_cgroup_read_stat().
296 */
297 struct mem_cgroup_stat_cpu nocpu_base;
298 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000299
Michal Hocko5f578162013-04-29 15:07:17 -0700300 atomic_t dead_count;
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700301#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000302 struct tcp_memcontrol tcp_mem;
303#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800304#if defined(CONFIG_MEMCG_KMEM)
305 /* analogous to slab_common's slab_caches list. per-memcg */
306 struct list_head memcg_slab_caches;
307 /* Not a spinlock, we can take a lot of time walking the list */
308 struct mutex slab_caches_mutex;
309 /* Index in the kmem_cache->memcg_params->memcg_caches array */
310 int kmemcg_id;
311#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800312
313 int last_scanned_node;
314#if MAX_NUMNODES > 1
315 nodemask_t scan_nodes;
316 atomic_t numainfo_events;
317 atomic_t numainfo_updating;
318#endif
Michal Hocko7d910c02013-09-12 15:13:28 -0700319 /*
320 * Protects soft_contributed transitions.
321 * See mem_cgroup_update_soft_limit
322 */
323 spinlock_t soft_lock;
324
325 /*
326 * If true then this group has increased parents' children_in_excess
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700327 * when it got over the soft limit.
Michal Hocko7d910c02013-09-12 15:13:28 -0700328 * When a group falls bellow the soft limit, parents' children_in_excess
329 * is decreased and soft_contributed changed to false.
330 */
331 bool soft_contributed;
332
333 /* Number of children that are in soft limit excess */
334 atomic_t children_in_excess;
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700335
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700336 struct mem_cgroup_per_node *nodeinfo[0];
337 /* WARNING: nodeinfo must be the last member here */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800338};
339
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800340static size_t memcg_size(void)
341{
342 return sizeof(struct mem_cgroup) +
343 nr_node_ids * sizeof(struct mem_cgroup_per_node);
344}
345
Glauber Costa510fc4e2012-12-18 14:21:47 -0800346/* internal only representation about the status of kmem accounting. */
347enum {
348 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800349 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800350 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800351};
352
Glauber Costaa8964b92012-12-18 14:22:09 -0800353/* We account when limit is on, but only after call sites are patched */
354#define KMEM_ACCOUNTED_MASK \
355 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800356
357#ifdef CONFIG_MEMCG_KMEM
358static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
359{
360 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
361}
Glauber Costa7de37682012-12-18 14:22:07 -0800362
363static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
364{
365 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
366}
367
Glauber Costaa8964b92012-12-18 14:22:09 -0800368static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
369{
370 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
371}
372
Glauber Costa55007d82012-12-18 14:22:38 -0800373static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
374{
375 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
376}
377
Glauber Costa7de37682012-12-18 14:22:07 -0800378static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
379{
Li Zefan10d5ebf2013-07-08 16:00:33 -0700380 /*
381 * Our caller must use css_get() first, because memcg_uncharge_kmem()
382 * will call css_put() if it sees the memcg is dead.
383 */
384 smp_wmb();
Glauber Costa7de37682012-12-18 14:22:07 -0800385 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
386 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
387}
388
389static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
390{
391 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
392 &memcg->kmem_account_flags);
393}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800394#endif
395
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800396/* Stuffs for move charges at task migration. */
397/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800398 * Types of charges to be moved. "move_charge_at_immitgrate" and
399 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800400 */
401enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800402 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700403 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800404 NR_MOVE_TYPE,
405};
406
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800407/* "mc" and its members are protected by cgroup_mutex */
408static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800409 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800410 struct mem_cgroup *from;
411 struct mem_cgroup *to;
Glauber Costaee5e8472013-02-22 16:34:50 -0800412 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800413 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800414 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800415 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800416 struct task_struct *moving_task; /* a task moving charges */
417 wait_queue_head_t waitq; /* a waitq for other context */
418} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700419 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800420 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
421};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800422
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700423static bool move_anon(void)
424{
Glauber Costaee5e8472013-02-22 16:34:50 -0800425 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700426}
427
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700428static bool move_file(void)
429{
Glauber Costaee5e8472013-02-22 16:34:50 -0800430 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700431}
432
Balbir Singh4e416952009-09-23 15:56:39 -0700433/*
434 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
435 * limit reclaim to prevent infinite loops, if they ever occur.
436 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700437#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Balbir Singh4e416952009-09-23 15:56:39 -0700438
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800439enum charge_type {
440 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700441 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800442 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700443 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700444 NR_CHARGE_TYPE,
445};
446
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800447/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800448enum res_type {
449 _MEM,
450 _MEMSWAP,
451 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800452 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800453};
454
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700455#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
456#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800457#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700458/* Used for OOM nofiier */
459#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800460
Balbir Singh75822b42009-09-23 15:56:38 -0700461/*
462 * Reclaim flags for mem_cgroup_hierarchical_reclaim
463 */
464#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
465#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
466#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
467#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
468
Glauber Costa09998212013-02-22 16:34:55 -0800469/*
470 * The memcg_create_mutex will be held whenever a new cgroup is created.
471 * As a consequence, any change that needs to protect against new child cgroups
472 * appearing has to hold it as well.
473 */
474static DEFINE_MUTEX(memcg_create_mutex);
475
Wanpeng Lib2145142012-07-31 16:46:01 -0700476struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
477{
Tejun Heoa7c6d552013-08-08 20:11:23 -0400478 return s ? container_of(s, struct mem_cgroup, css) : NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -0700479}
480
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700481/* Some nice accessors for the vmpressure. */
482struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
483{
484 if (!memcg)
485 memcg = root_mem_cgroup;
486 return &memcg->vmpressure;
487}
488
489struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
490{
491 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
492}
493
494struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
495{
496 return &mem_cgroup_from_css(css)->vmpressure;
497}
498
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700499static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
500{
501 return (memcg == root_mem_cgroup);
502}
503
Glauber Costae1aab162011-12-11 21:47:03 +0000504/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700505#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000506
Glauber Costae1aab162011-12-11 21:47:03 +0000507void sock_update_memcg(struct sock *sk)
508{
Glauber Costa376be5f2012-01-20 04:57:14 +0000509 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000510 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700511 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000512
513 BUG_ON(!sk->sk_prot->proto_cgroup);
514
Glauber Costaf3f511e2012-01-05 20:16:39 +0000515 /* Socket cloning can throw us here with sk_cgrp already
516 * filled. It won't however, necessarily happen from
517 * process context. So the test for root memcg given
518 * the current task's memcg won't help us in this case.
519 *
520 * Respecting the original socket's memcg is a better
521 * decision in this case.
522 */
523 if (sk->sk_cgrp) {
524 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
Li Zefan5347e5a2013-07-08 16:00:30 -0700525 css_get(&sk->sk_cgrp->memcg->css);
Glauber Costaf3f511e2012-01-05 20:16:39 +0000526 return;
527 }
528
Glauber Costae1aab162011-12-11 21:47:03 +0000529 rcu_read_lock();
530 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700531 cg_proto = sk->sk_prot->proto_cgroup(memcg);
Li Zefan5347e5a2013-07-08 16:00:30 -0700532 if (!mem_cgroup_is_root(memcg) &&
533 memcg_proto_active(cg_proto) && css_tryget(&memcg->css)) {
Glauber Costa3f134612012-05-29 15:07:11 -0700534 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000535 }
536 rcu_read_unlock();
537 }
538}
539EXPORT_SYMBOL(sock_update_memcg);
540
541void sock_release_memcg(struct sock *sk)
542{
Glauber Costa376be5f2012-01-20 04:57:14 +0000543 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000544 struct mem_cgroup *memcg;
545 WARN_ON(!sk->sk_cgrp->memcg);
546 memcg = sk->sk_cgrp->memcg;
Li Zefan5347e5a2013-07-08 16:00:30 -0700547 css_put(&sk->sk_cgrp->memcg->css);
Glauber Costae1aab162011-12-11 21:47:03 +0000548 }
549}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000550
551struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
552{
553 if (!memcg || mem_cgroup_is_root(memcg))
554 return NULL;
555
556 return &memcg->tcp_mem.cg_proto;
557}
558EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000559
Glauber Costa3f134612012-05-29 15:07:11 -0700560static void disarm_sock_keys(struct mem_cgroup *memcg)
561{
562 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
563 return;
564 static_key_slow_dec(&memcg_socket_limit_enabled);
565}
566#else
567static void disarm_sock_keys(struct mem_cgroup *memcg)
568{
569}
570#endif
571
Glauber Costaa8964b92012-12-18 14:22:09 -0800572#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800573/*
574 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
575 * There are two main reasons for not using the css_id for this:
576 * 1) this works better in sparse environments, where we have a lot of memcgs,
577 * but only a few kmem-limited. Or also, if we have, for instance, 200
578 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
579 * 200 entry array for that.
580 *
581 * 2) In order not to violate the cgroup API, we would like to do all memory
582 * allocation in ->create(). At that point, we haven't yet allocated the
583 * css_id. Having a separate index prevents us from messing with the cgroup
584 * core for this
585 *
586 * The current size of the caches array is stored in
587 * memcg_limited_groups_array_size. It will double each time we have to
588 * increase it.
589 */
590static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800591int memcg_limited_groups_array_size;
592
Glauber Costa55007d82012-12-18 14:22:38 -0800593/*
594 * MIN_SIZE is different than 1, because we would like to avoid going through
595 * the alloc/free process all the time. In a small machine, 4 kmem-limited
596 * cgroups is a reasonable guess. In the future, it could be a parameter or
597 * tunable, but that is strictly not necessary.
598 *
599 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
600 * this constant directly from cgroup, but it is understandable that this is
601 * better kept as an internal representation in cgroup.c. In any case, the
602 * css_id space is not getting any smaller, and we don't have to necessarily
603 * increase ours as well if it increases.
604 */
605#define MEMCG_CACHES_MIN_SIZE 4
606#define MEMCG_CACHES_MAX_SIZE 65535
607
Glauber Costad7f25f82012-12-18 14:22:40 -0800608/*
609 * A lot of the calls to the cache allocation functions are expected to be
610 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
611 * conditional to this static branch, we'll have to allow modules that does
612 * kmem_cache_alloc and the such to see this symbol as well
613 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800614struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800615EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800616
617static void disarm_kmem_keys(struct mem_cgroup *memcg)
618{
Glauber Costa55007d82012-12-18 14:22:38 -0800619 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800620 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800621 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
622 }
Glauber Costabea207c2012-12-18 14:22:11 -0800623 /*
624 * This check can't live in kmem destruction function,
625 * since the charges will outlive the cgroup
626 */
627 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800628}
629#else
630static void disarm_kmem_keys(struct mem_cgroup *memcg)
631{
632}
633#endif /* CONFIG_MEMCG_KMEM */
634
635static void disarm_static_keys(struct mem_cgroup *memcg)
636{
637 disarm_sock_keys(memcg);
638 disarm_kmem_keys(memcg);
639}
640
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700641static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800642
Balbir Singhf64c3f52009-09-23 15:56:37 -0700643static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700644mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700645{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800646 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Johannes Weiner54f72fe2013-07-08 15:59:49 -0700647 return &memcg->nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700648}
649
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700650struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100651{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100653}
654
Balbir Singhf64c3f52009-09-23 15:56:37 -0700655static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700656page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700657{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700658 int nid = page_to_nid(page);
659 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700661 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700662}
663
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700664/*
665 * Implementation Note: reading percpu statistics for memcg.
666 *
667 * Both of vmstat[] and percpu_counter has threshold and do periodic
668 * synchronization to implement "quick" read. There are trade-off between
669 * reading cost and precision of value. Then, we may have a chance to implement
670 * a periodic synchronizion of counter in memcg's counter.
671 *
672 * But this _read() function is used for user interface now. The user accounts
673 * memory usage by memory cgroup and he _always_ requires exact value because
674 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
675 * have to visit all online cpus and make sum. So, for now, unnecessary
676 * synchronization is not implemented. (just implemented for cpu hotplug)
677 *
678 * If there are kernel internal actions which can make use of some not-exact
679 * value, and reading all cpu value can be performance bottleneck in some
680 * common workload, threashold and synchonization as vmstat[] should be
681 * implemented.
682 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700683static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700684 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800685{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700686 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800687 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800688
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700689 get_online_cpus();
690 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700691 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700692#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700693 spin_lock(&memcg->pcp_counter_lock);
694 val += memcg->nocpu_base.count[idx];
695 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700696#endif
697 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800698 return val;
699}
700
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700701static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700702 bool charge)
703{
704 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700705 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700706}
707
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700708static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700709 enum mem_cgroup_events_index idx)
710{
711 unsigned long val = 0;
712 int cpu;
713
714 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700715 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700716#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700717 spin_lock(&memcg->pcp_counter_lock);
718 val += memcg->nocpu_base.events[idx];
719 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700720#endif
721 return val;
722}
723
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700724static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700725 struct page *page,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700726 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800727{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800728 preempt_disable();
729
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700730 /*
731 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
732 * counted as CACHE even if it's on ANON LRU.
733 */
734 if (anon)
735 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700736 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800737 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700738 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700739 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700740
David Rientjesb070e652013-05-07 16:18:09 -0700741 if (PageTransHuge(page))
742 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
743 nr_pages);
744
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800745 /* pagein of a big page is an event. So, ignore page size */
746 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700747 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800748 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700749 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800750 nr_pages = -nr_pages; /* for event */
751 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800752
Johannes Weiner13114712012-05-29 15:07:07 -0700753 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800754
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800755 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800756}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800757
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700758unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700759mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700760{
761 struct mem_cgroup_per_zone *mz;
762
763 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
764 return mz->lru_size[lru];
765}
766
767static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700769 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700770{
771 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700772 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700773 unsigned long ret = 0;
774
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700776
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700777 for_each_lru(lru) {
778 if (BIT(lru) & lru_mask)
779 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700780 }
781 return ret;
782}
783
784static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700785mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700786 int nid, unsigned int lru_mask)
787{
Ying Han889976d2011-05-26 16:25:33 -0700788 u64 total = 0;
789 int zid;
790
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700791 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700792 total += mem_cgroup_zone_nr_lru_pages(memcg,
793 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700794
Ying Han889976d2011-05-26 16:25:33 -0700795 return total;
796}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700797
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700798static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700799 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800800{
Ying Han889976d2011-05-26 16:25:33 -0700801 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800802 u64 total = 0;
803
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800804 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700805 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800806 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800807}
808
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800809static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
810 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800811{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700812 unsigned long val, next;
813
Johannes Weiner13114712012-05-29 15:07:07 -0700814 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700815 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700816 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800817 if ((long)next - (long)val < 0) {
818 switch (target) {
819 case MEM_CGROUP_TARGET_THRESH:
820 next = val + THRESHOLDS_EVENTS_TARGET;
821 break;
Michal Hocko7d910c02013-09-12 15:13:28 -0700822 case MEM_CGROUP_TARGET_SOFTLIMIT:
823 next = val + SOFTLIMIT_EVENTS_TARGET;
824 break;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800825 case MEM_CGROUP_TARGET_NUMAINFO:
826 next = val + NUMAINFO_EVENTS_TARGET;
827 break;
828 default:
829 break;
830 }
831 __this_cpu_write(memcg->stat->targets[target], next);
832 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700833 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800834 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800835}
836
837/*
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700838 * Called from rate-limited memcg_check_events when enough
Michal Hocko7d910c02013-09-12 15:13:28 -0700839 * MEM_CGROUP_TARGET_SOFTLIMIT events are accumulated and it makes sure
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700840 * that all the parents up the hierarchy will be notified that this group
Michal Hocko7d910c02013-09-12 15:13:28 -0700841 * is in excess or that it is not in excess anymore. mmecg->soft_contributed
842 * makes the transition a single action whenever the state flips from one to
Andrew Mortonf894ffa2013-09-12 15:13:35 -0700843 * the other.
Michal Hocko7d910c02013-09-12 15:13:28 -0700844 */
845static void mem_cgroup_update_soft_limit(struct mem_cgroup *memcg)
846{
847 unsigned long long excess = res_counter_soft_limit_excess(&memcg->res);
848 struct mem_cgroup *parent = memcg;
849 int delta = 0;
850
851 spin_lock(&memcg->soft_lock);
852 if (excess) {
853 if (!memcg->soft_contributed) {
854 delta = 1;
855 memcg->soft_contributed = true;
856 }
857 } else {
858 if (memcg->soft_contributed) {
859 delta = -1;
860 memcg->soft_contributed = false;
861 }
862 }
863
864 /*
865 * Necessary to update all ancestors when hierarchy is used
866 * because their event counter is not touched.
Michal Hocko1be171d2013-09-12 15:13:32 -0700867 * We track children even outside the hierarchy for the root
868 * cgroup because tree walk starting at root should visit
869 * all cgroups and we want to prevent from pointless tree
870 * walk if no children is below the limit.
Michal Hocko7d910c02013-09-12 15:13:28 -0700871 */
872 while (delta && (parent = parent_mem_cgroup(parent)))
873 atomic_add(delta, &parent->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -0700874 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
875 atomic_add(delta, &root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -0700876 spin_unlock(&memcg->soft_lock);
877}
878
879/*
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800880 * Check events in order.
881 *
882 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800884{
Steven Rostedt47994012011-11-02 13:38:33 -0700885 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800886 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800887 if (unlikely(mem_cgroup_event_ratelimit(memcg,
888 MEM_CGROUP_TARGET_THRESH))) {
Michal Hocko7d910c02013-09-12 15:13:28 -0700889 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800890 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800891
Michal Hocko7d910c02013-09-12 15:13:28 -0700892 do_softlimit = mem_cgroup_event_ratelimit(memcg,
893 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700894#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800895 do_numainfo = mem_cgroup_event_ratelimit(memcg,
896 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700897#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800898 preempt_enable();
899
900 mem_cgroup_threshold(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -0700901 if (unlikely(do_softlimit))
902 mem_cgroup_update_soft_limit(memcg);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800903#if MAX_NUMNODES > 1
904 if (unlikely(do_numainfo))
905 atomic_inc(&memcg->numainfo_events);
906#endif
907 } else
908 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800909}
910
Balbir Singhcf475ad2008-04-29 01:00:16 -0700911struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800912{
Balbir Singh31a78f22008-09-28 23:09:31 +0100913 /*
914 * mm_update_next_owner() may clear mm->owner to NULL
915 * if it races with swapoff, page migration, etc.
916 * So this can be called with p == NULL.
917 */
918 if (unlikely(!p))
919 return NULL;
920
Tejun Heo8af01f52013-08-08 20:11:22 -0400921 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800922}
923
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700924struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800925{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700926 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700927
928 if (!mm)
929 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800930 /*
931 * Because we have no locks, mm->owner's may be being moved to other
932 * cgroup. We use css_tryget() here even if this looks
933 * pessimistic (rather than adding locks here).
934 */
935 rcu_read_lock();
936 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700937 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
938 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800939 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700940 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800941 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700942 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800943}
944
Michal Hockode577802013-09-12 15:13:26 -0700945static enum mem_cgroup_filter_t
946mem_cgroup_filter(struct mem_cgroup *memcg, struct mem_cgroup *root,
947 mem_cgroup_iter_filter cond)
948{
949 if (!cond)
950 return VISIT;
951 return cond(memcg, root);
952}
953
Michal Hocko16248d82013-04-29 15:07:19 -0700954/*
955 * Returns a next (in a pre-order walk) alive memcg (with elevated css
956 * ref. count) or NULL if the whole root's subtree has been visited.
957 *
958 * helper function to be used by mem_cgroup_iter
959 */
960static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Michal Hockode577802013-09-12 15:13:26 -0700961 struct mem_cgroup *last_visited, mem_cgroup_iter_filter cond)
Michal Hocko16248d82013-04-29 15:07:19 -0700962{
Tejun Heo492eb212013-08-08 20:11:25 -0400963 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700964
Tejun Heobd8815a2013-08-08 20:11:27 -0400965 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -0700966skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -0400967 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -0700968
969 /*
970 * Even if we found a group we have to make sure it is
971 * alive. css && !memcg means that the groups should be
972 * skipped and we should continue the tree walk.
973 * last_visited css is safe to use because it is
974 * protected by css_get and the tree walk is rcu safe.
975 */
Tejun Heo492eb212013-08-08 20:11:25 -0400976 if (next_css) {
977 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
978
Michal Hockode577802013-09-12 15:13:26 -0700979 switch (mem_cgroup_filter(mem, root, cond)) {
980 case SKIP:
Tejun Heo492eb212013-08-08 20:11:25 -0400981 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700982 goto skip_node;
Michal Hockode577802013-09-12 15:13:26 -0700983 case SKIP_TREE:
984 if (mem == root)
985 return NULL;
986 /*
987 * css_rightmost_descendant is not an optimal way to
988 * skip through a subtree (especially for imbalanced
989 * trees leaning to right) but that's what we have right
990 * now. More effective solution would be traversing
991 * right-up for first non-NULL without calling
992 * css_next_descendant_pre afterwards.
993 */
994 prev_css = css_rightmost_descendant(next_css);
995 goto skip_node;
996 case VISIT:
997 if (css_tryget(&mem->css))
998 return mem;
999 else {
1000 prev_css = next_css;
1001 goto skip_node;
1002 }
1003 break;
Michal Hocko16248d82013-04-29 15:07:19 -07001004 }
1005 }
1006
1007 return NULL;
1008}
1009
Johannes Weiner519ebea2013-07-03 15:04:51 -07001010static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1011{
1012 /*
1013 * When a group in the hierarchy below root is destroyed, the
1014 * hierarchy iterator can no longer be trusted since it might
1015 * have pointed to the destroyed group. Invalidate it.
1016 */
1017 atomic_inc(&root->dead_count);
1018}
1019
1020static struct mem_cgroup *
1021mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1022 struct mem_cgroup *root,
1023 int *sequence)
1024{
1025 struct mem_cgroup *position = NULL;
1026 /*
1027 * A cgroup destruction happens in two stages: offlining and
1028 * release. They are separated by a RCU grace period.
1029 *
1030 * If the iterator is valid, we may still race with an
1031 * offlining. The RCU lock ensures the object won't be
1032 * released, tryget will fail if we lost the race.
1033 */
1034 *sequence = atomic_read(&root->dead_count);
1035 if (iter->last_dead_count == *sequence) {
1036 smp_rmb();
1037 position = iter->last_visited;
1038 if (position && !css_tryget(&position->css))
1039 position = NULL;
1040 }
1041 return position;
1042}
1043
1044static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1045 struct mem_cgroup *last_visited,
1046 struct mem_cgroup *new_position,
1047 int sequence)
1048{
1049 if (last_visited)
1050 css_put(&last_visited->css);
1051 /*
1052 * We store the sequence count from the time @last_visited was
1053 * loaded successfully instead of rereading it here so that we
1054 * don't lose destruction events in between. We could have
1055 * raced with the destruction of @new_position after all.
1056 */
1057 iter->last_visited = new_position;
1058 smp_wmb();
1059 iter->last_dead_count = sequence;
1060}
1061
Johannes Weiner56600482012-01-12 17:17:59 -08001062/**
1063 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1064 * @root: hierarchy root
1065 * @prev: previously returned memcg, NULL on first invocation
1066 * @reclaim: cookie for shared reclaim walks, NULL for full walks
Michal Hockode577802013-09-12 15:13:26 -07001067 * @cond: filter for visited nodes, NULL for no filter
Johannes Weiner56600482012-01-12 17:17:59 -08001068 *
1069 * Returns references to children of the hierarchy below @root, or
1070 * @root itself, or %NULL after a full round-trip.
1071 *
1072 * Caller must pass the return value in @prev on subsequent
1073 * invocations for reference counting, or use mem_cgroup_iter_break()
1074 * to cancel a hierarchy walk before the round-trip is complete.
1075 *
1076 * Reclaimers can specify a zone and a priority level in @reclaim to
1077 * divide up the memcgs in the hierarchy among all concurrent
1078 * reclaimers operating on the same zone and priority.
1079 */
Michal Hockode577802013-09-12 15:13:26 -07001080struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001081 struct mem_cgroup *prev,
Michal Hockode577802013-09-12 15:13:26 -07001082 struct mem_cgroup_reclaim_cookie *reclaim,
1083 mem_cgroup_iter_filter cond)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001084{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001085 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001086 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001087
Michal Hockode577802013-09-12 15:13:26 -07001088 if (mem_cgroup_disabled()) {
1089 /* first call must return non-NULL, second return NULL */
1090 return (struct mem_cgroup *)(unsigned long)!prev;
1091 }
Johannes Weiner56600482012-01-12 17:17:59 -08001092
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001093 if (!root)
1094 root = root_mem_cgroup;
1095
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001096 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001097 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001098
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001099 if (!root->use_hierarchy && root != root_mem_cgroup) {
1100 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001101 goto out_css_put;
Michal Hockode577802013-09-12 15:13:26 -07001102 if (mem_cgroup_filter(root, root, cond) == VISIT)
1103 return root;
1104 return NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001105 }
1106
Michal Hocko542f85f2013-04-29 15:07:15 -07001107 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001108 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001109 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001110 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001111
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001112 if (reclaim) {
1113 int nid = zone_to_nid(reclaim->zone);
1114 int zid = zone_idx(reclaim->zone);
1115 struct mem_cgroup_per_zone *mz;
1116
1117 mz = mem_cgroup_zoneinfo(root, nid, zid);
1118 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001119 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001120 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001121 goto out_unlock;
1122 }
Michal Hocko5f578162013-04-29 15:07:17 -07001123
Johannes Weiner519ebea2013-07-03 15:04:51 -07001124 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001125 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126
Michal Hockode577802013-09-12 15:13:26 -07001127 memcg = __mem_cgroup_iter_next(root, last_visited, cond);
Michal Hocko542f85f2013-04-29 15:07:15 -07001128
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001129 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001130 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001131
Michal Hocko19f39402013-04-29 15:07:18 -07001132 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001133 iter->generation++;
1134 else if (!prev && memcg)
1135 reclaim->generation = iter->generation;
1136 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001137
Michal Hockode577802013-09-12 15:13:26 -07001138 /*
1139 * We have finished the whole tree walk or no group has been
1140 * visited because filter told us to skip the root node.
1141 */
1142 if (!memcg && (prev || (cond && !last_visited)))
Michal Hocko542f85f2013-04-29 15:07:15 -07001143 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001144 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001145out_unlock:
1146 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001147out_css_put:
1148 if (prev && prev != root)
1149 css_put(&prev->css);
1150
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001151 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001152}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001153
Johannes Weiner56600482012-01-12 17:17:59 -08001154/**
1155 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1156 * @root: hierarchy root
1157 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1158 */
1159void mem_cgroup_iter_break(struct mem_cgroup *root,
1160 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001161{
1162 if (!root)
1163 root = root_mem_cgroup;
1164 if (prev && prev != root)
1165 css_put(&prev->css);
1166}
1167
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001168/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001169 * Iteration constructs for visiting all cgroups (under a tree). If
1170 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1171 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001172 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001173#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001174 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001175 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001176 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001177
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001178#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001179 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001180 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001181 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001182
David Rientjes68ae5642012-12-12 13:51:57 -08001183void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001184{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001185 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001186
Ying Han456f9982011-05-26 16:25:38 -07001187 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001188 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1189 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001190 goto out;
1191
1192 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001193 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001194 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1195 break;
1196 case PGMAJFAULT:
1197 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001198 break;
1199 default:
1200 BUG();
1201 }
1202out:
1203 rcu_read_unlock();
1204}
David Rientjes68ae5642012-12-12 13:51:57 -08001205EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001206
Johannes Weiner925b7672012-01-12 17:18:15 -08001207/**
1208 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1209 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001210 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001211 *
1212 * Returns the lru list vector holding pages for the given @zone and
1213 * @mem. This can be the global zone lruvec, if the memory controller
1214 * is disabled.
1215 */
1216struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1217 struct mem_cgroup *memcg)
1218{
1219 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001220 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001221
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001222 if (mem_cgroup_disabled()) {
1223 lruvec = &zone->lruvec;
1224 goto out;
1225 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001226
1227 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001228 lruvec = &mz->lruvec;
1229out:
1230 /*
1231 * Since a node can be onlined after the mem_cgroup was created,
1232 * we have to be prepared to initialize lruvec->zone here;
1233 * and if offlined then reonlined, we need to reinitialize it.
1234 */
1235 if (unlikely(lruvec->zone != zone))
1236 lruvec->zone = zone;
1237 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001238}
1239
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001240/*
1241 * Following LRU functions are allowed to be used without PCG_LOCK.
1242 * Operations are called by routine of global LRU independently from memcg.
1243 * What we have to take care of here is validness of pc->mem_cgroup.
1244 *
1245 * Changes to pc->mem_cgroup happens when
1246 * 1. charge
1247 * 2. moving account
1248 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1249 * It is added to LRU before charge.
1250 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1251 * When moving account, the page is not on LRU. It's isolated.
1252 */
1253
Johannes Weiner925b7672012-01-12 17:18:15 -08001254/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001255 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001256 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001257 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001258 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001259struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001260{
1261 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001262 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001263 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001264 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001265
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001266 if (mem_cgroup_disabled()) {
1267 lruvec = &zone->lruvec;
1268 goto out;
1269 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001270
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001271 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001272 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001273
1274 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001275 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001276 * an uncharged page off lru does nothing to secure
1277 * its former mem_cgroup from sudden removal.
1278 *
1279 * Our caller holds lru_lock, and PageCgroupUsed is updated
1280 * under page_cgroup lock: between them, they make all uses
1281 * of pc->mem_cgroup safe.
1282 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001283 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001284 pc->mem_cgroup = memcg = root_mem_cgroup;
1285
Johannes Weiner925b7672012-01-12 17:18:15 -08001286 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001287 lruvec = &mz->lruvec;
1288out:
1289 /*
1290 * Since a node can be onlined after the mem_cgroup was created,
1291 * we have to be prepared to initialize lruvec->zone here;
1292 * and if offlined then reonlined, we need to reinitialize it.
1293 */
1294 if (unlikely(lruvec->zone != zone))
1295 lruvec->zone = zone;
1296 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001297}
1298
1299/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001300 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1301 * @lruvec: mem_cgroup per zone lru vector
1302 * @lru: index of lru list the page is sitting on
1303 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001304 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001305 * This function must be called when a page is added to or removed from an
1306 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001307 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001308void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1309 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001310{
1311 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001312 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001313
1314 if (mem_cgroup_disabled())
1315 return;
1316
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001317 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1318 lru_size = mz->lru_size + lru;
1319 *lru_size += nr_pages;
1320 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001321}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001322
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001323/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001324 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001325 * hierarchy subtree
1326 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001327bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1328 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001329{
Johannes Weiner91c637342012-05-29 15:06:24 -07001330 if (root_memcg == memcg)
1331 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001332 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001333 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001334 return css_is_ancestor(&memcg->css, &root_memcg->css);
1335}
1336
1337static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1338 struct mem_cgroup *memcg)
1339{
1340 bool ret;
1341
Johannes Weiner91c637342012-05-29 15:06:24 -07001342 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001343 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001344 rcu_read_unlock();
1345 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001346}
1347
David Rientjesffbdccf2013-07-03 15:01:23 -07001348bool task_in_mem_cgroup(struct task_struct *task,
1349 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001350{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001351 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001352 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001353 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001354
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001355 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001356 if (p) {
1357 curr = try_get_mem_cgroup_from_mm(p->mm);
1358 task_unlock(p);
1359 } else {
1360 /*
1361 * All threads may have already detached their mm's, but the oom
1362 * killer still needs to detect if they have already been oom
1363 * killed to prevent needlessly killing additional tasks.
1364 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001365 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001366 curr = mem_cgroup_from_task(task);
1367 if (curr)
1368 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001369 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001370 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001371 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001372 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001373 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001374 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001375 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001376 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1377 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001378 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001379 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001380 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001381 return ret;
1382}
1383
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001384int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001385{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001386 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001387 unsigned long inactive;
1388 unsigned long active;
1389 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001390
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001391 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1392 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001393
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001394 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1395 if (gb)
1396 inactive_ratio = int_sqrt(10 * gb);
1397 else
1398 inactive_ratio = 1;
1399
Johannes Weiner9b272972011-11-02 13:38:23 -07001400 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001401}
1402
Balbir Singh6d61ef42009-01-07 18:08:06 -08001403#define mem_cgroup_from_res_counter(counter, member) \
1404 container_of(counter, struct mem_cgroup, member)
1405
Johannes Weiner19942822011-02-01 15:52:43 -08001406/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001407 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001408 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001409 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001410 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001411 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001412 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001413static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001414{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001415 unsigned long long margin;
1416
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001418 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001419 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001420 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001421}
1422
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001423int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001424{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001425 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001426 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001427 return vm_swappiness;
1428
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001429 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001430}
1431
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001432/*
1433 * memcg->moving_account is used for checking possibility that some thread is
1434 * calling move_account(). When a thread on CPU-A starts moving pages under
1435 * a memcg, other threads should check memcg->moving_account under
1436 * rcu_read_lock(), like this:
1437 *
1438 * CPU-A CPU-B
1439 * rcu_read_lock()
1440 * memcg->moving_account+1 if (memcg->mocing_account)
1441 * take heavy locks.
1442 * synchronize_rcu() update something.
1443 * rcu_read_unlock()
1444 * start move here.
1445 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001446
1447/* for quick checking without looking up memcg */
1448atomic_t memcg_moving __read_mostly;
1449
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001450static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001451{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001452 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001453 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001454 synchronize_rcu();
1455}
1456
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001457static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001458{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001459 /*
1460 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1461 * We check NULL in callee rather than caller.
1462 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001463 if (memcg) {
1464 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001465 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001466 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001467}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001468
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001469/*
1470 * 2 routines for checking "mem" is under move_account() or not.
1471 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001472 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1473 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001474 * pc->mem_cgroup may be overwritten.
1475 *
1476 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1477 * under hierarchy of moving cgroups. This is for
1478 * waiting at hith-memory prressure caused by "move".
1479 */
1480
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001481static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001482{
1483 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001484 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001485}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001486
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001487static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001488{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001489 struct mem_cgroup *from;
1490 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001491 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001492 /*
1493 * Unlike task_move routines, we access mc.to, mc.from not under
1494 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1495 */
1496 spin_lock(&mc.lock);
1497 from = mc.from;
1498 to = mc.to;
1499 if (!from)
1500 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001501
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001502 ret = mem_cgroup_same_or_subtree(memcg, from)
1503 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001504unlock:
1505 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001506 return ret;
1507}
1508
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001509static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001510{
1511 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001512 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001513 DEFINE_WAIT(wait);
1514 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1515 /* moving charge context might have finished. */
1516 if (mc.moving_task)
1517 schedule();
1518 finish_wait(&mc.waitq, &wait);
1519 return true;
1520 }
1521 }
1522 return false;
1523}
1524
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001525/*
1526 * Take this lock when
1527 * - a code tries to modify page's memcg while it's USED.
1528 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001529 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001530 */
1531static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1532 unsigned long *flags)
1533{
1534 spin_lock_irqsave(&memcg->move_lock, *flags);
1535}
1536
1537static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1538 unsigned long *flags)
1539{
1540 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1541}
1542
Sha Zhengju58cf1882013-02-22 16:32:05 -08001543#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001544/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001545 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001546 * @memcg: The memory cgroup that went over limit
1547 * @p: Task that is going to be killed
1548 *
1549 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1550 * enabled
1551 */
1552void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1553{
1554 struct cgroup *task_cgrp;
1555 struct cgroup *mem_cgrp;
1556 /*
1557 * Need a buffer in BSS, can't rely on allocations. The code relies
1558 * on the assumption that OOM is serialized for memory controller.
1559 * If this assumption is broken, revisit this code.
1560 */
1561 static char memcg_name[PATH_MAX];
1562 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001563 struct mem_cgroup *iter;
1564 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001565
Sha Zhengju58cf1882013-02-22 16:32:05 -08001566 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001567 return;
1568
Balbir Singhe2224322009-04-02 16:57:39 -07001569 rcu_read_lock();
1570
1571 mem_cgrp = memcg->css.cgroup;
1572 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1573
1574 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1575 if (ret < 0) {
1576 /*
1577 * Unfortunately, we are unable to convert to a useful name
1578 * But we'll still print out the usage information
1579 */
1580 rcu_read_unlock();
1581 goto done;
1582 }
1583 rcu_read_unlock();
1584
Andrew Mortond0451972013-02-22 16:32:06 -08001585 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001586
1587 rcu_read_lock();
1588 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1589 if (ret < 0) {
1590 rcu_read_unlock();
1591 goto done;
1592 }
1593 rcu_read_unlock();
1594
1595 /*
1596 * Continues from above, so we don't need an KERN_ level
1597 */
Andrew Mortond0451972013-02-22 16:32:06 -08001598 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001599done:
1600
Andrew Mortond0451972013-02-22 16:32:06 -08001601 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001602 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1603 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1604 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001605 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001606 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1607 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1608 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001609 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001610 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1611 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1612 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001613
1614 for_each_mem_cgroup_tree(iter, memcg) {
1615 pr_info("Memory cgroup stats");
1616
1617 rcu_read_lock();
1618 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1619 if (!ret)
1620 pr_cont(" for %s", memcg_name);
1621 rcu_read_unlock();
1622 pr_cont(":");
1623
1624 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1625 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1626 continue;
1627 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1628 K(mem_cgroup_read_stat(iter, i)));
1629 }
1630
1631 for (i = 0; i < NR_LRU_LISTS; i++)
1632 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1633 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1634
1635 pr_cont("\n");
1636 }
Balbir Singhe2224322009-04-02 16:57:39 -07001637}
1638
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001639/*
1640 * This function returns the number of memcg under hierarchy tree. Returns
1641 * 1(self count) if no children.
1642 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001643static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001644{
1645 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001646 struct mem_cgroup *iter;
1647
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001648 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001649 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001650 return num;
1651}
1652
Balbir Singh6d61ef42009-01-07 18:08:06 -08001653/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001654 * Return the memory (and swap, if configured) limit for a memcg.
1655 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001656static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001657{
1658 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001659
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001660 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001661
David Rientjesa63d83f2010-08-09 17:19:46 -07001662 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001663 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001664 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001665 if (mem_cgroup_swappiness(memcg)) {
1666 u64 memsw;
1667
1668 limit += total_swap_pages << PAGE_SHIFT;
1669 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1670
1671 /*
1672 * If memsw is finite and limits the amount of swap space
1673 * available to this memcg, return that limit.
1674 */
1675 limit = min(limit, memsw);
1676 }
1677
1678 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001679}
1680
David Rientjes19965462012-12-11 16:00:26 -08001681static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1682 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001683{
1684 struct mem_cgroup *iter;
1685 unsigned long chosen_points = 0;
1686 unsigned long totalpages;
1687 unsigned int points = 0;
1688 struct task_struct *chosen = NULL;
1689
David Rientjes876aafb2012-07-31 16:43:48 -07001690 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001691 * If current has a pending SIGKILL or is exiting, then automatically
1692 * select it. The goal is to allow it to allocate so that it may
1693 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001694 */
David Rientjes465adcf2013-04-29 15:08:45 -07001695 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001696 set_thread_flag(TIF_MEMDIE);
1697 return;
1698 }
1699
1700 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001701 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1702 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001703 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001704 struct task_struct *task;
1705
Tejun Heo72ec7022013-08-08 20:11:26 -04001706 css_task_iter_start(&iter->css, &it);
1707 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001708 switch (oom_scan_process_thread(task, totalpages, NULL,
1709 false)) {
1710 case OOM_SCAN_SELECT:
1711 if (chosen)
1712 put_task_struct(chosen);
1713 chosen = task;
1714 chosen_points = ULONG_MAX;
1715 get_task_struct(chosen);
1716 /* fall through */
1717 case OOM_SCAN_CONTINUE:
1718 continue;
1719 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001720 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001721 mem_cgroup_iter_break(memcg, iter);
1722 if (chosen)
1723 put_task_struct(chosen);
1724 return;
1725 case OOM_SCAN_OK:
1726 break;
1727 };
1728 points = oom_badness(task, memcg, NULL, totalpages);
1729 if (points > chosen_points) {
1730 if (chosen)
1731 put_task_struct(chosen);
1732 chosen = task;
1733 chosen_points = points;
1734 get_task_struct(chosen);
1735 }
1736 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001737 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001738 }
1739
1740 if (!chosen)
1741 return;
1742 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001743 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1744 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001745}
1746
Johannes Weiner56600482012-01-12 17:17:59 -08001747static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1748 gfp_t gfp_mask,
1749 unsigned long flags)
1750{
1751 unsigned long total = 0;
1752 bool noswap = false;
1753 int loop;
1754
1755 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1756 noswap = true;
1757 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1758 noswap = true;
1759
1760 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1761 if (loop)
1762 drain_all_stock_async(memcg);
1763 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1764 /*
1765 * Allow limit shrinkers, which are triggered directly
1766 * by userspace, to catch signals and stop reclaim
1767 * after minimal progress, regardless of the margin.
1768 */
1769 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1770 break;
1771 if (mem_cgroup_margin(memcg))
1772 break;
1773 /*
1774 * If nothing was reclaimed after two attempts, there
1775 * may be no reclaimable pages in this hierarchy.
1776 */
1777 if (loop && !total)
1778 break;
1779 }
1780 return total;
1781}
1782
Michal Hockoe8831102013-09-12 15:13:23 -07001783#if MAX_NUMNODES > 1
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001784/**
1785 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001786 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001787 * @nid: the node ID to be checked.
1788 * @noswap : specify true here if the user wants flle only information.
1789 *
1790 * This function returns whether the specified memcg contains any
1791 * reclaimable pages on a node. Returns true if there are any reclaimable
1792 * pages in the node.
1793 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001794static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001795 int nid, bool noswap)
1796{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001797 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001798 return true;
1799 if (noswap || !total_swap_pages)
1800 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001801 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001802 return true;
1803 return false;
1804
1805}
Ying Han889976d2011-05-26 16:25:33 -07001806
1807/*
1808 * Always updating the nodemask is not very good - even if we have an empty
1809 * list or the wrong list here, we can start from some node and traverse all
1810 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1811 *
1812 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001814{
1815 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001816 /*
1817 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1818 * pagein/pageout changes since the last update.
1819 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001820 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001821 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001822 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001823 return;
1824
Ying Han889976d2011-05-26 16:25:33 -07001825 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001826 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001827
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001828 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001829
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001830 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1831 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001832 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001833
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 atomic_set(&memcg->numainfo_events, 0);
1835 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001836}
1837
1838/*
1839 * Selecting a node where we start reclaim from. Because what we need is just
1840 * reducing usage counter, start from anywhere is O,K. Considering
1841 * memory reclaim from current node, there are pros. and cons.
1842 *
1843 * Freeing memory from current node means freeing memory from a node which
1844 * we'll use or we've used. So, it may make LRU bad. And if several threads
1845 * hit limits, it will see a contention on a node. But freeing from remote
1846 * node means more costs for memory reclaim because of memory latency.
1847 *
1848 * Now, we use round-robin. Better algorithm is welcomed.
1849 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001850int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001851{
1852 int node;
1853
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854 mem_cgroup_may_update_nodemask(memcg);
1855 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001856
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001857 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001858 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001860 /*
1861 * We call this when we hit limit, not when pages are added to LRU.
1862 * No LRU may hold pages because all pages are UNEVICTABLE or
1863 * memcg is too small and all pages are not on LRU. In that case,
1864 * we use curret node.
1865 */
1866 if (unlikely(node == MAX_NUMNODES))
1867 node = numa_node_id();
1868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001869 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001870 return node;
1871}
1872
1873#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001874int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001875{
1876 return 0;
1877}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001878
Ying Han889976d2011-05-26 16:25:33 -07001879#endif
1880
Michal Hocko3b387222013-09-12 15:13:21 -07001881/*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001882 * A group is eligible for the soft limit reclaim under the given root
1883 * hierarchy if
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001884 * a) it is over its soft limit
1885 * b) any parent up the hierarchy is over its soft limit
Michal Hocko7d910c02013-09-12 15:13:28 -07001886 *
1887 * If the given group doesn't have any children over the limit then it
1888 * doesn't make any sense to iterate its subtree.
Michal Hocko3b387222013-09-12 15:13:21 -07001889 */
Michal Hockode577802013-09-12 15:13:26 -07001890enum mem_cgroup_filter_t
1891mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -07001892 struct mem_cgroup *root)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001893{
Michal Hockoe839b6a2013-09-12 15:13:30 -07001894 struct mem_cgroup *parent;
1895
1896 if (!memcg)
1897 memcg = root_mem_cgroup;
1898 parent = memcg;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001899
Michal Hocko3b387222013-09-12 15:13:21 -07001900 if (res_counter_soft_limit_excess(&memcg->res))
Michal Hockode577802013-09-12 15:13:26 -07001901 return VISIT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001902
Michal Hocko3b387222013-09-12 15:13:21 -07001903 /*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001904 * If any parent up to the root in the hierarchy is over its soft limit
1905 * then we have to obey and reclaim from this group as well.
Michal Hocko3b387222013-09-12 15:13:21 -07001906 */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07001907 while ((parent = parent_mem_cgroup(parent))) {
Michal Hocko3b387222013-09-12 15:13:21 -07001908 if (res_counter_soft_limit_excess(&parent->res))
Michal Hockode577802013-09-12 15:13:26 -07001909 return VISIT;
Michal Hockoa5b7c872013-09-12 15:13:25 -07001910 if (parent == root)
1911 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001912 }
Michal Hocko3b387222013-09-12 15:13:21 -07001913
Michal Hocko7d910c02013-09-12 15:13:28 -07001914 if (!atomic_read(&memcg->children_in_excess))
1915 return SKIP_TREE;
Michal Hockode577802013-09-12 15:13:26 -07001916 return SKIP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001917}
1918
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001919static DEFINE_SPINLOCK(memcg_oom_lock);
1920
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001921/*
1922 * Check OOM-Killer is already running under our hierarchy.
1923 * If someone is running, return false.
1924 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001925static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001926{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001927 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001928
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001929 spin_lock(&memcg_oom_lock);
1930
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001931 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001932 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001933 /*
1934 * this subtree of our hierarchy is already locked
1935 * so we cannot give a lock.
1936 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001937 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001938 mem_cgroup_iter_break(memcg, iter);
1939 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001940 } else
1941 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001942 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001943
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001944 if (failed) {
1945 /*
1946 * OK, we failed to lock the whole subtree so we have
1947 * to clean up what we set up to the failing subtree
1948 */
1949 for_each_mem_cgroup_tree(iter, memcg) {
1950 if (iter == failed) {
1951 mem_cgroup_iter_break(memcg, iter);
1952 break;
1953 }
1954 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001955 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07001956 }
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001957
1958 spin_unlock(&memcg_oom_lock);
1959
1960 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001961}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001962
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001963static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001964{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001965 struct mem_cgroup *iter;
1966
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001967 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001968 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001969 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001970 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001971}
1972
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001973static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001974{
1975 struct mem_cgroup *iter;
1976
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001977 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001978 atomic_inc(&iter->under_oom);
1979}
1980
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001982{
1983 struct mem_cgroup *iter;
1984
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001985 /*
1986 * When a new child is created while the hierarchy is under oom,
1987 * mem_cgroup_oom_lock() may not be called. We have to use
1988 * atomic_add_unless() here.
1989 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001990 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001991 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001992}
1993
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001994static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1995
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001996struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001997 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001998 wait_queue_t wait;
1999};
2000
2001static int memcg_oom_wake_function(wait_queue_t *wait,
2002 unsigned mode, int sync, void *arg)
2003{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002004 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2005 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002006 struct oom_wait_info *oom_wait_info;
2007
2008 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002009 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002010
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002011 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002012 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002013 * Then we can use css_is_ancestor without taking care of RCU.
2014 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002015 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2016 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002017 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002018 return autoremove_wake_function(wait, mode, sync, arg);
2019}
2020
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002021static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002022{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002023 /* for filtering, pass "memcg" as argument. */
2024 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002025}
2026
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002027static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002028{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002029 if (memcg && atomic_read(&memcg->under_oom))
2030 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002031}
2032
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002033/*
2034 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2035 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002036static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2037 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002038{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002039 struct oom_wait_info owait;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002040 bool locked;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002041
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002042 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002043 owait.wait.flags = 0;
2044 owait.wait.func = memcg_oom_wake_function;
2045 owait.wait.private = current;
2046 INIT_LIST_HEAD(&owait.wait.task_list);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002047
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002048 /*
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002049 * As with any blocking lock, a contender needs to start
2050 * listening for wakeups before attempting the trylock,
2051 * otherwise it can miss the wakeup from the unlock and sleep
2052 * indefinitely. This is just open-coded because our locking
2053 * is so particular to memcg hierarchies.
2054 *
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002055 * Even if signal_pending(), we can't quit charge() loop without
2056 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2057 * under OOM is always welcomed, use TASK_KILLABLE here.
2058 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002059 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002060 mem_cgroup_mark_under_oom(memcg);
2061
2062 locked = mem_cgroup_oom_trylock(memcg);
2063
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002064 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002065 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002066
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002067 if (locked && !memcg->oom_kill_disable) {
2068 mem_cgroup_unmark_under_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002069 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002070 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002071 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002072 schedule();
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002073 mem_cgroup_unmark_under_oom(memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002074 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002075 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002076
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07002077 if (locked) {
2078 mem_cgroup_oom_unlock(memcg);
2079 /*
2080 * There is no guarantee that an OOM-lock contender
2081 * sees the wakeups triggered by the OOM kill
2082 * uncharges. Wake any sleepers explicitely.
2083 */
2084 memcg_oom_recover(memcg);
2085 }
Michal Hocko79dfdac2011-07-26 16:08:23 -07002086
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002087 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2088 return false;
2089 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002090 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002091 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002092}
2093
Balbir Singhd69b0422009-06-17 16:26:34 -07002094/*
2095 * Currently used to update mapped file statistics, but the routine can be
2096 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002097 *
2098 * Notes: Race condition
2099 *
2100 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2101 * it tends to be costly. But considering some conditions, we doesn't need
2102 * to do so _always_.
2103 *
2104 * Considering "charge", lock_page_cgroup() is not required because all
2105 * file-stat operations happen after a page is attached to radix-tree. There
2106 * are no race with "charge".
2107 *
2108 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2109 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2110 * if there are race with "uncharge". Statistics itself is properly handled
2111 * by flags.
2112 *
2113 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002114 * small, we check mm->moving_account and detect there are possibility of race
2115 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002116 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002117
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002118void __mem_cgroup_begin_update_page_stat(struct page *page,
2119 bool *locked, unsigned long *flags)
2120{
2121 struct mem_cgroup *memcg;
2122 struct page_cgroup *pc;
2123
2124 pc = lookup_page_cgroup(page);
2125again:
2126 memcg = pc->mem_cgroup;
2127 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2128 return;
2129 /*
2130 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002131 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002132 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002133 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002134 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002135 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002136 return;
2137
2138 move_lock_mem_cgroup(memcg, flags);
2139 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2140 move_unlock_mem_cgroup(memcg, flags);
2141 goto again;
2142 }
2143 *locked = true;
2144}
2145
2146void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2147{
2148 struct page_cgroup *pc = lookup_page_cgroup(page);
2149
2150 /*
2151 * It's guaranteed that pc->mem_cgroup never changes while
2152 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002153 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002154 */
2155 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2156}
2157
Greg Thelen2a7106f2011-01-13 15:47:37 -08002158void mem_cgroup_update_page_stat(struct page *page,
2159 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002160{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002162 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002163 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002164
Johannes Weinercfa44942012-01-12 17:18:38 -08002165 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002166 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002167
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002168 memcg = pc->mem_cgroup;
2169 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002170 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002171
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002172 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002173 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002174 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002175 break;
2176 default:
2177 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002178 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002179
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002180 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002181}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002182
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002183/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002184 * size of first charge trial. "32" comes from vmscan.c's magic value.
2185 * TODO: maybe necessary to use big numbers in big irons.
2186 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002187#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002188struct memcg_stock_pcp {
2189 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002190 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002191 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002192 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002193#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002194};
2195static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002196static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002197
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002198/**
2199 * consume_stock: Try to consume stocked charge on this cpu.
2200 * @memcg: memcg to consume from.
2201 * @nr_pages: how many pages to charge.
2202 *
2203 * The charges will only happen if @memcg matches the current cpu's memcg
2204 * stock, and at least @nr_pages are available in that stock. Failure to
2205 * service an allocation will refill the stock.
2206 *
2207 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002208 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002209static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002210{
2211 struct memcg_stock_pcp *stock;
2212 bool ret = true;
2213
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002214 if (nr_pages > CHARGE_BATCH)
2215 return false;
2216
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002217 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002218 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2219 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002220 else /* need to call res_counter_charge */
2221 ret = false;
2222 put_cpu_var(memcg_stock);
2223 return ret;
2224}
2225
2226/*
2227 * Returns stocks cached in percpu to res_counter and reset cached information.
2228 */
2229static void drain_stock(struct memcg_stock_pcp *stock)
2230{
2231 struct mem_cgroup *old = stock->cached;
2232
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002233 if (stock->nr_pages) {
2234 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2235
2236 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002237 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002238 res_counter_uncharge(&old->memsw, bytes);
2239 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002240 }
2241 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002242}
2243
2244/*
2245 * This must be called under preempt disabled or must be called by
2246 * a thread which is pinned to local cpu.
2247 */
2248static void drain_local_stock(struct work_struct *dummy)
2249{
2250 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2251 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002252 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002253}
2254
Michal Hockoe4777492013-02-22 16:35:40 -08002255static void __init memcg_stock_init(void)
2256{
2257 int cpu;
2258
2259 for_each_possible_cpu(cpu) {
2260 struct memcg_stock_pcp *stock =
2261 &per_cpu(memcg_stock, cpu);
2262 INIT_WORK(&stock->work, drain_local_stock);
2263 }
2264}
2265
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002266/*
2267 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002268 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002270static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002271{
2272 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2273
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002274 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002275 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002276 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002277 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002278 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002279 put_cpu_var(memcg_stock);
2280}
2281
2282/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002283 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002284 * of the hierarchy under it. sync flag says whether we should block
2285 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002286 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002287static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002288{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002289 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002290
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002291 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002292 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002293 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002294 for_each_online_cpu(cpu) {
2295 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002296 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002297
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002298 memcg = stock->cached;
2299 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002300 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002301 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002302 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002303 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2304 if (cpu == curcpu)
2305 drain_local_stock(&stock->work);
2306 else
2307 schedule_work_on(cpu, &stock->work);
2308 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002309 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002310 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002311
2312 if (!sync)
2313 goto out;
2314
2315 for_each_online_cpu(cpu) {
2316 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002317 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002318 flush_work(&stock->work);
2319 }
2320out:
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002321 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002322}
2323
2324/*
2325 * Tries to drain stocked charges in other cpus. This function is asynchronous
2326 * and just put a work per cpu for draining localy on each cpu. Caller can
2327 * expects some charges will be back to res_counter later but cannot wait for
2328 * it.
2329 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002330static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002331{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002332 /*
2333 * If someone calls draining, avoid adding more kworker runs.
2334 */
2335 if (!mutex_trylock(&percpu_charge_mutex))
2336 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002337 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002338 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002339}
2340
2341/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002342static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002343{
2344 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002345 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002346 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002347 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002348}
2349
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002350/*
2351 * This function drains percpu counter value from DEAD cpu and
2352 * move it to local cpu. Note that this function can be preempted.
2353 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002354static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002355{
2356 int i;
2357
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002359 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002360 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002361
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002362 per_cpu(memcg->stat->count[i], cpu) = 0;
2363 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002364 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002365 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002366 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002367
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002368 per_cpu(memcg->stat->events[i], cpu) = 0;
2369 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002370 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002371 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002372}
2373
Paul Gortmaker0db06282013-06-19 14:53:51 -04002374static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002375 unsigned long action,
2376 void *hcpu)
2377{
2378 int cpu = (unsigned long)hcpu;
2379 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002380 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002381
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002382 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002383 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002384
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002385 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002386 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002387
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002388 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002389 mem_cgroup_drain_pcp_counter(iter, cpu);
2390
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002391 stock = &per_cpu(memcg_stock, cpu);
2392 drain_stock(stock);
2393 return NOTIFY_OK;
2394}
2395
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002396
2397/* See __mem_cgroup_try_charge() for details */
2398enum {
2399 CHARGE_OK, /* success */
2400 CHARGE_RETRY, /* need to retry but retry is not bad */
2401 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2402 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2403 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2404};
2405
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002406static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002407 unsigned int nr_pages, unsigned int min_pages,
2408 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002409{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002410 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002411 struct mem_cgroup *mem_over_limit;
2412 struct res_counter *fail_res;
2413 unsigned long flags = 0;
2414 int ret;
2415
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002417
2418 if (likely(!ret)) {
2419 if (!do_swap_account)
2420 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002421 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002422 if (likely(!ret))
2423 return CHARGE_OK;
2424
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002425 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002426 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2427 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2428 } else
2429 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002430 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002431 * Never reclaim on behalf of optional batching, retry with a
2432 * single page instead.
2433 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002434 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002435 return CHARGE_RETRY;
2436
2437 if (!(gfp_mask & __GFP_WAIT))
2438 return CHARGE_WOULDBLOCK;
2439
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002440 if (gfp_mask & __GFP_NORETRY)
2441 return CHARGE_NOMEM;
2442
Johannes Weiner56600482012-01-12 17:17:59 -08002443 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002444 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002445 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002446 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002447 * Even though the limit is exceeded at this point, reclaim
2448 * may have been able to free some pages. Retry the charge
2449 * before killing the task.
2450 *
2451 * Only for regular pages, though: huge pages are rather
2452 * unlikely to succeed so close to the limit, and we fall back
2453 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002455 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002456 return CHARGE_RETRY;
2457
2458 /*
2459 * At task move, charge accounts can be doubly counted. So, it's
2460 * better to wait until the end of task_move if something is going on.
2461 */
2462 if (mem_cgroup_wait_acct_move(mem_over_limit))
2463 return CHARGE_RETRY;
2464
2465 /* If we don't need to call oom-killer at el, return immediately */
Johannes Weiner519e5242013-09-12 15:13:42 -07002466 if (!oom_check || !current->memcg_oom.may_oom)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002467 return CHARGE_NOMEM;
2468 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002469 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002470 return CHARGE_OOM_DIE;
2471
2472 return CHARGE_RETRY;
2473}
2474
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002475/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002476 * __mem_cgroup_try_charge() does
2477 * 1. detect memcg to be charged against from passed *mm and *ptr,
2478 * 2. update res_counter
2479 * 3. call memory reclaim if necessary.
2480 *
2481 * In some special case, if the task is fatal, fatal_signal_pending() or
2482 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2483 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2484 * as possible without any hazards. 2: all pages should have a valid
2485 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2486 * pointer, that is treated as a charge to root_mem_cgroup.
2487 *
2488 * So __mem_cgroup_try_charge() will return
2489 * 0 ... on success, filling *ptr with a valid memcg pointer.
2490 * -ENOMEM ... charge failure because of resource limits.
2491 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2492 *
2493 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2494 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002495 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002496static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002497 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002498 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002499 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002500 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002501{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002502 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002503 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002504 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002505 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002506
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002507 /*
2508 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2509 * in system level. So, allow to go ahead dying process in addition to
2510 * MEMDIE process.
2511 */
2512 if (unlikely(test_thread_flag(TIF_MEMDIE)
2513 || fatal_signal_pending(current)))
2514 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002515
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002516 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002517 * We always charge the cgroup the mm_struct belongs to.
2518 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002519 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002520 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002521 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002522 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002523 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002524again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002525 if (*ptr) { /* css should be a valid one */
2526 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002527 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002528 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002529 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002530 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002531 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002532 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002533 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002534
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002535 rcu_read_lock();
2536 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002537 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002538 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002539 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002540 * race with swapoff. Then, we have small risk of mis-accouning.
2541 * But such kind of mis-account by race always happens because
2542 * we don't have cgroup_mutex(). It's overkill and we allo that
2543 * small race, here.
2544 * (*) swapoff at el will charge against mm-struct not against
2545 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002546 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002547 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002548 if (!memcg)
2549 memcg = root_mem_cgroup;
2550 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002551 rcu_read_unlock();
2552 goto done;
2553 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002554 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002555 /*
2556 * It seems dagerous to access memcg without css_get().
2557 * But considering how consume_stok works, it's not
2558 * necessary. If consume_stock success, some charges
2559 * from this memcg are cached on this cpu. So, we
2560 * don't need to call css_get()/css_tryget() before
2561 * calling consume_stock().
2562 */
2563 rcu_read_unlock();
2564 goto done;
2565 }
2566 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002567 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002568 rcu_read_unlock();
2569 goto again;
2570 }
2571 rcu_read_unlock();
2572 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002573
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002574 do {
2575 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002576
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002577 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002578 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002579 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002580 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002581 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002582
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002583 oom_check = false;
2584 if (oom && !nr_oom_retries) {
2585 oom_check = true;
2586 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2587 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002588
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002589 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2590 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002591 switch (ret) {
2592 case CHARGE_OK:
2593 break;
2594 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002595 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002596 css_put(&memcg->css);
2597 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002598 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002599 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002600 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002601 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002602 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002603 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002604 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002605 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002606 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002607 /* If oom, we never return -ENOMEM */
2608 nr_oom_retries--;
2609 break;
2610 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002611 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002612 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002613 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002614 } while (ret != CHARGE_OK);
2615
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002616 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002617 refill_stock(memcg, batch - nr_pages);
2618 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002619done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002620 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002621 return 0;
2622nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002623 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002624 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002625bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002626 *ptr = root_mem_cgroup;
2627 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002628}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002629
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002630/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002631 * Somemtimes we have to undo a charge we got by try_charge().
2632 * This function is for that and do uncharge, put css's refcnt.
2633 * gotten by try_charge().
2634 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002635static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002636 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002637{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002638 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002639 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002640
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002641 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002642 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002643 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002644 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002645}
2646
2647/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002648 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2649 * This is useful when moving usage to parent cgroup.
2650 */
2651static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2652 unsigned int nr_pages)
2653{
2654 unsigned long bytes = nr_pages * PAGE_SIZE;
2655
2656 if (mem_cgroup_is_root(memcg))
2657 return;
2658
2659 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2660 if (do_swap_account)
2661 res_counter_uncharge_until(&memcg->memsw,
2662 memcg->memsw.parent, bytes);
2663}
2664
2665/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002666 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002667 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2668 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2669 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002670 */
2671static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2672{
2673 struct cgroup_subsys_state *css;
2674
2675 /* ID 0 is unused ID */
2676 if (!id)
2677 return NULL;
2678 css = css_lookup(&mem_cgroup_subsys, id);
2679 if (!css)
2680 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002681 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002682}
2683
Wu Fengguange42d9d52009-12-16 12:19:59 +01002684struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002685{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002686 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002687 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002688 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002689 swp_entry_t ent;
2690
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002691 VM_BUG_ON(!PageLocked(page));
2692
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002693 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002694 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002695 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002696 memcg = pc->mem_cgroup;
2697 if (memcg && !css_tryget(&memcg->css))
2698 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002699 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002700 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002701 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002702 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002703 memcg = mem_cgroup_lookup(id);
2704 if (memcg && !css_tryget(&memcg->css))
2705 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002706 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002707 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002708 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002709 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002710}
2711
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002712static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002713 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002714 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002715 enum charge_type ctype,
2716 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002717{
Johannes Weinerce587e62012-04-24 20:22:33 +02002718 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002719 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002720 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002721 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002722 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002723
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002724 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002725 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002726 /*
2727 * we don't need page_cgroup_lock about tail pages, becase they are not
2728 * accessed by any other context at this point.
2729 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002730
2731 /*
2732 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2733 * may already be on some other mem_cgroup's LRU. Take care of it.
2734 */
2735 if (lrucare) {
2736 zone = page_zone(page);
2737 spin_lock_irq(&zone->lru_lock);
2738 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002739 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002740 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002741 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002742 was_on_lru = true;
2743 }
2744 }
2745
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002746 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002747 /*
2748 * We access a page_cgroup asynchronously without lock_page_cgroup().
2749 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2750 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2751 * before USED bit, we need memory barrier here.
2752 * See mem_cgroup_add_lru_list(), etc.
Andrew Mortonf894ffa2013-09-12 15:13:35 -07002753 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002754 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002755 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002756
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002757 if (lrucare) {
2758 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002759 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002760 VM_BUG_ON(PageLRU(page));
2761 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002762 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002763 }
2764 spin_unlock_irq(&zone->lru_lock);
2765 }
2766
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002767 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002768 anon = true;
2769 else
2770 anon = false;
2771
David Rientjesb070e652013-05-07 16:18:09 -07002772 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002773 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002774
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002775 /*
Michal Hockoe8831102013-09-12 15:13:23 -07002776 * "charge_statistics" updated event counter.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002777 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002778 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002779}
2780
Glauber Costa7cf27982012-12-18 14:22:55 -08002781static DEFINE_MUTEX(set_limit_mutex);
2782
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002783#ifdef CONFIG_MEMCG_KMEM
2784static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2785{
2786 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2787 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2788}
2789
Glauber Costa1f458cb2012-12-18 14:22:50 -08002790/*
2791 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2792 * in the memcg_cache_params struct.
2793 */
2794static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2795{
2796 struct kmem_cache *cachep;
2797
2798 VM_BUG_ON(p->is_root_cache);
2799 cachep = p->root_cache;
2800 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2801}
2802
Glauber Costa749c5412012-12-18 14:23:01 -08002803#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002804static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2805 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002806{
Tejun Heo182446d2013-08-08 20:11:24 -04002807 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002808 struct memcg_cache_params *params;
2809
2810 if (!memcg_can_account_kmem(memcg))
2811 return -EIO;
2812
2813 print_slabinfo_header(m);
2814
2815 mutex_lock(&memcg->slab_caches_mutex);
2816 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2817 cache_show(memcg_params_to_cache(params), m);
2818 mutex_unlock(&memcg->slab_caches_mutex);
2819
2820 return 0;
2821}
2822#endif
2823
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002824static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2825{
2826 struct res_counter *fail_res;
2827 struct mem_cgroup *_memcg;
2828 int ret = 0;
2829 bool may_oom;
2830
2831 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2832 if (ret)
2833 return ret;
2834
2835 /*
2836 * Conditions under which we can wait for the oom_killer. Those are
2837 * the same conditions tested by the core page allocator
2838 */
2839 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2840
2841 _memcg = memcg;
2842 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2843 &_memcg, may_oom);
2844
2845 if (ret == -EINTR) {
2846 /*
2847 * __mem_cgroup_try_charge() chosed to bypass to root due to
2848 * OOM kill or fatal signal. Since our only options are to
2849 * either fail the allocation or charge it to this cgroup, do
2850 * it as a temporary condition. But we can't fail. From a
2851 * kmem/slab perspective, the cache has already been selected,
2852 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2853 * our minds.
2854 *
2855 * This condition will only trigger if the task entered
2856 * memcg_charge_kmem in a sane state, but was OOM-killed during
2857 * __mem_cgroup_try_charge() above. Tasks that were already
2858 * dying when the allocation triggers should have been already
2859 * directed to the root cgroup in memcontrol.h
2860 */
2861 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2862 if (do_swap_account)
2863 res_counter_charge_nofail(&memcg->memsw, size,
2864 &fail_res);
2865 ret = 0;
2866 } else if (ret)
2867 res_counter_uncharge(&memcg->kmem, size);
2868
2869 return ret;
2870}
2871
2872static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2873{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002874 res_counter_uncharge(&memcg->res, size);
2875 if (do_swap_account)
2876 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002877
2878 /* Not down to 0 */
2879 if (res_counter_uncharge(&memcg->kmem, size))
2880 return;
2881
Li Zefan10d5ebf2013-07-08 16:00:33 -07002882 /*
2883 * Releases a reference taken in kmem_cgroup_css_offline in case
2884 * this last uncharge is racing with the offlining code or it is
2885 * outliving the memcg existence.
2886 *
2887 * The memory barrier imposed by test&clear is paired with the
2888 * explicit one in memcg_kmem_mark_dead().
2889 */
Glauber Costa7de37682012-12-18 14:22:07 -08002890 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002891 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002892}
2893
Glauber Costa2633d7a2012-12-18 14:22:34 -08002894void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2895{
2896 if (!memcg)
2897 return;
2898
2899 mutex_lock(&memcg->slab_caches_mutex);
2900 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2901 mutex_unlock(&memcg->slab_caches_mutex);
2902}
2903
2904/*
2905 * helper for acessing a memcg's index. It will be used as an index in the
2906 * child cache array in kmem_cache, and also to derive its name. This function
2907 * will return -1 when this is not a kmem-limited memcg.
2908 */
2909int memcg_cache_id(struct mem_cgroup *memcg)
2910{
2911 return memcg ? memcg->kmemcg_id : -1;
2912}
2913
Glauber Costa55007d82012-12-18 14:22:38 -08002914/*
2915 * This ends up being protected by the set_limit mutex, during normal
2916 * operation, because that is its main call site.
2917 *
2918 * But when we create a new cache, we can call this as well if its parent
2919 * is kmem-limited. That will have to hold set_limit_mutex as well.
2920 */
2921int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2922{
2923 int num, ret;
2924
2925 num = ida_simple_get(&kmem_limited_groups,
2926 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2927 if (num < 0)
2928 return num;
2929 /*
2930 * After this point, kmem_accounted (that we test atomically in
2931 * the beginning of this conditional), is no longer 0. This
2932 * guarantees only one process will set the following boolean
2933 * to true. We don't need test_and_set because we're protected
2934 * by the set_limit_mutex anyway.
2935 */
2936 memcg_kmem_set_activated(memcg);
2937
2938 ret = memcg_update_all_caches(num+1);
2939 if (ret) {
2940 ida_simple_remove(&kmem_limited_groups, num);
2941 memcg_kmem_clear_activated(memcg);
2942 return ret;
2943 }
2944
2945 memcg->kmemcg_id = num;
2946 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2947 mutex_init(&memcg->slab_caches_mutex);
2948 return 0;
2949}
2950
2951static size_t memcg_caches_array_size(int num_groups)
2952{
2953 ssize_t size;
2954 if (num_groups <= 0)
2955 return 0;
2956
2957 size = 2 * num_groups;
2958 if (size < MEMCG_CACHES_MIN_SIZE)
2959 size = MEMCG_CACHES_MIN_SIZE;
2960 else if (size > MEMCG_CACHES_MAX_SIZE)
2961 size = MEMCG_CACHES_MAX_SIZE;
2962
2963 return size;
2964}
2965
2966/*
2967 * We should update the current array size iff all caches updates succeed. This
2968 * can only be done from the slab side. The slab mutex needs to be held when
2969 * calling this.
2970 */
2971void memcg_update_array_size(int num)
2972{
2973 if (num > memcg_limited_groups_array_size)
2974 memcg_limited_groups_array_size = memcg_caches_array_size(num);
2975}
2976
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08002977static void kmem_cache_destroy_work_func(struct work_struct *w);
2978
Glauber Costa55007d82012-12-18 14:22:38 -08002979int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
2980{
2981 struct memcg_cache_params *cur_params = s->memcg_params;
2982
2983 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
2984
2985 if (num_groups > memcg_limited_groups_array_size) {
2986 int i;
2987 ssize_t size = memcg_caches_array_size(num_groups);
2988
2989 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07002990 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08002991
2992 s->memcg_params = kzalloc(size, GFP_KERNEL);
2993 if (!s->memcg_params) {
2994 s->memcg_params = cur_params;
2995 return -ENOMEM;
2996 }
2997
2998 s->memcg_params->is_root_cache = true;
2999
3000 /*
3001 * There is the chance it will be bigger than
3002 * memcg_limited_groups_array_size, if we failed an allocation
3003 * in a cache, in which case all caches updated before it, will
3004 * have a bigger array.
3005 *
3006 * But if that is the case, the data after
3007 * memcg_limited_groups_array_size is certainly unused
3008 */
3009 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3010 if (!cur_params->memcg_caches[i])
3011 continue;
3012 s->memcg_params->memcg_caches[i] =
3013 cur_params->memcg_caches[i];
3014 }
3015
3016 /*
3017 * Ideally, we would wait until all caches succeed, and only
3018 * then free the old one. But this is not worth the extra
3019 * pointer per-cache we'd have to have for this.
3020 *
3021 * It is not a big deal if some caches are left with a size
3022 * bigger than the others. And all updates will reset this
3023 * anyway.
3024 */
3025 kfree(cur_params);
3026 }
3027 return 0;
3028}
3029
Glauber Costa943a4512012-12-18 14:23:03 -08003030int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3031 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003032{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003033 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003034
3035 if (!memcg_kmem_enabled())
3036 return 0;
3037
Andrey Vagin90c7a792013-09-11 14:22:18 -07003038 if (!memcg) {
3039 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003040 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003041 } else
3042 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003043
Glauber Costa2633d7a2012-12-18 14:22:34 -08003044 s->memcg_params = kzalloc(size, GFP_KERNEL);
3045 if (!s->memcg_params)
3046 return -ENOMEM;
3047
Glauber Costa943a4512012-12-18 14:23:03 -08003048 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003049 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003050 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003051 INIT_WORK(&s->memcg_params->destroy,
3052 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003053 } else
3054 s->memcg_params->is_root_cache = true;
3055
Glauber Costa2633d7a2012-12-18 14:22:34 -08003056 return 0;
3057}
3058
3059void memcg_release_cache(struct kmem_cache *s)
3060{
Glauber Costad7f25f82012-12-18 14:22:40 -08003061 struct kmem_cache *root;
3062 struct mem_cgroup *memcg;
3063 int id;
3064
3065 /*
3066 * This happens, for instance, when a root cache goes away before we
3067 * add any memcg.
3068 */
3069 if (!s->memcg_params)
3070 return;
3071
3072 if (s->memcg_params->is_root_cache)
3073 goto out;
3074
3075 memcg = s->memcg_params->memcg;
3076 id = memcg_cache_id(memcg);
3077
3078 root = s->memcg_params->root_cache;
3079 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003080
3081 mutex_lock(&memcg->slab_caches_mutex);
3082 list_del(&s->memcg_params->list);
3083 mutex_unlock(&memcg->slab_caches_mutex);
3084
Li Zefan20f05312013-07-08 16:00:31 -07003085 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003086out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003087 kfree(s->memcg_params);
3088}
3089
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003090/*
3091 * During the creation a new cache, we need to disable our accounting mechanism
3092 * altogether. This is true even if we are not creating, but rather just
3093 * enqueing new caches to be created.
3094 *
3095 * This is because that process will trigger allocations; some visible, like
3096 * explicit kmallocs to auxiliary data structures, name strings and internal
3097 * cache structures; some well concealed, like INIT_WORK() that can allocate
3098 * objects during debug.
3099 *
3100 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3101 * to it. This may not be a bounded recursion: since the first cache creation
3102 * failed to complete (waiting on the allocation), we'll just try to create the
3103 * cache again, failing at the same point.
3104 *
3105 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3106 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3107 * inside the following two functions.
3108 */
3109static inline void memcg_stop_kmem_account(void)
3110{
3111 VM_BUG_ON(!current->mm);
3112 current->memcg_kmem_skip_account++;
3113}
3114
3115static inline void memcg_resume_kmem_account(void)
3116{
3117 VM_BUG_ON(!current->mm);
3118 current->memcg_kmem_skip_account--;
3119}
3120
Glauber Costa1f458cb2012-12-18 14:22:50 -08003121static void kmem_cache_destroy_work_func(struct work_struct *w)
3122{
3123 struct kmem_cache *cachep;
3124 struct memcg_cache_params *p;
3125
3126 p = container_of(w, struct memcg_cache_params, destroy);
3127
3128 cachep = memcg_params_to_cache(p);
3129
Glauber Costa22933152012-12-18 14:22:59 -08003130 /*
3131 * If we get down to 0 after shrink, we could delete right away.
3132 * However, memcg_release_pages() already puts us back in the workqueue
3133 * in that case. If we proceed deleting, we'll get a dangling
3134 * reference, and removing the object from the workqueue in that case
3135 * is unnecessary complication. We are not a fast path.
3136 *
3137 * Note that this case is fundamentally different from racing with
3138 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3139 * kmem_cache_shrink, not only we would be reinserting a dead cache
3140 * into the queue, but doing so from inside the worker racing to
3141 * destroy it.
3142 *
3143 * So if we aren't down to zero, we'll just schedule a worker and try
3144 * again
3145 */
3146 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3147 kmem_cache_shrink(cachep);
3148 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3149 return;
3150 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003151 kmem_cache_destroy(cachep);
3152}
3153
3154void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3155{
3156 if (!cachep->memcg_params->dead)
3157 return;
3158
3159 /*
Glauber Costa22933152012-12-18 14:22:59 -08003160 * There are many ways in which we can get here.
3161 *
3162 * We can get to a memory-pressure situation while the delayed work is
3163 * still pending to run. The vmscan shrinkers can then release all
3164 * cache memory and get us to destruction. If this is the case, we'll
3165 * be executed twice, which is a bug (the second time will execute over
3166 * bogus data). In this case, cancelling the work should be fine.
3167 *
3168 * But we can also get here from the worker itself, if
3169 * kmem_cache_shrink is enough to shake all the remaining objects and
3170 * get the page count to 0. In this case, we'll deadlock if we try to
3171 * cancel the work (the worker runs with an internal lock held, which
3172 * is the same lock we would hold for cancel_work_sync().)
3173 *
3174 * Since we can't possibly know who got us here, just refrain from
3175 * running if there is already work pending
3176 */
3177 if (work_pending(&cachep->memcg_params->destroy))
3178 return;
3179 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003180 * We have to defer the actual destroying to a workqueue, because
3181 * we might currently be in a context that cannot sleep.
3182 */
3183 schedule_work(&cachep->memcg_params->destroy);
3184}
3185
Glauber Costad7f25f82012-12-18 14:22:40 -08003186/*
3187 * This lock protects updaters, not readers. We want readers to be as fast as
3188 * they can, and they will either see NULL or a valid cache value. Our model
3189 * allow them to see NULL, in which case the root memcg will be selected.
3190 *
3191 * We need this lock because multiple allocations to the same cache from a non
3192 * will span more than one worker. Only one of them can create the cache.
3193 */
3194static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003195
3196/*
3197 * Called with memcg_cache_mutex held
3198 */
3199static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3200 struct kmem_cache *s)
3201{
3202 struct kmem_cache *new;
3203 static char *tmp_name = NULL;
3204
3205 lockdep_assert_held(&memcg_cache_mutex);
3206
3207 /*
3208 * kmem_cache_create_memcg duplicates the given name and
3209 * cgroup_name for this name requires RCU context.
3210 * This static temporary buffer is used to prevent from
3211 * pointless shortliving allocation.
3212 */
3213 if (!tmp_name) {
3214 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3215 if (!tmp_name)
3216 return NULL;
3217 }
3218
3219 rcu_read_lock();
3220 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3221 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3222 rcu_read_unlock();
3223
3224 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3225 (s->flags & ~SLAB_PANIC), s->ctor, s);
3226
3227 if (new)
3228 new->allocflags |= __GFP_KMEMCG;
3229
3230 return new;
3231}
3232
Glauber Costad7f25f82012-12-18 14:22:40 -08003233static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3234 struct kmem_cache *cachep)
3235{
3236 struct kmem_cache *new_cachep;
3237 int idx;
3238
3239 BUG_ON(!memcg_can_account_kmem(memcg));
3240
3241 idx = memcg_cache_id(memcg);
3242
3243 mutex_lock(&memcg_cache_mutex);
3244 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003245 if (new_cachep) {
3246 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003247 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003248 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003249
3250 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003251 if (new_cachep == NULL) {
3252 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003253 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003254 goto out;
3255 }
3256
Glauber Costa1f458cb2012-12-18 14:22:50 -08003257 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003258
3259 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3260 /*
3261 * the readers won't lock, make sure everybody sees the updated value,
3262 * so they won't put stuff in the queue again for no reason
3263 */
3264 wmb();
3265out:
3266 mutex_unlock(&memcg_cache_mutex);
3267 return new_cachep;
3268}
3269
Glauber Costa7cf27982012-12-18 14:22:55 -08003270void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3271{
3272 struct kmem_cache *c;
3273 int i;
3274
3275 if (!s->memcg_params)
3276 return;
3277 if (!s->memcg_params->is_root_cache)
3278 return;
3279
3280 /*
3281 * If the cache is being destroyed, we trust that there is no one else
3282 * requesting objects from it. Even if there are, the sanity checks in
3283 * kmem_cache_destroy should caught this ill-case.
3284 *
3285 * Still, we don't want anyone else freeing memcg_caches under our
3286 * noses, which can happen if a new memcg comes to life. As usual,
3287 * we'll take the set_limit_mutex to protect ourselves against this.
3288 */
3289 mutex_lock(&set_limit_mutex);
3290 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3291 c = s->memcg_params->memcg_caches[i];
3292 if (!c)
3293 continue;
3294
3295 /*
3296 * We will now manually delete the caches, so to avoid races
3297 * we need to cancel all pending destruction workers and
3298 * proceed with destruction ourselves.
3299 *
3300 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3301 * and that could spawn the workers again: it is likely that
3302 * the cache still have active pages until this very moment.
3303 * This would lead us back to mem_cgroup_destroy_cache.
3304 *
3305 * But that will not execute at all if the "dead" flag is not
3306 * set, so flip it down to guarantee we are in control.
3307 */
3308 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003309 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003310 kmem_cache_destroy(c);
3311 }
3312 mutex_unlock(&set_limit_mutex);
3313}
3314
Glauber Costad7f25f82012-12-18 14:22:40 -08003315struct create_work {
3316 struct mem_cgroup *memcg;
3317 struct kmem_cache *cachep;
3318 struct work_struct work;
3319};
3320
Glauber Costa1f458cb2012-12-18 14:22:50 -08003321static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3322{
3323 struct kmem_cache *cachep;
3324 struct memcg_cache_params *params;
3325
3326 if (!memcg_kmem_is_active(memcg))
3327 return;
3328
3329 mutex_lock(&memcg->slab_caches_mutex);
3330 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3331 cachep = memcg_params_to_cache(params);
3332 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003333 schedule_work(&cachep->memcg_params->destroy);
3334 }
3335 mutex_unlock(&memcg->slab_caches_mutex);
3336}
3337
Glauber Costad7f25f82012-12-18 14:22:40 -08003338static void memcg_create_cache_work_func(struct work_struct *w)
3339{
3340 struct create_work *cw;
3341
3342 cw = container_of(w, struct create_work, work);
3343 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003344 kfree(cw);
3345}
3346
3347/*
3348 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003349 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003350static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3351 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003352{
3353 struct create_work *cw;
3354
3355 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003356 if (cw == NULL) {
3357 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003358 return;
3359 }
3360
3361 cw->memcg = memcg;
3362 cw->cachep = cachep;
3363
3364 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3365 schedule_work(&cw->work);
3366}
3367
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003368static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3369 struct kmem_cache *cachep)
3370{
3371 /*
3372 * We need to stop accounting when we kmalloc, because if the
3373 * corresponding kmalloc cache is not yet created, the first allocation
3374 * in __memcg_create_cache_enqueue will recurse.
3375 *
3376 * However, it is better to enclose the whole function. Depending on
3377 * the debugging options enabled, INIT_WORK(), for instance, can
3378 * trigger an allocation. This too, will make us recurse. Because at
3379 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3380 * the safest choice is to do it like this, wrapping the whole function.
3381 */
3382 memcg_stop_kmem_account();
3383 __memcg_create_cache_enqueue(memcg, cachep);
3384 memcg_resume_kmem_account();
3385}
Glauber Costad7f25f82012-12-18 14:22:40 -08003386/*
3387 * Return the kmem_cache we're supposed to use for a slab allocation.
3388 * We try to use the current memcg's version of the cache.
3389 *
3390 * If the cache does not exist yet, if we are the first user of it,
3391 * we either create it immediately, if possible, or create it asynchronously
3392 * in a workqueue.
3393 * In the latter case, we will let the current allocation go through with
3394 * the original cache.
3395 *
3396 * Can't be called in interrupt context or from kernel threads.
3397 * This function needs to be called with rcu_read_lock() held.
3398 */
3399struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3400 gfp_t gfp)
3401{
3402 struct mem_cgroup *memcg;
3403 int idx;
3404
3405 VM_BUG_ON(!cachep->memcg_params);
3406 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3407
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003408 if (!current->mm || current->memcg_kmem_skip_account)
3409 return cachep;
3410
Glauber Costad7f25f82012-12-18 14:22:40 -08003411 rcu_read_lock();
3412 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003413
3414 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003415 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003416
3417 idx = memcg_cache_id(memcg);
3418
3419 /*
3420 * barrier to mare sure we're always seeing the up to date value. The
3421 * code updating memcg_caches will issue a write barrier to match this.
3422 */
3423 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003424 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3425 cachep = cachep->memcg_params->memcg_caches[idx];
3426 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003427 }
3428
Li Zefanca0dde92013-04-29 15:08:57 -07003429 /* The corresponding put will be done in the workqueue. */
3430 if (!css_tryget(&memcg->css))
3431 goto out;
3432 rcu_read_unlock();
3433
3434 /*
3435 * If we are in a safe context (can wait, and not in interrupt
3436 * context), we could be be predictable and return right away.
3437 * This would guarantee that the allocation being performed
3438 * already belongs in the new cache.
3439 *
3440 * However, there are some clashes that can arrive from locking.
3441 * For instance, because we acquire the slab_mutex while doing
3442 * kmem_cache_dup, this means no further allocation could happen
3443 * with the slab_mutex held.
3444 *
3445 * Also, because cache creation issue get_online_cpus(), this
3446 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3447 * that ends up reversed during cpu hotplug. (cpuset allocates
3448 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3449 * better to defer everything.
3450 */
3451 memcg_create_cache_enqueue(memcg, cachep);
3452 return cachep;
3453out:
3454 rcu_read_unlock();
3455 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003456}
3457EXPORT_SYMBOL(__memcg_kmem_get_cache);
3458
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003459/*
3460 * We need to verify if the allocation against current->mm->owner's memcg is
3461 * possible for the given order. But the page is not allocated yet, so we'll
3462 * need a further commit step to do the final arrangements.
3463 *
3464 * It is possible for the task to switch cgroups in this mean time, so at
3465 * commit time, we can't rely on task conversion any longer. We'll then use
3466 * the handle argument to return to the caller which cgroup we should commit
3467 * against. We could also return the memcg directly and avoid the pointer
3468 * passing, but a boolean return value gives better semantics considering
3469 * the compiled-out case as well.
3470 *
3471 * Returning true means the allocation is possible.
3472 */
3473bool
3474__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3475{
3476 struct mem_cgroup *memcg;
3477 int ret;
3478
3479 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003480
3481 /*
3482 * Disabling accounting is only relevant for some specific memcg
3483 * internal allocations. Therefore we would initially not have such
3484 * check here, since direct calls to the page allocator that are marked
3485 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3486 * concerned with cache allocations, and by having this test at
3487 * memcg_kmem_get_cache, we are already able to relay the allocation to
3488 * the root cache and bypass the memcg cache altogether.
3489 *
3490 * There is one exception, though: the SLUB allocator does not create
3491 * large order caches, but rather service large kmallocs directly from
3492 * the page allocator. Therefore, the following sequence when backed by
3493 * the SLUB allocator:
3494 *
Andrew Mortonf894ffa2013-09-12 15:13:35 -07003495 * memcg_stop_kmem_account();
3496 * kmalloc(<large_number>)
3497 * memcg_resume_kmem_account();
Glauber Costa6d42c232013-07-08 16:00:00 -07003498 *
3499 * would effectively ignore the fact that we should skip accounting,
3500 * since it will drive us directly to this function without passing
3501 * through the cache selector memcg_kmem_get_cache. Such large
3502 * allocations are extremely rare but can happen, for instance, for the
3503 * cache arrays. We bring this test here.
3504 */
3505 if (!current->mm || current->memcg_kmem_skip_account)
3506 return true;
3507
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003508 memcg = try_get_mem_cgroup_from_mm(current->mm);
3509
3510 /*
3511 * very rare case described in mem_cgroup_from_task. Unfortunately there
3512 * isn't much we can do without complicating this too much, and it would
3513 * be gfp-dependent anyway. Just let it go
3514 */
3515 if (unlikely(!memcg))
3516 return true;
3517
3518 if (!memcg_can_account_kmem(memcg)) {
3519 css_put(&memcg->css);
3520 return true;
3521 }
3522
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003523 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3524 if (!ret)
3525 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003526
3527 css_put(&memcg->css);
3528 return (ret == 0);
3529}
3530
3531void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3532 int order)
3533{
3534 struct page_cgroup *pc;
3535
3536 VM_BUG_ON(mem_cgroup_is_root(memcg));
3537
3538 /* The page allocation failed. Revert */
3539 if (!page) {
3540 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003541 return;
3542 }
3543
3544 pc = lookup_page_cgroup(page);
3545 lock_page_cgroup(pc);
3546 pc->mem_cgroup = memcg;
3547 SetPageCgroupUsed(pc);
3548 unlock_page_cgroup(pc);
3549}
3550
3551void __memcg_kmem_uncharge_pages(struct page *page, int order)
3552{
3553 struct mem_cgroup *memcg = NULL;
3554 struct page_cgroup *pc;
3555
3556
3557 pc = lookup_page_cgroup(page);
3558 /*
3559 * Fast unlocked return. Theoretically might have changed, have to
3560 * check again after locking.
3561 */
3562 if (!PageCgroupUsed(pc))
3563 return;
3564
3565 lock_page_cgroup(pc);
3566 if (PageCgroupUsed(pc)) {
3567 memcg = pc->mem_cgroup;
3568 ClearPageCgroupUsed(pc);
3569 }
3570 unlock_page_cgroup(pc);
3571
3572 /*
3573 * We trust that only if there is a memcg associated with the page, it
3574 * is a valid allocation
3575 */
3576 if (!memcg)
3577 return;
3578
3579 VM_BUG_ON(mem_cgroup_is_root(memcg));
3580 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003581}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003582#else
3583static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3584{
3585}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003586#endif /* CONFIG_MEMCG_KMEM */
3587
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003588#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3589
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003590#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003591/*
3592 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003593 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3594 * charge/uncharge will be never happen and move_account() is done under
3595 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003596 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003597void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003598{
3599 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003600 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003601 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003602 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003603
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003604 if (mem_cgroup_disabled())
3605 return;
David Rientjesb070e652013-05-07 16:18:09 -07003606
3607 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003608 for (i = 1; i < HPAGE_PMD_NR; i++) {
3609 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003610 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003611 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003612 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3613 }
David Rientjesb070e652013-05-07 16:18:09 -07003614 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3615 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003616}
Hugh Dickins12d27102012-01-12 17:19:52 -08003617#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003618
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003619/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003620 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003621 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003622 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003623 * @pc: page_cgroup of the page.
3624 * @from: mem_cgroup which the page is moved from.
3625 * @to: mem_cgroup which the page is moved to. @from != @to.
3626 *
3627 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003628 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003629 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003630 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003631 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3632 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003633 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003634static int mem_cgroup_move_account(struct page *page,
3635 unsigned int nr_pages,
3636 struct page_cgroup *pc,
3637 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003638 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003639{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003640 unsigned long flags;
3641 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003642 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003643
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003644 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003645 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003646 /*
3647 * The page is isolated from LRU. So, collapse function
3648 * will not handle this page. But page splitting can happen.
3649 * Do this check under compound_page_lock(). The caller should
3650 * hold it.
3651 */
3652 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003653 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003654 goto out;
3655
3656 lock_page_cgroup(pc);
3657
3658 ret = -EINVAL;
3659 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3660 goto unlock;
3661
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003662 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003663
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003664 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003665 /* Update mapped_file data for mem_cgroup */
3666 preempt_disable();
3667 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3668 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3669 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003670 }
David Rientjesb070e652013-05-07 16:18:09 -07003671 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003672
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003673 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003674 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003675 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003676 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003677 ret = 0;
3678unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003679 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003680 /*
3681 * check events
3682 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003683 memcg_check_events(to, page);
3684 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003685out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003686 return ret;
3687}
3688
Michal Hocko2ef37d32012-10-26 13:37:30 +02003689/**
3690 * mem_cgroup_move_parent - moves page to the parent group
3691 * @page: the page to move
3692 * @pc: page_cgroup of the page
3693 * @child: page's cgroup
3694 *
3695 * move charges to its parent or the root cgroup if the group has no
3696 * parent (aka use_hierarchy==0).
3697 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3698 * mem_cgroup_move_account fails) the failure is always temporary and
3699 * it signals a race with a page removal/uncharge or migration. In the
3700 * first case the page is on the way out and it will vanish from the LRU
3701 * on the next attempt and the call should be retried later.
3702 * Isolation from the LRU fails only if page has been isolated from
3703 * the LRU since we looked at it and that usually means either global
3704 * reclaim or migration going on. The page will either get back to the
3705 * LRU or vanish.
3706 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3707 * (!PageCgroupUsed) or moved to a different group. The page will
3708 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003710static int mem_cgroup_move_parent(struct page *page,
3711 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003712 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003713{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003714 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003715 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003716 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003717 int ret;
3718
Michal Hockod8423012012-10-26 13:37:29 +02003719 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003720
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003721 ret = -EBUSY;
3722 if (!get_page_unless_zero(page))
3723 goto out;
3724 if (isolate_lru_page(page))
3725 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003726
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003727 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003728
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003729 parent = parent_mem_cgroup(child);
3730 /*
3731 * If no parent, move charges to root cgroup.
3732 */
3733 if (!parent)
3734 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003735
Michal Hocko2ef37d32012-10-26 13:37:30 +02003736 if (nr_pages > 1) {
3737 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003738 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003739 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003740
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003741 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003742 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003743 if (!ret)
3744 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003745
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003746 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003747 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003748 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003749put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003750 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003751out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003752 return ret;
3753}
3754
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003755/*
3756 * Charge the memory controller for page usage.
3757 * Return
3758 * 0 if the charge was successful
3759 * < 0 if the cgroup is over its limit
3760 */
3761static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003762 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003763{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003764 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003765 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003766 bool oom = true;
3767 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003768
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003769 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003770 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003771 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003772 /*
3773 * Never OOM-kill a process for a huge page. The
3774 * fault handler will fall back to regular pages.
3775 */
3776 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003777 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003778
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003779 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003780 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003781 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003782 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003783 return 0;
3784}
3785
3786int mem_cgroup_newpage_charge(struct page *page,
3787 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003788{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003789 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003790 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003791 VM_BUG_ON(page_mapped(page));
3792 VM_BUG_ON(page->mapping && !PageAnon(page));
3793 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003794 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003795 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003796}
3797
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003798/*
3799 * While swap-in, try_charge -> commit or cancel, the page is locked.
3800 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003801 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003802 * "commit()" or removed by "cancel()"
3803 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003804static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3805 struct page *page,
3806 gfp_t mask,
3807 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003808{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003809 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003810 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003811 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003812
Johannes Weiner90deb782012-07-31 16:45:47 -07003813 pc = lookup_page_cgroup(page);
3814 /*
3815 * Every swap fault against a single page tries to charge the
3816 * page, bail as early as possible. shmem_unuse() encounters
3817 * already charged pages, too. The USED bit is protected by
3818 * the page lock, which serializes swap cache removal, which
3819 * in turn serializes uncharging.
3820 */
3821 if (PageCgroupUsed(pc))
3822 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003823 if (!do_swap_account)
3824 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003825 memcg = try_get_mem_cgroup_from_page(page);
3826 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003827 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003828 *memcgp = memcg;
3829 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003830 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003831 if (ret == -EINTR)
3832 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003833 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003834charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003835 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3836 if (ret == -EINTR)
3837 ret = 0;
3838 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003839}
3840
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003841int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3842 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3843{
3844 *memcgp = NULL;
3845 if (mem_cgroup_disabled())
3846 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003847 /*
3848 * A racing thread's fault, or swapoff, may have already
3849 * updated the pte, and even removed page from swap cache: in
3850 * those cases unuse_pte()'s pte_same() test will fail; but
3851 * there's also a KSM case which does need to charge the page.
3852 */
3853 if (!PageSwapCache(page)) {
3854 int ret;
3855
3856 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3857 if (ret == -EINTR)
3858 ret = 0;
3859 return ret;
3860 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003861 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3862}
3863
Johannes Weiner827a03d2012-07-31 16:45:36 -07003864void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3865{
3866 if (mem_cgroup_disabled())
3867 return;
3868 if (!memcg)
3869 return;
3870 __mem_cgroup_cancel_charge(memcg, 1);
3871}
3872
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003873static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003874__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003875 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003876{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003877 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003878 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003879 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003880 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003881
Johannes Weinerce587e62012-04-24 20:22:33 +02003882 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003883 /*
3884 * Now swap is on-memory. This means this page may be
3885 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003886 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3887 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3888 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003889 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003890 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003891 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003892 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003893 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003894}
3895
Johannes Weiner72835c82012-01-12 17:18:32 -08003896void mem_cgroup_commit_charge_swapin(struct page *page,
3897 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003898{
Johannes Weiner72835c82012-01-12 17:18:32 -08003899 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003900 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003901}
3902
Johannes Weiner827a03d2012-07-31 16:45:36 -07003903int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3904 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003905{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003906 struct mem_cgroup *memcg = NULL;
3907 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3908 int ret;
3909
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003910 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003911 return 0;
3912 if (PageCompound(page))
3913 return 0;
3914
Johannes Weiner827a03d2012-07-31 16:45:36 -07003915 if (!PageSwapCache(page))
3916 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3917 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003918 ret = __mem_cgroup_try_charge_swapin(mm, page,
3919 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003920 if (!ret)
3921 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3922 }
3923 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003924}
3925
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003926static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003927 unsigned int nr_pages,
3928 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003929{
3930 struct memcg_batch_info *batch = NULL;
3931 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003932
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003933 /* If swapout, usage of swap doesn't decrease */
3934 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3935 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003936
3937 batch = &current->memcg_batch;
3938 /*
3939 * In usual, we do css_get() when we remember memcg pointer.
3940 * But in this case, we keep res->usage until end of a series of
3941 * uncharges. Then, it's ok to ignore memcg's refcnt.
3942 */
3943 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003944 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003945 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003946 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003947 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003948 * the same cgroup and we have chance to coalesce uncharges.
3949 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3950 * because we want to do uncharge as soon as possible.
3951 */
3952
3953 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3954 goto direct_uncharge;
3955
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003956 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003957 goto direct_uncharge;
3958
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003959 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003960 * In typical case, batch->memcg == mem. This means we can
3961 * merge a series of uncharges to an uncharge of res_counter.
3962 * If not, we uncharge res_counter ony by one.
3963 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003964 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003965 goto direct_uncharge;
3966 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003967 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003968 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003969 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003970 return;
3971direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003972 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003973 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003974 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3975 if (unlikely(batch->memcg != memcg))
3976 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003977}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003978
Balbir Singh8697d332008-02-07 00:13:59 -08003979/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003980 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003981 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003982static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003983__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3984 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003985{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003986 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003987 unsigned int nr_pages = 1;
3988 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003989 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003990
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003991 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003992 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003993
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003994 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003995 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003996 VM_BUG_ON(!PageTransHuge(page));
3997 }
Balbir Singh8697d332008-02-07 00:13:59 -08003998 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003999 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004000 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004001 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004002 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004003 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004004
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004005 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004006
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004007 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004008
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004009 if (!PageCgroupUsed(pc))
4010 goto unlock_out;
4011
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004012 anon = PageAnon(page);
4013
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004014 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004015 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004016 /*
4017 * Generally PageAnon tells if it's the anon statistics to be
4018 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4019 * used before page reached the stage of being marked PageAnon.
4020 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004021 anon = true;
4022 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004023 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004024 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004025 if (page_mapped(page))
4026 goto unlock_out;
4027 /*
4028 * Pages under migration may not be uncharged. But
4029 * end_migration() /must/ be the one uncharging the
4030 * unused post-migration page and so it has to call
4031 * here with the migration bit still set. See the
4032 * res_counter handling below.
4033 */
4034 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004035 goto unlock_out;
4036 break;
4037 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4038 if (!PageAnon(page)) { /* Shared memory */
4039 if (page->mapping && !page_is_file_cache(page))
4040 goto unlock_out;
4041 } else if (page_mapped(page)) /* Anon */
4042 goto unlock_out;
4043 break;
4044 default:
4045 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004046 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004047
David Rientjesb070e652013-05-07 16:18:09 -07004048 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004049
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004050 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004051 /*
4052 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4053 * freed from LRU. This is safe because uncharged page is expected not
4054 * to be reused (freed soon). Exception is SwapCache, it's handled by
4055 * special functions.
4056 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004057
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004058 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004059 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004060 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004061 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004062 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004063 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004064 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004065 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004066 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004067 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004068 /*
4069 * Migration does not charge the res_counter for the
4070 * replacement page, so leave it alone when phasing out the
4071 * page that is unused after the migration.
4072 */
4073 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004074 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004076 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004077
4078unlock_out:
4079 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004080 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004081}
4082
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004083void mem_cgroup_uncharge_page(struct page *page)
4084{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004085 /* early check. */
4086 if (page_mapped(page))
4087 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004088 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004089 /*
4090 * If the page is in swap cache, uncharge should be deferred
4091 * to the swap path, which also properly accounts swap usage
4092 * and handles memcg lifetime.
4093 *
4094 * Note that this check is not stable and reclaim may add the
4095 * page to swap cache at any time after this. However, if the
4096 * page is not in swap cache by the time page->mapcount hits
4097 * 0, there won't be any page table references to the swap
4098 * slot, and reclaim will free it and not actually write the
4099 * page to disk.
4100 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004101 if (PageSwapCache(page))
4102 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004103 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004104}
4105
4106void mem_cgroup_uncharge_cache_page(struct page *page)
4107{
4108 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004109 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004110 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004111}
4112
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004113/*
4114 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4115 * In that cases, pages are freed continuously and we can expect pages
4116 * are in the same memcg. All these calls itself limits the number of
4117 * pages freed at once, then uncharge_start/end() is called properly.
4118 * This may be called prural(2) times in a context,
4119 */
4120
4121void mem_cgroup_uncharge_start(void)
4122{
4123 current->memcg_batch.do_batch++;
4124 /* We can do nest. */
4125 if (current->memcg_batch.do_batch == 1) {
4126 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004127 current->memcg_batch.nr_pages = 0;
4128 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004129 }
4130}
4131
4132void mem_cgroup_uncharge_end(void)
4133{
4134 struct memcg_batch_info *batch = &current->memcg_batch;
4135
4136 if (!batch->do_batch)
4137 return;
4138
4139 batch->do_batch--;
4140 if (batch->do_batch) /* If stacked, do nothing. */
4141 return;
4142
4143 if (!batch->memcg)
4144 return;
4145 /*
4146 * This "batch->memcg" is valid without any css_get/put etc...
4147 * bacause we hide charges behind us.
4148 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004149 if (batch->nr_pages)
4150 res_counter_uncharge(&batch->memcg->res,
4151 batch->nr_pages * PAGE_SIZE);
4152 if (batch->memsw_nr_pages)
4153 res_counter_uncharge(&batch->memcg->memsw,
4154 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004155 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004156 /* forget this pointer (for sanity check) */
4157 batch->memcg = NULL;
4158}
4159
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004160#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004161/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004162 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004163 * memcg information is recorded to swap_cgroup of "ent"
4164 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004165void
4166mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004167{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004168 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004169 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004170
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004171 if (!swapout) /* this was a swap cache but the swap is unused ! */
4172 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4173
Johannes Weiner0030f532012-07-31 16:45:25 -07004174 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004175
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004176 /*
4177 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004178 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004179 */
4180 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004181 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004182}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004183#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004184
Andrew Mortonc255a452012-07-31 16:43:02 -07004185#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004186/*
4187 * called from swap_entry_free(). remove record in swap_cgroup and
4188 * uncharge "memsw" account.
4189 */
4190void mem_cgroup_uncharge_swap(swp_entry_t ent)
4191{
4192 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004193 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004194
4195 if (!do_swap_account)
4196 return;
4197
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004198 id = swap_cgroup_record(ent, 0);
4199 rcu_read_lock();
4200 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004201 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004202 /*
4203 * We uncharge this because swap is freed.
4204 * This memcg can be obsolete one. We avoid calling css_tryget
4205 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004206 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004207 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004208 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004209 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004210 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004211 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004212}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004213
4214/**
4215 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4216 * @entry: swap entry to be moved
4217 * @from: mem_cgroup which the entry is moved from
4218 * @to: mem_cgroup which the entry is moved to
4219 *
4220 * It succeeds only when the swap_cgroup's record for this entry is the same
4221 * as the mem_cgroup's id of @from.
4222 *
4223 * Returns 0 on success, -EINVAL on failure.
4224 *
4225 * The caller must have charged to @to, IOW, called res_counter_charge() about
4226 * both res and memsw, and called css_get().
4227 */
4228static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004229 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004230{
4231 unsigned short old_id, new_id;
4232
4233 old_id = css_id(&from->css);
4234 new_id = css_id(&to->css);
4235
4236 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004237 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004238 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004239 /*
4240 * This function is only called from task migration context now.
4241 * It postpones res_counter and refcount handling till the end
4242 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004243 * improvement. But we cannot postpone css_get(to) because if
4244 * the process that has been moved to @to does swap-in, the
4245 * refcount of @to might be decreased to 0.
4246 *
4247 * We are in attach() phase, so the cgroup is guaranteed to be
4248 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004249 */
Li Zefan40503772013-07-08 16:00:34 -07004250 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004251 return 0;
4252 }
4253 return -EINVAL;
4254}
4255#else
4256static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004257 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004258{
4259 return -EINVAL;
4260}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004261#endif
4262
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004263/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004264 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4265 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004266 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004267void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4268 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004269{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004270 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004271 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004272 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004273 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004274
Johannes Weiner72835c82012-01-12 17:18:32 -08004275 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004276
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004277 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004278 return;
Balbir Singh40779602008-04-04 14:29:59 -07004279
Mel Gormanb32967f2012-11-19 12:35:47 +00004280 if (PageTransHuge(page))
4281 nr_pages <<= compound_order(page);
4282
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004283 pc = lookup_page_cgroup(page);
4284 lock_page_cgroup(pc);
4285 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004286 memcg = pc->mem_cgroup;
4287 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004288 /*
4289 * At migrating an anonymous page, its mapcount goes down
4290 * to 0 and uncharge() will be called. But, even if it's fully
4291 * unmapped, migration may fail and this page has to be
4292 * charged again. We set MIGRATION flag here and delay uncharge
4293 * until end_migration() is called
4294 *
4295 * Corner Case Thinking
4296 * A)
4297 * When the old page was mapped as Anon and it's unmap-and-freed
4298 * while migration was ongoing.
4299 * If unmap finds the old page, uncharge() of it will be delayed
4300 * until end_migration(). If unmap finds a new page, it's
4301 * uncharged when it make mapcount to be 1->0. If unmap code
4302 * finds swap_migration_entry, the new page will not be mapped
4303 * and end_migration() will find it(mapcount==0).
4304 *
4305 * B)
4306 * When the old page was mapped but migraion fails, the kernel
4307 * remaps it. A charge for it is kept by MIGRATION flag even
4308 * if mapcount goes down to 0. We can do remap successfully
4309 * without charging it again.
4310 *
4311 * C)
4312 * The "old" page is under lock_page() until the end of
4313 * migration, so, the old page itself will not be swapped-out.
4314 * If the new page is swapped out before end_migraton, our
4315 * hook to usual swap-out path will catch the event.
4316 */
4317 if (PageAnon(page))
4318 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004319 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004320 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004321 /*
4322 * If the page is not charged at this point,
4323 * we return here.
4324 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004325 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004326 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004327
Johannes Weiner72835c82012-01-12 17:18:32 -08004328 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004329 /*
4330 * We charge new page before it's used/mapped. So, even if unlock_page()
4331 * is called before end_migration, we can catch all events on this new
4332 * page. In the case new page is migrated but not remapped, new page's
4333 * mapcount will be finally 0 and we call uncharge in end_migration().
4334 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004335 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004336 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004337 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004338 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004339 /*
4340 * The page is committed to the memcg, but it's not actually
4341 * charged to the res_counter since we plan on replacing the
4342 * old one and only one page is going to be left afterwards.
4343 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004344 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004345}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004346
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004347/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004348void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004349 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004350{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004351 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004352 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004353 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004354
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004355 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004356 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004357
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004358 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004359 used = oldpage;
4360 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004361 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004362 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004363 unused = oldpage;
4364 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004365 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004366 __mem_cgroup_uncharge_common(unused,
4367 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4368 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4369 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004370 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004371 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004372 * We disallowed uncharge of pages under migration because mapcount
4373 * of the page goes down to zero, temporarly.
4374 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004375 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004376 pc = lookup_page_cgroup(oldpage);
4377 lock_page_cgroup(pc);
4378 ClearPageCgroupMigration(pc);
4379 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004380
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004381 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004382 * If a page is a file cache, radix-tree replacement is very atomic
4383 * and we can skip this check. When it was an Anon page, its mapcount
4384 * goes down to 0. But because we added MIGRATION flage, it's not
4385 * uncharged yet. There are several case but page->mapcount check
4386 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4387 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004388 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004389 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004390 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004391}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004392
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004393/*
4394 * At replace page cache, newpage is not under any memcg but it's on
4395 * LRU. So, this function doesn't touch res_counter but handles LRU
4396 * in correct way. Both pages are locked so we cannot race with uncharge.
4397 */
4398void mem_cgroup_replace_page_cache(struct page *oldpage,
4399 struct page *newpage)
4400{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004401 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004402 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004403 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004404
4405 if (mem_cgroup_disabled())
4406 return;
4407
4408 pc = lookup_page_cgroup(oldpage);
4409 /* fix accounting on old pages */
4410 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004411 if (PageCgroupUsed(pc)) {
4412 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004413 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004414 ClearPageCgroupUsed(pc);
4415 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004416 unlock_page_cgroup(pc);
4417
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004418 /*
4419 * When called from shmem_replace_page(), in some cases the
4420 * oldpage has already been charged, and in some cases not.
4421 */
4422 if (!memcg)
4423 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004424 /*
4425 * Even if newpage->mapping was NULL before starting replacement,
4426 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4427 * LRU while we overwrite pc->mem_cgroup.
4428 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004429 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004430}
4431
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004432#ifdef CONFIG_DEBUG_VM
4433static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4434{
4435 struct page_cgroup *pc;
4436
4437 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004438 /*
4439 * Can be NULL while feeding pages into the page allocator for
4440 * the first time, i.e. during boot or memory hotplug;
4441 * or when mem_cgroup_disabled().
4442 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004443 if (likely(pc) && PageCgroupUsed(pc))
4444 return pc;
4445 return NULL;
4446}
4447
4448bool mem_cgroup_bad_page_check(struct page *page)
4449{
4450 if (mem_cgroup_disabled())
4451 return false;
4452
4453 return lookup_page_cgroup_used(page) != NULL;
4454}
4455
4456void mem_cgroup_print_bad_page(struct page *page)
4457{
4458 struct page_cgroup *pc;
4459
4460 pc = lookup_page_cgroup_used(page);
4461 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004462 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4463 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004464 }
4465}
4466#endif
4467
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004468static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004469 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004470{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004471 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004472 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004473 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004474 int children = mem_cgroup_count_children(memcg);
4475 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004476 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004477
4478 /*
4479 * For keeping hierarchical_reclaim simple, how long we should retry
4480 * is depends on callers. We set our retry-count to be function
4481 * of # of children which we should visit in this loop.
4482 */
4483 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4484
4485 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004486
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004487 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004488 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004489 if (signal_pending(current)) {
4490 ret = -EINTR;
4491 break;
4492 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004493 /*
4494 * Rather than hide all in some function, I do this in
4495 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004496 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004497 */
4498 mutex_lock(&set_limit_mutex);
4499 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4500 if (memswlimit < val) {
4501 ret = -EINVAL;
4502 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004503 break;
4504 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004505
4506 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4507 if (memlimit < val)
4508 enlarge = 1;
4509
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004510 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004511 if (!ret) {
4512 if (memswlimit == val)
4513 memcg->memsw_is_minimum = true;
4514 else
4515 memcg->memsw_is_minimum = false;
4516 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004517 mutex_unlock(&set_limit_mutex);
4518
4519 if (!ret)
4520 break;
4521
Johannes Weiner56600482012-01-12 17:17:59 -08004522 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4523 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004524 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4525 /* Usage is reduced ? */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004526 if (curusage >= oldusage)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004527 retry_count--;
4528 else
4529 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004530 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004531 if (!ret && enlarge)
4532 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004533
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004534 return ret;
4535}
4536
Li Zefan338c8432009-06-17 16:27:15 -07004537static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4538 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004539{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004540 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004541 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004542 int children = mem_cgroup_count_children(memcg);
4543 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004544 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004545
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004546 /* see mem_cgroup_resize_res_limit */
Andrew Mortonf894ffa2013-09-12 15:13:35 -07004547 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004548 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004549 while (retry_count) {
4550 if (signal_pending(current)) {
4551 ret = -EINTR;
4552 break;
4553 }
4554 /*
4555 * Rather than hide all in some function, I do this in
4556 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004557 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004558 */
4559 mutex_lock(&set_limit_mutex);
4560 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4561 if (memlimit > val) {
4562 ret = -EINVAL;
4563 mutex_unlock(&set_limit_mutex);
4564 break;
4565 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004566 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4567 if (memswlimit < val)
4568 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004569 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004570 if (!ret) {
4571 if (memlimit == val)
4572 memcg->memsw_is_minimum = true;
4573 else
4574 memcg->memsw_is_minimum = false;
4575 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004576 mutex_unlock(&set_limit_mutex);
4577
4578 if (!ret)
4579 break;
4580
Johannes Weiner56600482012-01-12 17:17:59 -08004581 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4582 MEM_CGROUP_RECLAIM_NOSWAP |
4583 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004584 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004585 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004586 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004587 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004588 else
4589 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004590 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004591 if (!ret && enlarge)
4592 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004593 return ret;
4594}
4595
Michal Hocko2ef37d32012-10-26 13:37:30 +02004596/**
4597 * mem_cgroup_force_empty_list - clears LRU of a group
4598 * @memcg: group to clear
4599 * @node: NUMA node
4600 * @zid: zone id
4601 * @lru: lru to to clear
4602 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004603 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004604 * reclaim the pages page themselves - pages are moved to the parent (or root)
4605 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004606 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004607static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004608 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004609{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004610 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004611 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004612 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004613 struct page *busy;
4614 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004615
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004616 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004617 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4618 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004619
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004620 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004621 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004622 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004623 struct page *page;
4624
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004625 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004626 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004627 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004628 break;
4629 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004630 page = list_entry(list->prev, struct page, lru);
4631 if (busy == page) {
4632 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004633 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004634 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004635 continue;
4636 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004637 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004638
Johannes Weiner925b7672012-01-12 17:18:15 -08004639 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004640
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004641 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004642 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004643 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004644 cond_resched();
4645 } else
4646 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004647 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004648}
4649
4650/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004651 * make mem_cgroup's charge to be 0 if there is no task by moving
4652 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004653 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004654 *
4655 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004656 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004657static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004658{
Michal Hockoc26251f2012-10-26 13:37:28 +02004659 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004660 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004661
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004662 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004663 /* This is for making all *used* pages to be on LRU. */
4664 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004665 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004666 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004667 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004668 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004669 enum lru_list lru;
4670 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004671 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004672 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004673 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004674 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004675 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004676 mem_cgroup_end_move(memcg);
4677 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004678 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004679
Michal Hocko2ef37d32012-10-26 13:37:30 +02004680 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004681 * Kernel memory may not necessarily be trackable to a specific
4682 * process. So they are not migrated, and therefore we can't
4683 * expect their value to drop to 0 here.
4684 * Having res filled up with kmem only is enough.
4685 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004686 * This is a safety check because mem_cgroup_force_empty_list
4687 * could have raced with mem_cgroup_replace_page_cache callers
4688 * so the lru seemed empty but the page could have been added
4689 * right after the check. RES_USAGE should be safe as we always
4690 * charge before adding to the LRU.
4691 */
Glauber Costabea207c2012-12-18 14:22:11 -08004692 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4693 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4694 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004695}
4696
4697/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004698 * This mainly exists for tests during the setting of set of use_hierarchy.
4699 * Since this is the very setting we are changing, the current hierarchy value
4700 * is meaningless
4701 */
4702static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4703{
Tejun Heo492eb212013-08-08 20:11:25 -04004704 struct cgroup_subsys_state *pos;
Glauber Costab5f99b52013-02-22 16:34:53 -08004705
4706 /* bounce at first found */
Tejun Heo492eb212013-08-08 20:11:25 -04004707 css_for_each_child(pos, &memcg->css)
Glauber Costab5f99b52013-02-22 16:34:53 -08004708 return true;
4709 return false;
4710}
4711
4712/*
Glauber Costa09998212013-02-22 16:34:55 -08004713 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4714 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004715 * from mem_cgroup_count_children(), in the sense that we don't really care how
4716 * many children we have; we only need to know if we have any. It also counts
4717 * any memcg without hierarchy as infertile.
4718 */
4719static inline bool memcg_has_children(struct mem_cgroup *memcg)
4720{
4721 return memcg->use_hierarchy && __memcg_has_children(memcg);
4722}
4723
4724/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004725 * Reclaims as many pages from the given memcg as possible and moves
4726 * the rest to the parent.
4727 *
4728 * Caller is responsible for holding css reference for memcg.
4729 */
4730static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4731{
4732 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4733 struct cgroup *cgrp = memcg->css.cgroup;
4734
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004735 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004736 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4737 return -EBUSY;
4738
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004739 /* we call try-to-free pages for make this cgroup empty */
4740 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004741 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004742 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004743 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004744
Michal Hockoc26251f2012-10-26 13:37:28 +02004745 if (signal_pending(current))
4746 return -EINTR;
4747
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004748 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004749 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004750 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004751 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004752 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004753 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004754 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004755
4756 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004757 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004758 mem_cgroup_reparent_charges(memcg);
4759
4760 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004761}
4762
Tejun Heo182446d2013-08-08 20:11:24 -04004763static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
4764 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004765{
Tejun Heo182446d2013-08-08 20:11:24 -04004766 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02004767
Michal Hockod8423012012-10-26 13:37:29 +02004768 if (mem_cgroup_is_root(memcg))
4769 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07004770 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004771}
4772
Tejun Heo182446d2013-08-08 20:11:24 -04004773static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
4774 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004775{
Tejun Heo182446d2013-08-08 20:11:24 -04004776 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004777}
4778
Tejun Heo182446d2013-08-08 20:11:24 -04004779static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4780 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004781{
4782 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004783 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04004784 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08004785
Glauber Costa09998212013-02-22 16:34:55 -08004786 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004787
4788 if (memcg->use_hierarchy == val)
4789 goto out;
4790
Balbir Singh18f59ea2009-01-07 18:08:07 -08004791 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004792 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004793 * in the child subtrees. If it is unset, then the change can
4794 * occur, provided the current cgroup has no children.
4795 *
4796 * For the root cgroup, parent_mem is NULL, we allow value to be
4797 * set if there are no children.
4798 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004799 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004800 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004801 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004802 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004803 else
4804 retval = -EBUSY;
4805 } else
4806 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004807
4808out:
Glauber Costa09998212013-02-22 16:34:55 -08004809 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004810
4811 return retval;
4812}
4813
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004816 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004817{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004818 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004819 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004820
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004821 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004822 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004823 val += mem_cgroup_read_stat(iter, idx);
4824
4825 if (val < 0) /* race ? */
4826 val = 0;
4827 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004828}
4829
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004830static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004831{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004832 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004833
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004834 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004835 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004836 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004837 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004838 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004839 }
4840
David Rientjesb070e652013-05-07 16:18:09 -07004841 /*
4842 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4843 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4844 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004845 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4846 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004847
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004848 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004849 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004850
4851 return val << PAGE_SHIFT;
4852}
4853
Tejun Heo182446d2013-08-08 20:11:24 -04004854static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
4855 struct cftype *cft, struct file *file,
4856 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004857{
Tejun Heo182446d2013-08-08 20:11:24 -04004858 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07004859 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004860 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004861 int name, len;
4862 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004863
4864 type = MEMFILE_TYPE(cft->private);
4865 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004866
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004867 switch (type) {
4868 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004869 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004870 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004871 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004872 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004873 break;
4874 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004875 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004876 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004877 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004878 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004879 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004880 case _KMEM:
4881 val = res_counter_read_u64(&memcg->kmem, name);
4882 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004883 default:
4884 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004885 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004886
4887 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4888 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004889}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004890
Tejun Heo182446d2013-08-08 20:11:24 -04004891static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004892{
4893 int ret = -EINVAL;
4894#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04004895 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004896 /*
4897 * For simplicity, we won't allow this to be disabled. It also can't
4898 * be changed if the cgroup has children already, or if tasks had
4899 * already joined.
4900 *
4901 * If tasks join before we set the limit, a person looking at
4902 * kmem.usage_in_bytes will have no way to determine when it took
4903 * place, which makes the value quite meaningless.
4904 *
4905 * After it first became limited, changes in the value of the limit are
4906 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004907 */
Glauber Costa09998212013-02-22 16:34:55 -08004908 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004909 mutex_lock(&set_limit_mutex);
4910 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04004911 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004912 ret = -EBUSY;
4913 goto out;
4914 }
4915 ret = res_counter_set_limit(&memcg->kmem, val);
4916 VM_BUG_ON(ret);
4917
Glauber Costa55007d82012-12-18 14:22:38 -08004918 ret = memcg_update_cache_sizes(memcg);
4919 if (ret) {
4920 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
4921 goto out;
4922 }
Glauber Costa692e89a2013-02-22 16:34:56 -08004923 static_key_slow_inc(&memcg_kmem_enabled_key);
4924 /*
4925 * setting the active bit after the inc will guarantee no one
4926 * starts accounting before all call sites are patched
4927 */
4928 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004929 } else
4930 ret = res_counter_set_limit(&memcg->kmem, val);
4931out:
4932 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08004933 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004934#endif
4935 return ret;
4936}
4937
Hugh Dickins6d0439902013-02-22 16:35:50 -08004938#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08004939static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004940{
Glauber Costa55007d82012-12-18 14:22:38 -08004941 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004942 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4943 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08004944 goto out;
4945
Glauber Costa510fc4e2012-12-18 14:21:47 -08004946 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08004947 /*
4948 * When that happen, we need to disable the static branch only on those
4949 * memcgs that enabled it. To achieve this, we would be forced to
4950 * complicate the code by keeping track of which memcgs were the ones
4951 * that actually enabled limits, and which ones got it from its
4952 * parents.
4953 *
4954 * It is a lot simpler just to do static_key_slow_inc() on every child
4955 * that is accounted.
4956 */
Glauber Costa55007d82012-12-18 14:22:38 -08004957 if (!memcg_kmem_is_active(memcg))
4958 goto out;
4959
4960 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07004961 * __mem_cgroup_free() will issue static_key_slow_dec() because this
4962 * memcg is active already. If the later initialization fails then the
4963 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08004964 */
Glauber Costa55007d82012-12-18 14:22:38 -08004965 static_key_slow_inc(&memcg_kmem_enabled_key);
4966
4967 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07004968 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004969 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07004970 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004971 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08004972out:
4973 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004974}
Hugh Dickins6d0439902013-02-22 16:35:50 -08004975#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08004976
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004977/*
4978 * The user of this function is...
4979 * RES_LIMIT.
4980 */
Tejun Heo182446d2013-08-08 20:11:24 -04004981static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07004982 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004983{
Tejun Heo182446d2013-08-08 20:11:24 -04004984 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004985 enum res_type type;
4986 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004987 unsigned long long val;
4988 int ret;
4989
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004990 type = MEMFILE_TYPE(cft->private);
4991 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004992
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004993 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004994 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004995 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4996 ret = -EINVAL;
4997 break;
4998 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004999 /* This function does all necessary parse...reuse it */
5000 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005001 if (ret)
5002 break;
5003 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005004 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005005 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005006 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005007 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04005008 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005009 else
5010 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005011 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005012 case RES_SOFT_LIMIT:
5013 ret = res_counter_memparse_write_strategy(buffer, &val);
5014 if (ret)
5015 break;
5016 /*
5017 * For memsw, soft limits are hard to implement in terms
5018 * of semantics, for now, we support soft limits for
5019 * control without swap
5020 */
5021 if (type == _MEM)
5022 ret = res_counter_set_soft_limit(&memcg->res, val);
5023 else
5024 ret = -EINVAL;
5025 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005026 default:
5027 ret = -EINVAL; /* should be BUG() ? */
5028 break;
5029 }
5030 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005031}
5032
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005033static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5034 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5035{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005036 unsigned long long min_limit, min_memsw_limit, tmp;
5037
5038 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5039 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005040 if (!memcg->use_hierarchy)
5041 goto out;
5042
Tejun Heo63876982013-08-08 20:11:23 -04005043 while (css_parent(&memcg->css)) {
5044 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005045 if (!memcg->use_hierarchy)
5046 break;
5047 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5048 min_limit = min(min_limit, tmp);
5049 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5050 min_memsw_limit = min(min_memsw_limit, tmp);
5051 }
5052out:
5053 *mem_limit = min_limit;
5054 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005055}
5056
Tejun Heo182446d2013-08-08 20:11:24 -04005057static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005058{
Tejun Heo182446d2013-08-08 20:11:24 -04005059 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005060 int name;
5061 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005062
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005063 type = MEMFILE_TYPE(event);
5064 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005065
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005066 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005067 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005068 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005069 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005070 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005071 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005072 else if (type == _KMEM)
5073 res_counter_reset_max(&memcg->kmem);
5074 else
5075 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005076 break;
5077 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005078 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005079 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005080 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005081 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005082 else if (type == _KMEM)
5083 res_counter_reset_failcnt(&memcg->kmem);
5084 else
5085 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005086 break;
5087 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005088
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005089 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005090}
5091
Tejun Heo182446d2013-08-08 20:11:24 -04005092static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005093 struct cftype *cft)
5094{
Tejun Heo182446d2013-08-08 20:11:24 -04005095 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005096}
5097
Daisuke Nishimura02491442010-03-10 15:22:17 -08005098#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005099static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005100 struct cftype *cft, u64 val)
5101{
Tejun Heo182446d2013-08-08 20:11:24 -04005102 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005103
5104 if (val >= (1 << NR_MOVE_TYPE))
5105 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005106
Glauber Costaee5e8472013-02-22 16:34:50 -08005107 /*
5108 * No kind of locking is needed in here, because ->can_attach() will
5109 * check this value once in the beginning of the process, and then carry
5110 * on with stale data. This means that changes to this value will only
5111 * affect task migrations starting after the change.
5112 */
5113 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005114 return 0;
5115}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005116#else
Tejun Heo182446d2013-08-08 20:11:24 -04005117static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005118 struct cftype *cft, u64 val)
5119{
5120 return -ENOSYS;
5121}
5122#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005123
Ying Han406eb0c2011-05-26 16:25:37 -07005124#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005125static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5126 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005127{
5128 int nid;
5129 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5130 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005131 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005132
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005133 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005134 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005135 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005136 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005137 seq_printf(m, " N%d=%lu", nid, node_nr);
5138 }
5139 seq_putc(m, '\n');
5140
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005141 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005142 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005143 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005144 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005145 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005146 seq_printf(m, " N%d=%lu", nid, node_nr);
5147 }
5148 seq_putc(m, '\n');
5149
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005150 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005151 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005152 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005153 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005154 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005155 seq_printf(m, " N%d=%lu", nid, node_nr);
5156 }
5157 seq_putc(m, '\n');
5158
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005159 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005160 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005161 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005162 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005163 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005164 seq_printf(m, " N%d=%lu", nid, node_nr);
5165 }
5166 seq_putc(m, '\n');
5167 return 0;
5168}
5169#endif /* CONFIG_NUMA */
5170
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005171static inline void mem_cgroup_lru_names_not_uptodate(void)
5172{
5173 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5174}
5175
Tejun Heo182446d2013-08-08 20:11:24 -04005176static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005177 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005178{
Tejun Heo182446d2013-08-08 20:11:24 -04005179 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005180 struct mem_cgroup *mi;
5181 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005182
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005183 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005184 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005185 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005186 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5187 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005188 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005189
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005190 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5191 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5192 mem_cgroup_read_events(memcg, i));
5193
5194 for (i = 0; i < NR_LRU_LISTS; i++)
5195 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5196 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5197
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005198 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005199 {
5200 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005201 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005202 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005203 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005204 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5205 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005206 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005207
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005208 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5209 long long val = 0;
5210
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005211 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005212 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005213 for_each_mem_cgroup_tree(mi, memcg)
5214 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5215 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5216 }
5217
5218 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5219 unsigned long long val = 0;
5220
5221 for_each_mem_cgroup_tree(mi, memcg)
5222 val += mem_cgroup_read_events(mi, i);
5223 seq_printf(m, "total_%s %llu\n",
5224 mem_cgroup_events_names[i], val);
5225 }
5226
5227 for (i = 0; i < NR_LRU_LISTS; i++) {
5228 unsigned long long val = 0;
5229
5230 for_each_mem_cgroup_tree(mi, memcg)
5231 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5232 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005233 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005234
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005235#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005236 {
5237 int nid, zid;
5238 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005239 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005240 unsigned long recent_rotated[2] = {0, 0};
5241 unsigned long recent_scanned[2] = {0, 0};
5242
5243 for_each_online_node(nid)
5244 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005245 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005246 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005247
Hugh Dickins89abfab2012-05-29 15:06:53 -07005248 recent_rotated[0] += rstat->recent_rotated[0];
5249 recent_rotated[1] += rstat->recent_rotated[1];
5250 recent_scanned[0] += rstat->recent_scanned[0];
5251 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005252 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005253 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5254 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5255 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5256 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005257 }
5258#endif
5259
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005260 return 0;
5261}
5262
Tejun Heo182446d2013-08-08 20:11:24 -04005263static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5264 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005265{
Tejun Heo182446d2013-08-08 20:11:24 -04005266 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005267
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005268 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005269}
5270
Tejun Heo182446d2013-08-08 20:11:24 -04005271static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5272 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005273{
Tejun Heo182446d2013-08-08 20:11:24 -04005274 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005275 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005276
Tejun Heo63876982013-08-08 20:11:23 -04005277 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005278 return -EINVAL;
5279
Glauber Costa09998212013-02-22 16:34:55 -08005280 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005281
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005282 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005283 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005284 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005285 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005286 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005287
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005288 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005289
Glauber Costa09998212013-02-22 16:34:55 -08005290 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005291
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005292 return 0;
5293}
5294
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005295static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5296{
5297 struct mem_cgroup_threshold_ary *t;
5298 u64 usage;
5299 int i;
5300
5301 rcu_read_lock();
5302 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005303 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005304 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005305 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005306
5307 if (!t)
5308 goto unlock;
5309
5310 usage = mem_cgroup_usage(memcg, swap);
5311
5312 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005313 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005314 * If it's not true, a threshold was crossed after last
5315 * call of __mem_cgroup_threshold().
5316 */
Phil Carmody5407a562010-05-26 14:42:42 -07005317 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005318
5319 /*
5320 * Iterate backward over array of thresholds starting from
5321 * current_threshold and check if a threshold is crossed.
5322 * If none of thresholds below usage is crossed, we read
5323 * only one element of the array here.
5324 */
5325 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5326 eventfd_signal(t->entries[i].eventfd, 1);
5327
5328 /* i = current_threshold + 1 */
5329 i++;
5330
5331 /*
5332 * Iterate forward over array of thresholds starting from
5333 * current_threshold+1 and check if a threshold is crossed.
5334 * If none of thresholds above usage is crossed, we read
5335 * only one element of the array here.
5336 */
5337 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5338 eventfd_signal(t->entries[i].eventfd, 1);
5339
5340 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005341 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005342unlock:
5343 rcu_read_unlock();
5344}
5345
5346static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5347{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005348 while (memcg) {
5349 __mem_cgroup_threshold(memcg, false);
5350 if (do_swap_account)
5351 __mem_cgroup_threshold(memcg, true);
5352
5353 memcg = parent_mem_cgroup(memcg);
5354 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005355}
5356
5357static int compare_thresholds(const void *a, const void *b)
5358{
5359 const struct mem_cgroup_threshold *_a = a;
5360 const struct mem_cgroup_threshold *_b = b;
5361
Greg Thelen2bff24a2013-09-11 14:23:08 -07005362 if (_a->threshold > _b->threshold)
5363 return 1;
5364
5365 if (_a->threshold < _b->threshold)
5366 return -1;
5367
5368 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005369}
5370
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005371static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005372{
5373 struct mem_cgroup_eventfd_list *ev;
5374
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005375 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005376 eventfd_signal(ev->eventfd, 1);
5377 return 0;
5378}
5379
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005380static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005381{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005382 struct mem_cgroup *iter;
5383
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005384 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005385 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005386}
5387
Tejun Heo81eeaf02013-08-08 20:11:26 -04005388static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005389 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005390{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005391 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005392 struct mem_cgroup_thresholds *thresholds;
5393 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005394 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005395 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005396 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005397
5398 ret = res_counter_memparse_write_strategy(args, &threshold);
5399 if (ret)
5400 return ret;
5401
5402 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005403
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005404 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005405 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005406 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005407 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005408 else
5409 BUG();
5410
5411 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5412
5413 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005414 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005415 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5416
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005417 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005418
5419 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005420 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005421 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005422 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005423 ret = -ENOMEM;
5424 goto unlock;
5425 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005426 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005427
5428 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005429 if (thresholds->primary) {
5430 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005431 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005432 }
5433
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005434 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005435 new->entries[size - 1].eventfd = eventfd;
5436 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005437
5438 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005439 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005440 compare_thresholds, NULL);
5441
5442 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005443 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005444 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005445 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005446 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005447 * new->current_threshold will not be used until
5448 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005449 * it here.
5450 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005451 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005452 } else
5453 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005454 }
5455
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005456 /* Free old spare buffer and save old primary buffer as spare */
5457 kfree(thresholds->spare);
5458 thresholds->spare = thresholds->primary;
5459
5460 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005461
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005462 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005463 synchronize_rcu();
5464
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005465unlock:
5466 mutex_unlock(&memcg->thresholds_lock);
5467
5468 return ret;
5469}
5470
Tejun Heo81eeaf02013-08-08 20:11:26 -04005471static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005472 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005473{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005474 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005475 struct mem_cgroup_thresholds *thresholds;
5476 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005477 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005478 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005479 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005480
5481 mutex_lock(&memcg->thresholds_lock);
5482 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005483 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005484 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005485 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005486 else
5487 BUG();
5488
Anton Vorontsov371528c2012-02-24 05:14:46 +04005489 if (!thresholds->primary)
5490 goto unlock;
5491
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005492 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5493
5494 /* Check if a threshold crossed before removing */
5495 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5496
5497 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005498 size = 0;
5499 for (i = 0; i < thresholds->primary->size; i++) {
5500 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005501 size++;
5502 }
5503
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005504 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005505
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506 /* Set thresholds array to NULL if we don't have thresholds */
5507 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005508 kfree(new);
5509 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005510 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005511 }
5512
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005513 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005514
5515 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005516 new->current_threshold = -1;
5517 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5518 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005519 continue;
5520
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005521 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005522 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005523 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005524 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005525 * until rcu_assign_pointer(), so it's safe to increment
5526 * it here.
5527 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005528 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529 }
5530 j++;
5531 }
5532
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005533swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005534 /* Swap primary and spare array */
5535 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005536 /* If all events are unregistered, free the spare array */
5537 if (!new) {
5538 kfree(thresholds->spare);
5539 thresholds->spare = NULL;
5540 }
5541
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005542 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005543
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005544 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005545 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005546unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005547 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005548}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005549
Tejun Heo81eeaf02013-08-08 20:11:26 -04005550static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005551 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5552{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005553 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005554 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005555 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005556
5557 BUG_ON(type != _OOM_TYPE);
5558 event = kmalloc(sizeof(*event), GFP_KERNEL);
5559 if (!event)
5560 return -ENOMEM;
5561
Michal Hocko1af8efe2011-07-26 16:08:24 -07005562 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005563
5564 event->eventfd = eventfd;
5565 list_add(&event->list, &memcg->oom_notify);
5566
5567 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005568 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005569 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005570 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005571
5572 return 0;
5573}
5574
Tejun Heo81eeaf02013-08-08 20:11:26 -04005575static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005576 struct cftype *cft, struct eventfd_ctx *eventfd)
5577{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005578 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005579 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005580 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005581
5582 BUG_ON(type != _OOM_TYPE);
5583
Michal Hocko1af8efe2011-07-26 16:08:24 -07005584 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005585
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005586 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005587 if (ev->eventfd == eventfd) {
5588 list_del(&ev->list);
5589 kfree(ev);
5590 }
5591 }
5592
Michal Hocko1af8efe2011-07-26 16:08:24 -07005593 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005594}
5595
Tejun Heo182446d2013-08-08 20:11:24 -04005596static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005597 struct cftype *cft, struct cgroup_map_cb *cb)
5598{
Tejun Heo182446d2013-08-08 20:11:24 -04005599 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005600
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005601 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005602
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005603 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005604 cb->fill(cb, "under_oom", 1);
5605 else
5606 cb->fill(cb, "under_oom", 0);
5607 return 0;
5608}
5609
Tejun Heo182446d2013-08-08 20:11:24 -04005610static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005611 struct cftype *cft, u64 val)
5612{
Tejun Heo182446d2013-08-08 20:11:24 -04005613 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005614 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005615
5616 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005617 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005618 return -EINVAL;
5619
Glauber Costa09998212013-02-22 16:34:55 -08005620 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005621 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005622 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005623 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005624 return -EINVAL;
5625 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005626 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005627 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005628 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005629 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005630 return 0;
5631}
5632
Andrew Mortonc255a452012-07-31 16:43:02 -07005633#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005634static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005635{
Glauber Costa55007d82012-12-18 14:22:38 -08005636 int ret;
5637
Glauber Costa2633d7a2012-12-18 14:22:34 -08005638 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005639 ret = memcg_propagate_kmem(memcg);
5640 if (ret)
5641 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005642
Glauber Costa1d62e432012-04-09 19:36:33 -03005643 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005644}
Glauber Costae5671df2011-12-11 21:47:01 +00005645
Li Zefan10d5ebf2013-07-08 16:00:33 -07005646static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005647{
Glauber Costa1d62e432012-04-09 19:36:33 -03005648 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005649}
5650
5651static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5652{
5653 if (!memcg_kmem_is_active(memcg))
5654 return;
5655
5656 /*
5657 * kmem charges can outlive the cgroup. In the case of slab
5658 * pages, for instance, a page contain objects from various
5659 * processes. As we prevent from taking a reference for every
5660 * such allocation we have to be careful when doing uncharge
5661 * (see memcg_uncharge_kmem) and here during offlining.
5662 *
5663 * The idea is that that only the _last_ uncharge which sees
5664 * the dead memcg will drop the last reference. An additional
5665 * reference is taken here before the group is marked dead
5666 * which is then paired with css_put during uncharge resp. here.
5667 *
5668 * Although this might sound strange as this path is called from
5669 * css_offline() when the referencemight have dropped down to 0
5670 * and shouldn't be incremented anymore (css_tryget would fail)
5671 * we do not have other options because of the kmem allocations
5672 * lifetime.
5673 */
5674 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005675
5676 memcg_kmem_mark_dead(memcg);
5677
5678 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5679 return;
5680
Glauber Costa7de37682012-12-18 14:22:07 -08005681 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005682 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005683}
Glauber Costae5671df2011-12-11 21:47:01 +00005684#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005685static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005686{
5687 return 0;
5688}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005689
Li Zefan10d5ebf2013-07-08 16:00:33 -07005690static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5691{
5692}
5693
5694static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005695{
5696}
Glauber Costae5671df2011-12-11 21:47:01 +00005697#endif
5698
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005699static struct cftype mem_cgroup_files[] = {
5700 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005701 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005702 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005703 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005704 .register_event = mem_cgroup_usage_register_event,
5705 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005706 },
5707 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005708 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005709 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005710 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005711 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005712 },
5713 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005714 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005715 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005716 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005717 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005718 },
5719 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005720 .name = "soft_limit_in_bytes",
5721 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5722 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005723 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005724 },
5725 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005726 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005727 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005728 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005729 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005730 },
Balbir Singh8697d332008-02-07 00:13:59 -08005731 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005732 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005733 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005734 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005735 {
5736 .name = "force_empty",
5737 .trigger = mem_cgroup_force_empty_write,
5738 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005739 {
5740 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005741 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005742 .write_u64 = mem_cgroup_hierarchy_write,
5743 .read_u64 = mem_cgroup_hierarchy_read,
5744 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005745 {
5746 .name = "swappiness",
5747 .read_u64 = mem_cgroup_swappiness_read,
5748 .write_u64 = mem_cgroup_swappiness_write,
5749 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005750 {
5751 .name = "move_charge_at_immigrate",
5752 .read_u64 = mem_cgroup_move_charge_read,
5753 .write_u64 = mem_cgroup_move_charge_write,
5754 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005755 {
5756 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005757 .read_map = mem_cgroup_oom_control_read,
5758 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005759 .register_event = mem_cgroup_oom_register_event,
5760 .unregister_event = mem_cgroup_oom_unregister_event,
5761 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5762 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005763 {
5764 .name = "pressure_level",
5765 .register_event = vmpressure_register_event,
5766 .unregister_event = vmpressure_unregister_event,
5767 },
Ying Han406eb0c2011-05-26 16:25:37 -07005768#ifdef CONFIG_NUMA
5769 {
5770 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005771 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005772 },
5773#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005774#ifdef CONFIG_MEMCG_KMEM
5775 {
5776 .name = "kmem.limit_in_bytes",
5777 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5778 .write_string = mem_cgroup_write,
5779 .read = mem_cgroup_read,
5780 },
5781 {
5782 .name = "kmem.usage_in_bytes",
5783 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5784 .read = mem_cgroup_read,
5785 },
5786 {
5787 .name = "kmem.failcnt",
5788 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5789 .trigger = mem_cgroup_reset,
5790 .read = mem_cgroup_read,
5791 },
5792 {
5793 .name = "kmem.max_usage_in_bytes",
5794 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5795 .trigger = mem_cgroup_reset,
5796 .read = mem_cgroup_read,
5797 },
Glauber Costa749c5412012-12-18 14:23:01 -08005798#ifdef CONFIG_SLABINFO
5799 {
5800 .name = "kmem.slabinfo",
5801 .read_seq_string = mem_cgroup_slabinfo_read,
5802 },
5803#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005804#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005805 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005806};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005807
Michal Hocko2d110852013-02-22 16:34:43 -08005808#ifdef CONFIG_MEMCG_SWAP
5809static struct cftype memsw_cgroup_files[] = {
5810 {
5811 .name = "memsw.usage_in_bytes",
5812 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5813 .read = mem_cgroup_read,
5814 .register_event = mem_cgroup_usage_register_event,
5815 .unregister_event = mem_cgroup_usage_unregister_event,
5816 },
5817 {
5818 .name = "memsw.max_usage_in_bytes",
5819 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5820 .trigger = mem_cgroup_reset,
5821 .read = mem_cgroup_read,
5822 },
5823 {
5824 .name = "memsw.limit_in_bytes",
5825 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5826 .write_string = mem_cgroup_write,
5827 .read = mem_cgroup_read,
5828 },
5829 {
5830 .name = "memsw.failcnt",
5831 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5832 .trigger = mem_cgroup_reset,
5833 .read = mem_cgroup_read,
5834 },
5835 { }, /* terminate */
5836};
5837#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005838static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005839{
5840 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005841 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005842 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005843 /*
5844 * This routine is called against possible nodes.
5845 * But it's BUG to call kmalloc() against offline node.
5846 *
5847 * TODO: this routine can waste much memory for nodes which will
5848 * never be onlined. It's better to use memory hotplug callback
5849 * function.
5850 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005851 if (!node_state(node, N_NORMAL_MEMORY))
5852 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005853 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005854 if (!pn)
5855 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005856
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005857 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5858 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005859 lruvec_init(&mz->lruvec);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005860 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005861 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005862 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005863 return 0;
5864}
5865
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005866static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005867{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005868 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005869}
5870
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005871static struct mem_cgroup *mem_cgroup_alloc(void)
5872{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005873 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005874 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005875
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005876 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005877 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005878 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005879 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005880 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005881
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005882 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005883 return NULL;
5884
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005885 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5886 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005887 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005888 spin_lock_init(&memcg->pcp_counter_lock);
5889 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005890
5891out_free:
5892 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005893 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005894 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005895 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005896 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005897}
5898
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005899/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005900 * At destroying mem_cgroup, references from swap_cgroup can remain.
5901 * (scanning all at force_empty is too costly...)
5902 *
5903 * Instead of clearing all references at force_empty, we remember
5904 * the number of reference from swap_cgroup and free mem_cgroup when
5905 * it goes down to 0.
5906 *
5907 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005908 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005909
5910static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005911{
Glauber Costac8b2a362012-12-18 14:22:13 -08005912 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005913 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005914
Glauber Costac8b2a362012-12-18 14:22:13 -08005915 free_css_id(&mem_cgroup_subsys, &memcg->css);
5916
5917 for_each_node(node)
5918 free_mem_cgroup_per_zone_info(memcg, node);
5919
5920 free_percpu(memcg->stat);
5921
Glauber Costa3f134612012-05-29 15:07:11 -07005922 /*
5923 * We need to make sure that (at least for now), the jump label
5924 * destruction code runs outside of the cgroup lock. This is because
5925 * get_online_cpus(), which is called from the static_branch update,
5926 * can't be called inside the cgroup_lock. cpusets are the ones
5927 * enforcing this dependency, so if they ever change, we might as well.
5928 *
5929 * schedule_work() will guarantee this happens. Be careful if you need
5930 * to move this code around, and make sure it is outside
5931 * the cgroup_lock.
5932 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005933 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005934 if (size < PAGE_SIZE)
5935 kfree(memcg);
5936 else
5937 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005938}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005939
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005940/*
5941 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5942 */
Glauber Costae1aab162011-12-11 21:47:03 +00005943struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005944{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005945 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005946 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005947 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005948}
Glauber Costae1aab162011-12-11 21:47:03 +00005949EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005950
Li Zefan0eb253e2009-01-15 13:51:25 -08005951static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04005952mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005953{
Glauber Costad142e3e2013-02-22 16:34:52 -08005954 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005955 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005956 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005957
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005958 memcg = mem_cgroup_alloc();
5959 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005960 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005961
Bob Liu3ed28fa2012-01-12 17:19:04 -08005962 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005963 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005964 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005965
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005966 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04005967 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08005968 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08005969 res_counter_init(&memcg->res, NULL);
5970 res_counter_init(&memcg->memsw, NULL);
5971 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005972 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005973
Glauber Costad142e3e2013-02-22 16:34:52 -08005974 memcg->last_scanned_node = MAX_NUMNODES;
5975 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005976 memcg->move_charge_at_immigrate = 0;
5977 mutex_init(&memcg->thresholds_lock);
5978 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005979 vmpressure_init(&memcg->vmpressure);
Michal Hocko7d910c02013-09-12 15:13:28 -07005980 spin_lock_init(&memcg->soft_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08005981
5982 return &memcg->css;
5983
5984free_out:
5985 __mem_cgroup_free(memcg);
5986 return ERR_PTR(error);
5987}
5988
5989static int
Tejun Heoeb954192013-08-08 20:11:23 -04005990mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005991{
Tejun Heoeb954192013-08-08 20:11:23 -04005992 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5993 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08005994 int error = 0;
5995
Tejun Heo63876982013-08-08 20:11:23 -04005996 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08005997 return 0;
5998
Glauber Costa09998212013-02-22 16:34:55 -08005999 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006000
6001 memcg->use_hierarchy = parent->use_hierarchy;
6002 memcg->oom_kill_disable = parent->oom_kill_disable;
6003 memcg->swappiness = mem_cgroup_swappiness(parent);
6004
6005 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006006 res_counter_init(&memcg->res, &parent->res);
6007 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006008 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006009
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006010 /*
Li Zefan8d76a972013-07-08 16:00:36 -07006011 * No need to take a reference to the parent because cgroup
6012 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006013 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08006014 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006015 res_counter_init(&memcg->res, NULL);
6016 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006017 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006018 /*
6019 * Deeper hierachy with use_hierarchy == false doesn't make
6020 * much sense so let cgroup subsystem know about this
6021 * unfortunate state in our controller.
6022 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006023 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006024 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006025 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006026
6027 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006028 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006029 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006030}
6031
Michal Hocko5f578162013-04-29 15:07:17 -07006032/*
6033 * Announce all parents that a group from their hierarchy is gone.
6034 */
6035static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6036{
6037 struct mem_cgroup *parent = memcg;
6038
6039 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006040 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006041
6042 /*
6043 * if the root memcg is not hierarchical we have to check it
6044 * explicitely.
6045 */
6046 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006047 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006048}
6049
Tejun Heoeb954192013-08-08 20:11:23 -04006050static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006051{
Tejun Heoeb954192013-08-08 20:11:23 -04006052 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006053
Li Zefan10d5ebf2013-07-08 16:00:33 -07006054 kmem_cgroup_css_offline(memcg);
6055
Michal Hocko5f578162013-04-29 15:07:17 -07006056 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006057 mem_cgroup_reparent_charges(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -07006058 if (memcg->soft_contributed) {
6059 while ((memcg = parent_mem_cgroup(memcg)))
6060 atomic_dec(&memcg->children_in_excess);
Michal Hocko1be171d2013-09-12 15:13:32 -07006061
6062 if (memcg != root_mem_cgroup && !root_mem_cgroup->use_hierarchy)
6063 atomic_dec(&root_mem_cgroup->children_in_excess);
Michal Hocko7d910c02013-09-12 15:13:28 -07006064 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08006065 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006066 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006067}
6068
Tejun Heoeb954192013-08-08 20:11:23 -04006069static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006070{
Tejun Heoeb954192013-08-08 20:11:23 -04006071 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006072
Li Zefan10d5ebf2013-07-08 16:00:33 -07006073 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006074 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006075}
6076
Daisuke Nishimura02491442010-03-10 15:22:17 -08006077#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006078/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006079#define PRECHARGE_COUNT_AT_ONCE 256
6080static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006081{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006082 int ret = 0;
6083 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006084 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006085
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006086 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006087 mc.precharge += count;
6088 /* we don't need css_get for root */
6089 return ret;
6090 }
6091 /* try to charge at once */
6092 if (count > 1) {
6093 struct res_counter *dummy;
6094 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006095 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006096 * by cgroup_lock_live_cgroup() that it is not removed and we
6097 * are still under the same cgroup_mutex. So we can postpone
6098 * css_get().
6099 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006100 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006101 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006102 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006103 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006104 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006105 goto one_by_one;
6106 }
6107 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006108 return ret;
6109 }
6110one_by_one:
6111 /* fall back to one by one charge */
6112 while (count--) {
6113 if (signal_pending(current)) {
6114 ret = -EINTR;
6115 break;
6116 }
6117 if (!batch_count--) {
6118 batch_count = PRECHARGE_COUNT_AT_ONCE;
6119 cond_resched();
6120 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006121 ret = __mem_cgroup_try_charge(NULL,
6122 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006123 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006124 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006125 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006126 mc.precharge++;
6127 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006128 return ret;
6129}
6130
6131/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006132 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006133 * @vma: the vma the pte to be checked belongs
6134 * @addr: the address corresponding to the pte to be checked
6135 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006136 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006137 *
6138 * Returns
6139 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6140 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6141 * move charge. if @target is not NULL, the page is stored in target->page
6142 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006143 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6144 * target for charge migration. if @target is not NULL, the entry is stored
6145 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006146 *
6147 * Called with pte lock held.
6148 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006149union mc_target {
6150 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006151 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006152};
6153
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006154enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006155 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006156 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006157 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006158};
6159
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006160static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6161 unsigned long addr, pte_t ptent)
6162{
6163 struct page *page = vm_normal_page(vma, addr, ptent);
6164
6165 if (!page || !page_mapped(page))
6166 return NULL;
6167 if (PageAnon(page)) {
6168 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006169 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006170 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006171 } else if (!move_file())
6172 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006173 return NULL;
6174 if (!get_page_unless_zero(page))
6175 return NULL;
6176
6177 return page;
6178}
6179
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006180#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006181static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6182 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6183{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006184 struct page *page = NULL;
6185 swp_entry_t ent = pte_to_swp_entry(ptent);
6186
6187 if (!move_anon() || non_swap_entry(ent))
6188 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006189 /*
6190 * Because lookup_swap_cache() updates some statistics counter,
6191 * we call find_get_page() with swapper_space directly.
6192 */
Shaohua Li33806f02013-02-22 16:34:37 -08006193 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006194 if (do_swap_account)
6195 entry->val = ent.val;
6196
6197 return page;
6198}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006199#else
6200static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6201 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6202{
6203 return NULL;
6204}
6205#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006206
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006207static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6208 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6209{
6210 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006211 struct address_space *mapping;
6212 pgoff_t pgoff;
6213
6214 if (!vma->vm_file) /* anonymous vma */
6215 return NULL;
6216 if (!move_file())
6217 return NULL;
6218
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006219 mapping = vma->vm_file->f_mapping;
6220 if (pte_none(ptent))
6221 pgoff = linear_page_index(vma, addr);
6222 else /* pte_file(ptent) is true */
6223 pgoff = pte_to_pgoff(ptent);
6224
6225 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006226 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006227
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006228#ifdef CONFIG_SWAP
6229 /* shmem/tmpfs may report page out on swap: account for that too. */
6230 if (radix_tree_exceptional_entry(page)) {
6231 swp_entry_t swap = radix_to_swp_entry(page);
6232 if (do_swap_account)
6233 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006234 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006235 }
6236#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006237 return page;
6238}
6239
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006240static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006241 unsigned long addr, pte_t ptent, union mc_target *target)
6242{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006243 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006244 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006245 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006246 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006247
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006248 if (pte_present(ptent))
6249 page = mc_handle_present_pte(vma, addr, ptent);
6250 else if (is_swap_pte(ptent))
6251 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006252 else if (pte_none(ptent) || pte_file(ptent))
6253 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006254
6255 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006256 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006257 if (page) {
6258 pc = lookup_page_cgroup(page);
6259 /*
6260 * Do only loose check w/o page_cgroup lock.
6261 * mem_cgroup_move_account() checks the pc is valid or not under
6262 * the lock.
6263 */
6264 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6265 ret = MC_TARGET_PAGE;
6266 if (target)
6267 target->page = page;
6268 }
6269 if (!ret || !target)
6270 put_page(page);
6271 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006272 /* There is a swap entry and a page doesn't exist or isn't charged */
6273 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006274 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006275 ret = MC_TARGET_SWAP;
6276 if (target)
6277 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006278 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006279 return ret;
6280}
6281
Naoya Horiguchi12724852012-03-21 16:34:28 -07006282#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6283/*
6284 * We don't consider swapping or file mapped pages because THP does not
6285 * support them for now.
6286 * Caller should make sure that pmd_trans_huge(pmd) is true.
6287 */
6288static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6289 unsigned long addr, pmd_t pmd, union mc_target *target)
6290{
6291 struct page *page = NULL;
6292 struct page_cgroup *pc;
6293 enum mc_target_type ret = MC_TARGET_NONE;
6294
6295 page = pmd_page(pmd);
6296 VM_BUG_ON(!page || !PageHead(page));
6297 if (!move_anon())
6298 return ret;
6299 pc = lookup_page_cgroup(page);
6300 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6301 ret = MC_TARGET_PAGE;
6302 if (target) {
6303 get_page(page);
6304 target->page = page;
6305 }
6306 }
6307 return ret;
6308}
6309#else
6310static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6311 unsigned long addr, pmd_t pmd, union mc_target *target)
6312{
6313 return MC_TARGET_NONE;
6314}
6315#endif
6316
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006317static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6318 unsigned long addr, unsigned long end,
6319 struct mm_walk *walk)
6320{
6321 struct vm_area_struct *vma = walk->private;
6322 pte_t *pte;
6323 spinlock_t *ptl;
6324
Naoya Horiguchi12724852012-03-21 16:34:28 -07006325 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6326 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6327 mc.precharge += HPAGE_PMD_NR;
6328 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006329 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006330 }
Dave Hansen03319322011-03-22 16:32:56 -07006331
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006332 if (pmd_trans_unstable(pmd))
6333 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006334 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6335 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006336 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006337 mc.precharge++; /* increment precharge temporarily */
6338 pte_unmap_unlock(pte - 1, ptl);
6339 cond_resched();
6340
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006341 return 0;
6342}
6343
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006344static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6345{
6346 unsigned long precharge;
6347 struct vm_area_struct *vma;
6348
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006349 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006350 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6351 struct mm_walk mem_cgroup_count_precharge_walk = {
6352 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6353 .mm = mm,
6354 .private = vma,
6355 };
6356 if (is_vm_hugetlb_page(vma))
6357 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006358 walk_page_range(vma->vm_start, vma->vm_end,
6359 &mem_cgroup_count_precharge_walk);
6360 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006361 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006362
6363 precharge = mc.precharge;
6364 mc.precharge = 0;
6365
6366 return precharge;
6367}
6368
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006369static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6370{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006371 unsigned long precharge = mem_cgroup_count_precharge(mm);
6372
6373 VM_BUG_ON(mc.moving_task);
6374 mc.moving_task = current;
6375 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006376}
6377
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006378/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6379static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006380{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006381 struct mem_cgroup *from = mc.from;
6382 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006383 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006384
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006385 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006386 if (mc.precharge) {
6387 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6388 mc.precharge = 0;
6389 }
6390 /*
6391 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6392 * we must uncharge here.
6393 */
6394 if (mc.moved_charge) {
6395 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6396 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006397 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006398 /* we must fixup refcnts and charges */
6399 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006400 /* uncharge swap account from the old cgroup */
6401 if (!mem_cgroup_is_root(mc.from))
6402 res_counter_uncharge(&mc.from->memsw,
6403 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006404
6405 for (i = 0; i < mc.moved_swap; i++)
6406 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006407
6408 if (!mem_cgroup_is_root(mc.to)) {
6409 /*
6410 * we charged both to->res and to->memsw, so we should
6411 * uncharge to->res.
6412 */
6413 res_counter_uncharge(&mc.to->res,
6414 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006415 }
Li Zefan40503772013-07-08 16:00:34 -07006416 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006417 mc.moved_swap = 0;
6418 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006419 memcg_oom_recover(from);
6420 memcg_oom_recover(to);
6421 wake_up_all(&mc.waitq);
6422}
6423
6424static void mem_cgroup_clear_mc(void)
6425{
6426 struct mem_cgroup *from = mc.from;
6427
6428 /*
6429 * we must clear moving_task before waking up waiters at the end of
6430 * task migration.
6431 */
6432 mc.moving_task = NULL;
6433 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006434 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435 mc.from = NULL;
6436 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006437 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006438 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006439}
6440
Tejun Heoeb954192013-08-08 20:11:23 -04006441static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006442 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006443{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006444 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006445 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006446 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006447 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006448
Glauber Costaee5e8472013-02-22 16:34:50 -08006449 /*
6450 * We are now commited to this value whatever it is. Changes in this
6451 * tunable will only affect upcoming migrations, not the current one.
6452 * So we need to save it, and keep it going.
6453 */
6454 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6455 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006456 struct mm_struct *mm;
6457 struct mem_cgroup *from = mem_cgroup_from_task(p);
6458
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006459 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006460
6461 mm = get_task_mm(p);
6462 if (!mm)
6463 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006464 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006465 if (mm->owner == p) {
6466 VM_BUG_ON(mc.from);
6467 VM_BUG_ON(mc.to);
6468 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006469 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006470 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006471 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006472 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006473 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006474 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006475 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006476 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006477 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006478
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006479 ret = mem_cgroup_precharge_mc(mm);
6480 if (ret)
6481 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006482 }
6483 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006484 }
6485 return ret;
6486}
6487
Tejun Heoeb954192013-08-08 20:11:23 -04006488static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006489 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006490{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006491 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006492}
6493
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006494static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6495 unsigned long addr, unsigned long end,
6496 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006497{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006498 int ret = 0;
6499 struct vm_area_struct *vma = walk->private;
6500 pte_t *pte;
6501 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006502 enum mc_target_type target_type;
6503 union mc_target target;
6504 struct page *page;
6505 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006506
Naoya Horiguchi12724852012-03-21 16:34:28 -07006507 /*
6508 * We don't take compound_lock() here but no race with splitting thp
6509 * happens because:
6510 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6511 * under splitting, which means there's no concurrent thp split,
6512 * - if another thread runs into split_huge_page() just after we
6513 * entered this if-block, the thread must wait for page table lock
6514 * to be unlocked in __split_huge_page_splitting(), where the main
6515 * part of thp split is not executed yet.
6516 */
6517 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006518 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006519 spin_unlock(&vma->vm_mm->page_table_lock);
6520 return 0;
6521 }
6522 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6523 if (target_type == MC_TARGET_PAGE) {
6524 page = target.page;
6525 if (!isolate_lru_page(page)) {
6526 pc = lookup_page_cgroup(page);
6527 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006528 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006529 mc.precharge -= HPAGE_PMD_NR;
6530 mc.moved_charge += HPAGE_PMD_NR;
6531 }
6532 putback_lru_page(page);
6533 }
6534 put_page(page);
6535 }
6536 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006537 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006538 }
6539
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006540 if (pmd_trans_unstable(pmd))
6541 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006542retry:
6543 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6544 for (; addr != end; addr += PAGE_SIZE) {
6545 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006546 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006547
6548 if (!mc.precharge)
6549 break;
6550
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006551 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006552 case MC_TARGET_PAGE:
6553 page = target.page;
6554 if (isolate_lru_page(page))
6555 goto put;
6556 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006557 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006558 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006559 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006560 /* we uncharge from mc.from later. */
6561 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006562 }
6563 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006564put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006565 put_page(page);
6566 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006567 case MC_TARGET_SWAP:
6568 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006569 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006570 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006571 /* we fixup refcnts and charges later. */
6572 mc.moved_swap++;
6573 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006574 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006575 default:
6576 break;
6577 }
6578 }
6579 pte_unmap_unlock(pte - 1, ptl);
6580 cond_resched();
6581
6582 if (addr != end) {
6583 /*
6584 * We have consumed all precharges we got in can_attach().
6585 * We try charge one by one, but don't do any additional
6586 * charges to mc.to if we have failed in charge once in attach()
6587 * phase.
6588 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006589 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006590 if (!ret)
6591 goto retry;
6592 }
6593
6594 return ret;
6595}
6596
6597static void mem_cgroup_move_charge(struct mm_struct *mm)
6598{
6599 struct vm_area_struct *vma;
6600
6601 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006602retry:
6603 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6604 /*
6605 * Someone who are holding the mmap_sem might be waiting in
6606 * waitq. So we cancel all extra charges, wake up all waiters,
6607 * and retry. Because we cancel precharges, we might not be able
6608 * to move enough charges, but moving charge is a best-effort
6609 * feature anyway, so it wouldn't be a big problem.
6610 */
6611 __mem_cgroup_clear_mc();
6612 cond_resched();
6613 goto retry;
6614 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006615 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6616 int ret;
6617 struct mm_walk mem_cgroup_move_charge_walk = {
6618 .pmd_entry = mem_cgroup_move_charge_pte_range,
6619 .mm = mm,
6620 .private = vma,
6621 };
6622 if (is_vm_hugetlb_page(vma))
6623 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006624 ret = walk_page_range(vma->vm_start, vma->vm_end,
6625 &mem_cgroup_move_charge_walk);
6626 if (ret)
6627 /*
6628 * means we have consumed all precharges and failed in
6629 * doing additional charge. Just abandon here.
6630 */
6631 break;
6632 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006633 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006634}
6635
Tejun Heoeb954192013-08-08 20:11:23 -04006636static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006637 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006638{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006639 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006640 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006641
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006642 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006643 if (mc.to)
6644 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006645 mmput(mm);
6646 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006647 if (mc.to)
6648 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006649}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006650#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006651static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006652 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006653{
6654 return 0;
6655}
Tejun Heoeb954192013-08-08 20:11:23 -04006656static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006657 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006658{
6659}
Tejun Heoeb954192013-08-08 20:11:23 -04006660static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006661 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006662{
6663}
6664#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006665
Tejun Heof00baae2013-04-15 13:41:15 -07006666/*
6667 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6668 * to verify sane_behavior flag on each mount attempt.
6669 */
Tejun Heoeb954192013-08-08 20:11:23 -04006670static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006671{
6672 /*
6673 * use_hierarchy is forced with sane_behavior. cgroup core
6674 * guarantees that @root doesn't have any children, so turning it
6675 * on for the root memcg is enough.
6676 */
Tejun Heoeb954192013-08-08 20:11:23 -04006677 if (cgroup_sane_behavior(root_css->cgroup))
6678 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006679}
6680
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006681struct cgroup_subsys mem_cgroup_subsys = {
6682 .name = "memory",
6683 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006684 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006685 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006686 .css_offline = mem_cgroup_css_offline,
6687 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006688 .can_attach = mem_cgroup_can_attach,
6689 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006690 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006691 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006692 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006693 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006694 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006695};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006696
Andrew Mortonc255a452012-07-31 16:43:02 -07006697#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006698static int __init enable_swap_account(char *s)
6699{
Michal Hockoa2c89902011-05-24 17:12:50 -07006700 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006701 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006702 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006703 really_do_swap_account = 0;
6704 return 1;
6705}
Michal Hockoa2c89902011-05-24 17:12:50 -07006706__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006707
Michal Hocko2d110852013-02-22 16:34:43 -08006708static void __init memsw_file_init(void)
6709{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006710 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006711}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006712
6713static void __init enable_swap_cgroup(void)
6714{
6715 if (!mem_cgroup_disabled() && really_do_swap_account) {
6716 do_swap_account = 1;
6717 memsw_file_init();
6718 }
6719}
6720
Michal Hocko2d110852013-02-22 16:34:43 -08006721#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006722static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006723{
6724}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006725#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006726
6727/*
Michal Hocko10813122013-02-22 16:35:41 -08006728 * subsys_initcall() for memory controller.
6729 *
6730 * Some parts like hotcpu_notifier() have to be initialized from this context
6731 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6732 * everything that doesn't depend on a specific mem_cgroup structure should
6733 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006734 */
6735static int __init mem_cgroup_init(void)
6736{
6737 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006738 enable_swap_cgroup();
Michal Hockoe4777492013-02-22 16:35:40 -08006739 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006740 return 0;
6741}
6742subsys_initcall(mem_cgroup_init);