blob: a18e228f140bf8ade69625a00686f58a57c6b043 [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 */
282 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
327 * when it got over the soft limit.
328 * 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/*
Michal Hocko7d910c02013-09-12 15:13:28 -0700838 * Called from rate-limitted memcg_check_events when enough
839 * MEM_CGROUP_TARGET_SOFTLIMIT events are accumulated and it makes sure
840 * that all the parents up the hierarchy will be noticed that this group
841 * 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
843 * other.
844 */
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.
867 */
868 while (delta && (parent = parent_mem_cgroup(parent)))
869 atomic_add(delta, &parent->children_in_excess);
870 spin_unlock(&memcg->soft_lock);
871}
872
873/*
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800874 * Check events in order.
875 *
876 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700877static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800878{
Steven Rostedt47994012011-11-02 13:38:33 -0700879 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800880 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800881 if (unlikely(mem_cgroup_event_ratelimit(memcg,
882 MEM_CGROUP_TARGET_THRESH))) {
Michal Hocko7d910c02013-09-12 15:13:28 -0700883 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800884 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800885
Michal Hocko7d910c02013-09-12 15:13:28 -0700886 do_softlimit = mem_cgroup_event_ratelimit(memcg,
887 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700888#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800889 do_numainfo = mem_cgroup_event_ratelimit(memcg,
890 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700891#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800892 preempt_enable();
893
894 mem_cgroup_threshold(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -0700895 if (unlikely(do_softlimit))
896 mem_cgroup_update_soft_limit(memcg);
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800897#if MAX_NUMNODES > 1
898 if (unlikely(do_numainfo))
899 atomic_inc(&memcg->numainfo_events);
900#endif
901 } else
902 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800903}
904
Balbir Singhcf475ad2008-04-29 01:00:16 -0700905struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800906{
Balbir Singh31a78f22008-09-28 23:09:31 +0100907 /*
908 * mm_update_next_owner() may clear mm->owner to NULL
909 * if it races with swapoff, page migration, etc.
910 * So this can be called with p == NULL.
911 */
912 if (unlikely(!p))
913 return NULL;
914
Tejun Heo8af01f52013-08-08 20:11:22 -0400915 return mem_cgroup_from_css(task_css(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800916}
917
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700918struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800919{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700920 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700921
922 if (!mm)
923 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800924 /*
925 * Because we have no locks, mm->owner's may be being moved to other
926 * cgroup. We use css_tryget() here even if this looks
927 * pessimistic (rather than adding locks here).
928 */
929 rcu_read_lock();
930 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700931 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
932 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800933 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700934 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800935 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700936 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800937}
938
Michal Hockode577802013-09-12 15:13:26 -0700939static enum mem_cgroup_filter_t
940mem_cgroup_filter(struct mem_cgroup *memcg, struct mem_cgroup *root,
941 mem_cgroup_iter_filter cond)
942{
943 if (!cond)
944 return VISIT;
945 return cond(memcg, root);
946}
947
Michal Hocko16248d82013-04-29 15:07:19 -0700948/*
949 * Returns a next (in a pre-order walk) alive memcg (with elevated css
950 * ref. count) or NULL if the whole root's subtree has been visited.
951 *
952 * helper function to be used by mem_cgroup_iter
953 */
954static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
Michal Hockode577802013-09-12 15:13:26 -0700955 struct mem_cgroup *last_visited, mem_cgroup_iter_filter cond)
Michal Hocko16248d82013-04-29 15:07:19 -0700956{
Tejun Heo492eb212013-08-08 20:11:25 -0400957 struct cgroup_subsys_state *prev_css, *next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700958
Tejun Heobd8815a2013-08-08 20:11:27 -0400959 prev_css = last_visited ? &last_visited->css : NULL;
Michal Hocko16248d82013-04-29 15:07:19 -0700960skip_node:
Tejun Heo492eb212013-08-08 20:11:25 -0400961 next_css = css_next_descendant_pre(prev_css, &root->css);
Michal Hocko16248d82013-04-29 15:07:19 -0700962
963 /*
964 * Even if we found a group we have to make sure it is
965 * alive. css && !memcg means that the groups should be
966 * skipped and we should continue the tree walk.
967 * last_visited css is safe to use because it is
968 * protected by css_get and the tree walk is rcu safe.
969 */
Tejun Heo492eb212013-08-08 20:11:25 -0400970 if (next_css) {
971 struct mem_cgroup *mem = mem_cgroup_from_css(next_css);
972
Michal Hockode577802013-09-12 15:13:26 -0700973 switch (mem_cgroup_filter(mem, root, cond)) {
974 case SKIP:
Tejun Heo492eb212013-08-08 20:11:25 -0400975 prev_css = next_css;
Michal Hocko16248d82013-04-29 15:07:19 -0700976 goto skip_node;
Michal Hockode577802013-09-12 15:13:26 -0700977 case SKIP_TREE:
978 if (mem == root)
979 return NULL;
980 /*
981 * css_rightmost_descendant is not an optimal way to
982 * skip through a subtree (especially for imbalanced
983 * trees leaning to right) but that's what we have right
984 * now. More effective solution would be traversing
985 * right-up for first non-NULL without calling
986 * css_next_descendant_pre afterwards.
987 */
988 prev_css = css_rightmost_descendant(next_css);
989 goto skip_node;
990 case VISIT:
991 if (css_tryget(&mem->css))
992 return mem;
993 else {
994 prev_css = next_css;
995 goto skip_node;
996 }
997 break;
Michal Hocko16248d82013-04-29 15:07:19 -0700998 }
999 }
1000
1001 return NULL;
1002}
1003
Johannes Weiner519ebea2013-07-03 15:04:51 -07001004static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1005{
1006 /*
1007 * When a group in the hierarchy below root is destroyed, the
1008 * hierarchy iterator can no longer be trusted since it might
1009 * have pointed to the destroyed group. Invalidate it.
1010 */
1011 atomic_inc(&root->dead_count);
1012}
1013
1014static struct mem_cgroup *
1015mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1016 struct mem_cgroup *root,
1017 int *sequence)
1018{
1019 struct mem_cgroup *position = NULL;
1020 /*
1021 * A cgroup destruction happens in two stages: offlining and
1022 * release. They are separated by a RCU grace period.
1023 *
1024 * If the iterator is valid, we may still race with an
1025 * offlining. The RCU lock ensures the object won't be
1026 * released, tryget will fail if we lost the race.
1027 */
1028 *sequence = atomic_read(&root->dead_count);
1029 if (iter->last_dead_count == *sequence) {
1030 smp_rmb();
1031 position = iter->last_visited;
1032 if (position && !css_tryget(&position->css))
1033 position = NULL;
1034 }
1035 return position;
1036}
1037
1038static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1039 struct mem_cgroup *last_visited,
1040 struct mem_cgroup *new_position,
1041 int sequence)
1042{
1043 if (last_visited)
1044 css_put(&last_visited->css);
1045 /*
1046 * We store the sequence count from the time @last_visited was
1047 * loaded successfully instead of rereading it here so that we
1048 * don't lose destruction events in between. We could have
1049 * raced with the destruction of @new_position after all.
1050 */
1051 iter->last_visited = new_position;
1052 smp_wmb();
1053 iter->last_dead_count = sequence;
1054}
1055
Johannes Weiner56600482012-01-12 17:17:59 -08001056/**
1057 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1058 * @root: hierarchy root
1059 * @prev: previously returned memcg, NULL on first invocation
1060 * @reclaim: cookie for shared reclaim walks, NULL for full walks
Michal Hockode577802013-09-12 15:13:26 -07001061 * @cond: filter for visited nodes, NULL for no filter
Johannes Weiner56600482012-01-12 17:17:59 -08001062 *
1063 * Returns references to children of the hierarchy below @root, or
1064 * @root itself, or %NULL after a full round-trip.
1065 *
1066 * Caller must pass the return value in @prev on subsequent
1067 * invocations for reference counting, or use mem_cgroup_iter_break()
1068 * to cancel a hierarchy walk before the round-trip is complete.
1069 *
1070 * Reclaimers can specify a zone and a priority level in @reclaim to
1071 * divide up the memcgs in the hierarchy among all concurrent
1072 * reclaimers operating on the same zone and priority.
1073 */
Michal Hockode577802013-09-12 15:13:26 -07001074struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -08001075 struct mem_cgroup *prev,
Michal Hockode577802013-09-12 15:13:26 -07001076 struct mem_cgroup_reclaim_cookie *reclaim,
1077 mem_cgroup_iter_filter cond)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001078{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001079 struct mem_cgroup *memcg = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001080 struct mem_cgroup *last_visited = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001081
Michal Hockode577802013-09-12 15:13:26 -07001082 if (mem_cgroup_disabled()) {
1083 /* first call must return non-NULL, second return NULL */
1084 return (struct mem_cgroup *)(unsigned long)!prev;
1085 }
Johannes Weiner56600482012-01-12 17:17:59 -08001086
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001087 if (!root)
1088 root = root_mem_cgroup;
1089
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001090 if (prev && !reclaim)
Michal Hocko542f85f2013-04-29 15:07:15 -07001091 last_visited = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001092
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001093 if (!root->use_hierarchy && root != root_mem_cgroup) {
1094 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001095 goto out_css_put;
Michal Hockode577802013-09-12 15:13:26 -07001096 if (mem_cgroup_filter(root, root, cond) == VISIT)
1097 return root;
1098 return NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001099 }
1100
Michal Hocko542f85f2013-04-29 15:07:15 -07001101 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001102 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001103 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner519ebea2013-07-03 15:04:51 -07001104 int uninitialized_var(seq);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001105
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001106 if (reclaim) {
1107 int nid = zone_to_nid(reclaim->zone);
1108 int zid = zone_idx(reclaim->zone);
1109 struct mem_cgroup_per_zone *mz;
1110
1111 mz = mem_cgroup_zoneinfo(root, nid, zid);
1112 iter = &mz->reclaim_iter[reclaim->priority];
Michal Hocko542f85f2013-04-29 15:07:15 -07001113 if (prev && reclaim->generation != iter->generation) {
Michal Hocko5f578162013-04-29 15:07:17 -07001114 iter->last_visited = NULL;
Michal Hocko542f85f2013-04-29 15:07:15 -07001115 goto out_unlock;
1116 }
Michal Hocko5f578162013-04-29 15:07:17 -07001117
Johannes Weiner519ebea2013-07-03 15:04:51 -07001118 last_visited = mem_cgroup_iter_load(iter, root, &seq);
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001119 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001120
Michal Hockode577802013-09-12 15:13:26 -07001121 memcg = __mem_cgroup_iter_next(root, last_visited, cond);
Michal Hocko542f85f2013-04-29 15:07:15 -07001122
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001123 if (reclaim) {
Johannes Weiner519ebea2013-07-03 15:04:51 -07001124 mem_cgroup_iter_update(iter, last_visited, memcg, seq);
Michal Hocko542f85f2013-04-29 15:07:15 -07001125
Michal Hocko19f39402013-04-29 15:07:18 -07001126 if (!memcg)
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001127 iter->generation++;
1128 else if (!prev && memcg)
1129 reclaim->generation = iter->generation;
1130 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001131
Michal Hockode577802013-09-12 15:13:26 -07001132 /*
1133 * We have finished the whole tree walk or no group has been
1134 * visited because filter told us to skip the root node.
1135 */
1136 if (!memcg && (prev || (cond && !last_visited)))
Michal Hocko542f85f2013-04-29 15:07:15 -07001137 goto out_unlock;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001138 }
Michal Hocko542f85f2013-04-29 15:07:15 -07001139out_unlock:
1140 rcu_read_unlock();
Michal Hockoc40046f2013-04-29 15:07:14 -07001141out_css_put:
1142 if (prev && prev != root)
1143 css_put(&prev->css);
1144
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001145 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001146}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001147
Johannes Weiner56600482012-01-12 17:17:59 -08001148/**
1149 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1150 * @root: hierarchy root
1151 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1152 */
1153void mem_cgroup_iter_break(struct mem_cgroup *root,
1154 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001155{
1156 if (!root)
1157 root = root_mem_cgroup;
1158 if (prev && prev != root)
1159 css_put(&prev->css);
1160}
1161
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001162/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001163 * Iteration constructs for visiting all cgroups (under a tree). If
1164 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1165 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001166 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001167#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001168 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001169 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001170 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001171
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001172#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001173 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001174 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001175 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001176
David Rientjes68ae5642012-12-12 13:51:57 -08001177void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001178{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001179 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001180
Ying Han456f9982011-05-26 16:25:38 -07001181 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001182 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1183 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001184 goto out;
1185
1186 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001187 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001188 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1189 break;
1190 case PGMAJFAULT:
1191 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001192 break;
1193 default:
1194 BUG();
1195 }
1196out:
1197 rcu_read_unlock();
1198}
David Rientjes68ae5642012-12-12 13:51:57 -08001199EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001200
Johannes Weiner925b7672012-01-12 17:18:15 -08001201/**
1202 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1203 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001204 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001205 *
1206 * Returns the lru list vector holding pages for the given @zone and
1207 * @mem. This can be the global zone lruvec, if the memory controller
1208 * is disabled.
1209 */
1210struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1211 struct mem_cgroup *memcg)
1212{
1213 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001214 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001215
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001216 if (mem_cgroup_disabled()) {
1217 lruvec = &zone->lruvec;
1218 goto out;
1219 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001220
1221 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001222 lruvec = &mz->lruvec;
1223out:
1224 /*
1225 * Since a node can be onlined after the mem_cgroup was created,
1226 * we have to be prepared to initialize lruvec->zone here;
1227 * and if offlined then reonlined, we need to reinitialize it.
1228 */
1229 if (unlikely(lruvec->zone != zone))
1230 lruvec->zone = zone;
1231 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001232}
1233
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001234/*
1235 * Following LRU functions are allowed to be used without PCG_LOCK.
1236 * Operations are called by routine of global LRU independently from memcg.
1237 * What we have to take care of here is validness of pc->mem_cgroup.
1238 *
1239 * Changes to pc->mem_cgroup happens when
1240 * 1. charge
1241 * 2. moving account
1242 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1243 * It is added to LRU before charge.
1244 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1245 * When moving account, the page is not on LRU. It's isolated.
1246 */
1247
Johannes Weiner925b7672012-01-12 17:18:15 -08001248/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001249 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001250 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001251 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001252 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001253struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001254{
1255 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001256 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001257 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001258 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001259
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001260 if (mem_cgroup_disabled()) {
1261 lruvec = &zone->lruvec;
1262 goto out;
1263 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001264
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001265 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001266 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001267
1268 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001269 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001270 * an uncharged page off lru does nothing to secure
1271 * its former mem_cgroup from sudden removal.
1272 *
1273 * Our caller holds lru_lock, and PageCgroupUsed is updated
1274 * under page_cgroup lock: between them, they make all uses
1275 * of pc->mem_cgroup safe.
1276 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001277 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001278 pc->mem_cgroup = memcg = root_mem_cgroup;
1279
Johannes Weiner925b7672012-01-12 17:18:15 -08001280 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001281 lruvec = &mz->lruvec;
1282out:
1283 /*
1284 * Since a node can be onlined after the mem_cgroup was created,
1285 * we have to be prepared to initialize lruvec->zone here;
1286 * and if offlined then reonlined, we need to reinitialize it.
1287 */
1288 if (unlikely(lruvec->zone != zone))
1289 lruvec->zone = zone;
1290 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001291}
1292
1293/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001294 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1295 * @lruvec: mem_cgroup per zone lru vector
1296 * @lru: index of lru list the page is sitting on
1297 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001298 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001299 * This function must be called when a page is added to or removed from an
1300 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001301 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001302void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1303 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001304{
1305 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001306 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001307
1308 if (mem_cgroup_disabled())
1309 return;
1310
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001311 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1312 lru_size = mz->lru_size + lru;
1313 *lru_size += nr_pages;
1314 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001315}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001316
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001317/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001318 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001319 * hierarchy subtree
1320 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001321bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1322 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001323{
Johannes Weiner91c637342012-05-29 15:06:24 -07001324 if (root_memcg == memcg)
1325 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001326 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001327 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001328 return css_is_ancestor(&memcg->css, &root_memcg->css);
1329}
1330
1331static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1332 struct mem_cgroup *memcg)
1333{
1334 bool ret;
1335
Johannes Weiner91c637342012-05-29 15:06:24 -07001336 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001337 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001338 rcu_read_unlock();
1339 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001340}
1341
David Rientjesffbdccf2013-07-03 15:01:23 -07001342bool task_in_mem_cgroup(struct task_struct *task,
1343 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001344{
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001345 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001346 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001347 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001348
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001349 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001350 if (p) {
1351 curr = try_get_mem_cgroup_from_mm(p->mm);
1352 task_unlock(p);
1353 } else {
1354 /*
1355 * All threads may have already detached their mm's, but the oom
1356 * killer still needs to detect if they have already been oom
1357 * killed to prevent needlessly killing additional tasks.
1358 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001359 rcu_read_lock();
David Rientjesde077d22012-01-12 17:18:52 -08001360 curr = mem_cgroup_from_task(task);
1361 if (curr)
1362 css_get(&curr->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001363 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001364 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001365 if (!curr)
David Rientjesffbdccf2013-07-03 15:01:23 -07001366 return false;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001367 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001368 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001369 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001370 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1371 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001372 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001373 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001374 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001375 return ret;
1376}
1377
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001378int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001379{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001380 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001381 unsigned long inactive;
1382 unsigned long active;
1383 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001384
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001385 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1386 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001387
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001388 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1389 if (gb)
1390 inactive_ratio = int_sqrt(10 * gb);
1391 else
1392 inactive_ratio = 1;
1393
Johannes Weiner9b272972011-11-02 13:38:23 -07001394 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001395}
1396
Balbir Singh6d61ef42009-01-07 18:08:06 -08001397#define mem_cgroup_from_res_counter(counter, member) \
1398 container_of(counter, struct mem_cgroup, member)
1399
Johannes Weiner19942822011-02-01 15:52:43 -08001400/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001401 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001402 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001403 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001404 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001405 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001406 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001407static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001408{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001409 unsigned long long margin;
1410
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001411 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001412 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001413 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001414 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001415}
1416
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001417int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001418{
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001419 /* root ? */
Tejun Heo63876982013-08-08 20:11:23 -04001420 if (!css_parent(&memcg->css))
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001421 return vm_swappiness;
1422
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001423 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001424}
1425
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001426/*
1427 * memcg->moving_account is used for checking possibility that some thread is
1428 * calling move_account(). When a thread on CPU-A starts moving pages under
1429 * a memcg, other threads should check memcg->moving_account under
1430 * rcu_read_lock(), like this:
1431 *
1432 * CPU-A CPU-B
1433 * rcu_read_lock()
1434 * memcg->moving_account+1 if (memcg->mocing_account)
1435 * take heavy locks.
1436 * synchronize_rcu() update something.
1437 * rcu_read_unlock()
1438 * start move here.
1439 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001440
1441/* for quick checking without looking up memcg */
1442atomic_t memcg_moving __read_mostly;
1443
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001444static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001445{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001446 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001447 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001448 synchronize_rcu();
1449}
1450
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001451static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001452{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001453 /*
1454 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1455 * We check NULL in callee rather than caller.
1456 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001457 if (memcg) {
1458 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001459 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001460 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001461}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001462
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001463/*
1464 * 2 routines for checking "mem" is under move_account() or not.
1465 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001466 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1467 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001468 * pc->mem_cgroup may be overwritten.
1469 *
1470 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1471 * under hierarchy of moving cgroups. This is for
1472 * waiting at hith-memory prressure caused by "move".
1473 */
1474
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001475static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001476{
1477 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001478 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001479}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001480
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001481static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001482{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001483 struct mem_cgroup *from;
1484 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001485 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001486 /*
1487 * Unlike task_move routines, we access mc.to, mc.from not under
1488 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1489 */
1490 spin_lock(&mc.lock);
1491 from = mc.from;
1492 to = mc.to;
1493 if (!from)
1494 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001495
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001496 ret = mem_cgroup_same_or_subtree(memcg, from)
1497 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001498unlock:
1499 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001500 return ret;
1501}
1502
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001503static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001504{
1505 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001506 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001507 DEFINE_WAIT(wait);
1508 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1509 /* moving charge context might have finished. */
1510 if (mc.moving_task)
1511 schedule();
1512 finish_wait(&mc.waitq, &wait);
1513 return true;
1514 }
1515 }
1516 return false;
1517}
1518
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001519/*
1520 * Take this lock when
1521 * - a code tries to modify page's memcg while it's USED.
1522 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001523 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001524 */
1525static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1526 unsigned long *flags)
1527{
1528 spin_lock_irqsave(&memcg->move_lock, *flags);
1529}
1530
1531static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1532 unsigned long *flags)
1533{
1534 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1535}
1536
Sha Zhengju58cf1882013-02-22 16:32:05 -08001537#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001538/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001539 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001540 * @memcg: The memory cgroup that went over limit
1541 * @p: Task that is going to be killed
1542 *
1543 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1544 * enabled
1545 */
1546void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1547{
1548 struct cgroup *task_cgrp;
1549 struct cgroup *mem_cgrp;
1550 /*
1551 * Need a buffer in BSS, can't rely on allocations. The code relies
1552 * on the assumption that OOM is serialized for memory controller.
1553 * If this assumption is broken, revisit this code.
1554 */
1555 static char memcg_name[PATH_MAX];
1556 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001557 struct mem_cgroup *iter;
1558 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001559
Sha Zhengju58cf1882013-02-22 16:32:05 -08001560 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001561 return;
1562
Balbir Singhe2224322009-04-02 16:57:39 -07001563 rcu_read_lock();
1564
1565 mem_cgrp = memcg->css.cgroup;
1566 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1567
1568 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1569 if (ret < 0) {
1570 /*
1571 * Unfortunately, we are unable to convert to a useful name
1572 * But we'll still print out the usage information
1573 */
1574 rcu_read_unlock();
1575 goto done;
1576 }
1577 rcu_read_unlock();
1578
Andrew Mortond0451972013-02-22 16:32:06 -08001579 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001580
1581 rcu_read_lock();
1582 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1583 if (ret < 0) {
1584 rcu_read_unlock();
1585 goto done;
1586 }
1587 rcu_read_unlock();
1588
1589 /*
1590 * Continues from above, so we don't need an KERN_ level
1591 */
Andrew Mortond0451972013-02-22 16:32:06 -08001592 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001593done:
1594
Andrew Mortond0451972013-02-22 16:32:06 -08001595 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001596 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1597 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1598 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001599 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001600 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1601 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1602 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001603 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001604 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1605 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1606 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001607
1608 for_each_mem_cgroup_tree(iter, memcg) {
1609 pr_info("Memory cgroup stats");
1610
1611 rcu_read_lock();
1612 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1613 if (!ret)
1614 pr_cont(" for %s", memcg_name);
1615 rcu_read_unlock();
1616 pr_cont(":");
1617
1618 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1619 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1620 continue;
1621 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1622 K(mem_cgroup_read_stat(iter, i)));
1623 }
1624
1625 for (i = 0; i < NR_LRU_LISTS; i++)
1626 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1627 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1628
1629 pr_cont("\n");
1630 }
Balbir Singhe2224322009-04-02 16:57:39 -07001631}
1632
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001633/*
1634 * This function returns the number of memcg under hierarchy tree. Returns
1635 * 1(self count) if no children.
1636 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001637static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001638{
1639 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001640 struct mem_cgroup *iter;
1641
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001642 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001643 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001644 return num;
1645}
1646
Balbir Singh6d61ef42009-01-07 18:08:06 -08001647/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001648 * Return the memory (and swap, if configured) limit for a memcg.
1649 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001650static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001651{
1652 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001653
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001654 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001655
David Rientjesa63d83f2010-08-09 17:19:46 -07001656 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001657 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001658 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001659 if (mem_cgroup_swappiness(memcg)) {
1660 u64 memsw;
1661
1662 limit += total_swap_pages << PAGE_SHIFT;
1663 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1664
1665 /*
1666 * If memsw is finite and limits the amount of swap space
1667 * available to this memcg, return that limit.
1668 */
1669 limit = min(limit, memsw);
1670 }
1671
1672 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001673}
1674
David Rientjes19965462012-12-11 16:00:26 -08001675static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1676 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001677{
1678 struct mem_cgroup *iter;
1679 unsigned long chosen_points = 0;
1680 unsigned long totalpages;
1681 unsigned int points = 0;
1682 struct task_struct *chosen = NULL;
1683
David Rientjes876aafb2012-07-31 16:43:48 -07001684 /*
David Rientjes465adcf2013-04-29 15:08:45 -07001685 * If current has a pending SIGKILL or is exiting, then automatically
1686 * select it. The goal is to allow it to allocate so that it may
1687 * quickly exit and free its memory.
David Rientjes876aafb2012-07-31 16:43:48 -07001688 */
David Rientjes465adcf2013-04-29 15:08:45 -07001689 if (fatal_signal_pending(current) || current->flags & PF_EXITING) {
David Rientjes876aafb2012-07-31 16:43:48 -07001690 set_thread_flag(TIF_MEMDIE);
1691 return;
1692 }
1693
1694 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001695 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1696 for_each_mem_cgroup_tree(iter, memcg) {
Tejun Heo72ec7022013-08-08 20:11:26 -04001697 struct css_task_iter it;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001698 struct task_struct *task;
1699
Tejun Heo72ec7022013-08-08 20:11:26 -04001700 css_task_iter_start(&iter->css, &it);
1701 while ((task = css_task_iter_next(&it))) {
David Rientjes9cbb78b2012-07-31 16:43:44 -07001702 switch (oom_scan_process_thread(task, totalpages, NULL,
1703 false)) {
1704 case OOM_SCAN_SELECT:
1705 if (chosen)
1706 put_task_struct(chosen);
1707 chosen = task;
1708 chosen_points = ULONG_MAX;
1709 get_task_struct(chosen);
1710 /* fall through */
1711 case OOM_SCAN_CONTINUE:
1712 continue;
1713 case OOM_SCAN_ABORT:
Tejun Heo72ec7022013-08-08 20:11:26 -04001714 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001715 mem_cgroup_iter_break(memcg, iter);
1716 if (chosen)
1717 put_task_struct(chosen);
1718 return;
1719 case OOM_SCAN_OK:
1720 break;
1721 };
1722 points = oom_badness(task, memcg, NULL, totalpages);
1723 if (points > chosen_points) {
1724 if (chosen)
1725 put_task_struct(chosen);
1726 chosen = task;
1727 chosen_points = points;
1728 get_task_struct(chosen);
1729 }
1730 }
Tejun Heo72ec7022013-08-08 20:11:26 -04001731 css_task_iter_end(&it);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001732 }
1733
1734 if (!chosen)
1735 return;
1736 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001737 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1738 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001739}
1740
Johannes Weiner56600482012-01-12 17:17:59 -08001741static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1742 gfp_t gfp_mask,
1743 unsigned long flags)
1744{
1745 unsigned long total = 0;
1746 bool noswap = false;
1747 int loop;
1748
1749 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1750 noswap = true;
1751 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1752 noswap = true;
1753
1754 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1755 if (loop)
1756 drain_all_stock_async(memcg);
1757 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1758 /*
1759 * Allow limit shrinkers, which are triggered directly
1760 * by userspace, to catch signals and stop reclaim
1761 * after minimal progress, regardless of the margin.
1762 */
1763 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1764 break;
1765 if (mem_cgroup_margin(memcg))
1766 break;
1767 /*
1768 * If nothing was reclaimed after two attempts, there
1769 * may be no reclaimable pages in this hierarchy.
1770 */
1771 if (loop && !total)
1772 break;
1773 }
1774 return total;
1775}
1776
Michal Hockoe8831102013-09-12 15:13:23 -07001777#if MAX_NUMNODES > 1
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001778/**
1779 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001780 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001781 * @nid: the node ID to be checked.
1782 * @noswap : specify true here if the user wants flle only information.
1783 *
1784 * This function returns whether the specified memcg contains any
1785 * reclaimable pages on a node. Returns true if there are any reclaimable
1786 * pages in the node.
1787 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001788static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001789 int nid, bool noswap)
1790{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001791 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001792 return true;
1793 if (noswap || !total_swap_pages)
1794 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001795 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001796 return true;
1797 return false;
1798
1799}
Ying Han889976d2011-05-26 16:25:33 -07001800
1801/*
1802 * Always updating the nodemask is not very good - even if we have an empty
1803 * list or the wrong list here, we can start from some node and traverse all
1804 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1805 *
1806 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001807static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001808{
1809 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001810 /*
1811 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1812 * pagein/pageout changes since the last update.
1813 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001814 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001815 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001816 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001817 return;
1818
Ying Han889976d2011-05-26 16:25:33 -07001819 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001820 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001821
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001822 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001823
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001824 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1825 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001826 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001828 atomic_set(&memcg->numainfo_events, 0);
1829 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001830}
1831
1832/*
1833 * Selecting a node where we start reclaim from. Because what we need is just
1834 * reducing usage counter, start from anywhere is O,K. Considering
1835 * memory reclaim from current node, there are pros. and cons.
1836 *
1837 * Freeing memory from current node means freeing memory from a node which
1838 * we'll use or we've used. So, it may make LRU bad. And if several threads
1839 * hit limits, it will see a contention on a node. But freeing from remote
1840 * node means more costs for memory reclaim because of memory latency.
1841 *
1842 * Now, we use round-robin. Better algorithm is welcomed.
1843 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001844int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001845{
1846 int node;
1847
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001848 mem_cgroup_may_update_nodemask(memcg);
1849 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001850
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001851 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001852 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001854 /*
1855 * We call this when we hit limit, not when pages are added to LRU.
1856 * No LRU may hold pages because all pages are UNEVICTABLE or
1857 * memcg is too small and all pages are not on LRU. In that case,
1858 * we use curret node.
1859 */
1860 if (unlikely(node == MAX_NUMNODES))
1861 node = numa_node_id();
1862
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001863 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001864 return node;
1865}
1866
1867#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001868int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001869{
1870 return 0;
1871}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001872
Ying Han889976d2011-05-26 16:25:33 -07001873#endif
1874
Michal Hocko3b387222013-09-12 15:13:21 -07001875/*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001876 * A group is eligible for the soft limit reclaim under the given root
1877 * hierarchy if
1878 * a) it is over its soft limit
Michal Hocko3b387222013-09-12 15:13:21 -07001879 * b) any parent up the hierarchy is over its soft limit
Michal Hocko7d910c02013-09-12 15:13:28 -07001880 *
1881 * If the given group doesn't have any children over the limit then it
1882 * doesn't make any sense to iterate its subtree.
Michal Hocko3b387222013-09-12 15:13:21 -07001883 */
Michal Hockode577802013-09-12 15:13:26 -07001884enum mem_cgroup_filter_t
1885mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -07001886 struct mem_cgroup *root)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001887{
Michal Hocko3b387222013-09-12 15:13:21 -07001888 struct mem_cgroup *parent = memcg;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001889
Michal Hocko3b387222013-09-12 15:13:21 -07001890 if (res_counter_soft_limit_excess(&memcg->res))
Michal Hockode577802013-09-12 15:13:26 -07001891 return VISIT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001892
Michal Hocko3b387222013-09-12 15:13:21 -07001893 /*
Michal Hockoa5b7c872013-09-12 15:13:25 -07001894 * If any parent up to the root in the hierarchy is over its soft limit
1895 * then we have to obey and reclaim from this group as well.
Michal Hocko3b387222013-09-12 15:13:21 -07001896 */
1897 while((parent = parent_mem_cgroup(parent))) {
1898 if (res_counter_soft_limit_excess(&parent->res))
Michal Hockode577802013-09-12 15:13:26 -07001899 return VISIT;
Michal Hockoa5b7c872013-09-12 15:13:25 -07001900 if (parent == root)
1901 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001902 }
Michal Hocko3b387222013-09-12 15:13:21 -07001903
Michal Hocko7d910c02013-09-12 15:13:28 -07001904 if (!atomic_read(&memcg->children_in_excess))
1905 return SKIP_TREE;
Michal Hockode577802013-09-12 15:13:26 -07001906 return SKIP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001907}
1908
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001909/*
1910 * Check OOM-Killer is already running under our hierarchy.
1911 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001912 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001913 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001914static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001915{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001916 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001917
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001918 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001919 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001920 /*
1921 * this subtree of our hierarchy is already locked
1922 * so we cannot give a lock.
1923 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001924 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001925 mem_cgroup_iter_break(memcg, iter);
1926 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001927 } else
1928 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001929 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001930
Michal Hocko79dfdac2011-07-26 16:08:23 -07001931 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001932 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001933
1934 /*
1935 * OK, we failed to lock the whole subtree so we have to clean up
1936 * what we set up to the failing subtree
1937 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001938 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001939 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001940 mem_cgroup_iter_break(memcg, iter);
1941 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001942 }
1943 iter->oom_lock = false;
1944 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001945 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001946}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001947
Michal Hocko79dfdac2011-07-26 16:08:23 -07001948/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001949 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001950 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001951static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001952{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001953 struct mem_cgroup *iter;
1954
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001955 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001956 iter->oom_lock = false;
1957 return 0;
1958}
1959
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001960static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001961{
1962 struct mem_cgroup *iter;
1963
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001964 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001965 atomic_inc(&iter->under_oom);
1966}
1967
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001968static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001969{
1970 struct mem_cgroup *iter;
1971
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001972 /*
1973 * When a new child is created while the hierarchy is under oom,
1974 * mem_cgroup_oom_lock() may not be called. We have to use
1975 * atomic_add_unless() here.
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_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001979}
1980
Michal Hocko1af8efe2011-07-26 16:08:24 -07001981static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001982static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1983
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001984struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001985 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001986 wait_queue_t wait;
1987};
1988
1989static int memcg_oom_wake_function(wait_queue_t *wait,
1990 unsigned mode, int sync, void *arg)
1991{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001992 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1993 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001994 struct oom_wait_info *oom_wait_info;
1995
1996 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001997 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001998
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001999 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002000 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002001 * Then we can use css_is_ancestor without taking care of RCU.
2002 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002003 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2004 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002005 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002006 return autoremove_wake_function(wait, mode, sync, arg);
2007}
2008
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002009static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002010{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002011 /* for filtering, pass "memcg" as argument. */
2012 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002013}
2014
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002015static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002016{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002017 if (memcg && atomic_read(&memcg->under_oom))
2018 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002019}
2020
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002021/*
2022 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2023 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002024static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2025 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002026{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002027 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002028 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002029
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002030 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002031 owait.wait.flags = 0;
2032 owait.wait.func = memcg_oom_wake_function;
2033 owait.wait.private = current;
2034 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002035 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002036 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002037
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002038 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002039 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002040 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002041 /*
2042 * Even if signal_pending(), we can't quit charge() loop without
2043 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2044 * under OOM is always welcomed, use TASK_KILLABLE here.
2045 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002046 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002047 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002048 need_to_kill = false;
2049 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002050 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002051 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002052
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002053 if (need_to_kill) {
2054 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002055 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002056 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002057 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002058 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002059 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002060 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002061 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002062 mem_cgroup_oom_unlock(memcg);
2063 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002064 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002066 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002067
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002068 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2069 return false;
2070 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002071 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002072 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002073}
2074
Balbir Singhd69b0422009-06-17 16:26:34 -07002075/*
2076 * Currently used to update mapped file statistics, but the routine can be
2077 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002078 *
2079 * Notes: Race condition
2080 *
2081 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2082 * it tends to be costly. But considering some conditions, we doesn't need
2083 * to do so _always_.
2084 *
2085 * Considering "charge", lock_page_cgroup() is not required because all
2086 * file-stat operations happen after a page is attached to radix-tree. There
2087 * are no race with "charge".
2088 *
2089 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2090 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2091 * if there are race with "uncharge". Statistics itself is properly handled
2092 * by flags.
2093 *
2094 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002095 * small, we check mm->moving_account and detect there are possibility of race
2096 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002097 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002098
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002099void __mem_cgroup_begin_update_page_stat(struct page *page,
2100 bool *locked, unsigned long *flags)
2101{
2102 struct mem_cgroup *memcg;
2103 struct page_cgroup *pc;
2104
2105 pc = lookup_page_cgroup(page);
2106again:
2107 memcg = pc->mem_cgroup;
2108 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2109 return;
2110 /*
2111 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002112 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002113 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002114 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002115 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002116 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002117 return;
2118
2119 move_lock_mem_cgroup(memcg, flags);
2120 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2121 move_unlock_mem_cgroup(memcg, flags);
2122 goto again;
2123 }
2124 *locked = true;
2125}
2126
2127void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2128{
2129 struct page_cgroup *pc = lookup_page_cgroup(page);
2130
2131 /*
2132 * It's guaranteed that pc->mem_cgroup never changes while
2133 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002134 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002135 */
2136 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2137}
2138
Greg Thelen2a7106f2011-01-13 15:47:37 -08002139void mem_cgroup_update_page_stat(struct page *page,
2140 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002141{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002142 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002143 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002144 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002145
Johannes Weinercfa44942012-01-12 17:18:38 -08002146 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002147 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002148
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002149 memcg = pc->mem_cgroup;
2150 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002151 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002152
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002153 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002154 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002155 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002156 break;
2157 default:
2158 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002159 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002160
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002161 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002162}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002163
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002164/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002165 * size of first charge trial. "32" comes from vmscan.c's magic value.
2166 * TODO: maybe necessary to use big numbers in big irons.
2167 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002168#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002169struct memcg_stock_pcp {
2170 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002171 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002172 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002173 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002174#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002175};
2176static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002177static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002178
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002179/**
2180 * consume_stock: Try to consume stocked charge on this cpu.
2181 * @memcg: memcg to consume from.
2182 * @nr_pages: how many pages to charge.
2183 *
2184 * The charges will only happen if @memcg matches the current cpu's memcg
2185 * stock, and at least @nr_pages are available in that stock. Failure to
2186 * service an allocation will refill the stock.
2187 *
2188 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002189 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002190static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002191{
2192 struct memcg_stock_pcp *stock;
2193 bool ret = true;
2194
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002195 if (nr_pages > CHARGE_BATCH)
2196 return false;
2197
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002198 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002199 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2200 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002201 else /* need to call res_counter_charge */
2202 ret = false;
2203 put_cpu_var(memcg_stock);
2204 return ret;
2205}
2206
2207/*
2208 * Returns stocks cached in percpu to res_counter and reset cached information.
2209 */
2210static void drain_stock(struct memcg_stock_pcp *stock)
2211{
2212 struct mem_cgroup *old = stock->cached;
2213
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002214 if (stock->nr_pages) {
2215 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2216
2217 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002218 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002219 res_counter_uncharge(&old->memsw, bytes);
2220 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002221 }
2222 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002223}
2224
2225/*
2226 * This must be called under preempt disabled or must be called by
2227 * a thread which is pinned to local cpu.
2228 */
2229static void drain_local_stock(struct work_struct *dummy)
2230{
2231 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2232 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002233 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002234}
2235
Michal Hockoe4777492013-02-22 16:35:40 -08002236static void __init memcg_stock_init(void)
2237{
2238 int cpu;
2239
2240 for_each_possible_cpu(cpu) {
2241 struct memcg_stock_pcp *stock =
2242 &per_cpu(memcg_stock, cpu);
2243 INIT_WORK(&stock->work, drain_local_stock);
2244 }
2245}
2246
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002247/*
2248 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002249 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002250 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002251static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002252{
2253 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2254
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002255 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002256 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002257 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002258 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002259 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002260 put_cpu_var(memcg_stock);
2261}
2262
2263/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002264 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002265 * of the hierarchy under it. sync flag says whether we should block
2266 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002267 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002268static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002270 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002271
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002272 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002273 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002274 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002275 for_each_online_cpu(cpu) {
2276 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002277 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002278
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002279 memcg = stock->cached;
2280 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002281 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002282 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002283 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002284 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2285 if (cpu == curcpu)
2286 drain_local_stock(&stock->work);
2287 else
2288 schedule_work_on(cpu, &stock->work);
2289 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002290 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002291 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002292
2293 if (!sync)
2294 goto out;
2295
2296 for_each_online_cpu(cpu) {
2297 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002298 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002299 flush_work(&stock->work);
2300 }
2301out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002302 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002303}
2304
2305/*
2306 * Tries to drain stocked charges in other cpus. This function is asynchronous
2307 * and just put a work per cpu for draining localy on each cpu. Caller can
2308 * expects some charges will be back to res_counter later but cannot wait for
2309 * it.
2310 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002311static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002312{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002313 /*
2314 * If someone calls draining, avoid adding more kworker runs.
2315 */
2316 if (!mutex_trylock(&percpu_charge_mutex))
2317 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002318 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002319 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002320}
2321
2322/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002323static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002324{
2325 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002326 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002327 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002328 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002329}
2330
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002331/*
2332 * This function drains percpu counter value from DEAD cpu and
2333 * move it to local cpu. Note that this function can be preempted.
2334 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002335static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002336{
2337 int i;
2338
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002339 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002340 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002341 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002342
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002343 per_cpu(memcg->stat->count[i], cpu) = 0;
2344 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002345 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002346 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002347 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002348
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002349 per_cpu(memcg->stat->events[i], cpu) = 0;
2350 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002351 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002352 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002353}
2354
Paul Gortmaker0db06282013-06-19 14:53:51 -04002355static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002356 unsigned long action,
2357 void *hcpu)
2358{
2359 int cpu = (unsigned long)hcpu;
2360 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002361 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002362
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002363 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002364 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002365
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002366 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002367 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002368
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002369 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002370 mem_cgroup_drain_pcp_counter(iter, cpu);
2371
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002372 stock = &per_cpu(memcg_stock, cpu);
2373 drain_stock(stock);
2374 return NOTIFY_OK;
2375}
2376
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002377
2378/* See __mem_cgroup_try_charge() for details */
2379enum {
2380 CHARGE_OK, /* success */
2381 CHARGE_RETRY, /* need to retry but retry is not bad */
2382 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2383 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2384 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2385};
2386
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002388 unsigned int nr_pages, unsigned int min_pages,
2389 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002390{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002391 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002392 struct mem_cgroup *mem_over_limit;
2393 struct res_counter *fail_res;
2394 unsigned long flags = 0;
2395 int ret;
2396
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002397 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002398
2399 if (likely(!ret)) {
2400 if (!do_swap_account)
2401 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002403 if (likely(!ret))
2404 return CHARGE_OK;
2405
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002406 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002407 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2408 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2409 } else
2410 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002411 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002412 * Never reclaim on behalf of optional batching, retry with a
2413 * single page instead.
2414 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002415 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002416 return CHARGE_RETRY;
2417
2418 if (!(gfp_mask & __GFP_WAIT))
2419 return CHARGE_WOULDBLOCK;
2420
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002421 if (gfp_mask & __GFP_NORETRY)
2422 return CHARGE_NOMEM;
2423
Johannes Weiner56600482012-01-12 17:17:59 -08002424 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002425 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002426 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002427 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002428 * Even though the limit is exceeded at this point, reclaim
2429 * may have been able to free some pages. Retry the charge
2430 * before killing the task.
2431 *
2432 * Only for regular pages, though: huge pages are rather
2433 * unlikely to succeed so close to the limit, and we fall back
2434 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002435 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002436 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002437 return CHARGE_RETRY;
2438
2439 /*
2440 * At task move, charge accounts can be doubly counted. So, it's
2441 * better to wait until the end of task_move if something is going on.
2442 */
2443 if (mem_cgroup_wait_acct_move(mem_over_limit))
2444 return CHARGE_RETRY;
2445
2446 /* If we don't need to call oom-killer at el, return immediately */
2447 if (!oom_check)
2448 return CHARGE_NOMEM;
2449 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002450 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002451 return CHARGE_OOM_DIE;
2452
2453 return CHARGE_RETRY;
2454}
2455
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002456/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002457 * __mem_cgroup_try_charge() does
2458 * 1. detect memcg to be charged against from passed *mm and *ptr,
2459 * 2. update res_counter
2460 * 3. call memory reclaim if necessary.
2461 *
2462 * In some special case, if the task is fatal, fatal_signal_pending() or
2463 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2464 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2465 * as possible without any hazards. 2: all pages should have a valid
2466 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2467 * pointer, that is treated as a charge to root_mem_cgroup.
2468 *
2469 * So __mem_cgroup_try_charge() will return
2470 * 0 ... on success, filling *ptr with a valid memcg pointer.
2471 * -ENOMEM ... charge failure because of resource limits.
2472 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2473 *
2474 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2475 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002476 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002477static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002478 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002479 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002480 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002481 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002482{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002483 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002484 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002486 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002487
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002488 /*
2489 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2490 * in system level. So, allow to go ahead dying process in addition to
2491 * MEMDIE process.
2492 */
2493 if (unlikely(test_thread_flag(TIF_MEMDIE)
2494 || fatal_signal_pending(current)))
2495 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002496
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002497 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002498 * We always charge the cgroup the mm_struct belongs to.
2499 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002500 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002501 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002502 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002503 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002504 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002505again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002506 if (*ptr) { /* css should be a valid one */
2507 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002508 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002509 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002510 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002511 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002512 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002513 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002514 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002515
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002516 rcu_read_lock();
2517 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002518 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002519 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002520 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002521 * race with swapoff. Then, we have small risk of mis-accouning.
2522 * But such kind of mis-account by race always happens because
2523 * we don't have cgroup_mutex(). It's overkill and we allo that
2524 * small race, here.
2525 * (*) swapoff at el will charge against mm-struct not against
2526 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002527 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002528 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002529 if (!memcg)
2530 memcg = root_mem_cgroup;
2531 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002532 rcu_read_unlock();
2533 goto done;
2534 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002535 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002536 /*
2537 * It seems dagerous to access memcg without css_get().
2538 * But considering how consume_stok works, it's not
2539 * necessary. If consume_stock success, some charges
2540 * from this memcg are cached on this cpu. So, we
2541 * don't need to call css_get()/css_tryget() before
2542 * calling consume_stock().
2543 */
2544 rcu_read_unlock();
2545 goto done;
2546 }
2547 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002548 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002549 rcu_read_unlock();
2550 goto again;
2551 }
2552 rcu_read_unlock();
2553 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002554
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002555 do {
2556 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002557
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002558 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002559 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002560 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002561 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002562 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002563
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002564 oom_check = false;
2565 if (oom && !nr_oom_retries) {
2566 oom_check = true;
2567 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2568 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002569
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002570 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2571 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002572 switch (ret) {
2573 case CHARGE_OK:
2574 break;
2575 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002576 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002577 css_put(&memcg->css);
2578 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002579 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002580 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002581 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002582 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002583 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002584 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002585 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002586 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002587 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002588 /* If oom, we never return -ENOMEM */
2589 nr_oom_retries--;
2590 break;
2591 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002592 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002593 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002594 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002595 } while (ret != CHARGE_OK);
2596
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002597 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002598 refill_stock(memcg, batch - nr_pages);
2599 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002600done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002601 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002602 return 0;
2603nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002604 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002605 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002606bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002607 *ptr = root_mem_cgroup;
2608 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002609}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002610
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002611/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002612 * Somemtimes we have to undo a charge we got by try_charge().
2613 * This function is for that and do uncharge, put css's refcnt.
2614 * gotten by try_charge().
2615 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002616static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002617 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002618{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002619 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002620 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002621
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002622 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002623 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002624 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002625 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002626}
2627
2628/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002629 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2630 * This is useful when moving usage to parent cgroup.
2631 */
2632static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2633 unsigned int nr_pages)
2634{
2635 unsigned long bytes = nr_pages * PAGE_SIZE;
2636
2637 if (mem_cgroup_is_root(memcg))
2638 return;
2639
2640 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2641 if (do_swap_account)
2642 res_counter_uncharge_until(&memcg->memsw,
2643 memcg->memsw.parent, bytes);
2644}
2645
2646/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002647 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002648 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2649 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2650 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002651 */
2652static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2653{
2654 struct cgroup_subsys_state *css;
2655
2656 /* ID 0 is unused ID */
2657 if (!id)
2658 return NULL;
2659 css = css_lookup(&mem_cgroup_subsys, id);
2660 if (!css)
2661 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002662 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002663}
2664
Wu Fengguange42d9d52009-12-16 12:19:59 +01002665struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002666{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002667 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002668 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002669 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002670 swp_entry_t ent;
2671
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002672 VM_BUG_ON(!PageLocked(page));
2673
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002674 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002675 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002676 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002677 memcg = pc->mem_cgroup;
2678 if (memcg && !css_tryget(&memcg->css))
2679 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002680 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002681 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002682 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002683 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002684 memcg = mem_cgroup_lookup(id);
2685 if (memcg && !css_tryget(&memcg->css))
2686 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002687 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002688 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002689 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002690 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002691}
2692
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002693static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002694 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002695 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002696 enum charge_type ctype,
2697 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002698{
Johannes Weinerce587e62012-04-24 20:22:33 +02002699 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002700 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002701 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002702 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002703 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002704
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002705 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002706 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002707 /*
2708 * we don't need page_cgroup_lock about tail pages, becase they are not
2709 * accessed by any other context at this point.
2710 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002711
2712 /*
2713 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2714 * may already be on some other mem_cgroup's LRU. Take care of it.
2715 */
2716 if (lrucare) {
2717 zone = page_zone(page);
2718 spin_lock_irq(&zone->lru_lock);
2719 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002720 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002721 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002722 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002723 was_on_lru = true;
2724 }
2725 }
2726
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002727 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002728 /*
2729 * We access a page_cgroup asynchronously without lock_page_cgroup().
2730 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2731 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2732 * before USED bit, we need memory barrier here.
2733 * See mem_cgroup_add_lru_list(), etc.
2734 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002735 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002736 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002737
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002738 if (lrucare) {
2739 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002740 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002741 VM_BUG_ON(PageLRU(page));
2742 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002743 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002744 }
2745 spin_unlock_irq(&zone->lru_lock);
2746 }
2747
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002748 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002749 anon = true;
2750 else
2751 anon = false;
2752
David Rientjesb070e652013-05-07 16:18:09 -07002753 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002754 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002755
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002756 /*
Michal Hockoe8831102013-09-12 15:13:23 -07002757 * "charge_statistics" updated event counter.
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002758 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002759 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002760}
2761
Glauber Costa7cf27982012-12-18 14:22:55 -08002762static DEFINE_MUTEX(set_limit_mutex);
2763
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002764#ifdef CONFIG_MEMCG_KMEM
2765static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2766{
2767 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2768 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2769}
2770
Glauber Costa1f458cb2012-12-18 14:22:50 -08002771/*
2772 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2773 * in the memcg_cache_params struct.
2774 */
2775static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2776{
2777 struct kmem_cache *cachep;
2778
2779 VM_BUG_ON(p->is_root_cache);
2780 cachep = p->root_cache;
2781 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2782}
2783
Glauber Costa749c5412012-12-18 14:23:01 -08002784#ifdef CONFIG_SLABINFO
Tejun Heo182446d2013-08-08 20:11:24 -04002785static int mem_cgroup_slabinfo_read(struct cgroup_subsys_state *css,
2786 struct cftype *cft, struct seq_file *m)
Glauber Costa749c5412012-12-18 14:23:01 -08002787{
Tejun Heo182446d2013-08-08 20:11:24 -04002788 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa749c5412012-12-18 14:23:01 -08002789 struct memcg_cache_params *params;
2790
2791 if (!memcg_can_account_kmem(memcg))
2792 return -EIO;
2793
2794 print_slabinfo_header(m);
2795
2796 mutex_lock(&memcg->slab_caches_mutex);
2797 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2798 cache_show(memcg_params_to_cache(params), m);
2799 mutex_unlock(&memcg->slab_caches_mutex);
2800
2801 return 0;
2802}
2803#endif
2804
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002805static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2806{
2807 struct res_counter *fail_res;
2808 struct mem_cgroup *_memcg;
2809 int ret = 0;
2810 bool may_oom;
2811
2812 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2813 if (ret)
2814 return ret;
2815
2816 /*
2817 * Conditions under which we can wait for the oom_killer. Those are
2818 * the same conditions tested by the core page allocator
2819 */
2820 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2821
2822 _memcg = memcg;
2823 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2824 &_memcg, may_oom);
2825
2826 if (ret == -EINTR) {
2827 /*
2828 * __mem_cgroup_try_charge() chosed to bypass to root due to
2829 * OOM kill or fatal signal. Since our only options are to
2830 * either fail the allocation or charge it to this cgroup, do
2831 * it as a temporary condition. But we can't fail. From a
2832 * kmem/slab perspective, the cache has already been selected,
2833 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2834 * our minds.
2835 *
2836 * This condition will only trigger if the task entered
2837 * memcg_charge_kmem in a sane state, but was OOM-killed during
2838 * __mem_cgroup_try_charge() above. Tasks that were already
2839 * dying when the allocation triggers should have been already
2840 * directed to the root cgroup in memcontrol.h
2841 */
2842 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2843 if (do_swap_account)
2844 res_counter_charge_nofail(&memcg->memsw, size,
2845 &fail_res);
2846 ret = 0;
2847 } else if (ret)
2848 res_counter_uncharge(&memcg->kmem, size);
2849
2850 return ret;
2851}
2852
2853static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2854{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002855 res_counter_uncharge(&memcg->res, size);
2856 if (do_swap_account)
2857 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002858
2859 /* Not down to 0 */
2860 if (res_counter_uncharge(&memcg->kmem, size))
2861 return;
2862
Li Zefan10d5ebf2013-07-08 16:00:33 -07002863 /*
2864 * Releases a reference taken in kmem_cgroup_css_offline in case
2865 * this last uncharge is racing with the offlining code or it is
2866 * outliving the memcg existence.
2867 *
2868 * The memory barrier imposed by test&clear is paired with the
2869 * explicit one in memcg_kmem_mark_dead().
2870 */
Glauber Costa7de37682012-12-18 14:22:07 -08002871 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07002872 css_put(&memcg->css);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002873}
2874
Glauber Costa2633d7a2012-12-18 14:22:34 -08002875void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2876{
2877 if (!memcg)
2878 return;
2879
2880 mutex_lock(&memcg->slab_caches_mutex);
2881 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2882 mutex_unlock(&memcg->slab_caches_mutex);
2883}
2884
2885/*
2886 * helper for acessing a memcg's index. It will be used as an index in the
2887 * child cache array in kmem_cache, and also to derive its name. This function
2888 * will return -1 when this is not a kmem-limited memcg.
2889 */
2890int memcg_cache_id(struct mem_cgroup *memcg)
2891{
2892 return memcg ? memcg->kmemcg_id : -1;
2893}
2894
Glauber Costa55007d82012-12-18 14:22:38 -08002895/*
2896 * This ends up being protected by the set_limit mutex, during normal
2897 * operation, because that is its main call site.
2898 *
2899 * But when we create a new cache, we can call this as well if its parent
2900 * is kmem-limited. That will have to hold set_limit_mutex as well.
2901 */
2902int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2903{
2904 int num, ret;
2905
2906 num = ida_simple_get(&kmem_limited_groups,
2907 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2908 if (num < 0)
2909 return num;
2910 /*
2911 * After this point, kmem_accounted (that we test atomically in
2912 * the beginning of this conditional), is no longer 0. This
2913 * guarantees only one process will set the following boolean
2914 * to true. We don't need test_and_set because we're protected
2915 * by the set_limit_mutex anyway.
2916 */
2917 memcg_kmem_set_activated(memcg);
2918
2919 ret = memcg_update_all_caches(num+1);
2920 if (ret) {
2921 ida_simple_remove(&kmem_limited_groups, num);
2922 memcg_kmem_clear_activated(memcg);
2923 return ret;
2924 }
2925
2926 memcg->kmemcg_id = num;
2927 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2928 mutex_init(&memcg->slab_caches_mutex);
2929 return 0;
2930}
2931
2932static size_t memcg_caches_array_size(int num_groups)
2933{
2934 ssize_t size;
2935 if (num_groups <= 0)
2936 return 0;
2937
2938 size = 2 * num_groups;
2939 if (size < MEMCG_CACHES_MIN_SIZE)
2940 size = MEMCG_CACHES_MIN_SIZE;
2941 else if (size > MEMCG_CACHES_MAX_SIZE)
2942 size = MEMCG_CACHES_MAX_SIZE;
2943
2944 return size;
2945}
2946
2947/*
2948 * We should update the current array size iff all caches updates succeed. This
2949 * can only be done from the slab side. The slab mutex needs to be held when
2950 * calling this.
2951 */
2952void memcg_update_array_size(int num)
2953{
2954 if (num > memcg_limited_groups_array_size)
2955 memcg_limited_groups_array_size = memcg_caches_array_size(num);
2956}
2957
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08002958static void kmem_cache_destroy_work_func(struct work_struct *w);
2959
Glauber Costa55007d82012-12-18 14:22:38 -08002960int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
2961{
2962 struct memcg_cache_params *cur_params = s->memcg_params;
2963
2964 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
2965
2966 if (num_groups > memcg_limited_groups_array_size) {
2967 int i;
2968 ssize_t size = memcg_caches_array_size(num_groups);
2969
2970 size *= sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07002971 size += offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08002972
2973 s->memcg_params = kzalloc(size, GFP_KERNEL);
2974 if (!s->memcg_params) {
2975 s->memcg_params = cur_params;
2976 return -ENOMEM;
2977 }
2978
2979 s->memcg_params->is_root_cache = true;
2980
2981 /*
2982 * There is the chance it will be bigger than
2983 * memcg_limited_groups_array_size, if we failed an allocation
2984 * in a cache, in which case all caches updated before it, will
2985 * have a bigger array.
2986 *
2987 * But if that is the case, the data after
2988 * memcg_limited_groups_array_size is certainly unused
2989 */
2990 for (i = 0; i < memcg_limited_groups_array_size; i++) {
2991 if (!cur_params->memcg_caches[i])
2992 continue;
2993 s->memcg_params->memcg_caches[i] =
2994 cur_params->memcg_caches[i];
2995 }
2996
2997 /*
2998 * Ideally, we would wait until all caches succeed, and only
2999 * then free the old one. But this is not worth the extra
3000 * pointer per-cache we'd have to have for this.
3001 *
3002 * It is not a big deal if some caches are left with a size
3003 * bigger than the others. And all updates will reset this
3004 * anyway.
3005 */
3006 kfree(cur_params);
3007 }
3008 return 0;
3009}
3010
Glauber Costa943a4512012-12-18 14:23:03 -08003011int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3012 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003013{
Andrey Vagin90c7a792013-09-11 14:22:18 -07003014 size_t size;
Glauber Costa2633d7a2012-12-18 14:22:34 -08003015
3016 if (!memcg_kmem_enabled())
3017 return 0;
3018
Andrey Vagin90c7a792013-09-11 14:22:18 -07003019 if (!memcg) {
3020 size = offsetof(struct memcg_cache_params, memcg_caches);
Glauber Costa55007d82012-12-18 14:22:38 -08003021 size += memcg_limited_groups_array_size * sizeof(void *);
Andrey Vagin90c7a792013-09-11 14:22:18 -07003022 } else
3023 size = sizeof(struct memcg_cache_params);
Glauber Costa55007d82012-12-18 14:22:38 -08003024
Glauber Costa2633d7a2012-12-18 14:22:34 -08003025 s->memcg_params = kzalloc(size, GFP_KERNEL);
3026 if (!s->memcg_params)
3027 return -ENOMEM;
3028
Glauber Costa943a4512012-12-18 14:23:03 -08003029 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003030 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003031 s->memcg_params->root_cache = root_cache;
Andrey Vagin3e6b11d2013-08-13 16:00:47 -07003032 INIT_WORK(&s->memcg_params->destroy,
3033 kmem_cache_destroy_work_func);
Glauber Costa4ba902b2013-02-12 13:46:22 -08003034 } else
3035 s->memcg_params->is_root_cache = true;
3036
Glauber Costa2633d7a2012-12-18 14:22:34 -08003037 return 0;
3038}
3039
3040void memcg_release_cache(struct kmem_cache *s)
3041{
Glauber Costad7f25f82012-12-18 14:22:40 -08003042 struct kmem_cache *root;
3043 struct mem_cgroup *memcg;
3044 int id;
3045
3046 /*
3047 * This happens, for instance, when a root cache goes away before we
3048 * add any memcg.
3049 */
3050 if (!s->memcg_params)
3051 return;
3052
3053 if (s->memcg_params->is_root_cache)
3054 goto out;
3055
3056 memcg = s->memcg_params->memcg;
3057 id = memcg_cache_id(memcg);
3058
3059 root = s->memcg_params->root_cache;
3060 root->memcg_params->memcg_caches[id] = NULL;
Glauber Costad7f25f82012-12-18 14:22:40 -08003061
3062 mutex_lock(&memcg->slab_caches_mutex);
3063 list_del(&s->memcg_params->list);
3064 mutex_unlock(&memcg->slab_caches_mutex);
3065
Li Zefan20f05312013-07-08 16:00:31 -07003066 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003067out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003068 kfree(s->memcg_params);
3069}
3070
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003071/*
3072 * During the creation a new cache, we need to disable our accounting mechanism
3073 * altogether. This is true even if we are not creating, but rather just
3074 * enqueing new caches to be created.
3075 *
3076 * This is because that process will trigger allocations; some visible, like
3077 * explicit kmallocs to auxiliary data structures, name strings and internal
3078 * cache structures; some well concealed, like INIT_WORK() that can allocate
3079 * objects during debug.
3080 *
3081 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3082 * to it. This may not be a bounded recursion: since the first cache creation
3083 * failed to complete (waiting on the allocation), we'll just try to create the
3084 * cache again, failing at the same point.
3085 *
3086 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3087 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3088 * inside the following two functions.
3089 */
3090static inline void memcg_stop_kmem_account(void)
3091{
3092 VM_BUG_ON(!current->mm);
3093 current->memcg_kmem_skip_account++;
3094}
3095
3096static inline void memcg_resume_kmem_account(void)
3097{
3098 VM_BUG_ON(!current->mm);
3099 current->memcg_kmem_skip_account--;
3100}
3101
Glauber Costa1f458cb2012-12-18 14:22:50 -08003102static void kmem_cache_destroy_work_func(struct work_struct *w)
3103{
3104 struct kmem_cache *cachep;
3105 struct memcg_cache_params *p;
3106
3107 p = container_of(w, struct memcg_cache_params, destroy);
3108
3109 cachep = memcg_params_to_cache(p);
3110
Glauber Costa22933152012-12-18 14:22:59 -08003111 /*
3112 * If we get down to 0 after shrink, we could delete right away.
3113 * However, memcg_release_pages() already puts us back in the workqueue
3114 * in that case. If we proceed deleting, we'll get a dangling
3115 * reference, and removing the object from the workqueue in that case
3116 * is unnecessary complication. We are not a fast path.
3117 *
3118 * Note that this case is fundamentally different from racing with
3119 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3120 * kmem_cache_shrink, not only we would be reinserting a dead cache
3121 * into the queue, but doing so from inside the worker racing to
3122 * destroy it.
3123 *
3124 * So if we aren't down to zero, we'll just schedule a worker and try
3125 * again
3126 */
3127 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3128 kmem_cache_shrink(cachep);
3129 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3130 return;
3131 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003132 kmem_cache_destroy(cachep);
3133}
3134
3135void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3136{
3137 if (!cachep->memcg_params->dead)
3138 return;
3139
3140 /*
Glauber Costa22933152012-12-18 14:22:59 -08003141 * There are many ways in which we can get here.
3142 *
3143 * We can get to a memory-pressure situation while the delayed work is
3144 * still pending to run. The vmscan shrinkers can then release all
3145 * cache memory and get us to destruction. If this is the case, we'll
3146 * be executed twice, which is a bug (the second time will execute over
3147 * bogus data). In this case, cancelling the work should be fine.
3148 *
3149 * But we can also get here from the worker itself, if
3150 * kmem_cache_shrink is enough to shake all the remaining objects and
3151 * get the page count to 0. In this case, we'll deadlock if we try to
3152 * cancel the work (the worker runs with an internal lock held, which
3153 * is the same lock we would hold for cancel_work_sync().)
3154 *
3155 * Since we can't possibly know who got us here, just refrain from
3156 * running if there is already work pending
3157 */
3158 if (work_pending(&cachep->memcg_params->destroy))
3159 return;
3160 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003161 * We have to defer the actual destroying to a workqueue, because
3162 * we might currently be in a context that cannot sleep.
3163 */
3164 schedule_work(&cachep->memcg_params->destroy);
3165}
3166
Glauber Costad7f25f82012-12-18 14:22:40 -08003167/*
3168 * This lock protects updaters, not readers. We want readers to be as fast as
3169 * they can, and they will either see NULL or a valid cache value. Our model
3170 * allow them to see NULL, in which case the root memcg will be selected.
3171 *
3172 * We need this lock because multiple allocations to the same cache from a non
3173 * will span more than one worker. Only one of them can create the cache.
3174 */
3175static DEFINE_MUTEX(memcg_cache_mutex);
Michal Hockod9c10dd2013-03-28 08:48:14 +01003176
3177/*
3178 * Called with memcg_cache_mutex held
3179 */
3180static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3181 struct kmem_cache *s)
3182{
3183 struct kmem_cache *new;
3184 static char *tmp_name = NULL;
3185
3186 lockdep_assert_held(&memcg_cache_mutex);
3187
3188 /*
3189 * kmem_cache_create_memcg duplicates the given name and
3190 * cgroup_name for this name requires RCU context.
3191 * This static temporary buffer is used to prevent from
3192 * pointless shortliving allocation.
3193 */
3194 if (!tmp_name) {
3195 tmp_name = kmalloc(PATH_MAX, GFP_KERNEL);
3196 if (!tmp_name)
3197 return NULL;
3198 }
3199
3200 rcu_read_lock();
3201 snprintf(tmp_name, PATH_MAX, "%s(%d:%s)", s->name,
3202 memcg_cache_id(memcg), cgroup_name(memcg->css.cgroup));
3203 rcu_read_unlock();
3204
3205 new = kmem_cache_create_memcg(memcg, tmp_name, s->object_size, s->align,
3206 (s->flags & ~SLAB_PANIC), s->ctor, s);
3207
3208 if (new)
3209 new->allocflags |= __GFP_KMEMCG;
3210
3211 return new;
3212}
3213
Glauber Costad7f25f82012-12-18 14:22:40 -08003214static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3215 struct kmem_cache *cachep)
3216{
3217 struct kmem_cache *new_cachep;
3218 int idx;
3219
3220 BUG_ON(!memcg_can_account_kmem(memcg));
3221
3222 idx = memcg_cache_id(memcg);
3223
3224 mutex_lock(&memcg_cache_mutex);
3225 new_cachep = cachep->memcg_params->memcg_caches[idx];
Li Zefan20f05312013-07-08 16:00:31 -07003226 if (new_cachep) {
3227 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003228 goto out;
Li Zefan20f05312013-07-08 16:00:31 -07003229 }
Glauber Costad7f25f82012-12-18 14:22:40 -08003230
3231 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003232 if (new_cachep == NULL) {
3233 new_cachep = cachep;
Li Zefan20f05312013-07-08 16:00:31 -07003234 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003235 goto out;
3236 }
3237
Glauber Costa1f458cb2012-12-18 14:22:50 -08003238 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003239
3240 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3241 /*
3242 * the readers won't lock, make sure everybody sees the updated value,
3243 * so they won't put stuff in the queue again for no reason
3244 */
3245 wmb();
3246out:
3247 mutex_unlock(&memcg_cache_mutex);
3248 return new_cachep;
3249}
3250
Glauber Costa7cf27982012-12-18 14:22:55 -08003251void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3252{
3253 struct kmem_cache *c;
3254 int i;
3255
3256 if (!s->memcg_params)
3257 return;
3258 if (!s->memcg_params->is_root_cache)
3259 return;
3260
3261 /*
3262 * If the cache is being destroyed, we trust that there is no one else
3263 * requesting objects from it. Even if there are, the sanity checks in
3264 * kmem_cache_destroy should caught this ill-case.
3265 *
3266 * Still, we don't want anyone else freeing memcg_caches under our
3267 * noses, which can happen if a new memcg comes to life. As usual,
3268 * we'll take the set_limit_mutex to protect ourselves against this.
3269 */
3270 mutex_lock(&set_limit_mutex);
3271 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3272 c = s->memcg_params->memcg_caches[i];
3273 if (!c)
3274 continue;
3275
3276 /*
3277 * We will now manually delete the caches, so to avoid races
3278 * we need to cancel all pending destruction workers and
3279 * proceed with destruction ourselves.
3280 *
3281 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3282 * and that could spawn the workers again: it is likely that
3283 * the cache still have active pages until this very moment.
3284 * This would lead us back to mem_cgroup_destroy_cache.
3285 *
3286 * But that will not execute at all if the "dead" flag is not
3287 * set, so flip it down to guarantee we are in control.
3288 */
3289 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003290 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003291 kmem_cache_destroy(c);
3292 }
3293 mutex_unlock(&set_limit_mutex);
3294}
3295
Glauber Costad7f25f82012-12-18 14:22:40 -08003296struct create_work {
3297 struct mem_cgroup *memcg;
3298 struct kmem_cache *cachep;
3299 struct work_struct work;
3300};
3301
Glauber Costa1f458cb2012-12-18 14:22:50 -08003302static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3303{
3304 struct kmem_cache *cachep;
3305 struct memcg_cache_params *params;
3306
3307 if (!memcg_kmem_is_active(memcg))
3308 return;
3309
3310 mutex_lock(&memcg->slab_caches_mutex);
3311 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3312 cachep = memcg_params_to_cache(params);
3313 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003314 schedule_work(&cachep->memcg_params->destroy);
3315 }
3316 mutex_unlock(&memcg->slab_caches_mutex);
3317}
3318
Glauber Costad7f25f82012-12-18 14:22:40 -08003319static void memcg_create_cache_work_func(struct work_struct *w)
3320{
3321 struct create_work *cw;
3322
3323 cw = container_of(w, struct create_work, work);
3324 memcg_create_kmem_cache(cw->memcg, cw->cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003325 kfree(cw);
3326}
3327
3328/*
3329 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08003330 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003331static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3332 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003333{
3334 struct create_work *cw;
3335
3336 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
Li Zefanca0dde92013-04-29 15:08:57 -07003337 if (cw == NULL) {
3338 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08003339 return;
3340 }
3341
3342 cw->memcg = memcg;
3343 cw->cachep = cachep;
3344
3345 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3346 schedule_work(&cw->work);
3347}
3348
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003349static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3350 struct kmem_cache *cachep)
3351{
3352 /*
3353 * We need to stop accounting when we kmalloc, because if the
3354 * corresponding kmalloc cache is not yet created, the first allocation
3355 * in __memcg_create_cache_enqueue will recurse.
3356 *
3357 * However, it is better to enclose the whole function. Depending on
3358 * the debugging options enabled, INIT_WORK(), for instance, can
3359 * trigger an allocation. This too, will make us recurse. Because at
3360 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3361 * the safest choice is to do it like this, wrapping the whole function.
3362 */
3363 memcg_stop_kmem_account();
3364 __memcg_create_cache_enqueue(memcg, cachep);
3365 memcg_resume_kmem_account();
3366}
Glauber Costad7f25f82012-12-18 14:22:40 -08003367/*
3368 * Return the kmem_cache we're supposed to use for a slab allocation.
3369 * We try to use the current memcg's version of the cache.
3370 *
3371 * If the cache does not exist yet, if we are the first user of it,
3372 * we either create it immediately, if possible, or create it asynchronously
3373 * in a workqueue.
3374 * In the latter case, we will let the current allocation go through with
3375 * the original cache.
3376 *
3377 * Can't be called in interrupt context or from kernel threads.
3378 * This function needs to be called with rcu_read_lock() held.
3379 */
3380struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3381 gfp_t gfp)
3382{
3383 struct mem_cgroup *memcg;
3384 int idx;
3385
3386 VM_BUG_ON(!cachep->memcg_params);
3387 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3388
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003389 if (!current->mm || current->memcg_kmem_skip_account)
3390 return cachep;
3391
Glauber Costad7f25f82012-12-18 14:22:40 -08003392 rcu_read_lock();
3393 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
Glauber Costad7f25f82012-12-18 14:22:40 -08003394
3395 if (!memcg_can_account_kmem(memcg))
Li Zefanca0dde92013-04-29 15:08:57 -07003396 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003397
3398 idx = memcg_cache_id(memcg);
3399
3400 /*
3401 * barrier to mare sure we're always seeing the up to date value. The
3402 * code updating memcg_caches will issue a write barrier to match this.
3403 */
3404 read_barrier_depends();
Li Zefanca0dde92013-04-29 15:08:57 -07003405 if (likely(cachep->memcg_params->memcg_caches[idx])) {
3406 cachep = cachep->memcg_params->memcg_caches[idx];
3407 goto out;
Glauber Costad7f25f82012-12-18 14:22:40 -08003408 }
3409
Li Zefanca0dde92013-04-29 15:08:57 -07003410 /* The corresponding put will be done in the workqueue. */
3411 if (!css_tryget(&memcg->css))
3412 goto out;
3413 rcu_read_unlock();
3414
3415 /*
3416 * If we are in a safe context (can wait, and not in interrupt
3417 * context), we could be be predictable and return right away.
3418 * This would guarantee that the allocation being performed
3419 * already belongs in the new cache.
3420 *
3421 * However, there are some clashes that can arrive from locking.
3422 * For instance, because we acquire the slab_mutex while doing
3423 * kmem_cache_dup, this means no further allocation could happen
3424 * with the slab_mutex held.
3425 *
3426 * Also, because cache creation issue get_online_cpus(), this
3427 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3428 * that ends up reversed during cpu hotplug. (cpuset allocates
3429 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3430 * better to defer everything.
3431 */
3432 memcg_create_cache_enqueue(memcg, cachep);
3433 return cachep;
3434out:
3435 rcu_read_unlock();
3436 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08003437}
3438EXPORT_SYMBOL(__memcg_kmem_get_cache);
3439
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003440/*
3441 * We need to verify if the allocation against current->mm->owner's memcg is
3442 * possible for the given order. But the page is not allocated yet, so we'll
3443 * need a further commit step to do the final arrangements.
3444 *
3445 * It is possible for the task to switch cgroups in this mean time, so at
3446 * commit time, we can't rely on task conversion any longer. We'll then use
3447 * the handle argument to return to the caller which cgroup we should commit
3448 * against. We could also return the memcg directly and avoid the pointer
3449 * passing, but a boolean return value gives better semantics considering
3450 * the compiled-out case as well.
3451 *
3452 * Returning true means the allocation is possible.
3453 */
3454bool
3455__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3456{
3457 struct mem_cgroup *memcg;
3458 int ret;
3459
3460 *_memcg = NULL;
Glauber Costa6d42c232013-07-08 16:00:00 -07003461
3462 /*
3463 * Disabling accounting is only relevant for some specific memcg
3464 * internal allocations. Therefore we would initially not have such
3465 * check here, since direct calls to the page allocator that are marked
3466 * with GFP_KMEMCG only happen outside memcg core. We are mostly
3467 * concerned with cache allocations, and by having this test at
3468 * memcg_kmem_get_cache, we are already able to relay the allocation to
3469 * the root cache and bypass the memcg cache altogether.
3470 *
3471 * There is one exception, though: the SLUB allocator does not create
3472 * large order caches, but rather service large kmallocs directly from
3473 * the page allocator. Therefore, the following sequence when backed by
3474 * the SLUB allocator:
3475 *
3476 * memcg_stop_kmem_account();
3477 * kmalloc(<large_number>)
3478 * memcg_resume_kmem_account();
3479 *
3480 * would effectively ignore the fact that we should skip accounting,
3481 * since it will drive us directly to this function without passing
3482 * through the cache selector memcg_kmem_get_cache. Such large
3483 * allocations are extremely rare but can happen, for instance, for the
3484 * cache arrays. We bring this test here.
3485 */
3486 if (!current->mm || current->memcg_kmem_skip_account)
3487 return true;
3488
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003489 memcg = try_get_mem_cgroup_from_mm(current->mm);
3490
3491 /*
3492 * very rare case described in mem_cgroup_from_task. Unfortunately there
3493 * isn't much we can do without complicating this too much, and it would
3494 * be gfp-dependent anyway. Just let it go
3495 */
3496 if (unlikely(!memcg))
3497 return true;
3498
3499 if (!memcg_can_account_kmem(memcg)) {
3500 css_put(&memcg->css);
3501 return true;
3502 }
3503
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003504 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3505 if (!ret)
3506 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003507
3508 css_put(&memcg->css);
3509 return (ret == 0);
3510}
3511
3512void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3513 int order)
3514{
3515 struct page_cgroup *pc;
3516
3517 VM_BUG_ON(mem_cgroup_is_root(memcg));
3518
3519 /* The page allocation failed. Revert */
3520 if (!page) {
3521 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003522 return;
3523 }
3524
3525 pc = lookup_page_cgroup(page);
3526 lock_page_cgroup(pc);
3527 pc->mem_cgroup = memcg;
3528 SetPageCgroupUsed(pc);
3529 unlock_page_cgroup(pc);
3530}
3531
3532void __memcg_kmem_uncharge_pages(struct page *page, int order)
3533{
3534 struct mem_cgroup *memcg = NULL;
3535 struct page_cgroup *pc;
3536
3537
3538 pc = lookup_page_cgroup(page);
3539 /*
3540 * Fast unlocked return. Theoretically might have changed, have to
3541 * check again after locking.
3542 */
3543 if (!PageCgroupUsed(pc))
3544 return;
3545
3546 lock_page_cgroup(pc);
3547 if (PageCgroupUsed(pc)) {
3548 memcg = pc->mem_cgroup;
3549 ClearPageCgroupUsed(pc);
3550 }
3551 unlock_page_cgroup(pc);
3552
3553 /*
3554 * We trust that only if there is a memcg associated with the page, it
3555 * is a valid allocation
3556 */
3557 if (!memcg)
3558 return;
3559
3560 VM_BUG_ON(mem_cgroup_is_root(memcg));
3561 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003562}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003563#else
3564static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3565{
3566}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003567#endif /* CONFIG_MEMCG_KMEM */
3568
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003569#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3570
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003571#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003572/*
3573 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003574 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3575 * charge/uncharge will be never happen and move_account() is done under
3576 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003577 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003578void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003579{
3580 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003581 struct page_cgroup *pc;
David Rientjesb070e652013-05-07 16:18:09 -07003582 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003583 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003584
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003585 if (mem_cgroup_disabled())
3586 return;
David Rientjesb070e652013-05-07 16:18:09 -07003587
3588 memcg = head_pc->mem_cgroup;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003589 for (i = 1; i < HPAGE_PMD_NR; i++) {
3590 pc = head_pc + i;
David Rientjesb070e652013-05-07 16:18:09 -07003591 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003592 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003593 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3594 }
David Rientjesb070e652013-05-07 16:18:09 -07003595 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3596 HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003597}
Hugh Dickins12d27102012-01-12 17:19:52 -08003598#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003599
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003600/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003601 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003602 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003603 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003604 * @pc: page_cgroup of the page.
3605 * @from: mem_cgroup which the page is moved from.
3606 * @to: mem_cgroup which the page is moved to. @from != @to.
3607 *
3608 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003609 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003610 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003611 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003612 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3613 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003614 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003615static int mem_cgroup_move_account(struct page *page,
3616 unsigned int nr_pages,
3617 struct page_cgroup *pc,
3618 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003619 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003620{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003621 unsigned long flags;
3622 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003623 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003624
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003625 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003626 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003627 /*
3628 * The page is isolated from LRU. So, collapse function
3629 * will not handle this page. But page splitting can happen.
3630 * Do this check under compound_page_lock(). The caller should
3631 * hold it.
3632 */
3633 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003634 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003635 goto out;
3636
3637 lock_page_cgroup(pc);
3638
3639 ret = -EINVAL;
3640 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3641 goto unlock;
3642
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003643 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003644
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003645 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003646 /* Update mapped_file data for mem_cgroup */
3647 preempt_disable();
3648 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3649 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3650 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003651 }
David Rientjesb070e652013-05-07 16:18:09 -07003652 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003653
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003654 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003655 pc->mem_cgroup = to;
David Rientjesb070e652013-05-07 16:18:09 -07003656 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003657 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003658 ret = 0;
3659unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003660 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003661 /*
3662 * check events
3663 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003664 memcg_check_events(to, page);
3665 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003666out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003667 return ret;
3668}
3669
Michal Hocko2ef37d32012-10-26 13:37:30 +02003670/**
3671 * mem_cgroup_move_parent - moves page to the parent group
3672 * @page: the page to move
3673 * @pc: page_cgroup of the page
3674 * @child: page's cgroup
3675 *
3676 * move charges to its parent or the root cgroup if the group has no
3677 * parent (aka use_hierarchy==0).
3678 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3679 * mem_cgroup_move_account fails) the failure is always temporary and
3680 * it signals a race with a page removal/uncharge or migration. In the
3681 * first case the page is on the way out and it will vanish from the LRU
3682 * on the next attempt and the call should be retried later.
3683 * Isolation from the LRU fails only if page has been isolated from
3684 * the LRU since we looked at it and that usually means either global
3685 * reclaim or migration going on. The page will either get back to the
3686 * LRU or vanish.
3687 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3688 * (!PageCgroupUsed) or moved to a different group. The page will
3689 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003690 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003691static int mem_cgroup_move_parent(struct page *page,
3692 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003693 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003694{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003695 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003696 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003697 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003698 int ret;
3699
Michal Hockod8423012012-10-26 13:37:29 +02003700 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003701
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003702 ret = -EBUSY;
3703 if (!get_page_unless_zero(page))
3704 goto out;
3705 if (isolate_lru_page(page))
3706 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003707
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003708 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003709
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003710 parent = parent_mem_cgroup(child);
3711 /*
3712 * If no parent, move charges to root cgroup.
3713 */
3714 if (!parent)
3715 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003716
Michal Hocko2ef37d32012-10-26 13:37:30 +02003717 if (nr_pages > 1) {
3718 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003719 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003720 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003721
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003722 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003723 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003724 if (!ret)
3725 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003726
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003727 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003728 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003729 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003730put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003731 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003732out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003733 return ret;
3734}
3735
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003736/*
3737 * Charge the memory controller for page usage.
3738 * Return
3739 * 0 if the charge was successful
3740 * < 0 if the cgroup is over its limit
3741 */
3742static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003743 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003744{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003745 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003746 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003747 bool oom = true;
3748 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003749
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003750 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003751 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003752 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003753 /*
3754 * Never OOM-kill a process for a huge page. The
3755 * fault handler will fall back to regular pages.
3756 */
3757 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003758 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003759
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003760 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003761 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003762 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003763 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003764 return 0;
3765}
3766
3767int mem_cgroup_newpage_charge(struct page *page,
3768 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003769{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003770 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003771 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003772 VM_BUG_ON(page_mapped(page));
3773 VM_BUG_ON(page->mapping && !PageAnon(page));
3774 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003775 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003776 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003777}
3778
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003779/*
3780 * While swap-in, try_charge -> commit or cancel, the page is locked.
3781 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003782 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003783 * "commit()" or removed by "cancel()"
3784 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003785static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3786 struct page *page,
3787 gfp_t mask,
3788 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003789{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003790 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003791 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003792 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793
Johannes Weiner90deb782012-07-31 16:45:47 -07003794 pc = lookup_page_cgroup(page);
3795 /*
3796 * Every swap fault against a single page tries to charge the
3797 * page, bail as early as possible. shmem_unuse() encounters
3798 * already charged pages, too. The USED bit is protected by
3799 * the page lock, which serializes swap cache removal, which
3800 * in turn serializes uncharging.
3801 */
3802 if (PageCgroupUsed(pc))
3803 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003804 if (!do_swap_account)
3805 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003806 memcg = try_get_mem_cgroup_from_page(page);
3807 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003808 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003809 *memcgp = memcg;
3810 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003811 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003812 if (ret == -EINTR)
3813 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003814 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003815charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003816 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3817 if (ret == -EINTR)
3818 ret = 0;
3819 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003820}
3821
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003822int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3823 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3824{
3825 *memcgp = NULL;
3826 if (mem_cgroup_disabled())
3827 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003828 /*
3829 * A racing thread's fault, or swapoff, may have already
3830 * updated the pte, and even removed page from swap cache: in
3831 * those cases unuse_pte()'s pte_same() test will fail; but
3832 * there's also a KSM case which does need to charge the page.
3833 */
3834 if (!PageSwapCache(page)) {
3835 int ret;
3836
3837 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3838 if (ret == -EINTR)
3839 ret = 0;
3840 return ret;
3841 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003842 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3843}
3844
Johannes Weiner827a03d2012-07-31 16:45:36 -07003845void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3846{
3847 if (mem_cgroup_disabled())
3848 return;
3849 if (!memcg)
3850 return;
3851 __mem_cgroup_cancel_charge(memcg, 1);
3852}
3853
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003854static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003855__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003856 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003857{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003858 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003859 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003860 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003861 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003862
Johannes Weinerce587e62012-04-24 20:22:33 +02003863 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003864 /*
3865 * Now swap is on-memory. This means this page may be
3866 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003867 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3868 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3869 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003870 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003871 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003872 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003873 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003874 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003875}
3876
Johannes Weiner72835c82012-01-12 17:18:32 -08003877void mem_cgroup_commit_charge_swapin(struct page *page,
3878 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003879{
Johannes Weiner72835c82012-01-12 17:18:32 -08003880 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003881 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003882}
3883
Johannes Weiner827a03d2012-07-31 16:45:36 -07003884int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3885 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003886{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003887 struct mem_cgroup *memcg = NULL;
3888 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3889 int ret;
3890
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003891 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003892 return 0;
3893 if (PageCompound(page))
3894 return 0;
3895
Johannes Weiner827a03d2012-07-31 16:45:36 -07003896 if (!PageSwapCache(page))
3897 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3898 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003899 ret = __mem_cgroup_try_charge_swapin(mm, page,
3900 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003901 if (!ret)
3902 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3903 }
3904 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003905}
3906
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003907static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003908 unsigned int nr_pages,
3909 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003910{
3911 struct memcg_batch_info *batch = NULL;
3912 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003913
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003914 /* If swapout, usage of swap doesn't decrease */
3915 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3916 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003917
3918 batch = &current->memcg_batch;
3919 /*
3920 * In usual, we do css_get() when we remember memcg pointer.
3921 * But in this case, we keep res->usage until end of a series of
3922 * uncharges. Then, it's ok to ignore memcg's refcnt.
3923 */
3924 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003925 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003926 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003927 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003928 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003929 * the same cgroup and we have chance to coalesce uncharges.
3930 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3931 * because we want to do uncharge as soon as possible.
3932 */
3933
3934 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3935 goto direct_uncharge;
3936
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003937 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003938 goto direct_uncharge;
3939
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003940 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003941 * In typical case, batch->memcg == mem. This means we can
3942 * merge a series of uncharges to an uncharge of res_counter.
3943 * If not, we uncharge res_counter ony by one.
3944 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003945 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003946 goto direct_uncharge;
3947 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003948 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003949 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003950 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003951 return;
3952direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003953 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003954 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003955 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3956 if (unlikely(batch->memcg != memcg))
3957 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003958}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003959
Balbir Singh8697d332008-02-07 00:13:59 -08003960/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003961 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003962 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003963static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003964__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3965 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003966{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003967 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003968 unsigned int nr_pages = 1;
3969 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003970 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003971
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003972 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003973 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003974
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003975 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003976 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003977 VM_BUG_ON(!PageTransHuge(page));
3978 }
Balbir Singh8697d332008-02-07 00:13:59 -08003979 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003980 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003981 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003982 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003983 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003984 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003985
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003986 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003987
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003988 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003989
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003990 if (!PageCgroupUsed(pc))
3991 goto unlock_out;
3992
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003993 anon = PageAnon(page);
3994
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003995 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003996 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003997 /*
3998 * Generally PageAnon tells if it's the anon statistics to be
3999 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4000 * used before page reached the stage of being marked PageAnon.
4001 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004002 anon = true;
4003 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004004 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004005 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004006 if (page_mapped(page))
4007 goto unlock_out;
4008 /*
4009 * Pages under migration may not be uncharged. But
4010 * end_migration() /must/ be the one uncharging the
4011 * unused post-migration page and so it has to call
4012 * here with the migration bit still set. See the
4013 * res_counter handling below.
4014 */
4015 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004016 goto unlock_out;
4017 break;
4018 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4019 if (!PageAnon(page)) { /* Shared memory */
4020 if (page->mapping && !page_is_file_cache(page))
4021 goto unlock_out;
4022 } else if (page_mapped(page)) /* Anon */
4023 goto unlock_out;
4024 break;
4025 default:
4026 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004027 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004028
David Rientjesb070e652013-05-07 16:18:09 -07004029 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004030
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004031 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004032 /*
4033 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4034 * freed from LRU. This is safe because uncharged page is expected not
4035 * to be reused (freed soon). Exception is SwapCache, it's handled by
4036 * special functions.
4037 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004038
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004039 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004040 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004041 * even after unlock, we have memcg->res.usage here and this memcg
Li Zefan40503772013-07-08 16:00:34 -07004042 * will never be freed, so it's safe to call css_get().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004043 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004044 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004045 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004046 mem_cgroup_swap_statistics(memcg, true);
Li Zefan40503772013-07-08 16:00:34 -07004047 css_get(&memcg->css);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004048 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004049 /*
4050 * Migration does not charge the res_counter for the
4051 * replacement page, so leave it alone when phasing out the
4052 * page that is unused after the migration.
4053 */
4054 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004055 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004056
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004057 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004058
4059unlock_out:
4060 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004061 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004062}
4063
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004064void mem_cgroup_uncharge_page(struct page *page)
4065{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004066 /* early check. */
4067 if (page_mapped(page))
4068 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004069 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner28ccddf2013-05-24 15:55:15 -07004070 /*
4071 * If the page is in swap cache, uncharge should be deferred
4072 * to the swap path, which also properly accounts swap usage
4073 * and handles memcg lifetime.
4074 *
4075 * Note that this check is not stable and reclaim may add the
4076 * page to swap cache at any time after this. However, if the
4077 * page is not in swap cache by the time page->mapcount hits
4078 * 0, there won't be any page table references to the swap
4079 * slot, and reclaim will free it and not actually write the
4080 * page to disk.
4081 */
Johannes Weiner0c59b892012-07-31 16:45:31 -07004082 if (PageSwapCache(page))
4083 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004084 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004085}
4086
4087void mem_cgroup_uncharge_cache_page(struct page *page)
4088{
4089 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004090 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004091 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004092}
4093
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004094/*
4095 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4096 * In that cases, pages are freed continuously and we can expect pages
4097 * are in the same memcg. All these calls itself limits the number of
4098 * pages freed at once, then uncharge_start/end() is called properly.
4099 * This may be called prural(2) times in a context,
4100 */
4101
4102void mem_cgroup_uncharge_start(void)
4103{
4104 current->memcg_batch.do_batch++;
4105 /* We can do nest. */
4106 if (current->memcg_batch.do_batch == 1) {
4107 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004108 current->memcg_batch.nr_pages = 0;
4109 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004110 }
4111}
4112
4113void mem_cgroup_uncharge_end(void)
4114{
4115 struct memcg_batch_info *batch = &current->memcg_batch;
4116
4117 if (!batch->do_batch)
4118 return;
4119
4120 batch->do_batch--;
4121 if (batch->do_batch) /* If stacked, do nothing. */
4122 return;
4123
4124 if (!batch->memcg)
4125 return;
4126 /*
4127 * This "batch->memcg" is valid without any css_get/put etc...
4128 * bacause we hide charges behind us.
4129 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004130 if (batch->nr_pages)
4131 res_counter_uncharge(&batch->memcg->res,
4132 batch->nr_pages * PAGE_SIZE);
4133 if (batch->memsw_nr_pages)
4134 res_counter_uncharge(&batch->memcg->memsw,
4135 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004136 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004137 /* forget this pointer (for sanity check) */
4138 batch->memcg = NULL;
4139}
4140
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004141#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004142/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004143 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004144 * memcg information is recorded to swap_cgroup of "ent"
4145 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004146void
4147mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004148{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004149 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004150 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004151
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004152 if (!swapout) /* this was a swap cache but the swap is unused ! */
4153 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4154
Johannes Weiner0030f532012-07-31 16:45:25 -07004155 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004156
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004157 /*
4158 * record memcg information, if swapout && memcg != NULL,
Li Zefan40503772013-07-08 16:00:34 -07004159 * css_get() was called in uncharge().
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004160 */
4161 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004162 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004163}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004164#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004165
Andrew Mortonc255a452012-07-31 16:43:02 -07004166#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004167/*
4168 * called from swap_entry_free(). remove record in swap_cgroup and
4169 * uncharge "memsw" account.
4170 */
4171void mem_cgroup_uncharge_swap(swp_entry_t ent)
4172{
4173 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004174 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004175
4176 if (!do_swap_account)
4177 return;
4178
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004179 id = swap_cgroup_record(ent, 0);
4180 rcu_read_lock();
4181 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004182 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004183 /*
4184 * We uncharge this because swap is freed.
4185 * This memcg can be obsolete one. We avoid calling css_tryget
4186 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004187 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004188 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004189 mem_cgroup_swap_statistics(memcg, false);
Li Zefan40503772013-07-08 16:00:34 -07004190 css_put(&memcg->css);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004191 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004192 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004193}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004194
4195/**
4196 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4197 * @entry: swap entry to be moved
4198 * @from: mem_cgroup which the entry is moved from
4199 * @to: mem_cgroup which the entry is moved to
4200 *
4201 * It succeeds only when the swap_cgroup's record for this entry is the same
4202 * as the mem_cgroup's id of @from.
4203 *
4204 * Returns 0 on success, -EINVAL on failure.
4205 *
4206 * The caller must have charged to @to, IOW, called res_counter_charge() about
4207 * both res and memsw, and called css_get().
4208 */
4209static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004210 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004211{
4212 unsigned short old_id, new_id;
4213
4214 old_id = css_id(&from->css);
4215 new_id = css_id(&to->css);
4216
4217 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004218 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004219 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004220 /*
4221 * This function is only called from task migration context now.
4222 * It postpones res_counter and refcount handling till the end
4223 * of task migration(mem_cgroup_clear_mc()) for performance
Li Zefan40503772013-07-08 16:00:34 -07004224 * improvement. But we cannot postpone css_get(to) because if
4225 * the process that has been moved to @to does swap-in, the
4226 * refcount of @to might be decreased to 0.
4227 *
4228 * We are in attach() phase, so the cgroup is guaranteed to be
4229 * alive, so we can just call css_get().
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004230 */
Li Zefan40503772013-07-08 16:00:34 -07004231 css_get(&to->css);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004232 return 0;
4233 }
4234 return -EINVAL;
4235}
4236#else
4237static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004238 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004239{
4240 return -EINVAL;
4241}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004242#endif
4243
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004244/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004245 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4246 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004247 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004248void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4249 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004250{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004251 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004252 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004253 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004254 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004255
Johannes Weiner72835c82012-01-12 17:18:32 -08004256 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004257
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004258 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004259 return;
Balbir Singh40779602008-04-04 14:29:59 -07004260
Mel Gormanb32967f2012-11-19 12:35:47 +00004261 if (PageTransHuge(page))
4262 nr_pages <<= compound_order(page);
4263
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004264 pc = lookup_page_cgroup(page);
4265 lock_page_cgroup(pc);
4266 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004267 memcg = pc->mem_cgroup;
4268 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004269 /*
4270 * At migrating an anonymous page, its mapcount goes down
4271 * to 0 and uncharge() will be called. But, even if it's fully
4272 * unmapped, migration may fail and this page has to be
4273 * charged again. We set MIGRATION flag here and delay uncharge
4274 * until end_migration() is called
4275 *
4276 * Corner Case Thinking
4277 * A)
4278 * When the old page was mapped as Anon and it's unmap-and-freed
4279 * while migration was ongoing.
4280 * If unmap finds the old page, uncharge() of it will be delayed
4281 * until end_migration(). If unmap finds a new page, it's
4282 * uncharged when it make mapcount to be 1->0. If unmap code
4283 * finds swap_migration_entry, the new page will not be mapped
4284 * and end_migration() will find it(mapcount==0).
4285 *
4286 * B)
4287 * When the old page was mapped but migraion fails, the kernel
4288 * remaps it. A charge for it is kept by MIGRATION flag even
4289 * if mapcount goes down to 0. We can do remap successfully
4290 * without charging it again.
4291 *
4292 * C)
4293 * The "old" page is under lock_page() until the end of
4294 * migration, so, the old page itself will not be swapped-out.
4295 * If the new page is swapped out before end_migraton, our
4296 * hook to usual swap-out path will catch the event.
4297 */
4298 if (PageAnon(page))
4299 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004300 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004301 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004302 /*
4303 * If the page is not charged at this point,
4304 * we return here.
4305 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004306 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004307 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004308
Johannes Weiner72835c82012-01-12 17:18:32 -08004309 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004310 /*
4311 * We charge new page before it's used/mapped. So, even if unlock_page()
4312 * is called before end_migration, we can catch all events on this new
4313 * page. In the case new page is migrated but not remapped, new page's
4314 * mapcount will be finally 0 and we call uncharge in end_migration().
4315 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004316 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004317 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004318 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004319 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004320 /*
4321 * The page is committed to the memcg, but it's not actually
4322 * charged to the res_counter since we plan on replacing the
4323 * old one and only one page is going to be left afterwards.
4324 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004325 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004326}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004327
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004328/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004329void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004330 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004331{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004332 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004333 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004334 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004336 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004337 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004338
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004339 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004340 used = oldpage;
4341 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004342 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004343 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004344 unused = oldpage;
4345 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004346 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004347 __mem_cgroup_uncharge_common(unused,
4348 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4349 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4350 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004351 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004352 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004353 * We disallowed uncharge of pages under migration because mapcount
4354 * of the page goes down to zero, temporarly.
4355 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004356 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004357 pc = lookup_page_cgroup(oldpage);
4358 lock_page_cgroup(pc);
4359 ClearPageCgroupMigration(pc);
4360 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004361
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004362 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004363 * If a page is a file cache, radix-tree replacement is very atomic
4364 * and we can skip this check. When it was an Anon page, its mapcount
4365 * goes down to 0. But because we added MIGRATION flage, it's not
4366 * uncharged yet. There are several case but page->mapcount check
4367 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4368 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004369 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004370 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004371 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004372}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004373
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004374/*
4375 * At replace page cache, newpage is not under any memcg but it's on
4376 * LRU. So, this function doesn't touch res_counter but handles LRU
4377 * in correct way. Both pages are locked so we cannot race with uncharge.
4378 */
4379void mem_cgroup_replace_page_cache(struct page *oldpage,
4380 struct page *newpage)
4381{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004382 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004383 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004384 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004385
4386 if (mem_cgroup_disabled())
4387 return;
4388
4389 pc = lookup_page_cgroup(oldpage);
4390 /* fix accounting on old pages */
4391 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004392 if (PageCgroupUsed(pc)) {
4393 memcg = pc->mem_cgroup;
David Rientjesb070e652013-05-07 16:18:09 -07004394 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004395 ClearPageCgroupUsed(pc);
4396 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004397 unlock_page_cgroup(pc);
4398
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004399 /*
4400 * When called from shmem_replace_page(), in some cases the
4401 * oldpage has already been charged, and in some cases not.
4402 */
4403 if (!memcg)
4404 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004405 /*
4406 * Even if newpage->mapping was NULL before starting replacement,
4407 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4408 * LRU while we overwrite pc->mem_cgroup.
4409 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004410 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004411}
4412
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004413#ifdef CONFIG_DEBUG_VM
4414static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4415{
4416 struct page_cgroup *pc;
4417
4418 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004419 /*
4420 * Can be NULL while feeding pages into the page allocator for
4421 * the first time, i.e. during boot or memory hotplug;
4422 * or when mem_cgroup_disabled().
4423 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004424 if (likely(pc) && PageCgroupUsed(pc))
4425 return pc;
4426 return NULL;
4427}
4428
4429bool mem_cgroup_bad_page_check(struct page *page)
4430{
4431 if (mem_cgroup_disabled())
4432 return false;
4433
4434 return lookup_page_cgroup_used(page) != NULL;
4435}
4436
4437void mem_cgroup_print_bad_page(struct page *page)
4438{
4439 struct page_cgroup *pc;
4440
4441 pc = lookup_page_cgroup_used(page);
4442 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004443 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4444 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004445 }
4446}
4447#endif
4448
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004449static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004450 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004451{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004452 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004453 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004454 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004455 int children = mem_cgroup_count_children(memcg);
4456 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004457 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004458
4459 /*
4460 * For keeping hierarchical_reclaim simple, how long we should retry
4461 * is depends on callers. We set our retry-count to be function
4462 * of # of children which we should visit in this loop.
4463 */
4464 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4465
4466 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004467
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004468 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004469 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004470 if (signal_pending(current)) {
4471 ret = -EINTR;
4472 break;
4473 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004474 /*
4475 * Rather than hide all in some function, I do this in
4476 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004477 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004478 */
4479 mutex_lock(&set_limit_mutex);
4480 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4481 if (memswlimit < val) {
4482 ret = -EINVAL;
4483 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004484 break;
4485 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004486
4487 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4488 if (memlimit < val)
4489 enlarge = 1;
4490
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004491 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004492 if (!ret) {
4493 if (memswlimit == val)
4494 memcg->memsw_is_minimum = true;
4495 else
4496 memcg->memsw_is_minimum = false;
4497 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004498 mutex_unlock(&set_limit_mutex);
4499
4500 if (!ret)
4501 break;
4502
Johannes Weiner56600482012-01-12 17:17:59 -08004503 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4504 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004505 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4506 /* Usage is reduced ? */
4507 if (curusage >= oldusage)
4508 retry_count--;
4509 else
4510 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004511 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004512 if (!ret && enlarge)
4513 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004514
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004515 return ret;
4516}
4517
Li Zefan338c8432009-06-17 16:27:15 -07004518static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4519 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004520{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004521 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004522 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004523 int children = mem_cgroup_count_children(memcg);
4524 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004525 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004526
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004527 /* see mem_cgroup_resize_res_limit */
4528 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4529 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004530 while (retry_count) {
4531 if (signal_pending(current)) {
4532 ret = -EINTR;
4533 break;
4534 }
4535 /*
4536 * Rather than hide all in some function, I do this in
4537 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004538 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004539 */
4540 mutex_lock(&set_limit_mutex);
4541 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4542 if (memlimit > val) {
4543 ret = -EINVAL;
4544 mutex_unlock(&set_limit_mutex);
4545 break;
4546 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004547 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4548 if (memswlimit < val)
4549 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004550 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004551 if (!ret) {
4552 if (memlimit == val)
4553 memcg->memsw_is_minimum = true;
4554 else
4555 memcg->memsw_is_minimum = false;
4556 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004557 mutex_unlock(&set_limit_mutex);
4558
4559 if (!ret)
4560 break;
4561
Johannes Weiner56600482012-01-12 17:17:59 -08004562 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4563 MEM_CGROUP_RECLAIM_NOSWAP |
4564 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004565 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004566 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004567 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004568 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004569 else
4570 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004571 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004572 if (!ret && enlarge)
4573 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004574 return ret;
4575}
4576
Michal Hocko2ef37d32012-10-26 13:37:30 +02004577/**
4578 * mem_cgroup_force_empty_list - clears LRU of a group
4579 * @memcg: group to clear
4580 * @node: NUMA node
4581 * @zid: zone id
4582 * @lru: lru to to clear
4583 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004584 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004585 * reclaim the pages page themselves - pages are moved to the parent (or root)
4586 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004587 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004588static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004589 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004590{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004591 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004592 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004593 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004594 struct page *busy;
4595 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004596
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004597 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004598 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4599 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004600
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004601 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004602 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004603 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004604 struct page *page;
4605
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004606 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004607 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004608 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004609 break;
4610 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004611 page = list_entry(list->prev, struct page, lru);
4612 if (busy == page) {
4613 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004614 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004615 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004616 continue;
4617 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004618 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004619
Johannes Weiner925b7672012-01-12 17:18:15 -08004620 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004621
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004622 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004623 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004624 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004625 cond_resched();
4626 } else
4627 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004628 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004629}
4630
4631/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004632 * make mem_cgroup's charge to be 0 if there is no task by moving
4633 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004634 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004635 *
4636 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004637 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004638static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004639{
Michal Hockoc26251f2012-10-26 13:37:28 +02004640 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004641 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004642
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004643 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004644 /* This is for making all *used* pages to be on LRU. */
4645 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004646 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004647 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004648 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004649 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004650 enum lru_list lru;
4651 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004652 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004653 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004654 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004655 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004656 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004657 mem_cgroup_end_move(memcg);
4658 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004659 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004660
Michal Hocko2ef37d32012-10-26 13:37:30 +02004661 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004662 * Kernel memory may not necessarily be trackable to a specific
4663 * process. So they are not migrated, and therefore we can't
4664 * expect their value to drop to 0 here.
4665 * Having res filled up with kmem only is enough.
4666 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004667 * This is a safety check because mem_cgroup_force_empty_list
4668 * could have raced with mem_cgroup_replace_page_cache callers
4669 * so the lru seemed empty but the page could have been added
4670 * right after the check. RES_USAGE should be safe as we always
4671 * charge before adding to the LRU.
4672 */
Glauber Costabea207c2012-12-18 14:22:11 -08004673 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4674 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4675 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004676}
4677
4678/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004679 * This mainly exists for tests during the setting of set of use_hierarchy.
4680 * Since this is the very setting we are changing, the current hierarchy value
4681 * is meaningless
4682 */
4683static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4684{
Tejun Heo492eb212013-08-08 20:11:25 -04004685 struct cgroup_subsys_state *pos;
Glauber Costab5f99b52013-02-22 16:34:53 -08004686
4687 /* bounce at first found */
Tejun Heo492eb212013-08-08 20:11:25 -04004688 css_for_each_child(pos, &memcg->css)
Glauber Costab5f99b52013-02-22 16:34:53 -08004689 return true;
4690 return false;
4691}
4692
4693/*
Glauber Costa09998212013-02-22 16:34:55 -08004694 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4695 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004696 * from mem_cgroup_count_children(), in the sense that we don't really care how
4697 * many children we have; we only need to know if we have any. It also counts
4698 * any memcg without hierarchy as infertile.
4699 */
4700static inline bool memcg_has_children(struct mem_cgroup *memcg)
4701{
4702 return memcg->use_hierarchy && __memcg_has_children(memcg);
4703}
4704
4705/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004706 * Reclaims as many pages from the given memcg as possible and moves
4707 * the rest to the parent.
4708 *
4709 * Caller is responsible for holding css reference for memcg.
4710 */
4711static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4712{
4713 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4714 struct cgroup *cgrp = memcg->css.cgroup;
4715
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004716 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004717 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4718 return -EBUSY;
4719
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004720 /* we call try-to-free pages for make this cgroup empty */
4721 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004722 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004723 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004724 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004725
Michal Hockoc26251f2012-10-26 13:37:28 +02004726 if (signal_pending(current))
4727 return -EINTR;
4728
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004729 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004730 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004731 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004732 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004733 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004734 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004735 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004736
4737 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004738 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004739 mem_cgroup_reparent_charges(memcg);
4740
4741 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004742}
4743
Tejun Heo182446d2013-08-08 20:11:24 -04004744static int mem_cgroup_force_empty_write(struct cgroup_subsys_state *css,
4745 unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004746{
Tejun Heo182446d2013-08-08 20:11:24 -04004747 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Michal Hockoc26251f2012-10-26 13:37:28 +02004748
Michal Hockod8423012012-10-26 13:37:29 +02004749 if (mem_cgroup_is_root(memcg))
4750 return -EINVAL;
Li Zefanc33bd832013-09-12 15:13:19 -07004751 return mem_cgroup_force_empty(memcg);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004752}
4753
Tejun Heo182446d2013-08-08 20:11:24 -04004754static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
4755 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004756{
Tejun Heo182446d2013-08-08 20:11:24 -04004757 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004758}
4759
Tejun Heo182446d2013-08-08 20:11:24 -04004760static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
4761 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08004762{
4763 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04004764 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04004765 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(css_parent(&memcg->css));
Balbir Singh18f59ea2009-01-07 18:08:07 -08004766
Glauber Costa09998212013-02-22 16:34:55 -08004767 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004768
4769 if (memcg->use_hierarchy == val)
4770 goto out;
4771
Balbir Singh18f59ea2009-01-07 18:08:07 -08004772 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004773 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004774 * in the child subtrees. If it is unset, then the change can
4775 * occur, provided the current cgroup has no children.
4776 *
4777 * For the root cgroup, parent_mem is NULL, we allow value to be
4778 * set if there are no children.
4779 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004780 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004781 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004782 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004783 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004784 else
4785 retval = -EBUSY;
4786 } else
4787 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004788
4789out:
Glauber Costa09998212013-02-22 16:34:55 -08004790 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004791
4792 return retval;
4793}
4794
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004795
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004796static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004797 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004798{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004799 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004800 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004801
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004802 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004803 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004804 val += mem_cgroup_read_stat(iter, idx);
4805
4806 if (val < 0) /* race ? */
4807 val = 0;
4808 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004809}
4810
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004811static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004812{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004813 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004815 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004816 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004817 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004818 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004819 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004820 }
4821
David Rientjesb070e652013-05-07 16:18:09 -07004822 /*
4823 * Transparent hugepages are still accounted for in MEM_CGROUP_STAT_RSS
4824 * as well as in MEM_CGROUP_STAT_RSS_HUGE.
4825 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004826 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4827 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004828
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004829 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004830 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004831
4832 return val << PAGE_SHIFT;
4833}
4834
Tejun Heo182446d2013-08-08 20:11:24 -04004835static ssize_t mem_cgroup_read(struct cgroup_subsys_state *css,
4836 struct cftype *cft, struct file *file,
4837 char __user *buf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004838{
Tejun Heo182446d2013-08-08 20:11:24 -04004839 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heoaf36f902012-04-01 12:09:55 -07004840 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004841 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004842 int name, len;
4843 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004844
4845 type = MEMFILE_TYPE(cft->private);
4846 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004847
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004848 switch (type) {
4849 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004850 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004851 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004852 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004853 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004854 break;
4855 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004856 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004857 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004858 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004859 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004860 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004861 case _KMEM:
4862 val = res_counter_read_u64(&memcg->kmem, name);
4863 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004864 default:
4865 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004866 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004867
4868 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4869 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004870}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004871
Tejun Heo182446d2013-08-08 20:11:24 -04004872static int memcg_update_kmem_limit(struct cgroup_subsys_state *css, u64 val)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004873{
4874 int ret = -EINVAL;
4875#ifdef CONFIG_MEMCG_KMEM
Tejun Heo182446d2013-08-08 20:11:24 -04004876 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004877 /*
4878 * For simplicity, we won't allow this to be disabled. It also can't
4879 * be changed if the cgroup has children already, or if tasks had
4880 * already joined.
4881 *
4882 * If tasks join before we set the limit, a person looking at
4883 * kmem.usage_in_bytes will have no way to determine when it took
4884 * place, which makes the value quite meaningless.
4885 *
4886 * After it first became limited, changes in the value of the limit are
4887 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004888 */
Glauber Costa09998212013-02-22 16:34:55 -08004889 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004890 mutex_lock(&set_limit_mutex);
4891 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Tejun Heo182446d2013-08-08 20:11:24 -04004892 if (cgroup_task_count(css->cgroup) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004893 ret = -EBUSY;
4894 goto out;
4895 }
4896 ret = res_counter_set_limit(&memcg->kmem, val);
4897 VM_BUG_ON(ret);
4898
Glauber Costa55007d82012-12-18 14:22:38 -08004899 ret = memcg_update_cache_sizes(memcg);
4900 if (ret) {
4901 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
4902 goto out;
4903 }
Glauber Costa692e89a2013-02-22 16:34:56 -08004904 static_key_slow_inc(&memcg_kmem_enabled_key);
4905 /*
4906 * setting the active bit after the inc will guarantee no one
4907 * starts accounting before all call sites are patched
4908 */
4909 memcg_kmem_set_active(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004910 } else
4911 ret = res_counter_set_limit(&memcg->kmem, val);
4912out:
4913 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08004914 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004915#endif
4916 return ret;
4917}
4918
Hugh Dickins6d0439902013-02-22 16:35:50 -08004919#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08004920static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08004921{
Glauber Costa55007d82012-12-18 14:22:38 -08004922 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004923 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4924 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08004925 goto out;
4926
Glauber Costa510fc4e2012-12-18 14:21:47 -08004927 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08004928 /*
4929 * When that happen, we need to disable the static branch only on those
4930 * memcgs that enabled it. To achieve this, we would be forced to
4931 * complicate the code by keeping track of which memcgs were the ones
4932 * that actually enabled limits, and which ones got it from its
4933 * parents.
4934 *
4935 * It is a lot simpler just to do static_key_slow_inc() on every child
4936 * that is accounted.
4937 */
Glauber Costa55007d82012-12-18 14:22:38 -08004938 if (!memcg_kmem_is_active(memcg))
4939 goto out;
4940
4941 /*
Li Zefan10d5ebf2013-07-08 16:00:33 -07004942 * __mem_cgroup_free() will issue static_key_slow_dec() because this
4943 * memcg is active already. If the later initialization fails then the
4944 * cgroup core triggers the cleanup so we do not have to do it here.
Glauber Costa55007d82012-12-18 14:22:38 -08004945 */
Glauber Costa55007d82012-12-18 14:22:38 -08004946 static_key_slow_inc(&memcg_kmem_enabled_key);
4947
4948 mutex_lock(&set_limit_mutex);
Glauber Costa425c5982013-07-08 16:00:01 -07004949 memcg_stop_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004950 ret = memcg_update_cache_sizes(memcg);
Glauber Costa425c5982013-07-08 16:00:01 -07004951 memcg_resume_kmem_account();
Glauber Costa55007d82012-12-18 14:22:38 -08004952 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08004953out:
4954 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004955}
Hugh Dickins6d0439902013-02-22 16:35:50 -08004956#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08004957
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004958/*
4959 * The user of this function is...
4960 * RES_LIMIT.
4961 */
Tejun Heo182446d2013-08-08 20:11:24 -04004962static int mem_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
Paul Menage856c13a2008-07-25 01:47:04 -07004963 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004964{
Tejun Heo182446d2013-08-08 20:11:24 -04004965 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004966 enum res_type type;
4967 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004968 unsigned long long val;
4969 int ret;
4970
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004971 type = MEMFILE_TYPE(cft->private);
4972 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004973
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004974 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004975 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004976 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4977 ret = -EINVAL;
4978 break;
4979 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004980 /* This function does all necessary parse...reuse it */
4981 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004982 if (ret)
4983 break;
4984 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004985 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004986 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004987 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004988 else if (type == _KMEM)
Tejun Heo182446d2013-08-08 20:11:24 -04004989 ret = memcg_update_kmem_limit(css, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004990 else
4991 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004992 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004993 case RES_SOFT_LIMIT:
4994 ret = res_counter_memparse_write_strategy(buffer, &val);
4995 if (ret)
4996 break;
4997 /*
4998 * For memsw, soft limits are hard to implement in terms
4999 * of semantics, for now, we support soft limits for
5000 * control without swap
5001 */
5002 if (type == _MEM)
5003 ret = res_counter_set_soft_limit(&memcg->res, val);
5004 else
5005 ret = -EINVAL;
5006 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005007 default:
5008 ret = -EINVAL; /* should be BUG() ? */
5009 break;
5010 }
5011 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005012}
5013
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005014static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5015 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5016{
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005017 unsigned long long min_limit, min_memsw_limit, tmp;
5018
5019 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5020 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005021 if (!memcg->use_hierarchy)
5022 goto out;
5023
Tejun Heo63876982013-08-08 20:11:23 -04005024 while (css_parent(&memcg->css)) {
5025 memcg = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005026 if (!memcg->use_hierarchy)
5027 break;
5028 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5029 min_limit = min(min_limit, tmp);
5030 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5031 min_memsw_limit = min(min_memsw_limit, tmp);
5032 }
5033out:
5034 *mem_limit = min_limit;
5035 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005036}
5037
Tejun Heo182446d2013-08-08 20:11:24 -04005038static int mem_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005039{
Tejun Heo182446d2013-08-08 20:11:24 -04005040 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005041 int name;
5042 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005043
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005044 type = MEMFILE_TYPE(event);
5045 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005046
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005047 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005048 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005049 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005050 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005051 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005052 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005053 else if (type == _KMEM)
5054 res_counter_reset_max(&memcg->kmem);
5055 else
5056 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005057 break;
5058 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005059 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005060 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005061 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005062 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005063 else if (type == _KMEM)
5064 res_counter_reset_failcnt(&memcg->kmem);
5065 else
5066 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005067 break;
5068 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005069
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005070 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005071}
5072
Tejun Heo182446d2013-08-08 20:11:24 -04005073static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005074 struct cftype *cft)
5075{
Tejun Heo182446d2013-08-08 20:11:24 -04005076 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005077}
5078
Daisuke Nishimura02491442010-03-10 15:22:17 -08005079#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04005080static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005081 struct cftype *cft, u64 val)
5082{
Tejun Heo182446d2013-08-08 20:11:24 -04005083 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005084
5085 if (val >= (1 << NR_MOVE_TYPE))
5086 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005087
Glauber Costaee5e8472013-02-22 16:34:50 -08005088 /*
5089 * No kind of locking is needed in here, because ->can_attach() will
5090 * check this value once in the beginning of the process, and then carry
5091 * on with stale data. This means that changes to this value will only
5092 * affect task migrations starting after the change.
5093 */
5094 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005095 return 0;
5096}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005097#else
Tejun Heo182446d2013-08-08 20:11:24 -04005098static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005099 struct cftype *cft, u64 val)
5100{
5101 return -ENOSYS;
5102}
5103#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005104
Ying Han406eb0c2011-05-26 16:25:37 -07005105#ifdef CONFIG_NUMA
Tejun Heo182446d2013-08-08 20:11:24 -04005106static int memcg_numa_stat_show(struct cgroup_subsys_state *css,
5107 struct cftype *cft, struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005108{
5109 int nid;
5110 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5111 unsigned long node_nr;
Tejun Heo182446d2013-08-08 20:11:24 -04005112 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Ying Han406eb0c2011-05-26 16:25:37 -07005113
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005114 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005115 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005116 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005117 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005118 seq_printf(m, " N%d=%lu", nid, node_nr);
5119 }
5120 seq_putc(m, '\n');
5121
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005122 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005123 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005124 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005125 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005126 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005127 seq_printf(m, " N%d=%lu", nid, node_nr);
5128 }
5129 seq_putc(m, '\n');
5130
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005131 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005132 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005133 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005134 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005135 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005136 seq_printf(m, " N%d=%lu", nid, node_nr);
5137 }
5138 seq_putc(m, '\n');
5139
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005140 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005141 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005142 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005143 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005144 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005145 seq_printf(m, " N%d=%lu", nid, node_nr);
5146 }
5147 seq_putc(m, '\n');
5148 return 0;
5149}
5150#endif /* CONFIG_NUMA */
5151
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005152static inline void mem_cgroup_lru_names_not_uptodate(void)
5153{
5154 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5155}
5156
Tejun Heo182446d2013-08-08 20:11:24 -04005157static int memcg_stat_show(struct cgroup_subsys_state *css, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005158 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005159{
Tejun Heo182446d2013-08-08 20:11:24 -04005160 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005161 struct mem_cgroup *mi;
5162 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005163
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005164 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005165 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005166 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005167 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5168 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005169 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005170
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005171 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5172 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5173 mem_cgroup_read_events(memcg, i));
5174
5175 for (i = 0; i < NR_LRU_LISTS; i++)
5176 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5177 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5178
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005179 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005180 {
5181 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005182 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005183 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005184 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005185 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5186 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005187 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005188
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005189 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5190 long long val = 0;
5191
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005192 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005193 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005194 for_each_mem_cgroup_tree(mi, memcg)
5195 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5196 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5197 }
5198
5199 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5200 unsigned long long val = 0;
5201
5202 for_each_mem_cgroup_tree(mi, memcg)
5203 val += mem_cgroup_read_events(mi, i);
5204 seq_printf(m, "total_%s %llu\n",
5205 mem_cgroup_events_names[i], val);
5206 }
5207
5208 for (i = 0; i < NR_LRU_LISTS; i++) {
5209 unsigned long long val = 0;
5210
5211 for_each_mem_cgroup_tree(mi, memcg)
5212 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5213 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005214 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005215
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005216#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005217 {
5218 int nid, zid;
5219 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005220 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005221 unsigned long recent_rotated[2] = {0, 0};
5222 unsigned long recent_scanned[2] = {0, 0};
5223
5224 for_each_online_node(nid)
5225 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005226 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005227 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005228
Hugh Dickins89abfab2012-05-29 15:06:53 -07005229 recent_rotated[0] += rstat->recent_rotated[0];
5230 recent_rotated[1] += rstat->recent_rotated[1];
5231 recent_scanned[0] += rstat->recent_scanned[0];
5232 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005233 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005234 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5235 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5236 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5237 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005238 }
5239#endif
5240
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005241 return 0;
5242}
5243
Tejun Heo182446d2013-08-08 20:11:24 -04005244static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
5245 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005246{
Tejun Heo182446d2013-08-08 20:11:24 -04005247 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005248
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005249 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005250}
5251
Tejun Heo182446d2013-08-08 20:11:24 -04005252static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
5253 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005254{
Tejun Heo182446d2013-08-08 20:11:24 -04005255 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005256 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
Li Zefan068b38c2009-01-15 13:51:26 -08005257
Tejun Heo63876982013-08-08 20:11:23 -04005258 if (val > 100 || !parent)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005259 return -EINVAL;
5260
Glauber Costa09998212013-02-22 16:34:55 -08005261 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005262
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005263 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005264 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005265 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005266 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005267 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005268
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005269 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005270
Glauber Costa09998212013-02-22 16:34:55 -08005271 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005272
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005273 return 0;
5274}
5275
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005276static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5277{
5278 struct mem_cgroup_threshold_ary *t;
5279 u64 usage;
5280 int i;
5281
5282 rcu_read_lock();
5283 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005284 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005285 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005286 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005287
5288 if (!t)
5289 goto unlock;
5290
5291 usage = mem_cgroup_usage(memcg, swap);
5292
5293 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005294 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005295 * If it's not true, a threshold was crossed after last
5296 * call of __mem_cgroup_threshold().
5297 */
Phil Carmody5407a562010-05-26 14:42:42 -07005298 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005299
5300 /*
5301 * Iterate backward over array of thresholds starting from
5302 * current_threshold and check if a threshold is crossed.
5303 * If none of thresholds below usage is crossed, we read
5304 * only one element of the array here.
5305 */
5306 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5307 eventfd_signal(t->entries[i].eventfd, 1);
5308
5309 /* i = current_threshold + 1 */
5310 i++;
5311
5312 /*
5313 * Iterate forward over array of thresholds starting from
5314 * current_threshold+1 and check if a threshold is crossed.
5315 * If none of thresholds above usage is crossed, we read
5316 * only one element of the array here.
5317 */
5318 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5319 eventfd_signal(t->entries[i].eventfd, 1);
5320
5321 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005322 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005323unlock:
5324 rcu_read_unlock();
5325}
5326
5327static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5328{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005329 while (memcg) {
5330 __mem_cgroup_threshold(memcg, false);
5331 if (do_swap_account)
5332 __mem_cgroup_threshold(memcg, true);
5333
5334 memcg = parent_mem_cgroup(memcg);
5335 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005336}
5337
5338static int compare_thresholds(const void *a, const void *b)
5339{
5340 const struct mem_cgroup_threshold *_a = a;
5341 const struct mem_cgroup_threshold *_b = b;
5342
Greg Thelen2bff24a2013-09-11 14:23:08 -07005343 if (_a->threshold > _b->threshold)
5344 return 1;
5345
5346 if (_a->threshold < _b->threshold)
5347 return -1;
5348
5349 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005350}
5351
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005352static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005353{
5354 struct mem_cgroup_eventfd_list *ev;
5355
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005356 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005357 eventfd_signal(ev->eventfd, 1);
5358 return 0;
5359}
5360
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005361static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005362{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005363 struct mem_cgroup *iter;
5364
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005365 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005366 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005367}
5368
Tejun Heo81eeaf02013-08-08 20:11:26 -04005369static int mem_cgroup_usage_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005370 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005371{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005372 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005373 struct mem_cgroup_thresholds *thresholds;
5374 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005375 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005376 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005377 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005378
5379 ret = res_counter_memparse_write_strategy(args, &threshold);
5380 if (ret)
5381 return ret;
5382
5383 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005384
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005385 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005386 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005387 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005388 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005389 else
5390 BUG();
5391
5392 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5393
5394 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005395 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005396 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5397
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005398 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005399
5400 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005401 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005402 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005403 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005404 ret = -ENOMEM;
5405 goto unlock;
5406 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005407 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005408
5409 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005410 if (thresholds->primary) {
5411 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005412 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005413 }
5414
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005415 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005416 new->entries[size - 1].eventfd = eventfd;
5417 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005418
5419 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005420 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005421 compare_thresholds, NULL);
5422
5423 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005424 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005425 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005426 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005427 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005428 * new->current_threshold will not be used until
5429 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005430 * it here.
5431 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005432 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005433 } else
5434 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005435 }
5436
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005437 /* Free old spare buffer and save old primary buffer as spare */
5438 kfree(thresholds->spare);
5439 thresholds->spare = thresholds->primary;
5440
5441 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005442
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005443 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005444 synchronize_rcu();
5445
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005446unlock:
5447 mutex_unlock(&memcg->thresholds_lock);
5448
5449 return ret;
5450}
5451
Tejun Heo81eeaf02013-08-08 20:11:26 -04005452static void mem_cgroup_usage_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005453 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005454{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005455 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005456 struct mem_cgroup_thresholds *thresholds;
5457 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005458 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005459 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005460 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005461
5462 mutex_lock(&memcg->thresholds_lock);
5463 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005464 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005465 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005466 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005467 else
5468 BUG();
5469
Anton Vorontsov371528c2012-02-24 05:14:46 +04005470 if (!thresholds->primary)
5471 goto unlock;
5472
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005473 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5474
5475 /* Check if a threshold crossed before removing */
5476 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5477
5478 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005479 size = 0;
5480 for (i = 0; i < thresholds->primary->size; i++) {
5481 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005482 size++;
5483 }
5484
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005485 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005486
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005487 /* Set thresholds array to NULL if we don't have thresholds */
5488 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005489 kfree(new);
5490 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005491 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005492 }
5493
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005494 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005495
5496 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005497 new->current_threshold = -1;
5498 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5499 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005500 continue;
5501
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005502 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005503 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005504 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506 * until rcu_assign_pointer(), so it's safe to increment
5507 * it here.
5508 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005509 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005510 }
5511 j++;
5512 }
5513
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005514swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005515 /* Swap primary and spare array */
5516 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005517 /* If all events are unregistered, free the spare array */
5518 if (!new) {
5519 kfree(thresholds->spare);
5520 thresholds->spare = NULL;
5521 }
5522
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005523 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005524
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005525 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005526 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005527unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005528 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005530
Tejun Heo81eeaf02013-08-08 20:11:26 -04005531static int mem_cgroup_oom_register_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005532 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5533{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005534 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005535 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005536 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005537
5538 BUG_ON(type != _OOM_TYPE);
5539 event = kmalloc(sizeof(*event), GFP_KERNEL);
5540 if (!event)
5541 return -ENOMEM;
5542
Michal Hocko1af8efe2011-07-26 16:08:24 -07005543 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005544
5545 event->eventfd = eventfd;
5546 list_add(&event->list, &memcg->oom_notify);
5547
5548 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005549 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005550 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005551 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005552
5553 return 0;
5554}
5555
Tejun Heo81eeaf02013-08-08 20:11:26 -04005556static void mem_cgroup_oom_unregister_event(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005557 struct cftype *cft, struct eventfd_ctx *eventfd)
5558{
Tejun Heo81eeaf02013-08-08 20:11:26 -04005559 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005560 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005561 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005562
5563 BUG_ON(type != _OOM_TYPE);
5564
Michal Hocko1af8efe2011-07-26 16:08:24 -07005565 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005566
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005567 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005568 if (ev->eventfd == eventfd) {
5569 list_del(&ev->list);
5570 kfree(ev);
5571 }
5572 }
5573
Michal Hocko1af8efe2011-07-26 16:08:24 -07005574 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005575}
5576
Tejun Heo182446d2013-08-08 20:11:24 -04005577static int mem_cgroup_oom_control_read(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005578 struct cftype *cft, struct cgroup_map_cb *cb)
5579{
Tejun Heo182446d2013-08-08 20:11:24 -04005580 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005581
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005582 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005583
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005584 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005585 cb->fill(cb, "under_oom", 1);
5586 else
5587 cb->fill(cb, "under_oom", 0);
5588 return 0;
5589}
5590
Tejun Heo182446d2013-08-08 20:11:24 -04005591static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005592 struct cftype *cft, u64 val)
5593{
Tejun Heo182446d2013-08-08 20:11:24 -04005594 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo63876982013-08-08 20:11:23 -04005595 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(&memcg->css));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005596
5597 /* cannot set to root cgroup and only 0 and 1 are allowed */
Tejun Heo63876982013-08-08 20:11:23 -04005598 if (!parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005599 return -EINVAL;
5600
Glauber Costa09998212013-02-22 16:34:55 -08005601 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005602 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005603 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005604 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005605 return -EINVAL;
5606 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005607 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005608 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005609 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005610 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005611 return 0;
5612}
5613
Andrew Mortonc255a452012-07-31 16:43:02 -07005614#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005615static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005616{
Glauber Costa55007d82012-12-18 14:22:38 -08005617 int ret;
5618
Glauber Costa2633d7a2012-12-18 14:22:34 -08005619 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005620 ret = memcg_propagate_kmem(memcg);
5621 if (ret)
5622 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005623
Glauber Costa1d62e432012-04-09 19:36:33 -03005624 return mem_cgroup_sockets_init(memcg, ss);
Michel Lespinasse573b4002013-04-29 15:08:13 -07005625}
Glauber Costae5671df2011-12-11 21:47:01 +00005626
Li Zefan10d5ebf2013-07-08 16:00:33 -07005627static void memcg_destroy_kmem(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005628{
Glauber Costa1d62e432012-04-09 19:36:33 -03005629 mem_cgroup_sockets_destroy(memcg);
Li Zefan10d5ebf2013-07-08 16:00:33 -07005630}
5631
5632static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
5633{
5634 if (!memcg_kmem_is_active(memcg))
5635 return;
5636
5637 /*
5638 * kmem charges can outlive the cgroup. In the case of slab
5639 * pages, for instance, a page contain objects from various
5640 * processes. As we prevent from taking a reference for every
5641 * such allocation we have to be careful when doing uncharge
5642 * (see memcg_uncharge_kmem) and here during offlining.
5643 *
5644 * The idea is that that only the _last_ uncharge which sees
5645 * the dead memcg will drop the last reference. An additional
5646 * reference is taken here before the group is marked dead
5647 * which is then paired with css_put during uncharge resp. here.
5648 *
5649 * Although this might sound strange as this path is called from
5650 * css_offline() when the referencemight have dropped down to 0
5651 * and shouldn't be incremented anymore (css_tryget would fail)
5652 * we do not have other options because of the kmem allocations
5653 * lifetime.
5654 */
5655 css_get(&memcg->css);
Glauber Costa7de37682012-12-18 14:22:07 -08005656
5657 memcg_kmem_mark_dead(memcg);
5658
5659 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5660 return;
5661
Glauber Costa7de37682012-12-18 14:22:07 -08005662 if (memcg_kmem_test_and_clear_dead(memcg))
Li Zefan10d5ebf2013-07-08 16:00:33 -07005663 css_put(&memcg->css);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005664}
Glauber Costae5671df2011-12-11 21:47:01 +00005665#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005666static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005667{
5668 return 0;
5669}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005670
Li Zefan10d5ebf2013-07-08 16:00:33 -07005671static void memcg_destroy_kmem(struct mem_cgroup *memcg)
5672{
5673}
5674
5675static void kmem_cgroup_css_offline(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005676{
5677}
Glauber Costae5671df2011-12-11 21:47:01 +00005678#endif
5679
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005680static struct cftype mem_cgroup_files[] = {
5681 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005682 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005683 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005684 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005685 .register_event = mem_cgroup_usage_register_event,
5686 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005687 },
5688 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005689 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005690 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005691 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005692 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005693 },
5694 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005695 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005696 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005697 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005698 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005699 },
5700 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005701 .name = "soft_limit_in_bytes",
5702 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5703 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005704 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005705 },
5706 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005707 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005708 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005709 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005710 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005711 },
Balbir Singh8697d332008-02-07 00:13:59 -08005712 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005713 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005714 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005715 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005716 {
5717 .name = "force_empty",
5718 .trigger = mem_cgroup_force_empty_write,
5719 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005720 {
5721 .name = "use_hierarchy",
Tejun Heof00baae2013-04-15 13:41:15 -07005722 .flags = CFTYPE_INSANE,
Balbir Singh18f59ea2009-01-07 18:08:07 -08005723 .write_u64 = mem_cgroup_hierarchy_write,
5724 .read_u64 = mem_cgroup_hierarchy_read,
5725 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005726 {
5727 .name = "swappiness",
5728 .read_u64 = mem_cgroup_swappiness_read,
5729 .write_u64 = mem_cgroup_swappiness_write,
5730 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005731 {
5732 .name = "move_charge_at_immigrate",
5733 .read_u64 = mem_cgroup_move_charge_read,
5734 .write_u64 = mem_cgroup_move_charge_write,
5735 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005736 {
5737 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005738 .read_map = mem_cgroup_oom_control_read,
5739 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005740 .register_event = mem_cgroup_oom_register_event,
5741 .unregister_event = mem_cgroup_oom_unregister_event,
5742 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5743 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005744 {
5745 .name = "pressure_level",
5746 .register_event = vmpressure_register_event,
5747 .unregister_event = vmpressure_unregister_event,
5748 },
Ying Han406eb0c2011-05-26 16:25:37 -07005749#ifdef CONFIG_NUMA
5750 {
5751 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005752 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005753 },
5754#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005755#ifdef CONFIG_MEMCG_KMEM
5756 {
5757 .name = "kmem.limit_in_bytes",
5758 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5759 .write_string = mem_cgroup_write,
5760 .read = mem_cgroup_read,
5761 },
5762 {
5763 .name = "kmem.usage_in_bytes",
5764 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5765 .read = mem_cgroup_read,
5766 },
5767 {
5768 .name = "kmem.failcnt",
5769 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5770 .trigger = mem_cgroup_reset,
5771 .read = mem_cgroup_read,
5772 },
5773 {
5774 .name = "kmem.max_usage_in_bytes",
5775 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5776 .trigger = mem_cgroup_reset,
5777 .read = mem_cgroup_read,
5778 },
Glauber Costa749c5412012-12-18 14:23:01 -08005779#ifdef CONFIG_SLABINFO
5780 {
5781 .name = "kmem.slabinfo",
5782 .read_seq_string = mem_cgroup_slabinfo_read,
5783 },
5784#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005785#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005786 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005787};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005788
Michal Hocko2d110852013-02-22 16:34:43 -08005789#ifdef CONFIG_MEMCG_SWAP
5790static struct cftype memsw_cgroup_files[] = {
5791 {
5792 .name = "memsw.usage_in_bytes",
5793 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5794 .read = mem_cgroup_read,
5795 .register_event = mem_cgroup_usage_register_event,
5796 .unregister_event = mem_cgroup_usage_unregister_event,
5797 },
5798 {
5799 .name = "memsw.max_usage_in_bytes",
5800 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5801 .trigger = mem_cgroup_reset,
5802 .read = mem_cgroup_read,
5803 },
5804 {
5805 .name = "memsw.limit_in_bytes",
5806 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5807 .write_string = mem_cgroup_write,
5808 .read = mem_cgroup_read,
5809 },
5810 {
5811 .name = "memsw.failcnt",
5812 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5813 .trigger = mem_cgroup_reset,
5814 .read = mem_cgroup_read,
5815 },
5816 { }, /* terminate */
5817};
5818#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005819static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005820{
5821 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005822 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005823 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005824 /*
5825 * This routine is called against possible nodes.
5826 * But it's BUG to call kmalloc() against offline node.
5827 *
5828 * TODO: this routine can waste much memory for nodes which will
5829 * never be onlined. It's better to use memory hotplug callback
5830 * function.
5831 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005832 if (!node_state(node, N_NORMAL_MEMORY))
5833 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005834 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005835 if (!pn)
5836 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005837
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005838 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5839 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005840 lruvec_init(&mz->lruvec);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005841 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005842 }
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005843 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005844 return 0;
5845}
5846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005847static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005848{
Johannes Weiner54f72fe2013-07-08 15:59:49 -07005849 kfree(memcg->nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005850}
5851
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005852static struct mem_cgroup *mem_cgroup_alloc(void)
5853{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005854 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005855 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005856
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005857 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005858 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005859 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005860 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005861 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005862
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005863 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005864 return NULL;
5865
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005866 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5867 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005868 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005869 spin_lock_init(&memcg->pcp_counter_lock);
5870 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005871
5872out_free:
5873 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005874 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005875 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005876 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005877 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005878}
5879
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005880/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005881 * At destroying mem_cgroup, references from swap_cgroup can remain.
5882 * (scanning all at force_empty is too costly...)
5883 *
5884 * Instead of clearing all references at force_empty, we remember
5885 * the number of reference from swap_cgroup and free mem_cgroup when
5886 * it goes down to 0.
5887 *
5888 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005889 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005890
5891static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005892{
Glauber Costac8b2a362012-12-18 14:22:13 -08005893 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005894 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005895
Glauber Costac8b2a362012-12-18 14:22:13 -08005896 free_css_id(&mem_cgroup_subsys, &memcg->css);
5897
5898 for_each_node(node)
5899 free_mem_cgroup_per_zone_info(memcg, node);
5900
5901 free_percpu(memcg->stat);
5902
Glauber Costa3f134612012-05-29 15:07:11 -07005903 /*
5904 * We need to make sure that (at least for now), the jump label
5905 * destruction code runs outside of the cgroup lock. This is because
5906 * get_online_cpus(), which is called from the static_branch update,
5907 * can't be called inside the cgroup_lock. cpusets are the ones
5908 * enforcing this dependency, so if they ever change, we might as well.
5909 *
5910 * schedule_work() will guarantee this happens. Be careful if you need
5911 * to move this code around, and make sure it is outside
5912 * the cgroup_lock.
5913 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005914 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005915 if (size < PAGE_SIZE)
5916 kfree(memcg);
5917 else
5918 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005919}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005920
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005921/*
5922 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5923 */
Glauber Costae1aab162011-12-11 21:47:03 +00005924struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005925{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005926 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005927 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005928 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005929}
Glauber Costae1aab162011-12-11 21:47:03 +00005930EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005931
Li Zefan0eb253e2009-01-15 13:51:25 -08005932static struct cgroup_subsys_state * __ref
Tejun Heoeb954192013-08-08 20:11:23 -04005933mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005934{
Glauber Costad142e3e2013-02-22 16:34:52 -08005935 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005936 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005937 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005939 memcg = mem_cgroup_alloc();
5940 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005941 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005942
Bob Liu3ed28fa2012-01-12 17:19:04 -08005943 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005944 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005945 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005946
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005947 /* root ? */
Tejun Heoeb954192013-08-08 20:11:23 -04005948 if (parent_css == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08005949 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08005950 res_counter_init(&memcg->res, NULL);
5951 res_counter_init(&memcg->memsw, NULL);
5952 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005953 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005954
Glauber Costad142e3e2013-02-22 16:34:52 -08005955 memcg->last_scanned_node = MAX_NUMNODES;
5956 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08005957 memcg->move_charge_at_immigrate = 0;
5958 mutex_init(&memcg->thresholds_lock);
5959 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07005960 vmpressure_init(&memcg->vmpressure);
Michal Hocko7d910c02013-09-12 15:13:28 -07005961 spin_lock_init(&memcg->soft_lock);
Glauber Costad142e3e2013-02-22 16:34:52 -08005962
5963 return &memcg->css;
5964
5965free_out:
5966 __mem_cgroup_free(memcg);
5967 return ERR_PTR(error);
5968}
5969
5970static int
Tejun Heoeb954192013-08-08 20:11:23 -04005971mem_cgroup_css_online(struct cgroup_subsys_state *css)
Glauber Costad142e3e2013-02-22 16:34:52 -08005972{
Tejun Heoeb954192013-08-08 20:11:23 -04005973 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5974 struct mem_cgroup *parent = mem_cgroup_from_css(css_parent(css));
Glauber Costad142e3e2013-02-22 16:34:52 -08005975 int error = 0;
5976
Tejun Heo63876982013-08-08 20:11:23 -04005977 if (!parent)
Glauber Costad142e3e2013-02-22 16:34:52 -08005978 return 0;
5979
Glauber Costa09998212013-02-22 16:34:55 -08005980 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08005981
5982 memcg->use_hierarchy = parent->use_hierarchy;
5983 memcg->oom_kill_disable = parent->oom_kill_disable;
5984 memcg->swappiness = mem_cgroup_swappiness(parent);
5985
5986 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005987 res_counter_init(&memcg->res, &parent->res);
5988 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005989 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08005990
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005991 /*
Li Zefan8d76a972013-07-08 16:00:36 -07005992 * No need to take a reference to the parent because cgroup
5993 * core guarantees its existence.
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005994 */
Balbir Singh18f59ea2009-01-07 18:08:07 -08005995 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005996 res_counter_init(&memcg->res, NULL);
5997 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005998 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005999 /*
6000 * Deeper hierachy with use_hierarchy == false doesn't make
6001 * much sense so let cgroup subsystem know about this
6002 * unfortunate state in our controller.
6003 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006004 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006005 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006006 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006007
6008 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006009 mutex_unlock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006010 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006011}
6012
Michal Hocko5f578162013-04-29 15:07:17 -07006013/*
6014 * Announce all parents that a group from their hierarchy is gone.
6015 */
6016static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6017{
6018 struct mem_cgroup *parent = memcg;
6019
6020 while ((parent = parent_mem_cgroup(parent)))
Johannes Weiner519ebea2013-07-03 15:04:51 -07006021 mem_cgroup_iter_invalidate(parent);
Michal Hocko5f578162013-04-29 15:07:17 -07006022
6023 /*
6024 * if the root memcg is not hierarchical we have to check it
6025 * explicitely.
6026 */
6027 if (!root_mem_cgroup->use_hierarchy)
Johannes Weiner519ebea2013-07-03 15:04:51 -07006028 mem_cgroup_iter_invalidate(root_mem_cgroup);
Michal Hocko5f578162013-04-29 15:07:17 -07006029}
6030
Tejun Heoeb954192013-08-08 20:11:23 -04006031static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006032{
Tejun Heoeb954192013-08-08 20:11:23 -04006033 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006034
Li Zefan10d5ebf2013-07-08 16:00:33 -07006035 kmem_cgroup_css_offline(memcg);
6036
Michal Hocko5f578162013-04-29 15:07:17 -07006037 mem_cgroup_invalidate_reclaim_iterators(memcg);
Michal Hockoab5196c2012-10-26 13:37:32 +02006038 mem_cgroup_reparent_charges(memcg);
Michal Hocko7d910c02013-09-12 15:13:28 -07006039 if (memcg->soft_contributed) {
6040 while ((memcg = parent_mem_cgroup(memcg)))
6041 atomic_dec(&memcg->children_in_excess);
6042 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08006043 mem_cgroup_destroy_all_caches(memcg);
Michal Hocko33cb8762013-07-31 13:53:51 -07006044 vmpressure_cleanup(&memcg->vmpressure);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006045}
6046
Tejun Heoeb954192013-08-08 20:11:23 -04006047static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006048{
Tejun Heoeb954192013-08-08 20:11:23 -04006049 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006050
Li Zefan10d5ebf2013-07-08 16:00:33 -07006051 memcg_destroy_kmem(memcg);
Li Zefan465939a2013-07-08 16:00:38 -07006052 __mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006053}
6054
Daisuke Nishimura02491442010-03-10 15:22:17 -08006055#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006056/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006057#define PRECHARGE_COUNT_AT_ONCE 256
6058static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006059{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006060 int ret = 0;
6061 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006062 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006063
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006064 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006065 mc.precharge += count;
6066 /* we don't need css_get for root */
6067 return ret;
6068 }
6069 /* try to charge at once */
6070 if (count > 1) {
6071 struct res_counter *dummy;
6072 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006073 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006074 * by cgroup_lock_live_cgroup() that it is not removed and we
6075 * are still under the same cgroup_mutex. So we can postpone
6076 * css_get().
6077 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006078 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006079 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006080 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006081 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006082 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006083 goto one_by_one;
6084 }
6085 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006086 return ret;
6087 }
6088one_by_one:
6089 /* fall back to one by one charge */
6090 while (count--) {
6091 if (signal_pending(current)) {
6092 ret = -EINTR;
6093 break;
6094 }
6095 if (!batch_count--) {
6096 batch_count = PRECHARGE_COUNT_AT_ONCE;
6097 cond_resched();
6098 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006099 ret = __mem_cgroup_try_charge(NULL,
6100 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006101 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006102 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006103 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006104 mc.precharge++;
6105 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006106 return ret;
6107}
6108
6109/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006110 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006111 * @vma: the vma the pte to be checked belongs
6112 * @addr: the address corresponding to the pte to be checked
6113 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006114 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006115 *
6116 * Returns
6117 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6118 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6119 * move charge. if @target is not NULL, the page is stored in target->page
6120 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006121 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6122 * target for charge migration. if @target is not NULL, the entry is stored
6123 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006124 *
6125 * Called with pte lock held.
6126 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006127union mc_target {
6128 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006129 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006130};
6131
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006132enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006133 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006134 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006135 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006136};
6137
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006138static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6139 unsigned long addr, pte_t ptent)
6140{
6141 struct page *page = vm_normal_page(vma, addr, ptent);
6142
6143 if (!page || !page_mapped(page))
6144 return NULL;
6145 if (PageAnon(page)) {
6146 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006147 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006148 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006149 } else if (!move_file())
6150 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006151 return NULL;
6152 if (!get_page_unless_zero(page))
6153 return NULL;
6154
6155 return page;
6156}
6157
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006158#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006159static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6160 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6161{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006162 struct page *page = NULL;
6163 swp_entry_t ent = pte_to_swp_entry(ptent);
6164
6165 if (!move_anon() || non_swap_entry(ent))
6166 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006167 /*
6168 * Because lookup_swap_cache() updates some statistics counter,
6169 * we call find_get_page() with swapper_space directly.
6170 */
Shaohua Li33806f02013-02-22 16:34:37 -08006171 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006172 if (do_swap_account)
6173 entry->val = ent.val;
6174
6175 return page;
6176}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006177#else
6178static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6179 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6180{
6181 return NULL;
6182}
6183#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006184
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006185static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6186 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6187{
6188 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006189 struct address_space *mapping;
6190 pgoff_t pgoff;
6191
6192 if (!vma->vm_file) /* anonymous vma */
6193 return NULL;
6194 if (!move_file())
6195 return NULL;
6196
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006197 mapping = vma->vm_file->f_mapping;
6198 if (pte_none(ptent))
6199 pgoff = linear_page_index(vma, addr);
6200 else /* pte_file(ptent) is true */
6201 pgoff = pte_to_pgoff(ptent);
6202
6203 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006204 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006205
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006206#ifdef CONFIG_SWAP
6207 /* shmem/tmpfs may report page out on swap: account for that too. */
6208 if (radix_tree_exceptional_entry(page)) {
6209 swp_entry_t swap = radix_to_swp_entry(page);
6210 if (do_swap_account)
6211 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006212 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006213 }
6214#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006215 return page;
6216}
6217
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006218static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006219 unsigned long addr, pte_t ptent, union mc_target *target)
6220{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006221 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006222 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006223 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006224 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006225
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006226 if (pte_present(ptent))
6227 page = mc_handle_present_pte(vma, addr, ptent);
6228 else if (is_swap_pte(ptent))
6229 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006230 else if (pte_none(ptent) || pte_file(ptent))
6231 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006232
6233 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006234 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006235 if (page) {
6236 pc = lookup_page_cgroup(page);
6237 /*
6238 * Do only loose check w/o page_cgroup lock.
6239 * mem_cgroup_move_account() checks the pc is valid or not under
6240 * the lock.
6241 */
6242 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6243 ret = MC_TARGET_PAGE;
6244 if (target)
6245 target->page = page;
6246 }
6247 if (!ret || !target)
6248 put_page(page);
6249 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006250 /* There is a swap entry and a page doesn't exist or isn't charged */
6251 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006252 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006253 ret = MC_TARGET_SWAP;
6254 if (target)
6255 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006256 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006257 return ret;
6258}
6259
Naoya Horiguchi12724852012-03-21 16:34:28 -07006260#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6261/*
6262 * We don't consider swapping or file mapped pages because THP does not
6263 * support them for now.
6264 * Caller should make sure that pmd_trans_huge(pmd) is true.
6265 */
6266static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6267 unsigned long addr, pmd_t pmd, union mc_target *target)
6268{
6269 struct page *page = NULL;
6270 struct page_cgroup *pc;
6271 enum mc_target_type ret = MC_TARGET_NONE;
6272
6273 page = pmd_page(pmd);
6274 VM_BUG_ON(!page || !PageHead(page));
6275 if (!move_anon())
6276 return ret;
6277 pc = lookup_page_cgroup(page);
6278 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6279 ret = MC_TARGET_PAGE;
6280 if (target) {
6281 get_page(page);
6282 target->page = page;
6283 }
6284 }
6285 return ret;
6286}
6287#else
6288static inline 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 return MC_TARGET_NONE;
6292}
6293#endif
6294
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006295static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6296 unsigned long addr, unsigned long end,
6297 struct mm_walk *walk)
6298{
6299 struct vm_area_struct *vma = walk->private;
6300 pte_t *pte;
6301 spinlock_t *ptl;
6302
Naoya Horiguchi12724852012-03-21 16:34:28 -07006303 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6304 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6305 mc.precharge += HPAGE_PMD_NR;
6306 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006307 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006308 }
Dave Hansen03319322011-03-22 16:32:56 -07006309
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006310 if (pmd_trans_unstable(pmd))
6311 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006312 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6313 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006314 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006315 mc.precharge++; /* increment precharge temporarily */
6316 pte_unmap_unlock(pte - 1, ptl);
6317 cond_resched();
6318
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006319 return 0;
6320}
6321
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006322static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6323{
6324 unsigned long precharge;
6325 struct vm_area_struct *vma;
6326
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006327 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006328 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6329 struct mm_walk mem_cgroup_count_precharge_walk = {
6330 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6331 .mm = mm,
6332 .private = vma,
6333 };
6334 if (is_vm_hugetlb_page(vma))
6335 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006336 walk_page_range(vma->vm_start, vma->vm_end,
6337 &mem_cgroup_count_precharge_walk);
6338 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006339 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006340
6341 precharge = mc.precharge;
6342 mc.precharge = 0;
6343
6344 return precharge;
6345}
6346
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006347static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6348{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006349 unsigned long precharge = mem_cgroup_count_precharge(mm);
6350
6351 VM_BUG_ON(mc.moving_task);
6352 mc.moving_task = current;
6353 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006354}
6355
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006356/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6357static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006358{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006359 struct mem_cgroup *from = mc.from;
6360 struct mem_cgroup *to = mc.to;
Li Zefan40503772013-07-08 16:00:34 -07006361 int i;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006362
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006363 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006364 if (mc.precharge) {
6365 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6366 mc.precharge = 0;
6367 }
6368 /*
6369 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6370 * we must uncharge here.
6371 */
6372 if (mc.moved_charge) {
6373 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6374 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006375 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006376 /* we must fixup refcnts and charges */
6377 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006378 /* uncharge swap account from the old cgroup */
6379 if (!mem_cgroup_is_root(mc.from))
6380 res_counter_uncharge(&mc.from->memsw,
6381 PAGE_SIZE * mc.moved_swap);
Li Zefan40503772013-07-08 16:00:34 -07006382
6383 for (i = 0; i < mc.moved_swap; i++)
6384 css_put(&mc.from->css);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006385
6386 if (!mem_cgroup_is_root(mc.to)) {
6387 /*
6388 * we charged both to->res and to->memsw, so we should
6389 * uncharge to->res.
6390 */
6391 res_counter_uncharge(&mc.to->res,
6392 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006393 }
Li Zefan40503772013-07-08 16:00:34 -07006394 /* we've already done css_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006395 mc.moved_swap = 0;
6396 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006397 memcg_oom_recover(from);
6398 memcg_oom_recover(to);
6399 wake_up_all(&mc.waitq);
6400}
6401
6402static void mem_cgroup_clear_mc(void)
6403{
6404 struct mem_cgroup *from = mc.from;
6405
6406 /*
6407 * we must clear moving_task before waking up waiters at the end of
6408 * task migration.
6409 */
6410 mc.moving_task = NULL;
6411 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006412 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006413 mc.from = NULL;
6414 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006415 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006416 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006417}
6418
Tejun Heoeb954192013-08-08 20:11:23 -04006419static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006420 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006421{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006422 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006423 int ret = 0;
Tejun Heoeb954192013-08-08 20:11:23 -04006424 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Glauber Costaee5e8472013-02-22 16:34:50 -08006425 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006426
Glauber Costaee5e8472013-02-22 16:34:50 -08006427 /*
6428 * We are now commited to this value whatever it is. Changes in this
6429 * tunable will only affect upcoming migrations, not the current one.
6430 * So we need to save it, and keep it going.
6431 */
6432 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6433 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006434 struct mm_struct *mm;
6435 struct mem_cgroup *from = mem_cgroup_from_task(p);
6436
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006437 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006438
6439 mm = get_task_mm(p);
6440 if (!mm)
6441 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006442 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006443 if (mm->owner == p) {
6444 VM_BUG_ON(mc.from);
6445 VM_BUG_ON(mc.to);
6446 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006447 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006448 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006449 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006450 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006451 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006452 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006453 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006454 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006455 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006456
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006457 ret = mem_cgroup_precharge_mc(mm);
6458 if (ret)
6459 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006460 }
6461 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006462 }
6463 return ret;
6464}
6465
Tejun Heoeb954192013-08-08 20:11:23 -04006466static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006467 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006468{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006469 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006470}
6471
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006472static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6473 unsigned long addr, unsigned long end,
6474 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006475{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006476 int ret = 0;
6477 struct vm_area_struct *vma = walk->private;
6478 pte_t *pte;
6479 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006480 enum mc_target_type target_type;
6481 union mc_target target;
6482 struct page *page;
6483 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006484
Naoya Horiguchi12724852012-03-21 16:34:28 -07006485 /*
6486 * We don't take compound_lock() here but no race with splitting thp
6487 * happens because:
6488 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6489 * under splitting, which means there's no concurrent thp split,
6490 * - if another thread runs into split_huge_page() just after we
6491 * entered this if-block, the thread must wait for page table lock
6492 * to be unlocked in __split_huge_page_splitting(), where the main
6493 * part of thp split is not executed yet.
6494 */
6495 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006496 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006497 spin_unlock(&vma->vm_mm->page_table_lock);
6498 return 0;
6499 }
6500 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6501 if (target_type == MC_TARGET_PAGE) {
6502 page = target.page;
6503 if (!isolate_lru_page(page)) {
6504 pc = lookup_page_cgroup(page);
6505 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006506 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006507 mc.precharge -= HPAGE_PMD_NR;
6508 mc.moved_charge += HPAGE_PMD_NR;
6509 }
6510 putback_lru_page(page);
6511 }
6512 put_page(page);
6513 }
6514 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006515 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006516 }
6517
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006518 if (pmd_trans_unstable(pmd))
6519 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006520retry:
6521 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6522 for (; addr != end; addr += PAGE_SIZE) {
6523 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006524 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006525
6526 if (!mc.precharge)
6527 break;
6528
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006529 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006530 case MC_TARGET_PAGE:
6531 page = target.page;
6532 if (isolate_lru_page(page))
6533 goto put;
6534 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006535 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006536 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006537 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006538 /* we uncharge from mc.from later. */
6539 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006540 }
6541 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006542put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006543 put_page(page);
6544 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006545 case MC_TARGET_SWAP:
6546 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006547 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006548 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006549 /* we fixup refcnts and charges later. */
6550 mc.moved_swap++;
6551 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006552 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006553 default:
6554 break;
6555 }
6556 }
6557 pte_unmap_unlock(pte - 1, ptl);
6558 cond_resched();
6559
6560 if (addr != end) {
6561 /*
6562 * We have consumed all precharges we got in can_attach().
6563 * We try charge one by one, but don't do any additional
6564 * charges to mc.to if we have failed in charge once in attach()
6565 * phase.
6566 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006567 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006568 if (!ret)
6569 goto retry;
6570 }
6571
6572 return ret;
6573}
6574
6575static void mem_cgroup_move_charge(struct mm_struct *mm)
6576{
6577 struct vm_area_struct *vma;
6578
6579 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006580retry:
6581 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6582 /*
6583 * Someone who are holding the mmap_sem might be waiting in
6584 * waitq. So we cancel all extra charges, wake up all waiters,
6585 * and retry. Because we cancel precharges, we might not be able
6586 * to move enough charges, but moving charge is a best-effort
6587 * feature anyway, so it wouldn't be a big problem.
6588 */
6589 __mem_cgroup_clear_mc();
6590 cond_resched();
6591 goto retry;
6592 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006593 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6594 int ret;
6595 struct mm_walk mem_cgroup_move_charge_walk = {
6596 .pmd_entry = mem_cgroup_move_charge_pte_range,
6597 .mm = mm,
6598 .private = vma,
6599 };
6600 if (is_vm_hugetlb_page(vma))
6601 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006602 ret = walk_page_range(vma->vm_start, vma->vm_end,
6603 &mem_cgroup_move_charge_walk);
6604 if (ret)
6605 /*
6606 * means we have consumed all precharges and failed in
6607 * doing additional charge. Just abandon here.
6608 */
6609 break;
6610 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006611 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006612}
6613
Tejun Heoeb954192013-08-08 20:11:23 -04006614static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006615 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006616{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006617 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006618 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006619
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006620 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006621 if (mc.to)
6622 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006623 mmput(mm);
6624 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006625 if (mc.to)
6626 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006627}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006628#else /* !CONFIG_MMU */
Tejun Heoeb954192013-08-08 20:11:23 -04006629static int mem_cgroup_can_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006630 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006631{
6632 return 0;
6633}
Tejun Heoeb954192013-08-08 20:11:23 -04006634static void mem_cgroup_cancel_attach(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006635 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006636{
6637}
Tejun Heoeb954192013-08-08 20:11:23 -04006638static void mem_cgroup_move_task(struct cgroup_subsys_state *css,
Li Zefan761b3ef2012-01-31 13:47:36 +08006639 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006640{
6641}
6642#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006643
Tejun Heof00baae2013-04-15 13:41:15 -07006644/*
6645 * Cgroup retains root cgroups across [un]mount cycles making it necessary
6646 * to verify sane_behavior flag on each mount attempt.
6647 */
Tejun Heoeb954192013-08-08 20:11:23 -04006648static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07006649{
6650 /*
6651 * use_hierarchy is forced with sane_behavior. cgroup core
6652 * guarantees that @root doesn't have any children, so turning it
6653 * on for the root memcg is enough.
6654 */
Tejun Heoeb954192013-08-08 20:11:23 -04006655 if (cgroup_sane_behavior(root_css->cgroup))
6656 mem_cgroup_from_css(root_css)->use_hierarchy = true;
Tejun Heof00baae2013-04-15 13:41:15 -07006657}
6658
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006659struct cgroup_subsys mem_cgroup_subsys = {
6660 .name = "memory",
6661 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006662 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006663 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006664 .css_offline = mem_cgroup_css_offline,
6665 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006666 .can_attach = mem_cgroup_can_attach,
6667 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006668 .attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07006669 .bind = mem_cgroup_bind,
Tejun Heo6bc10342012-04-01 12:09:55 -07006670 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006671 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006672 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006673};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006674
Andrew Mortonc255a452012-07-31 16:43:02 -07006675#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006676static int __init enable_swap_account(char *s)
6677{
Michal Hockoa2c89902011-05-24 17:12:50 -07006678 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006679 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006680 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006681 really_do_swap_account = 0;
6682 return 1;
6683}
Michal Hockoa2c89902011-05-24 17:12:50 -07006684__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006685
Michal Hocko2d110852013-02-22 16:34:43 -08006686static void __init memsw_file_init(void)
6687{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006688 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006689}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006690
6691static void __init enable_swap_cgroup(void)
6692{
6693 if (!mem_cgroup_disabled() && really_do_swap_account) {
6694 do_swap_account = 1;
6695 memsw_file_init();
6696 }
6697}
6698
Michal Hocko2d110852013-02-22 16:34:43 -08006699#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006700static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006701{
6702}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006703#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006704
6705/*
Michal Hocko10813122013-02-22 16:35:41 -08006706 * subsys_initcall() for memory controller.
6707 *
6708 * Some parts like hotcpu_notifier() have to be initialized from this context
6709 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6710 * everything that doesn't depend on a specific mem_cgroup structure should
6711 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006712 */
6713static int __init mem_cgroup_init(void)
6714{
6715 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006716 enable_swap_cgroup();
Michal Hockoe4777492013-02-22 16:35:40 -08006717 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006718 return 0;
6719}
6720subsys_initcall(mem_cgroup_init);